1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Reflection;
|
---|
4 | using HeuristicLab.Persistence.Interfaces;
|
---|
5 |
|
---|
6 | namespace 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 | } |
---|