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