From 2ca4b7d03adfa3cc7c9c6a597a11762142d5b34b Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Mon, 4 Mar 2013 00:43:06 -0500 Subject: Created IConfigurationManager --- .../Configuration/BaseConfigurationManager.cs | 123 +++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs (limited to 'MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs') diff --git a/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs new file mode 100644 index 000000000..2f50f5f7a --- /dev/null +++ b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs @@ -0,0 +1,123 @@ +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Events; +using MediaBrowser.Model.Configuration; +using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Serialization; +using System; +using System.Threading; + +namespace MediaBrowser.Common.Implementations.Configuration +{ + /// + /// Class BaseConfigurationManager + /// + public abstract class BaseConfigurationManager : IConfigurationManager + { + /// + /// Gets the type of the configuration. + /// + /// The type of the configuration. + protected abstract Type ConfigurationType { get; } + + /// + /// Occurs when [configuration updated]. + /// + public event EventHandler ConfigurationUpdated; + + /// + /// Gets the logger. + /// + /// The logger. + protected ILogger Logger { get; private set; } + /// + /// Gets the XML serializer. + /// + /// The XML serializer. + protected IXmlSerializer XmlSerializer { get; private set; } + + /// + /// Gets or sets the application paths. + /// + /// The application paths. + public IApplicationPaths CommonApplicationPaths { get; private set; } + + /// + /// The _configuration loaded + /// + private bool _configurationLoaded; + /// + /// The _configuration sync lock + /// + private object _configurationSyncLock = new object(); + /// + /// The _configuration + /// + private BaseApplicationConfiguration _configuration; + /// + /// Gets the system configuration + /// + /// The configuration. + public BaseApplicationConfiguration CommonConfiguration + { + get + { + // Lazy load + LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationLoaded, ref _configurationSyncLock, () => (BaseApplicationConfiguration)ConfigurationHelper.GetXmlConfiguration(ConfigurationType, CommonApplicationPaths.SystemConfigurationFilePath, XmlSerializer)); + return _configuration; + } + protected set + { + _configuration = value; + + _configurationLoaded = value != null; + } + } + + /// + /// Initializes a new instance of the class. + /// + /// The application paths. + /// The log manager. + /// The XML serializer. + protected BaseConfigurationManager(IApplicationPaths applicationPaths, ILogManager logManager, IXmlSerializer xmlSerializer) + { + CommonApplicationPaths = applicationPaths; + XmlSerializer = xmlSerializer; + Logger = logManager.GetLogger(GetType().Name); + } + + /// + /// The _save lock + /// + private readonly object _configurationSaveLock = new object(); + + /// + /// Saves the configuration. + /// + public void SaveConfiguration() + { + lock (_configurationSaveLock) + { + XmlSerializer.SerializeToFile(CommonConfiguration, CommonApplicationPaths.SystemConfigurationFilePath); + } + + EventHelper.QueueEventIfNotNull(ConfigurationUpdated, this, EventArgs.Empty, Logger); + } + + /// + /// Replaces the configuration. + /// + /// The new configuration. + /// newConfiguration + public void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration) + { + if (newConfiguration == null) + { + throw new ArgumentNullException("newConfiguration"); + } + + CommonConfiguration = newConfiguration; + SaveConfiguration(); + } + } +} -- cgit v1.2.3