Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PluginInfrastructure Refactoring/HeuristicLab.Persistence/3.3/Core/ConfigurationService.cs @ 2591

Last change on this file since 2591 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 7.7 KB
Line 
1using System;
2using System.IO;
3using System.Linq;
4using System.Collections.Generic;
5using System.Reflection;
6using System.Text;
7using HeuristicLab.Persistence.Default.Xml;
8using HeuristicLab.Persistence.Interfaces;
9using HeuristicLab.Tracing;
10using HeuristicLab.Persistence.Core.Tokens;
11using HeuristicLab.Persistence.Auxiliary;
12
13namespace HeuristicLab.Persistence.Core {
14
15  public class ConfigurationService {
16
17    private static ConfigurationService instance;
18    private readonly Dictionary<IFormat, Configuration> customConfigurations;
19    public Dictionary<Type, List<IPrimitiveSerializer>> PrimitiveSerializers { get; private set; }
20    public List<ICompositeSerializer> CompositeSerializers { get; private set; }
21    public List<IFormat> Formats { get; private set; }
22
23    public static ConfigurationService Instance {
24      get {
25        if (instance == null)
26          instance = new ConfigurationService();
27        return instance;
28      }
29    }
30
31    private ConfigurationService() {
32      PrimitiveSerializers = new Dictionary<Type, List<IPrimitiveSerializer>>();
33      CompositeSerializers = new List<ICompositeSerializer>();
34      customConfigurations = new Dictionary<IFormat, Configuration>();
35      Formats = new List<IFormat>();
36      Reset();
37      LoadSettings();
38    }
39
40    public void LoadSettings() {
41      LoadSettings(false);
42    }
43
44    public void LoadSettings(bool throwOnError) {
45      try {
46        TryLoadSettings();
47      } catch (Exception e) {
48        if (throwOnError) {
49          throw new PersistenceException("Could not load persistence settings.", e);
50        } else {
51          Logger.Warn("Could not load settings.", e);
52        }
53      }
54    }
55
56    protected void TryLoadSettings() {
57      if (String.IsNullOrEmpty(Properties.Settings.Default.CustomConfigurations) ||
58          String.IsNullOrEmpty(Properties.Settings.Default.CustomConfigurationsTypeCache))
59        return;
60      Deserializer deSerializer = new Deserializer(
61        XmlParser.ParseTypeCache(
62        new StringReader(
63          Properties.Settings.Default.CustomConfigurationsTypeCache)));
64      XmlParser parser = new XmlParser(
65        new StringReader(
66          Properties.Settings.Default.CustomConfigurations));
67      var newCustomConfigurations = (Dictionary<IFormat, Configuration>)
68        deSerializer.Deserialize(parser);
69      foreach (var config in newCustomConfigurations) {
70        customConfigurations[config.Key] = config.Value;
71      }
72    }
73
74    protected void SaveSettings() {
75      Serializer serializer = new Serializer(
76        customConfigurations,
77        GetDefaultConfig(new XmlFormat()),
78        "CustomConfigurations");
79      XmlGenerator generator = new XmlGenerator();
80      StringBuilder configurationString = new StringBuilder();
81      foreach (ISerializationToken token in serializer) {
82        configurationString.Append(generator.Format(token));
83      }
84      StringBuilder configurationTypeCacheString = new StringBuilder();
85      foreach (string s in generator.Format(serializer.TypeCache))
86        configurationTypeCacheString.Append(s);
87      Properties.Settings.Default.CustomConfigurations =
88        configurationString.ToString();
89      Properties.Settings.Default.CustomConfigurationsTypeCache =
90        configurationTypeCacheString.ToString();
91      Properties.Settings.Default.Save();
92    }
93
94    public void Reset() {
95      customConfigurations.Clear();
96      PrimitiveSerializers.Clear();
97      CompositeSerializers.Clear();
98      Assembly defaultAssembly = Assembly.GetExecutingAssembly();
99      DiscoverFrom(defaultAssembly);
100      foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
101        if (a != defaultAssembly)
102          DiscoverFrom(a);
103      SortCompositeSerializers();
104    }
105
106    class PriortiySorter : IComparer<ICompositeSerializer> {
107      public int Compare(ICompositeSerializer x, ICompositeSerializer y) {
108        return y.Priority - x.Priority;
109      }
110    }
111
112    protected void SortCompositeSerializers() {
113      CompositeSerializers.Sort(new PriortiySorter());
114    }
115
116    protected void DiscoverFrom(Assembly a) {
117      foreach (Type t in a.GetTypes()) {
118        if (t.GetInterface(typeof(IPrimitiveSerializer).FullName) != null) {
119          try {
120            IPrimitiveSerializer primitiveSerializer =
121              (IPrimitiveSerializer)Activator.CreateInstance(t, true);
122            if (!PrimitiveSerializers.ContainsKey(primitiveSerializer.SerialDataType)) {
123              PrimitiveSerializers.Add(primitiveSerializer.SerialDataType, new List<IPrimitiveSerializer>());
124            }
125            PrimitiveSerializers[primitiveSerializer.SerialDataType].Add(primitiveSerializer);
126            Logger.Debug(String.Format("discovered primitive serializer {0} ({1} -> {2})",
127              t.VersionInvariantName(),
128              primitiveSerializer.SourceType.AssemblyQualifiedName,
129              primitiveSerializer.SerialDataType.AssemblyQualifiedName));
130          } catch (MissingMethodException e) {
131            Logger.Warn("Could not instantiate " + t.AssemblyQualifiedName, e);
132          } catch (ArgumentException e) {
133            Logger.Warn("Could not instantiate " + t.AssemblyQualifiedName, e);
134          }
135        }
136        if (t.GetInterface(typeof(ICompositeSerializer).FullName) != null) {
137          try {
138            CompositeSerializers.Add((ICompositeSerializer)Activator.CreateInstance(t, true));
139            Logger.Debug("discovered composite serializer " + t.AssemblyQualifiedName);
140          } catch (MissingMethodException e) {
141            Logger.Warn("Could not instantiate " + t.AssemblyQualifiedName, e);
142          } catch (ArgumentException e) {
143            Logger.Warn("Could not instantiate " + t.AssemblyQualifiedName, e);
144          }
145        }
146        if (t.GetInterface(typeof(IFormat).FullName) != null) {
147          try {
148            IFormat format = (IFormat)Activator.CreateInstance(t, true);
149            Formats.Add(format);
150            Logger.Debug(String.Format("discovered format {0} ({2}) with serial data {1}.",
151              format.Name,
152              format.SerialDataType,
153              t.AssemblyQualifiedName));
154          } catch (MissingMethodException e) {
155            Logger.Warn("Could not instantiate " + t.AssemblyQualifiedName, e);
156          } catch (ArgumentException e) {
157            Logger.Warn("Could not instantiate " + t.AssemblyQualifiedName, e);
158          }
159        }
160      }
161    }
162
163    public Configuration GetDefaultConfig(IFormat format) {
164      Dictionary<Type, IPrimitiveSerializer> primitiveConfig = new Dictionary<Type, IPrimitiveSerializer>();
165      if (PrimitiveSerializers.ContainsKey(format.SerialDataType)) {
166        foreach (IPrimitiveSerializer f in PrimitiveSerializers[format.SerialDataType]) {
167          if (!primitiveConfig.ContainsKey(f.SourceType))
168            primitiveConfig.Add(f.SourceType, f);
169        }
170      } else {
171        Logger.Warn(String.Format(
172          "No primitive serializers found for format {0} with serial data type {1}",
173          format.GetType().AssemblyQualifiedName,
174          format.SerialDataType.AssemblyQualifiedName));
175      }
176      return new Configuration(
177        format,
178        primitiveConfig.Values,
179        CompositeSerializers.Where((d) => d.Priority > 0));
180    }
181
182    public Configuration GetConfiguration(IFormat format) {
183      if (customConfigurations.ContainsKey(format))
184        return customConfigurations[format];
185      return GetDefaultConfig(format);
186    }
187
188    public void DefineConfiguration(Configuration configuration) {
189      customConfigurations[configuration.Format] = configuration;
190      SaveSettings();
191    }
192
193  }
194
195}
Note: See TracBrowser for help on using the repository browser.