2012-07-26 13:51:26 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2012-07-26 02:33:11 +00:00
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
|
using System.ComponentModel.Composition.Hosting;
|
2012-07-21 18:39:47 +00:00
|
|
|
|
using System.IO;
|
2012-07-26 02:33:11 +00:00
|
|
|
|
using System.Linq;
|
2012-07-21 18:39:47 +00:00
|
|
|
|
using System.Reflection;
|
2012-08-19 20:38:31 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2012-07-31 03:38:00 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.Logging;
|
2012-07-21 18:39:47 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Common.Plugins;
|
2012-07-31 16:29:07 +00:00
|
|
|
|
using MediaBrowser.Common.Serialization;
|
2012-07-30 19:03:07 +00:00
|
|
|
|
using MediaBrowser.Model.Progress;
|
2012-07-21 18:39:47 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Kernel
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a shared base kernel for both the UI and server apps
|
|
|
|
|
/// </summary>
|
2012-08-19 22:47:02 +00:00
|
|
|
|
public abstract class BaseKernel<TConfigurationType, TApplicationPathsType> : IDisposable, IKernel
|
2012-07-29 15:19:25 +00:00
|
|
|
|
where TConfigurationType : BaseApplicationConfiguration, new()
|
2012-08-18 20:38:02 +00:00
|
|
|
|
where TApplicationPathsType : BaseApplicationPaths, new()
|
2012-07-21 18:39:47 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the current configuration
|
|
|
|
|
/// </summary>
|
2012-07-26 02:33:11 +00:00
|
|
|
|
public TConfigurationType Configuration { get; private set; }
|
2012-07-21 18:39:47 +00:00
|
|
|
|
|
2012-08-18 20:38:02 +00:00
|
|
|
|
public TApplicationPathsType ApplicationPaths { get; private set; }
|
|
|
|
|
|
2012-07-26 02:33:11 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the list of currently loaded plugins
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ImportMany(typeof(BasePlugin))]
|
|
|
|
|
public IEnumerable<BasePlugin> Plugins { get; private set; }
|
2012-07-26 13:51:26 +00:00
|
|
|
|
|
2012-07-21 18:39:47 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 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.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public HttpServer HttpServer { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the kernel context. The UI kernel will have to override this.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected KernelContext KernelContext { get { return KernelContext.Server; } }
|
|
|
|
|
|
|
|
|
|
public BaseKernel()
|
|
|
|
|
{
|
2012-08-18 20:38:02 +00:00
|
|
|
|
ApplicationPaths = new TApplicationPathsType();
|
2012-07-21 18:39:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
public virtual Task Init(IProgress<TaskProgress> progress)
|
2012-07-21 18:39:47 +00:00
|
|
|
|
{
|
2012-08-19 15:58:35 +00:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
ReloadLogger();
|
2012-07-30 13:44:28 +00:00
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
progress.Report(new TaskProgress() { Description = "Loading configuration", PercentComplete = 0 });
|
|
|
|
|
ReloadConfiguration();
|
2012-07-21 18:39:47 +00:00
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
progress.Report(new TaskProgress() { Description = "Starting Http server", PercentComplete = 5 });
|
|
|
|
|
ReloadHttpServer();
|
2012-07-31 03:38:00 +00:00
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
progress.Report(new TaskProgress() { Description = "Loading Plugins", PercentComplete = 10 });
|
|
|
|
|
ReloadComposableParts();
|
|
|
|
|
});
|
2012-07-26 02:33:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-02 03:13:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the path to the current log file
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string LogFilePath { get; set; }
|
|
|
|
|
|
2012-07-30 13:44:28 +00:00
|
|
|
|
private void ReloadLogger()
|
|
|
|
|
{
|
|
|
|
|
DisposeLogger();
|
2012-07-31 03:38:00 +00:00
|
|
|
|
|
2012-07-30 13:44:28 +00:00
|
|
|
|
DateTime now = DateTime.Now;
|
|
|
|
|
|
2012-08-02 03:13:44 +00:00
|
|
|
|
LogFilePath = Path.Combine(ApplicationPaths.LogDirectoryPath, Assembly.GetExecutingAssembly().GetType().Name + "-" + now.ToString("dMyyyy") + "-" + now.Ticks + ".log");
|
2012-07-30 13:44:28 +00:00
|
|
|
|
|
2012-07-31 03:38:00 +00:00
|
|
|
|
FileStream fs = new FileStream(LogFilePath, FileMode.Append, FileAccess.Write, FileShare.Read);
|
|
|
|
|
|
2012-07-30 13:44:28 +00:00
|
|
|
|
Logger.LoggerInstance = new StreamLogger(fs);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Uses MEF to locate plugins
|
|
|
|
|
/// Subclasses can use this to locate types within plugins
|
|
|
|
|
/// </summary>
|
2012-07-26 02:33:11 +00:00
|
|
|
|
protected void ReloadComposableParts()
|
|
|
|
|
{
|
2012-08-19 20:38:31 +00:00
|
|
|
|
DisposeComposableParts();
|
|
|
|
|
|
2012-07-26 17:00:53 +00:00
|
|
|
|
// Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
|
|
|
|
|
// This will prevent the .dll file from getting locked, and allow us to replace it when needed
|
2012-08-02 03:13:44 +00:00
|
|
|
|
IEnumerable<Assembly> pluginAssemblies = Directory.GetFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.AllDirectories).Select(f => Assembly.Load(File.ReadAllBytes((f))));
|
2012-07-26 02:33:11 +00:00
|
|
|
|
|
2012-07-26 17:00:53 +00:00
|
|
|
|
var catalog = new AggregateCatalog(pluginAssemblies.Select(a => new AssemblyCatalog(a)));
|
2012-08-20 15:55:05 +00:00
|
|
|
|
|
|
|
|
|
// Include composable parts in the Common assembly
|
|
|
|
|
// Uncomment this if it's ever needed
|
2012-07-26 02:33:11 +00:00
|
|
|
|
//catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
|
2012-08-20 15:55:05 +00:00
|
|
|
|
|
|
|
|
|
// Include composable parts in the subclass assembly
|
|
|
|
|
catalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));
|
2012-07-26 02:33:11 +00:00
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
var container = new CompositionContainer(catalog);
|
|
|
|
|
|
|
|
|
|
container.ComposeParts(this);
|
2012-07-26 02:33:11 +00:00
|
|
|
|
|
|
|
|
|
OnComposablePartsLoaded();
|
2012-07-26 13:51:26 +00:00
|
|
|
|
|
|
|
|
|
catalog.Dispose();
|
|
|
|
|
container.Dispose();
|
2012-07-26 02:33:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fires after MEF finishes finding composable parts within plugin assemblies
|
|
|
|
|
/// </summary>
|
2012-07-26 02:33:11 +00:00
|
|
|
|
protected virtual void OnComposablePartsLoaded()
|
|
|
|
|
{
|
2012-07-26 13:51:26 +00:00
|
|
|
|
// This event handler will allow any plugin to reference another
|
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(CurrentDomain_AssemblyResolve);
|
|
|
|
|
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
|
|
|
|
|
|
2012-07-26 02:33:11 +00:00
|
|
|
|
StartPlugins();
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes all plugins
|
|
|
|
|
/// </summary>
|
2012-07-26 02:33:11 +00:00
|
|
|
|
private void StartPlugins()
|
|
|
|
|
{
|
2012-07-26 13:51:26 +00:00
|
|
|
|
foreach (BasePlugin plugin in Plugins)
|
2012-07-26 02:33:11 +00:00
|
|
|
|
{
|
2012-07-26 17:00:53 +00:00
|
|
|
|
Assembly assembly = plugin.GetType().Assembly;
|
|
|
|
|
AssemblyName assemblyName = assembly.GetName();
|
|
|
|
|
|
|
|
|
|
plugin.Version = assemblyName.Version;
|
2012-08-02 03:13:44 +00:00
|
|
|
|
plugin.Path = Path.Combine(ApplicationPaths.PluginsPath, assemblyName.Name);
|
2012-07-26 17:00:53 +00:00
|
|
|
|
|
2012-07-29 15:19:25 +00:00
|
|
|
|
plugin.Context = KernelContext;
|
|
|
|
|
|
2012-07-26 02:33:11 +00:00
|
|
|
|
plugin.ReloadConfiguration();
|
|
|
|
|
|
|
|
|
|
if (plugin.Enabled)
|
|
|
|
|
{
|
2012-07-29 15:19:25 +00:00
|
|
|
|
plugin.Init();
|
2012-07-26 02:33:11 +00:00
|
|
|
|
}
|
2012-07-26 13:51:26 +00:00
|
|
|
|
}
|
2012-07-21 18:39:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reloads application configuration from the config file
|
|
|
|
|
/// </summary>
|
2012-08-01 19:09:24 +00:00
|
|
|
|
protected virtual void ReloadConfiguration()
|
2012-07-21 18:39:47 +00:00
|
|
|
|
{
|
2012-08-02 03:13:44 +00:00
|
|
|
|
//Configuration information for anything other than server-specific configuration will have to come via the API... -ebr
|
|
|
|
|
|
2012-08-02 12:51:43 +00:00
|
|
|
|
// Deserialize config
|
2012-08-18 20:38:02 +00:00
|
|
|
|
if (!File.Exists(ApplicationPaths.SystemConfigurationFilePath))
|
2012-08-02 12:51:43 +00:00
|
|
|
|
{
|
|
|
|
|
Configuration = new TConfigurationType();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-08-18 20:38:02 +00:00
|
|
|
|
Configuration = XmlSerializer.DeserializeFromFile<TConfigurationType>(ApplicationPaths.SystemConfigurationFilePath);
|
2012-08-02 12:51:43 +00:00
|
|
|
|
}
|
2012-07-26 02:33:11 +00:00
|
|
|
|
|
2012-08-02 12:51:43 +00:00
|
|
|
|
Logger.LoggerInstance.LogSeverity = Configuration.LogSeverity;
|
|
|
|
|
}
|
2012-07-21 18:39:47 +00:00
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Restarts the Http Server, or starts it if not currently running
|
|
|
|
|
/// </summary>
|
2012-07-21 18:39:47 +00:00
|
|
|
|
private void ReloadHttpServer()
|
2012-07-30 04:06:05 +00:00
|
|
|
|
{
|
|
|
|
|
DisposeHttpServer();
|
|
|
|
|
|
|
|
|
|
HttpServer = new HttpServer("http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/");
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This snippet will allow any plugin to reference another
|
|
|
|
|
/// </summary>
|
|
|
|
|
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
|
2012-07-21 18:39:47 +00:00
|
|
|
|
{
|
2012-07-26 13:51:26 +00:00
|
|
|
|
AssemblyName assemblyName = new AssemblyName(args.Name);
|
|
|
|
|
|
|
|
|
|
// Look for the .dll recursively within the plugins directory
|
2012-08-02 03:13:44 +00:00
|
|
|
|
string dll = Directory.GetFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.AllDirectories)
|
2012-07-26 13:51:26 +00:00
|
|
|
|
.FirstOrDefault(f => Path.GetFileNameWithoutExtension(f) == assemblyName.Name);
|
2012-07-21 18:39:47 +00:00
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
// If we found a matching assembly, load it now
|
|
|
|
|
if (!string.IsNullOrEmpty(dll))
|
2012-07-21 18:39:47 +00:00
|
|
|
|
{
|
2012-07-26 13:51:26 +00:00
|
|
|
|
return Assembly.Load(File.ReadAllBytes(dll));
|
2012-07-21 18:39:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
return null;
|
2012-07-21 18:39:47 +00:00
|
|
|
|
}
|
2012-07-30 04:06:05 +00:00
|
|
|
|
|
2012-07-30 19:03:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes all resources currently in use.
|
|
|
|
|
/// </summary>
|
2012-08-01 19:09:24 +00:00
|
|
|
|
public virtual void Dispose()
|
2012-07-30 04:06:05 +00:00
|
|
|
|
{
|
2012-08-19 20:38:31 +00:00
|
|
|
|
DisposeComposableParts();
|
2012-07-30 04:06:05 +00:00
|
|
|
|
DisposeHttpServer();
|
2012-07-30 13:44:28 +00:00
|
|
|
|
DisposeLogger();
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes all objects gathered through MEF composable parts
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void DisposeComposableParts()
|
|
|
|
|
{
|
|
|
|
|
DisposePlugins();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes all plugins
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void DisposePlugins()
|
|
|
|
|
{
|
|
|
|
|
if (Plugins != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (BasePlugin plugin in Plugins)
|
|
|
|
|
{
|
|
|
|
|
plugin.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-30 19:03:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the current HttpServer
|
|
|
|
|
/// </summary>
|
2012-07-30 13:44:28 +00:00
|
|
|
|
private void DisposeHttpServer()
|
|
|
|
|
{
|
|
|
|
|
if (HttpServer != null)
|
|
|
|
|
{
|
|
|
|
|
HttpServer.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-30 19:03:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the current Logger instance
|
|
|
|
|
/// </summary>
|
2012-07-30 13:44:28 +00:00
|
|
|
|
private void DisposeLogger()
|
|
|
|
|
{
|
|
|
|
|
if (Logger.LoggerInstance != null)
|
|
|
|
|
{
|
|
|
|
|
Logger.LoggerInstance.Dispose();
|
|
|
|
|
}
|
2012-07-30 04:06:05 +00:00
|
|
|
|
}
|
2012-08-19 21:29:15 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the current application version
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Version ApplicationVersion
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return GetType().Assembly.GetName().Version;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-21 18:39:47 +00:00
|
|
|
|
}
|
2012-08-19 22:47:02 +00:00
|
|
|
|
|
|
|
|
|
public interface IKernel
|
|
|
|
|
{
|
|
|
|
|
Task Init(IProgress<TaskProgress> progress);
|
|
|
|
|
void Dispose();
|
|
|
|
|
}
|
2012-07-21 18:39:47 +00:00
|
|
|
|
}
|