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 ++---------------------- MediaBrowser.Common/Kernel/IApplicationHost.cs | 120 ------------- MediaBrowser.Common/Kernel/IApplicationPaths.cs | 82 --------- MediaBrowser.Common/Kernel/IKernel.cs | 46 +---- MediaBrowser.Common/Kernel/ResourcePool.cs | 79 --------- 5 files changed, 14 insertions(+), 538 deletions(-) delete mode 100644 MediaBrowser.Common/Kernel/IApplicationHost.cs delete mode 100644 MediaBrowser.Common/Kernel/IApplicationPaths.cs delete mode 100644 MediaBrowser.Common/Kernel/ResourcePool.cs (limited to 'MediaBrowser.Common/Kernel') diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs index 489423d9eb..cf8133e977 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; } } } diff --git a/MediaBrowser.Common/Kernel/IApplicationHost.cs b/MediaBrowser.Common/Kernel/IApplicationHost.cs deleted file mode 100644 index 38a1cb3180..0000000000 --- a/MediaBrowser.Common/Kernel/IApplicationHost.cs +++ /dev/null @@ -1,120 +0,0 @@ -using MediaBrowser.Common.Plugins; -using MediaBrowser.Model.Updates; -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; - -namespace MediaBrowser.Common.Kernel -{ - /// - /// An interface to be implemented by the applications hosting a kernel - /// - public interface IApplicationHost - { - /// - /// Restarts this instance. - /// - void Restart(); - - /// - /// Configures the auto run at startup. - /// - /// if set to true [autorun]. - void ConfigureAutoRunAtStartup(bool autorun); - - /// - /// Gets the application version. - /// - /// The application version. - Version ApplicationVersion { get; } - - /// - /// Gets or sets a value indicating whether this instance can self update. - /// - /// true if this instance can self update; otherwise, false. - bool CanSelfUpdate { get; } - - /// - /// Gets a value indicating whether this instance is first run. - /// - /// true if this instance is first run; otherwise, false. - bool IsFirstRun { get; } - - /// - /// Gets the failed assemblies. - /// - /// The failed assemblies. - List FailedAssemblies { get; } - - /// - /// Gets all concrete types. - /// - /// All concrete types. - Type[] AllConcreteTypes { get; } - - /// - /// Gets the exports. - /// - /// - /// if set to true [manage liftime]. - /// IEnumerable{``0}. - IEnumerable GetExports(bool manageLiftime = true); - - /// - /// Checks for update. - /// - /// Task{CheckForUpdateResult}. - Task CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress progress); - - /// - /// Updates the application. - /// - /// Task. - Task UpdateApplication(PackageVersionInfo package, CancellationToken cancellationToken, IProgress progress); - - /// - /// Creates an instance of type and resolves all constructor dependancies - /// - /// The type. - /// System.Object. - object CreateInstance(Type type); - - /// - /// Resolves this instance. - /// - /// - /// ``0. - T Resolve(); - - /// - /// Resolves this instance. - /// - /// - /// ``0. - T TryResolve(); - - /// - /// Shuts down. - /// - void Shutdown(); - - /// - /// Gets the plugins. - /// - /// The plugins. - IEnumerable Plugins { get; } - - /// - /// Removes the plugin. - /// - /// The plugin. - void RemovePlugin(IPlugin plugin); - - /// - /// Inits this instance. - /// - /// Task. - Task Init(); - } -} diff --git a/MediaBrowser.Common/Kernel/IApplicationPaths.cs b/MediaBrowser.Common/Kernel/IApplicationPaths.cs deleted file mode 100644 index 52c3b199d8..0000000000 --- a/MediaBrowser.Common/Kernel/IApplicationPaths.cs +++ /dev/null @@ -1,82 +0,0 @@ - -namespace MediaBrowser.Common.Kernel -{ - /// - /// Interface IApplicationPaths - /// - public interface IApplicationPaths - { - /// - /// Gets the path to the program data folder - /// - /// The program data path. - string ProgramDataPath { get; } - - /// - /// Gets the path to the program system folder - /// - /// The program data path. - string ProgramSystemPath { get; } - - /// - /// Gets the folder path to the data directory - /// - /// The data directory. - string DataPath { get; } - - /// - /// Gets the image cache path. - /// - /// The image cache path. - string ImageCachePath { get; } - - /// - /// Gets the path to the plugin directory - /// - /// The plugins path. - string PluginsPath { get; } - - /// - /// Gets the path to the plugin configurations directory - /// - /// The plugin configurations path. - string PluginConfigurationsPath { get; } - - /// - /// Gets the path to where temporary update files will be stored - /// - /// The plugin configurations path. - string TempUpdatePath { get; } - - /// - /// Gets the path to the log directory - /// - /// The log directory path. - string LogDirectoryPath { get; } - - /// - /// Gets the path to the application configuration root directory - /// - /// The configuration directory path. - string ConfigurationDirectoryPath { get; } - - /// - /// Gets the path to the system configuration file - /// - /// The system configuration file path. - string SystemConfigurationFilePath { get; } - - /// - /// Gets the folder path to the cache directory - /// - /// The cache directory. - string CachePath { get; } - - /// - /// Gets the folder path to the temp directory within the cache folder - /// - /// The temp directory. - string TempDirectory { get; } - } - -} diff --git a/MediaBrowser.Common/Kernel/IKernel.cs b/MediaBrowser.Common/Kernel/IKernel.cs index 1a2d86d7f2..51677677ac 100644 --- a/MediaBrowser.Common/Kernel/IKernel.cs +++ b/MediaBrowser.Common/Kernel/IKernel.cs @@ -1,35 +1,18 @@ -using MediaBrowser.Common.Plugins; -using MediaBrowser.Common.Security; -using MediaBrowser.Model.Configuration; -using MediaBrowser.Model.System; +using MediaBrowser.Model.System; using System; -using System.Collections.Generic; -using System.Threading.Tasks; namespace MediaBrowser.Common.Kernel { /// /// Interface IKernel /// - public interface IKernel : IDisposable + public interface IKernel { /// /// Occurs when [has pending restart changed]. /// event EventHandler HasPendingRestartChanged; - /// - /// Gets the application paths. - /// - /// The application paths. - IApplicationPaths ApplicationPaths { get; } - - /// - /// Gets the configuration. - /// - /// The configuration. - BaseApplicationConfiguration Configuration { get; } - /// /// Gets the kernel context. /// @@ -83,34 +66,9 @@ namespace MediaBrowser.Common.Kernel /// The HTTP server URL prefix. string HttpServerUrlPrefix { get; } - /// - /// Gets the plug-in security manager. - /// - /// The plug-in security manager. - ISecurityManager SecurityManager { get; set; } - - /// - /// Occurs when [configuration updated]. - /// - event EventHandler ConfigurationUpdated; - /// /// Notifies the pending restart. /// void NotifyPendingRestart(); - - /// - /// Gets the XML configuration. - /// - /// The type. - /// The path. - /// System.Object. - object GetXmlConfiguration(Type type, string path); - - /// - /// Limits simultaneous access to various resources - /// - /// The resource pools. - ResourcePool ResourcePools { get; set; } } } diff --git a/MediaBrowser.Common/Kernel/ResourcePool.cs b/MediaBrowser.Common/Kernel/ResourcePool.cs deleted file mode 100644 index 8a2ab8af8e..0000000000 --- a/MediaBrowser.Common/Kernel/ResourcePool.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System; -using System.Threading; - -namespace MediaBrowser.Common.Kernel -{ - /// - /// This is just a collection of semaphores to control the number of concurrent executions of various resources - /// - public class ResourcePool : IDisposable - { - /// - /// You tube - /// - public readonly SemaphoreSlim YouTube = new SemaphoreSlim(5, 5); - - /// - /// The trakt - /// - public readonly SemaphoreSlim Trakt = new SemaphoreSlim(5, 5); - - /// - /// The tv db - /// - public readonly SemaphoreSlim TvDb = new SemaphoreSlim(5, 5); - - /// - /// The movie db - /// - public readonly SemaphoreSlim MovieDb = new SemaphoreSlim(5, 5); - - /// - /// The fan art - /// - public readonly SemaphoreSlim FanArt = new SemaphoreSlim(5, 5); - - /// - /// The mb - /// - public readonly SemaphoreSlim Mb = new SemaphoreSlim(5, 5); - - /// - /// Apple doesn't seem to like too many simulataneous requests. - /// - public readonly SemaphoreSlim AppleTrailerVideos = new SemaphoreSlim(1, 1); - - /// - /// The apple trailer images - /// - public readonly SemaphoreSlim AppleTrailerImages = new SemaphoreSlim(1, 1); - - /// - /// 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) - { - if (dispose) - { - YouTube.Dispose(); - Trakt.Dispose(); - TvDb.Dispose(); - MovieDb.Dispose(); - FanArt.Dispose(); - Mb.Dispose(); - AppleTrailerVideos.Dispose(); - AppleTrailerImages.Dispose(); - } - } - } -} -- cgit v1.2.3