1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Reflection;
|
---|
5 | using System.Text;
|
---|
6 | using HeuristicLab.Persistence.Default.Xml;
|
---|
7 | using HeuristicLab.Persistence.Interfaces;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Persistence.Core {
|
---|
10 |
|
---|
11 | public class ConfigurationService {
|
---|
12 |
|
---|
13 | private static ConfigurationService instance;
|
---|
14 | private readonly Dictionary<IFormat, Configuration> customConfigurations;
|
---|
15 | public readonly Dictionary<IFormat, List<IFormatter>> Formatters;
|
---|
16 | public readonly List<IDecomposer> Decomposers;
|
---|
17 |
|
---|
18 | public static ConfigurationService Instance {
|
---|
19 | get {
|
---|
20 | if (instance == null)
|
---|
21 | instance = new ConfigurationService();
|
---|
22 | return instance;
|
---|
23 | }
|
---|
24 | }
|
---|
25 |
|
---|
26 | public ConfigurationService() {
|
---|
27 | Formatters = new Dictionary<IFormat, List<IFormatter>>();
|
---|
28 | Decomposers = new List<IDecomposer>();
|
---|
29 | customConfigurations = new Dictionary<IFormat, Configuration>();
|
---|
30 | Reset();
|
---|
31 | LoadSettings();
|
---|
32 | }
|
---|
33 |
|
---|
34 | public void LoadSettings() {
|
---|
35 | try {
|
---|
36 | if (String.IsNullOrEmpty(Properties.Settings.Default.customConfigurations) ||
|
---|
37 | String.IsNullOrEmpty(Properties.Settings.Default.customConfigurationsTypeCache))
|
---|
38 | return;
|
---|
39 | DeSerializer deSerializer = new DeSerializer(
|
---|
40 | XmlParser.ParseTypeCache(
|
---|
41 | new StringReader(
|
---|
42 | Properties.Settings.Default.customConfigurationsTypeCache)));
|
---|
43 | XmlParser parser = new XmlParser(
|
---|
44 | new StringReader(
|
---|
45 | Properties.Settings.Default.customConfigurations));
|
---|
46 | var newCustomConfigurations = (Dictionary<IFormat, Configuration>)
|
---|
47 | deSerializer.DeSerialize(parser);
|
---|
48 | foreach (var config in newCustomConfigurations) {
|
---|
49 | customConfigurations[config.Key] = config.Value;
|
---|
50 | }
|
---|
51 | } catch {
|
---|
52 | Console.WriteLine("WARNING: Could not load settings.");
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void SaveSettings() {
|
---|
57 | Serializer serializer = new Serializer(
|
---|
58 | customConfigurations,
|
---|
59 | GetDefaultConfig(XmlFormat.Instance),
|
---|
60 | "CustomConfigurations");
|
---|
61 | XmlGenerator generator = new XmlGenerator();
|
---|
62 | StringBuilder configurationString = new StringBuilder();
|
---|
63 | foreach (ISerializationToken token in serializer) {
|
---|
64 | configurationString.Append(generator.Format(token));
|
---|
65 | }
|
---|
66 | StringBuilder configurationTypeCacheString = new StringBuilder();
|
---|
67 | foreach (string s in generator.Format(serializer.TypeCache))
|
---|
68 | configurationTypeCacheString.Append(s);
|
---|
69 | Properties.Settings.Default.customConfigurations =
|
---|
70 | configurationString.ToString();
|
---|
71 | Properties.Settings.Default.customConfigurationsTypeCache =
|
---|
72 | configurationTypeCacheString.ToString();
|
---|
73 | Properties.Settings.Default.Save();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void Reset() {
|
---|
77 | customConfigurations.Clear();
|
---|
78 | Formatters.Clear();
|
---|
79 | Decomposers.Clear();
|
---|
80 | Assembly defaultAssembly = Assembly.GetExecutingAssembly();
|
---|
81 | DiscoverFrom(defaultAssembly);
|
---|
82 | foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
|
---|
83 | if ( a != defaultAssembly )
|
---|
84 | DiscoverFrom(a);
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected void DiscoverFrom(Assembly a) {
|
---|
88 | foreach (Type t in a.GetTypes()) {
|
---|
89 | if (t.GetInterface(typeof (IFormatter).FullName) != null) {
|
---|
90 | try {
|
---|
91 | IFormatter formatter = (IFormatter) Activator.CreateInstance(t, true);
|
---|
92 | if ( ! Formatters.ContainsKey(formatter.Format) ) {
|
---|
93 | Formatters.Add(formatter.Format, new List<IFormatter>());
|
---|
94 | }
|
---|
95 | Formatters[formatter.Format].Add(formatter);
|
---|
96 | } catch (MissingMethodException) {
|
---|
97 | Console.WriteLine("WARNING: Could not instantiate {0}", t.VersionInvariantName());
|
---|
98 | }
|
---|
99 | }
|
---|
100 | if (t.GetInterface(typeof (IDecomposer).FullName) != null) {
|
---|
101 | try {
|
---|
102 | Decomposers.Add((IDecomposer) Activator.CreateInstance(t, true));
|
---|
103 | } catch (MissingMethodException) {
|
---|
104 | Console.WriteLine("WARNING: Could not instantiate {0}", t.VersionInvariantName());
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public Configuration GetDefaultConfig(IFormat format) {
|
---|
111 | Dictionary<Type, IFormatter> formatterConfig = new Dictionary<Type, IFormatter>();
|
---|
112 | foreach ( IFormatter f in Formatters[format] ) {
|
---|
113 | if ( ! formatterConfig.ContainsKey(f.Type) )
|
---|
114 | formatterConfig.Add(f.Type, f);
|
---|
115 | }
|
---|
116 | return new Configuration(formatterConfig, Decomposers);
|
---|
117 | }
|
---|
118 |
|
---|
119 | public Configuration GetConfiguration(IFormat format) {
|
---|
120 | if (customConfigurations.ContainsKey(format))
|
---|
121 | return customConfigurations[format];
|
---|
122 | return GetDefaultConfig(format);
|
---|
123 | }
|
---|
124 |
|
---|
125 | public void DefineConfiguration(IFormat format, Configuration configuration) {
|
---|
126 | customConfigurations[format] = configuration;
|
---|
127 | SaveSettings();
|
---|
128 | }
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | } |
---|