Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/Core/ConfigurationService.cs @ 1425

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

Use version invariant assembly qualified name and update plug-in assembly name. (#506)

File size: 4.9 KB
Line 
1using System;
2using System.Collections.Specialized;
3using System.IO;
4using System.Linq;
5using System.Collections.Generic;
6using System.Reflection;
7using System.Text;
8using HeuristicLab.Persistence.Default.Xml;
9using HeuristicLab.Persistence.Interfaces;
10
11namespace HeuristicLab.Persistence.Core {
12 
13  public class ConfigurationService {
14
15    private static ConfigurationService instance;
16    private readonly Dictionary<IFormat, Configuration> customConfigurations;
17    public readonly Dictionary<IFormat, List<IFormatter>> Formatters;
18    public readonly List<IDecomposer> Decomposers;
19   
20    public static ConfigurationService Instance {
21      get {
22        if (instance == null)
23          instance = new ConfigurationService();
24        return instance;
25      }
26    }
27
28    public ConfigurationService() {
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 {
38        if (String.IsNullOrEmpty(Properties.Settings.Default.customConfigurations) ||
39          String.IsNullOrEmpty(Properties.Settings.Default.customConfigurationsTypeCache))
40          return;
41        DeSerializer deSerializer = new DeSerializer(
42          XmlParser.ParseTypeCache(
43          new StringReader(
44            Properties.Settings.Default.customConfigurationsTypeCache)));
45        XmlParser parser = new XmlParser(
46          new StringReader(
47            Properties.Settings.Default.customConfigurations));
48        var newCustomConfigurations = (Dictionary<IFormat, Configuration>)
49          deSerializer.DeSerialize(parser);
50        foreach (var config in newCustomConfigurations) {
51          customConfigurations[config.Key] = config.Value;
52        }
53      } catch {
54        Console.WriteLine("WARNING: Could not load settings.");
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);
71      Properties.Settings.Default.customConfigurations =
72        configurationString.ToString();
73      Properties.Settings.Default.customConfigurationsTypeCache =
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);
87    }
88
89    protected void DiscoverFrom(Assembly a) {
90      foreach (Type t in a.GetTypes()) {
91        if (t.GetInterface(typeof (IFormatter).FullName) != null) {
92          try {
93            IFormatter formatter = (IFormatter) Activator.CreateInstance(t, true);
94            if ( ! Formatters.ContainsKey(formatter.Format) ) {
95              Formatters.Add(formatter.Format, new List<IFormatter>());
96            }
97            Formatters[formatter.Format].Add(formatter);
98          } catch (MissingMethodException) {
99            Console.WriteLine("WARNING: Could not instantiate {0}", t.VersionInvariantName());
100          }         
101        }
102        if (t.GetInterface(typeof (IDecomposer).FullName) != null) {
103          try {
104            Decomposers.Add((IDecomposer) Activator.CreateInstance(t, true));
105          } catch (MissingMethodException) {
106            Console.WriteLine("WARNING: Could not instantiate {0}", t.VersionInvariantName());
107          }
108        }
109      }
110    }
111
112    public Configuration GetDefaultConfig(IFormat format) {
113      Dictionary<Type, IFormatter> formatterConfig = new Dictionary<Type, IFormatter>();
114      foreach ( IFormatter f in Formatters[format] ) {
115        if ( ! formatterConfig.ContainsKey(f.Type) )
116          formatterConfig.Add(f.Type, f);
117      }
118      return new Configuration(formatterConfig, Decomposers);
119    }
120
121    public Configuration GetConfiguration(IFormat format) {
122      if (customConfigurations.ContainsKey(format))
123        return customConfigurations[format];
124      return GetDefaultConfig(format);
125    }
126
127    public void DefineConfiguration(IFormat format, Configuration configuration) {
128      customConfigurations[format] = configuration;
129      SaveSettings();
130    }
131
132  }
133 
134}
Note: See TracBrowser for help on using the repository browser.