Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Core/ConfigurationService.cs @ 1555

Last change on this file since 1555 was 1554, checked in by epitzer, 15 years ago

XML formatters for all primitive numeric types. (#548)

File size: 5.7 KB
RevLine 
[1454]1using System;
2using System.IO;
3using System.Collections.Generic;
4using System.Reflection;
5using System.Text;
6using HeuristicLab.Persistence.Default.Xml;
7using HeuristicLab.Persistence.Interfaces;
[1469]8using HeuristicLab.Tracing;
[1542]9using HeuristicLab.Persistence.Interfaces.Tokens;
[1454]10
11namespace HeuristicLab.Persistence.Core {
12 
13  public class ConfigurationService {
14
15    private static ConfigurationService instance;
16    private readonly Dictionary<IFormat, Configuration> customConfigurations;
[1542]17    public Dictionary<IFormat, List<IFormatter>> Formatters { get; private set; }
18    public List<IDecomposer> Decomposers { get; private set; }
[1454]19   
20    public static ConfigurationService Instance {
21      get {
22        if (instance == null)
23          instance = new ConfigurationService();
24        return instance;
25      }
26    }
27
[1542]28    private ConfigurationService() {
[1454]29      Formatters = new Dictionary<IFormat, List<IFormatter>>();
30      Decomposers = new List<IDecomposer>();
31      customConfigurations = new Dictionary<IFormat, Configuration>();     
32      Reset();
33      LoadSettings();
34    }
35
36    public void LoadSettings() {
37      try {
[1542]38        if (String.IsNullOrEmpty(Properties.Settings.Default.CustomConfigurations) ||
39          String.IsNullOrEmpty(Properties.Settings.Default.CustomConfigurationsTypeCache))
[1454]40          return;
[1542]41        Deserializer deSerializer = new Deserializer(
[1454]42          XmlParser.ParseTypeCache(
43          new StringReader(
[1542]44            Properties.Settings.Default.CustomConfigurationsTypeCache)));
[1454]45        XmlParser parser = new XmlParser(
46          new StringReader(
[1542]47            Properties.Settings.Default.CustomConfigurations));
[1454]48        var newCustomConfigurations = (Dictionary<IFormat, Configuration>)
[1542]49          deSerializer.Deserialize(parser);
[1454]50        foreach (var config in newCustomConfigurations) {
51          customConfigurations[config.Key] = config.Value;
52        }
[1473]53      } catch (Exception e) {
54        Logger.Warn("Could not load settings.", e);       
[1454]55      }
56    }
57
58    public void SaveSettings() {     
59      Serializer serializer = new Serializer(
60        customConfigurations,
61        GetDefaultConfig(XmlFormat.Instance),
62        "CustomConfigurations");
63      XmlGenerator generator = new XmlGenerator();
64      StringBuilder configurationString = new StringBuilder();
65      foreach (ISerializationToken token in serializer) {
66        configurationString.Append(generator.Format(token));
67      }
68      StringBuilder configurationTypeCacheString = new StringBuilder();
69      foreach (string s in generator.Format(serializer.TypeCache))
70        configurationTypeCacheString.Append(s);
[1542]71      Properties.Settings.Default.CustomConfigurations =
[1454]72        configurationString.ToString();
[1542]73      Properties.Settings.Default.CustomConfigurationsTypeCache =
[1454]74        configurationTypeCacheString.ToString();
75      Properties.Settings.Default.Save();
76    }
77
78    public void Reset() {     
79      customConfigurations.Clear();
80      Formatters.Clear();
81      Decomposers.Clear();
82      Assembly defaultAssembly = Assembly.GetExecutingAssembly();
83      DiscoverFrom(defaultAssembly);
84      foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
85        if ( a != defaultAssembly )
86          DiscoverFrom(a);
[1539]87      SortDecomposers();
[1454]88    }
89
[1539]90    class PriortiySorter : IComparer<IDecomposer> {
91      public int Compare(IDecomposer x, IDecomposer y) {
92        return y.Priority - x.Priority;
93      }
94    }
95
96    protected void SortDecomposers() {
97      Decomposers.Sort(new PriortiySorter());
98    }
99
[1454]100    protected void DiscoverFrom(Assembly a) {
101      foreach (Type t in a.GetTypes()) {
102        if (t.GetInterface(typeof (IFormatter).FullName) != null) {
103          try {
104            IFormatter formatter = (IFormatter) Activator.CreateInstance(t, true);
105            if ( ! Formatters.ContainsKey(formatter.Format) ) {
106              Formatters.Add(formatter.Format, new List<IFormatter>());
107            }
108            Formatters[formatter.Format].Add(formatter);
[1554]109            Logger.Debug("discovered formatter " + t.VersionInvariantName());
[1473]110          } catch (MissingMethodException e) {
111            Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e);           
[1554]112          } catch (ArgumentException e) {
113            Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e);
114          }
[1454]115        }
116        if (t.GetInterface(typeof (IDecomposer).FullName) != null) {
117          try {
118            Decomposers.Add((IDecomposer) Activator.CreateInstance(t, true));
[1554]119            Logger.Debug("discovered decomposer " + t.VersionInvariantName());
[1473]120          } catch (MissingMethodException e) {
[1554]121            Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e);         
122          } catch (ArgumentException e) {
123            Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e);
[1454]124          }
125        }
126      }
127    }
128
129    public Configuration GetDefaultConfig(IFormat format) {
130      Dictionary<Type, IFormatter> formatterConfig = new Dictionary<Type, IFormatter>();
131      foreach ( IFormatter f in Formatters[format] ) {
132        if ( ! formatterConfig.ContainsKey(f.Type) )
133          formatterConfig.Add(f.Type, f);
134      }
135      return new Configuration(formatterConfig, Decomposers);
136    }
137
138    public Configuration GetConfiguration(IFormat format) {
139      if (customConfigurations.ContainsKey(format))
140        return customConfigurations[format];
141      return GetDefaultConfig(format);
142    }
143
144    public void DefineConfiguration(IFormat format, Configuration configuration) {
145      customConfigurations[format] = configuration;
146      SaveSettings();
147    }
148
149  }
150 
151}
Note: See TracBrowser for help on using the repository browser.