From 2ca4b7d03adfa3cc7c9c6a597a11762142d5b34b Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Mon, 4 Mar 2013 00:43:06 -0500 Subject: Created IConfigurationManager --- MediaBrowser.Common/Kernel/BaseKernel.cs | 225 ++----------------------------- 1 file changed, 12 insertions(+), 213 deletions(-) (limited to 'MediaBrowser.Common/Kernel/BaseKernel.cs') diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs index 489423d9e..cf8133e97 100644 --- a/MediaBrowser.Common/Kernel/BaseKernel.cs +++ b/MediaBrowser.Common/Kernel/BaseKernel.cs @@ -1,45 +1,21 @@ -using MediaBrowser.Common.Events; -using MediaBrowser.Common.Security; -using MediaBrowser.Model.Configuration; +using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Events; using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Serialization; using MediaBrowser.Model.System; using System; -using System.IO; -using System.Linq; -using System.Threading; namespace MediaBrowser.Common.Kernel { /// /// Represents a shared base kernel for both the Ui and server apps /// - /// The type of the T configuration type. - /// The type of the T application paths type. - public abstract class BaseKernel : IDisposable, IKernel - where TConfigurationType : BaseApplicationConfiguration, new() - where TApplicationPathsType : IApplicationPaths + public abstract class BaseKernel : IKernel { /// /// Occurs when [has pending restart changed]. /// public event EventHandler HasPendingRestartChanged; - #region ConfigurationUpdated Event - /// - /// Occurs when [configuration updated]. - /// - public event EventHandler ConfigurationUpdated; - - /// - /// Called when [configuration updated]. - /// - internal void OnConfigurationUpdated() - { - EventHelper.QueueEventIfNotNull(ConfigurationUpdated, this, EventArgs.Empty, Logger); - } - #endregion - #region ApplicationUpdated Event /// /// Occurs when [application updated]. @@ -57,65 +33,12 @@ namespace MediaBrowser.Common.Kernel } #endregion - /// - /// The _configuration loaded - /// - private bool _configurationLoaded; - /// - /// The _configuration sync lock - /// - private object _configurationSyncLock = new object(); - /// - /// The _configuration - /// - private TConfigurationType _configuration; - /// - /// Gets the system configuration - /// - /// The configuration. - public TConfigurationType Configuration - { - get - { - // Lazy load - LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationLoaded, ref _configurationSyncLock, () => GetXmlConfiguration(ApplicationPaths.SystemConfigurationFilePath)); - return _configuration; - } - protected set - { - _configuration = value; - - if (value == null) - { - _configurationLoaded = false; - } - } - } - /// /// Gets or sets a value indicating whether this instance has changes that require the entire application to restart. /// /// true if this instance has pending application restart; otherwise, false. public bool HasPendingRestart { get; private set; } - /// - /// Gets the application paths. - /// - /// The application paths. - public TApplicationPathsType ApplicationPaths { get; private set; } - - /// - /// Gets or sets the TCP manager. - /// - /// The TCP manager. - private IServerManager ServerManager { get; set; } - - /// - /// Gets the plug-in security manager. - /// - /// The plug-in security manager. - public ISecurityManager SecurityManager { get; set; } - /// /// Gets the UDP server port number. /// This can't be configurable because then the user would have to configure their client to discover the server. @@ -141,7 +64,7 @@ namespace MediaBrowser.Common.Kernel { get { - return "http://+:" + Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/"; + return "http://+:" + _configurationManager.CommonConfiguration.HttpServerPortNumber + "/" + WebApplicationName + "/"; } } @@ -163,25 +86,18 @@ namespace MediaBrowser.Common.Kernel /// The application host. protected IApplicationHost ApplicationHost { get; private set; } - /// - /// The _XML serializer - /// - private readonly IXmlSerializer _xmlSerializer; + private readonly IConfigurationManager _configurationManager; /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The app host. - /// The app paths. - /// The XML serializer. - /// The logger. - /// isoManager - protected BaseKernel(IApplicationHost appHost, TApplicationPathsType appPaths, IXmlSerializer xmlSerializer, ILogger logger) + /// The log manager. + protected BaseKernel(IApplicationHost appHost, ILogManager logManager, IConfigurationManager configurationManager) { - ApplicationPaths = appPaths; ApplicationHost = appHost; - _xmlSerializer = xmlSerializer; - Logger = logger; + _configurationManager = configurationManager; + Logger = logManager.GetLogger("Kernel"); } /// @@ -201,7 +117,6 @@ namespace MediaBrowser.Common.Kernel /// Task. protected virtual void ReloadInternal() { - ServerManager = ApplicationHost.Resolve(); } /// @@ -214,24 +129,6 @@ namespace MediaBrowser.Common.Kernel EventHelper.QueueEventIfNotNull(HasPendingRestartChanged, this, EventArgs.Empty, Logger); } - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// Releases unmanaged and - optionally - managed resources. - /// - /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected virtual void Dispose(bool dispose) - { - - } - /// /// Performs the pending restart. /// @@ -261,108 +158,10 @@ namespace MediaBrowser.Common.Kernel HasPendingRestart = HasPendingRestart, Version = ApplicationHost.ApplicationVersion.ToString(), IsNetworkDeployed = ApplicationHost.CanSelfUpdate, - WebSocketPortNumber = ServerManager.WebSocketPortNumber, - SupportsNativeWebSocket = ServerManager.SupportsNativeWebSocket, + WebSocketPortNumber = ApplicationHost.Resolve().WebSocketPortNumber, + SupportsNativeWebSocket = ApplicationHost.Resolve().SupportsNativeWebSocket, FailedPluginAssemblies = ApplicationHost.FailedAssemblies.ToArray() }; } - - /// - /// The _save lock - /// - private readonly object _configurationSaveLock = new object(); - - /// - /// Saves the current configuration - /// - public void SaveConfiguration() - { - lock (_configurationSaveLock) - { - _xmlSerializer.SerializeToFile(Configuration, ApplicationPaths.SystemConfigurationFilePath); - } - - OnConfigurationUpdated(); - } - - /// - /// Gets the application paths. - /// - /// The application paths. - IApplicationPaths IKernel.ApplicationPaths - { - get { return ApplicationPaths; } - } - /// - /// Gets the configuration. - /// - /// The configuration. - BaseApplicationConfiguration IKernel.Configuration - { - get { return Configuration; } - } - - /// - /// Reads an xml configuration file from the file system - /// It will immediately re-serialize and save if new serialization data is available due to property changes - /// - /// The type. - /// The path. - /// System.Object. - public object GetXmlConfiguration(Type type, string path) - { - Logger.Info("Loading {0} at {1}", type.Name, path); - - object configuration; - - byte[] buffer = null; - - // Use try/catch to avoid the extra file system lookup using File.Exists - try - { - buffer = File.ReadAllBytes(path); - - configuration = _xmlSerializer.DeserializeFromBytes(type, buffer); - } - catch (FileNotFoundException) - { - configuration = Activator.CreateInstance(type); - } - - // Take the object we just got and serialize it back to bytes - var newBytes = _xmlSerializer.SerializeToBytes(configuration); - - // If the file didn't exist before, or if something has changed, re-save - if (buffer == null || !buffer.SequenceEqual(newBytes)) - { - Logger.Info("Saving {0} to {1}", type.Name, path); - - // Save it after load in case we got new items - File.WriteAllBytes(path, newBytes); - } - - return configuration; - } - - - /// - /// Reads an xml configuration file from the file system - /// It will immediately save the configuration after loading it, just - /// in case there are new serializable properties - /// - /// - /// The path. - /// ``0. - private T GetXmlConfiguration(string path) - where T : class - { - return GetXmlConfiguration(typeof(T), path) as T; - } - - /// - /// Limits simultaneous access to various resources - /// - /// The resource pools. - public ResourcePool ResourcePools { get; set; } } } -- cgit v1.2.3