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 | using HeuristicLab.Persistence.Interfaces.Tokens;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Persistence.Core {
|
---|
12 |
|
---|
13 | public class ConfigurationService {
|
---|
14 |
|
---|
15 | private static ConfigurationService instance;
|
---|
16 | private readonly Dictionary<IFormat, Configuration> customConfigurations;
|
---|
17 | public Dictionary<IFormat, List<IFormatter>> Formatters { get; private set; }
|
---|
18 | public List<IDecomposer> Decomposers { get; private set; }
|
---|
19 |
|
---|
20 | public static ConfigurationService Instance {
|
---|
21 | get {
|
---|
22 | if (instance == null)
|
---|
23 | instance = new ConfigurationService();
|
---|
24 | return instance;
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | private 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 (Exception e) {
|
---|
54 | Logger.Warn("Could not load settings.", e);
|
---|
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 | SortDecomposers();
|
---|
88 | }
|
---|
89 |
|
---|
90 | class PriortiySorter : IComparer<IDecomposer> {
|
---|
91 | public int Compare(IDecomposer x, IDecomposer y) {
|
---|
92 | return y.Priority - x.Priority;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected void SortDecomposers() {
|
---|
97 | Decomposers.Sort(new PriortiySorter());
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected void DiscoverFrom(Assembly a) {
|
---|
101 | foreach (Type t in a.GetTypes()) {
|
---|
102 | if (t.GetInterface(typeof (IFormatter).FullName) != null) {
|
---|
103 | try {
|
---|
104 | IFormatter formatter = (IFormatter) Activator.CreateInstance(t, true);
|
---|
105 | if ( ! Formatters.ContainsKey(formatter.Format) ) {
|
---|
106 | Formatters.Add(formatter.Format, new List<IFormatter>());
|
---|
107 | }
|
---|
108 | Formatters[formatter.Format].Add(formatter);
|
---|
109 | } catch (MissingMethodException e) {
|
---|
110 | Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | if (t.GetInterface(typeof (IDecomposer).FullName) != null) {
|
---|
114 | try {
|
---|
115 | Decomposers.Add((IDecomposer) Activator.CreateInstance(t, true));
|
---|
116 | } catch (MissingMethodException e) {
|
---|
117 | Logger.Warn("Could not instantiate " + t.VersionInvariantName(), e);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public Configuration GetDefaultConfig(IFormat format) {
|
---|
124 | Dictionary<Type, IFormatter> formatterConfig = new Dictionary<Type, IFormatter>();
|
---|
125 | foreach ( IFormatter f in Formatters[format] ) {
|
---|
126 | if ( ! formatterConfig.ContainsKey(f.Type) )
|
---|
127 | formatterConfig.Add(f.Type, f);
|
---|
128 | }
|
---|
129 | return new Configuration(formatterConfig, Decomposers);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public Configuration GetConfiguration(IFormat format) {
|
---|
133 | if (customConfigurations.ContainsKey(format))
|
---|
134 | return customConfigurations[format];
|
---|
135 | return GetDefaultConfig(format);
|
---|
136 | }
|
---|
137 |
|
---|
138 | public void DefineConfiguration(IFormat format, Configuration configuration) {
|
---|
139 | customConfigurations[format] = configuration;
|
---|
140 | SaveSettings();
|
---|
141 | }
|
---|
142 |
|
---|
143 | }
|
---|
144 |
|
---|
145 | } |
---|