Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Persistence/3.3/Core/ConfigurationService.cs @ 17698

Last change on this file since 17698 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 10.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Reflection;
27using System.Text;
28using HEAL.Attic;
29using HeuristicLab.Persistence.Default.Xml;
30using HeuristicLab.Persistence.Interfaces;
31using HeuristicLab.Tracing;
32
33namespace HeuristicLab.Persistence.Core {
34
35  /// <summary>
36  /// Provides a persistable configuration of primitive and composite serializers for
37  /// all registered serial formats. Custom formats can be defined and will be saved
38  /// for future sessions. A default configuration can be generated through reflection.
39  ///
40  /// This class has only a single instance.
41  /// </summary>
42  public class ConfigurationService {
43
44    private static ConfigurationService instance;
45    private static object locker = new object();
46    private readonly Dictionary<IFormat, Configuration> customConfigurations;
47
48    /// <summary>
49    /// List of all available primitive serializers.
50    /// </summary>
51    public Dictionary<Type, List<IPrimitiveSerializer>> PrimitiveSerializers { get; private set; }
52
53    /// <summary>
54    /// List of all available composite serializers (discovered through reflection).
55    /// </summary>
56    public List<ICompositeSerializer> CompositeSerializers { get; private set; }
57
58    /// <summary>
59    /// List of all available formats (discovered through reflection).   
60    /// </summary>
61    public List<IFormat> Formats { get; private set; }
62
63    /// <summary>
64    /// Gets the singleton instance.
65    /// </summary>
66    /// <value>The singleton instance.</value>
67    public static ConfigurationService Instance {
68      get {
69        lock (locker) {
70          if (instance == null)
71            instance = new ConfigurationService();
72          return instance;
73        }
74      }
75    }
76
77    private ConfigurationService() {
78      PrimitiveSerializers = new Dictionary<Type, List<IPrimitiveSerializer>>();
79      CompositeSerializers = new List<ICompositeSerializer>();
80      customConfigurations = new Dictionary<IFormat, Configuration>();
81      Formats = new List<IFormat>();
82      Reset();
83      LoadSettings();
84    }
85
86    /// <summary>
87    /// Loads the settings.
88    /// </summary>
89    public void LoadSettings() {
90      LoadSettings(false);
91    }
92
93    /// <summary>
94    /// Loads the settings.
95    /// </summary>
96    /// <param name="throwOnError">if set to <c>true</c> throw on error.</param>
97    public void LoadSettings(bool throwOnError) {
98      try {
99        TryLoadSettings();
100      } catch (Exception e) {
101        if (throwOnError) {
102          throw new PersistenceException("Could not load persistence settings.", e);
103        } else {
104          Logger.Warn("Could not load settings.", e);
105        }
106      }
107    }
108
109    /// <summary>
110    /// Tries to load the settings (i.e custom configurations).
111    /// </summary>
112    protected void TryLoadSettings() {
113      if (String.IsNullOrEmpty(Properties.Settings.Default.CustomConfigurations) ||
114          String.IsNullOrEmpty(Properties.Settings.Default.CustomConfigurationsTypeCache))
115        return;
116      Deserializer deSerializer = new Deserializer(
117        XmlParser.ParseTypeCache(
118        new StringReader(
119          Properties.Settings.Default.CustomConfigurationsTypeCache)));
120      XmlParser parser = new XmlParser(
121        new StringReader(
122          Properties.Settings.Default.CustomConfigurations));
123      var newCustomConfigurations = (Dictionary<IFormat, Configuration>)
124        deSerializer.Deserialize(parser);
125      foreach (var config in newCustomConfigurations) {
126        customConfigurations[config.Key] = config.Value;
127      }
128    }
129
130    /// <summary>
131    /// Saves the settings (i.e custom configurations).
132    /// </summary>
133    protected void SaveSettings() {
134      Serializer serializer = new Serializer(
135        customConfigurations,
136        GetDefaultConfig(new XmlFormat()),
137        "CustomConfigurations");
138      XmlGenerator generator = new XmlGenerator();
139      StringBuilder configurationString = new StringBuilder();
140      foreach (ISerializationToken token in serializer) {
141        configurationString.Append(generator.Format(token));
142      }
143      StringBuilder configurationTypeCacheString = new StringBuilder();
144      foreach (string s in generator.Format(serializer.TypeCache))
145        configurationTypeCacheString.Append(s);
146      Properties.Settings.Default.CustomConfigurations =
147        configurationString.ToString();
148      Properties.Settings.Default.CustomConfigurationsTypeCache =
149        configurationTypeCacheString.ToString();
150      Properties.Settings.Default.Save();
151    }
152
153
154    /// <summary>
155    /// Rediscover available serializers and discard all custom configurations.
156    /// </summary>
157    public void Reset() {
158      customConfigurations.Clear();
159      PrimitiveSerializers.Clear();
160      CompositeSerializers.Clear();
161      Assembly defaultAssembly = Assembly.GetExecutingAssembly();
162      DiscoverFrom(defaultAssembly);
163      try {
164        foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
165          if (a != defaultAssembly)
166            DiscoverFrom(a);
167      } catch (AppDomainUnloadedException x) {
168        Logger.Warn("could not get list of assemblies, AppDomain has already been unloaded", x);
169      }
170      SortCompositeSerializers();
171    }
172
173    private class PriortiySorter : IComparer<ICompositeSerializer> {
174      public int Compare(ICompositeSerializer x, ICompositeSerializer y) {
175        return y.Priority - x.Priority;
176      }
177    }
178
179    /// <summary>
180    /// Sorts the composite serializers according to their priority.
181    /// </summary>
182    protected void SortCompositeSerializers() {
183      CompositeSerializers.Sort(new PriortiySorter());
184    }
185
186    /// <summary>
187    /// Discovers serializers from an assembly.
188    /// </summary>
189    /// <param name="a">An Assembly.</param>
190    protected void DiscoverFrom(Assembly a) {
191      try {
192        foreach (Type t in a.GetTypes()) {
193          if (t.GetInterface(typeof(IPrimitiveSerializer).FullName) != null &&
194              !t.IsAbstract && t.GetConstructor(Type.EmptyTypes) != null && !t.ContainsGenericParameters) {
195            IPrimitiveSerializer primitiveSerializer =
196              (IPrimitiveSerializer)Activator.CreateInstance(t, true);
197            if (!PrimitiveSerializers.ContainsKey(primitiveSerializer.SerialDataType))
198              PrimitiveSerializers.Add(primitiveSerializer.SerialDataType, new List<IPrimitiveSerializer>());
199            PrimitiveSerializers[primitiveSerializer.SerialDataType].Add(primitiveSerializer);
200          }
201          if (t.GetInterface(typeof(ICompositeSerializer).FullName) != null &&
202              !t.IsAbstract && t.GetConstructor(Type.EmptyTypes) != null && !t.ContainsGenericParameters) {
203            CompositeSerializers.Add((ICompositeSerializer)Activator.CreateInstance(t, true));
204          }
205          if (t.GetInterface(typeof(IFormat).FullName) != null &&
206             !t.IsAbstract && t.GetConstructor(Type.EmptyTypes) != null && !t.ContainsGenericParameters) {
207            IFormat format = (IFormat)Activator.CreateInstance(t, true);
208            Formats.Add(format);
209          }
210        }
211      } catch (ReflectionTypeLoadException e) {
212        Logger.Warn("could not analyse assembly: " + a.FullName, e);
213      }
214    }
215
216    /// <summary>
217    /// Get the default (automatically discovered) configuration for a certain format.
218    /// </summary>
219    /// <param name="format">The format.</param>
220    /// <returns>The default (auto discovered) configuration.</returns>
221    public Configuration GetDefaultConfig(IFormat format) {
222      Dictionary<Type, IPrimitiveSerializer> primitiveConfig = new Dictionary<Type, IPrimitiveSerializer>();
223      if (PrimitiveSerializers.ContainsKey(format.SerialDataType)) {
224        foreach (IPrimitiveSerializer f in PrimitiveSerializers[format.SerialDataType]) {
225          if (!primitiveConfig.ContainsKey(f.SourceType))
226            primitiveConfig.Add(f.SourceType, (IPrimitiveSerializer)Activator.CreateInstance(f.GetType()));
227        }
228      } else {
229        Logger.Warn(String.Format(
230          "No primitive serializers found for format {0} with serial data type {1}",
231          format.GetType().AssemblyQualifiedName,
232          format.SerialDataType.AssemblyQualifiedName));
233      }
234      return new Configuration(
235        format,
236        primitiveConfig.Values,
237        CompositeSerializers.Where((d) => d.Priority > 0).Select(d => (ICompositeSerializer)Activator.CreateInstance(d.GetType())));
238    }
239
240
241    /// <summary>
242    /// Get a configuration for a certain format. This returns a fresh copy of a custom configuration,
243    /// if defined, otherwise returns the default (automatically discovered) configuration.
244    /// </summary>
245    /// <param name="format">The format.</param>
246    /// <returns>A Configuration</returns>
247    public Configuration GetConfiguration(IFormat format) {
248      if (customConfigurations.ContainsKey(format))
249        return customConfigurations[format].Copy();
250      return GetDefaultConfig(format);
251    }
252
253    /// <summary>
254    /// Define a new custom configuration for a ceratin format.
255    /// </summary>
256    /// <param name="configuration">The new configuration.</param>
257    public void DefineConfiguration(Configuration configuration) {
258      customConfigurations[configuration.Format] = configuration.Copy();
259      SaveSettings();
260    }
261
262  }
263
264}
Note: See TracBrowser for help on using the repository browser.