Free cookie consent management tool by TermsFeed Policy Generator

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

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

Deposit GUI-generated Configuration object at ConfigurationService. (#506)

File size: 2.9 KB
RevLine 
[1400]1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using HeuristicLab.Persistence.Interfaces;
5
6namespace HeuristicLab.Persistence.Core {
7 
8  public class ConfigurationService {
9
10    private static ConfigurationService instance;
11    private Dictionary<IFormat, Configuration> customConfigurations;
12    public readonly Dictionary<IFormat, List<IFormatter>> Formatters;
13    public readonly List<IDecomposer> Decomposers;
14   
15    public static ConfigurationService Instance {
16      get {
17        if (instance == null)
18          instance = new ConfigurationService();
19        return instance;
20      }
21    }
22
23    public ConfigurationService() {
24      Formatters = new Dictionary<IFormat, List<IFormatter>>();
25      Decomposers = new List<IDecomposer>();
26      customConfigurations = new Dictionary<IFormat, Configuration>();
27      Reset();
28    }
29
30    public void Reset() {     
31      customConfigurations.Clear();
32      Assembly defaultAssembly = Assembly.GetExecutingAssembly();
33      DiscoverFrom(defaultAssembly);
34      foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
35        if ( a != defaultAssembly )
36          DiscoverFrom(a);
37    }
38
39    protected void DiscoverFrom(Assembly a) {
40      foreach (Type t in a.GetTypes()) {
41        if (t.GetInterface(typeof (IFormatter).FullName) != null) {
42          try {
43            IFormatter formatter = (IFormatter) Activator.CreateInstance(t, true);
44            if ( ! Formatters.ContainsKey(formatter.Format) ) {
45              Formatters.Add(formatter.Format, new List<IFormatter>());
46            }
47            Formatters[formatter.Format].Add(formatter);
48          } catch (MissingMethodException) {
49            Console.WriteLine("WARNING: Could not instantiate {0}", t.FullName);
50          }         
51        }
52        if (t.GetInterface(typeof (IDecomposer).FullName) != null) {
53          try {
54            Decomposers.Add((IDecomposer) Activator.CreateInstance(t, true));
55          } catch (MissingMethodException) {
56            Console.WriteLine("WARNING: Could not instantiate {0}", t.FullName);
57          }
58        }
59      }
60    }
61
62    public Configuration GetDefaultConfig(IFormat format) {
63      Dictionary<Type, IFormatter> formatterConfig = new Dictionary<Type, IFormatter>();
64      foreach ( IFormatter f in Formatters[format] ) {
65        if ( ! formatterConfig.ContainsKey(f.Type) )
66          formatterConfig.Add(f.Type, f);
67      }
68      return new Configuration(formatterConfig, Decomposers);
69    }
70
71    public Configuration GetConfiguration(IFormat format) {
72      if (customConfigurations.ContainsKey(format))
73        return customConfigurations[format];
74      return GetDefaultConfig(format);
75    }
76
77    public void DefineConfiguration(IFormat format, Configuration configuration) {
78      customConfigurations[format] = configuration;
79    }
80
81  }
82 
83}
Note: See TracBrowser for help on using the repository browser.