From 5d55b36487b25b2efaf6923a3c069f4b0b59a449 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 20 Feb 2017 15:50:58 -0500 Subject: make more classes portable --- .../Configuration/BaseConfigurationManager.cs | 329 --------------------- 1 file changed, 329 deletions(-) delete mode 100644 Emby.Common.Implementations/Configuration/BaseConfigurationManager.cs (limited to 'Emby.Common.Implementations/Configuration/BaseConfigurationManager.cs') diff --git a/Emby.Common.Implementations/Configuration/BaseConfigurationManager.cs b/Emby.Common.Implementations/Configuration/BaseConfigurationManager.cs deleted file mode 100644 index 27c9fe615..000000000 --- a/Emby.Common.Implementations/Configuration/BaseConfigurationManager.cs +++ /dev/null @@ -1,329 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.Events; -using MediaBrowser.Common.Extensions; -using Emby.Common.Implementations; -using MediaBrowser.Model.Configuration; -using MediaBrowser.Model.IO; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Serialization; - -namespace Emby.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; - - /// - /// Occurs when [configuration updating]. - /// - public event EventHandler NamedConfigurationUpdating; - - /// - /// Occurs when [named configuration updated]. - /// - public event EventHandler NamedConfigurationUpdated; - - /// - /// 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; } - public readonly IFileSystem FileSystem; - - /// - /// 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, FileSystem)); - return _configuration; - } - protected set - { - _configuration = value; - - _configurationLoaded = value != null; - } - } - - private ConfigurationStore[] _configurationStores = { }; - private IConfigurationFactory[] _configurationFactories = { }; - - /// - /// Initializes a new instance of the class. - /// - /// The application paths. - /// The log manager. - /// The XML serializer. - protected BaseConfigurationManager(IApplicationPaths applicationPaths, ILogManager logManager, IXmlSerializer xmlSerializer, IFileSystem fileSystem) - { - CommonApplicationPaths = applicationPaths; - XmlSerializer = xmlSerializer; - FileSystem = fileSystem; - Logger = logManager.GetLogger(GetType().Name); - - UpdateCachePath(); - } - - public virtual void AddParts(IEnumerable factories) - { - _configurationFactories = factories.ToArray(); - - _configurationStores = _configurationFactories - .SelectMany(i => i.GetConfigurations()) - .ToArray(); - } - - /// - /// Saves the configuration. - /// - public void SaveConfiguration() - { - Logger.Info("Saving system configuration"); - var path = CommonApplicationPaths.SystemConfigurationFilePath; - - FileSystem.CreateDirectory(Path.GetDirectoryName(path)); - - lock (_configurationSyncLock) - { - XmlSerializer.SerializeToFile(CommonConfiguration, path); - } - - OnConfigurationUpdated(); - } - - /// - /// Called when [configuration updated]. - /// - protected virtual void OnConfigurationUpdated() - { - UpdateCachePath(); - - EventHelper.QueueEventIfNotNull(ConfigurationUpdated, this, EventArgs.Empty, Logger); - } - - /// - /// Replaces the configuration. - /// - /// The new configuration. - /// newConfiguration - public virtual void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration) - { - if (newConfiguration == null) - { - throw new ArgumentNullException("newConfiguration"); - } - - ValidateCachePath(newConfiguration); - - CommonConfiguration = newConfiguration; - SaveConfiguration(); - } - - /// - /// Updates the items by name path. - /// - private void UpdateCachePath() - { - string cachePath; - - if (string.IsNullOrWhiteSpace(CommonConfiguration.CachePath)) - { - cachePath = null; - } - else - { - cachePath = Path.Combine(CommonConfiguration.CachePath, "cache"); - } - - ((BaseApplicationPaths)CommonApplicationPaths).CachePath = cachePath; - } - - /// - /// Replaces the cache path. - /// - /// The new configuration. - /// - private void ValidateCachePath(BaseApplicationConfiguration newConfig) - { - var newPath = newConfig.CachePath; - - if (!string.IsNullOrWhiteSpace(newPath) - && !string.Equals(CommonConfiguration.CachePath ?? string.Empty, newPath)) - { - // Validate - if (!FileSystem.DirectoryExists(newPath)) - { - throw new FileNotFoundException(string.Format("{0} does not exist.", newPath)); - } - - EnsureWriteAccess(newPath); - } - } - - protected void EnsureWriteAccess(string path) - { - var file = Path.Combine(path, Guid.NewGuid().ToString()); - - FileSystem.WriteAllText(file, string.Empty); - FileSystem.DeleteFile(file); - } - - private readonly ConcurrentDictionary _configurations = new ConcurrentDictionary(); - - private string GetConfigurationFile(string key) - { - return Path.Combine(CommonApplicationPaths.ConfigurationDirectoryPath, key.ToLower() + ".xml"); - } - - public object GetConfiguration(string key) - { - return _configurations.GetOrAdd(key, k => - { - var file = GetConfigurationFile(key); - - var configurationInfo = _configurationStores - .FirstOrDefault(i => string.Equals(i.Key, key, StringComparison.OrdinalIgnoreCase)); - - if (configurationInfo == null) - { - throw new ResourceNotFoundException("Configuration with key " + key + " not found."); - } - - var configurationType = configurationInfo.ConfigurationType; - - lock (_configurationSyncLock) - { - return LoadConfiguration(file, configurationType); - } - }); - } - - private object LoadConfiguration(string path, Type configurationType) - { - try - { - return XmlSerializer.DeserializeFromFile(configurationType, path); - } - catch (FileNotFoundException) - { - return Activator.CreateInstance(configurationType); - } - catch (IOException) - { - return Activator.CreateInstance(configurationType); - } - catch (Exception ex) - { - Logger.ErrorException("Error loading configuration file: {0}", ex, path); - - return Activator.CreateInstance(configurationType); - } - } - - public void SaveConfiguration(string key, object configuration) - { - var configurationStore = GetConfigurationStore(key); - var configurationType = configurationStore.ConfigurationType; - - if (configuration.GetType() != configurationType) - { - throw new ArgumentException("Expected configuration type is " + configurationType.Name); - } - - var validatingStore = configurationStore as IValidatingConfiguration; - if (validatingStore != null) - { - var currentConfiguration = GetConfiguration(key); - - validatingStore.Validate(currentConfiguration, configuration); - } - - EventHelper.FireEventIfNotNull(NamedConfigurationUpdating, this, new ConfigurationUpdateEventArgs - { - Key = key, - NewConfiguration = configuration - - }, Logger); - - _configurations.AddOrUpdate(key, configuration, (k, v) => configuration); - - var path = GetConfigurationFile(key); - FileSystem.CreateDirectory(Path.GetDirectoryName(path)); - - lock (_configurationSyncLock) - { - XmlSerializer.SerializeToFile(configuration, path); - } - - OnNamedConfigurationUpdated(key, configuration); - } - - protected virtual void OnNamedConfigurationUpdated(string key, object configuration) - { - EventHelper.FireEventIfNotNull(NamedConfigurationUpdated, this, new ConfigurationUpdateEventArgs - { - Key = key, - NewConfiguration = configuration - - }, Logger); - } - - public Type GetConfigurationType(string key) - { - return GetConfigurationStore(key) - .ConfigurationType; - } - - private ConfigurationStore GetConfigurationStore(string key) - { - return _configurationStores - .First(i => string.Equals(i.Key, key, StringComparison.OrdinalIgnoreCase)); - } - } -} -- cgit v1.2.3