|
@@ -1,4 +1,5 @@
|
|
-using MediaBrowser.Common.Logging;
|
|
|
|
|
|
+using MediaBrowser.Common.Events;
|
|
|
|
+using MediaBrowser.Common.Logging;
|
|
using MediaBrowser.Common.Net;
|
|
using MediaBrowser.Common.Net;
|
|
using MediaBrowser.Common.Net.Handlers;
|
|
using MediaBrowser.Common.Net.Handlers;
|
|
using MediaBrowser.Common.Plugins;
|
|
using MediaBrowser.Common.Plugins;
|
|
@@ -9,7 +10,6 @@ using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.Composition;
|
|
using System.ComponentModel.Composition;
|
|
using System.ComponentModel.Composition.Hosting;
|
|
using System.ComponentModel.Composition.Hosting;
|
|
-using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Reflection;
|
|
@@ -24,6 +24,34 @@ namespace MediaBrowser.Common.Kernel
|
|
where TConfigurationType : BaseApplicationConfiguration, new()
|
|
where TConfigurationType : BaseApplicationConfiguration, new()
|
|
where TApplicationPathsType : BaseApplicationPaths, new()
|
|
where TApplicationPathsType : BaseApplicationPaths, new()
|
|
{
|
|
{
|
|
|
|
+ #region ReloadBeginning Event
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Fires whenever the kernel begins reloading
|
|
|
|
+ /// </summary>
|
|
|
|
+ public event EventHandler<GenericEventArgs<IProgress<TaskProgress>>> ReloadBeginning;
|
|
|
|
+ private void OnReloadBeginning(IProgress<TaskProgress> progress)
|
|
|
|
+ {
|
|
|
|
+ if (ReloadBeginning != null)
|
|
|
|
+ {
|
|
|
|
+ ReloadBeginning(this, new GenericEventArgs<IProgress<TaskProgress>> { Argument = progress });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ #endregion
|
|
|
|
+
|
|
|
|
+ #region ReloadCompleted Event
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Fires whenever the kernel completes reloading
|
|
|
|
+ /// </summary>
|
|
|
|
+ public event EventHandler<GenericEventArgs<IProgress<TaskProgress>>> ReloadCompleted;
|
|
|
|
+ private void OnReloadCompleted(IProgress<TaskProgress> progress)
|
|
|
|
+ {
|
|
|
|
+ if (ReloadCompleted != null)
|
|
|
|
+ {
|
|
|
|
+ ReloadCompleted(this, new GenericEventArgs<IProgress<TaskProgress>> { Argument = progress });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ #endregion
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Gets the current configuration
|
|
/// Gets the current configuration
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -43,6 +71,12 @@ namespace MediaBrowser.Common.Kernel
|
|
[ImportMany(typeof(BaseHandler))]
|
|
[ImportMany(typeof(BaseHandler))]
|
|
private IEnumerable<BaseHandler> HttpHandlers { get; set; }
|
|
private IEnumerable<BaseHandler> HttpHandlers { get; set; }
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Gets the list of currently registered Loggers
|
|
|
|
+ /// </summary>
|
|
|
|
+ [ImportMany(typeof(BaseLogger))]
|
|
|
|
+ public IEnumerable<BaseLogger> Loggers { get; set; }
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Both the Ui and server will have a built-in HttpServer.
|
|
/// Both the Ui and server will have a built-in HttpServer.
|
|
/// People will inevitably want remote control apps so it's needed in the Ui too.
|
|
/// People will inevitably want remote control apps so it's needed in the Ui too.
|
|
@@ -54,6 +88,8 @@ namespace MediaBrowser.Common.Kernel
|
|
/// </summary>
|
|
/// </summary>
|
|
private IDisposable HttpListener { get; set; }
|
|
private IDisposable HttpListener { get; set; }
|
|
|
|
|
|
|
|
+ private CompositionContainer CompositionContainer { get; set; }
|
|
|
|
+
|
|
protected virtual string HttpServerUrlPrefix
|
|
protected virtual string HttpServerUrlPrefix
|
|
{
|
|
{
|
|
get
|
|
get
|
|
@@ -72,13 +108,13 @@ namespace MediaBrowser.Common.Kernel
|
|
/// </summary>
|
|
/// </summary>
|
|
public async Task Init(IProgress<TaskProgress> progress)
|
|
public async Task Init(IProgress<TaskProgress> progress)
|
|
{
|
|
{
|
|
|
|
+ Logger.Kernel = this;
|
|
|
|
+
|
|
// Performs initializations that only occur once
|
|
// Performs initializations that only occur once
|
|
InitializeInternal(progress);
|
|
InitializeInternal(progress);
|
|
|
|
|
|
// Performs initializations that can be reloaded at anytime
|
|
// Performs initializations that can be reloaded at anytime
|
|
await Reload(progress).ConfigureAwait(false);
|
|
await Reload(progress).ConfigureAwait(false);
|
|
-
|
|
|
|
- progress.Report(new TaskProgress { Description = "Loading Complete" });
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -87,44 +123,39 @@ namespace MediaBrowser.Common.Kernel
|
|
protected virtual void InitializeInternal(IProgress<TaskProgress> progress)
|
|
protected virtual void InitializeInternal(IProgress<TaskProgress> progress)
|
|
{
|
|
{
|
|
ApplicationPaths = new TApplicationPathsType();
|
|
ApplicationPaths = new TApplicationPathsType();
|
|
-
|
|
|
|
- ReloadLogger();
|
|
|
|
|
|
|
|
- progress.Report(new TaskProgress { Description = "Loading configuration" });
|
|
|
|
|
|
+ ReportProgress(progress, "Loading Configuration");
|
|
ReloadConfiguration();
|
|
ReloadConfiguration();
|
|
|
|
|
|
- progress.Report(new TaskProgress { Description = "Starting Http server" });
|
|
|
|
|
|
+ ReportProgress(progress, "Loading Http Server");
|
|
ReloadHttpServer();
|
|
ReloadHttpServer();
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Performs initializations that can be reloaded at anytime
|
|
/// Performs initializations that can be reloaded at anytime
|
|
/// </summary>
|
|
/// </summary>
|
|
- public virtual async Task Reload(IProgress<TaskProgress> progress)
|
|
|
|
|
|
+ public async Task Reload(IProgress<TaskProgress> progress)
|
|
{
|
|
{
|
|
- await Task.Run(() =>
|
|
|
|
- {
|
|
|
|
- progress.Report(new TaskProgress { Description = "Loading Plugins" });
|
|
|
|
- ReloadComposableParts();
|
|
|
|
|
|
+ OnReloadBeginning(progress);
|
|
|
|
|
|
- }).ConfigureAwait(false);
|
|
|
|
|
|
+ await ReloadInternal(progress).ConfigureAwait(false);
|
|
|
|
+
|
|
|
|
+ OnReloadCompleted(progress);
|
|
|
|
+
|
|
|
|
+ ReportProgress(progress, "Kernel.Reload Complete");
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
- /// Disposes the current logger and creates a new one
|
|
|
|
|
|
+ /// Performs initializations that can be reloaded at anytime
|
|
/// </summary>
|
|
/// </summary>
|
|
- private void ReloadLogger()
|
|
|
|
|
|
+ protected virtual async Task ReloadInternal(IProgress<TaskProgress> progress)
|
|
{
|
|
{
|
|
- DisposeLogger();
|
|
|
|
-
|
|
|
|
- DateTime now = DateTime.Now;
|
|
|
|
-
|
|
|
|
- string logFilePath = Path.Combine(ApplicationPaths.LogDirectoryPath, "log-" + now.ToString("dMyyyy") + "-" + now.Ticks + ".log");
|
|
|
|
-
|
|
|
|
- Trace.Listeners.Add(new TextWriterTraceListener(logFilePath));
|
|
|
|
- Trace.AutoFlush = true;
|
|
|
|
|
|
+ await Task.Run(() =>
|
|
|
|
+ {
|
|
|
|
+ ReportProgress(progress, "Loading Plugins");
|
|
|
|
+ ReloadComposableParts();
|
|
|
|
|
|
- Logger.LoggerInstance = new TraceLogger();
|
|
|
|
|
|
+ }).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -135,14 +166,13 @@ namespace MediaBrowser.Common.Kernel
|
|
{
|
|
{
|
|
DisposeComposableParts();
|
|
DisposeComposableParts();
|
|
|
|
|
|
- var container = GetCompositionContainer(includeCurrentAssembly: true);
|
|
|
|
|
|
+ CompositionContainer = GetCompositionContainer(includeCurrentAssembly: true);
|
|
|
|
|
|
- container.ComposeParts(this);
|
|
|
|
|
|
+ CompositionContainer.ComposeParts(this);
|
|
|
|
|
|
OnComposablePartsLoaded();
|
|
OnComposablePartsLoaded();
|
|
|
|
|
|
- container.Catalog.Dispose();
|
|
|
|
- container.Dispose();
|
|
|
|
|
|
+ CompositionContainer.Catalog.Dispose();
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -157,8 +187,7 @@ namespace MediaBrowser.Common.Kernel
|
|
var catalog = new AggregateCatalog(pluginAssemblies.Select(a => new AssemblyCatalog(a)));
|
|
var catalog = new AggregateCatalog(pluginAssemblies.Select(a => new AssemblyCatalog(a)));
|
|
|
|
|
|
// Include composable parts in the Common assembly
|
|
// Include composable parts in the Common assembly
|
|
- // Uncomment this if it's ever needed
|
|
|
|
- //catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
|
|
|
|
|
|
+ catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
|
|
|
|
|
|
if (includeCurrentAssembly)
|
|
if (includeCurrentAssembly)
|
|
{
|
|
{
|
|
@@ -174,8 +203,13 @@ namespace MediaBrowser.Common.Kernel
|
|
/// </summary>
|
|
/// </summary>
|
|
protected virtual void OnComposablePartsLoaded()
|
|
protected virtual void OnComposablePartsLoaded()
|
|
{
|
|
{
|
|
|
|
+ foreach (var logger in Loggers)
|
|
|
|
+ {
|
|
|
|
+ logger.Initialize(this);
|
|
|
|
+ }
|
|
|
|
+
|
|
// Start-up each plugin
|
|
// Start-up each plugin
|
|
- foreach (BasePlugin plugin in Plugins)
|
|
|
|
|
|
+ foreach (var plugin in Plugins)
|
|
{
|
|
{
|
|
plugin.Initialize(this);
|
|
plugin.Initialize(this);
|
|
}
|
|
}
|
|
@@ -189,17 +223,16 @@ namespace MediaBrowser.Common.Kernel
|
|
//Configuration information for anything other than server-specific configuration will have to come via the API... -ebr
|
|
//Configuration information for anything other than server-specific configuration will have to come via the API... -ebr
|
|
|
|
|
|
// Deserialize config
|
|
// Deserialize config
|
|
- if (!File.Exists(ApplicationPaths.SystemConfigurationFilePath))
|
|
|
|
|
|
+ // Use try/catch to avoid the extra file system lookup using File.Exists
|
|
|
|
+ try
|
|
{
|
|
{
|
|
- Configuration = new TConfigurationType();
|
|
|
|
- XmlSerializer.SerializeToFile(Configuration, ApplicationPaths.SystemConfigurationFilePath);
|
|
|
|
|
|
+ Configuration = XmlSerializer.DeserializeFromFile<TConfigurationType>(ApplicationPaths.SystemConfigurationFilePath);
|
|
}
|
|
}
|
|
- else
|
|
|
|
|
|
+ catch (FileNotFoundException)
|
|
{
|
|
{
|
|
- Configuration = XmlSerializer.DeserializeFromFile<TConfigurationType>(ApplicationPaths.SystemConfigurationFilePath);
|
|
|
|
|
|
+ Configuration = new TConfigurationType();
|
|
|
|
+ XmlSerializer.SerializeToFile(Configuration, ApplicationPaths.SystemConfigurationFilePath);
|
|
}
|
|
}
|
|
-
|
|
|
|
- Logger.LoggerInstance.LogSeverity = Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -233,12 +266,10 @@ namespace MediaBrowser.Common.Kernel
|
|
public virtual void Dispose()
|
|
public virtual void Dispose()
|
|
{
|
|
{
|
|
Logger.LogInfo("Beginning Kernel.Dispose");
|
|
Logger.LogInfo("Beginning Kernel.Dispose");
|
|
-
|
|
|
|
- DisposeComposableParts();
|
|
|
|
|
|
|
|
DisposeHttpServer();
|
|
DisposeHttpServer();
|
|
|
|
|
|
- DisposeLogger();
|
|
|
|
|
|
+ DisposeComposableParts();
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -246,22 +277,9 @@ namespace MediaBrowser.Common.Kernel
|
|
/// </summary>
|
|
/// </summary>
|
|
protected virtual void DisposeComposableParts()
|
|
protected virtual void DisposeComposableParts()
|
|
{
|
|
{
|
|
- DisposePlugins();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// <summary>
|
|
|
|
- /// Disposes all plugins
|
|
|
|
- /// </summary>
|
|
|
|
- private void DisposePlugins()
|
|
|
|
- {
|
|
|
|
- if (Plugins != null)
|
|
|
|
|
|
+ if (CompositionContainer != null)
|
|
{
|
|
{
|
|
- Logger.LogInfo("Disposing Plugins");
|
|
|
|
-
|
|
|
|
- foreach (BasePlugin plugin in Plugins)
|
|
|
|
- {
|
|
|
|
- plugin.Dispose();
|
|
|
|
- }
|
|
|
|
|
|
+ CompositionContainer.Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -273,7 +291,7 @@ namespace MediaBrowser.Common.Kernel
|
|
if (HttpServer != null)
|
|
if (HttpServer != null)
|
|
{
|
|
{
|
|
Logger.LogInfo("Disposing Http Server");
|
|
Logger.LogInfo("Disposing Http Server");
|
|
-
|
|
|
|
|
|
+
|
|
HttpServer.Dispose();
|
|
HttpServer.Dispose();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -283,21 +301,6 @@ namespace MediaBrowser.Common.Kernel
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
|
- /// Disposes the current Logger instance
|
|
|
|
- /// </summary>
|
|
|
|
- private void DisposeLogger()
|
|
|
|
- {
|
|
|
|
- Trace.Listeners.Clear();
|
|
|
|
-
|
|
|
|
- if (Logger.LoggerInstance != null)
|
|
|
|
- {
|
|
|
|
- Logger.LogInfo("Disposing Logger");
|
|
|
|
-
|
|
|
|
- Logger.LoggerInstance.Dispose();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Gets the current application version
|
|
/// Gets the current application version
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -309,6 +312,13 @@ namespace MediaBrowser.Common.Kernel
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ protected void ReportProgress(IProgress<TaskProgress> progress, string message)
|
|
|
|
+ {
|
|
|
|
+ progress.Report(new TaskProgress { Description = message });
|
|
|
|
+
|
|
|
|
+ Logger.LogInfo(message);
|
|
|
|
+ }
|
|
|
|
+
|
|
BaseApplicationPaths IKernel.ApplicationPaths
|
|
BaseApplicationPaths IKernel.ApplicationPaths
|
|
{
|
|
{
|
|
get { return ApplicationPaths; }
|
|
get { return ApplicationPaths; }
|
|
@@ -322,6 +332,7 @@ namespace MediaBrowser.Common.Kernel
|
|
|
|
|
|
Task Init(IProgress<TaskProgress> progress);
|
|
Task Init(IProgress<TaskProgress> progress);
|
|
Task Reload(IProgress<TaskProgress> progress);
|
|
Task Reload(IProgress<TaskProgress> progress);
|
|
|
|
+ IEnumerable<BaseLogger> Loggers { get; }
|
|
void Dispose();
|
|
void Dispose();
|
|
}
|
|
}
|
|
}
|
|
}
|