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;
|
|
|
|
|
using System.Configuration;
|
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;
|
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.Json;
|
|
|
|
|
using MediaBrowser.Common.Logging;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Common.Plugins;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Kernel
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a shared base kernel for both the UI and server apps
|
|
|
|
|
/// </summary>
|
2012-07-26 02:33:11 +00:00
|
|
|
|
public abstract class BaseKernel<TConfigurationType>
|
2012-07-21 18:39:47 +00:00
|
|
|
|
where TConfigurationType : BaseConfiguration, new()
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the path to the program data folder
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string ProgramDataPath { get; private set; }
|
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the path to the plugin directory
|
|
|
|
|
/// </summary>
|
2012-07-26 02:33:11 +00:00
|
|
|
|
protected string PluginsPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(ProgramDataPath, "plugins");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the path to the application configuration file
|
|
|
|
|
/// </summary>
|
2012-07-26 02:33:11 +00:00
|
|
|
|
protected string ConfigurationPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(ProgramDataPath, "config.js");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-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()
|
|
|
|
|
{
|
|
|
|
|
ProgramDataPath = GetProgramDataPath();
|
|
|
|
|
|
|
|
|
|
Logger.LoggerInstance = new FileLogger(Path.Combine(ProgramDataPath, "Logs"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Init()
|
|
|
|
|
{
|
|
|
|
|
ReloadConfiguration();
|
|
|
|
|
|
|
|
|
|
ReloadHttpServer();
|
|
|
|
|
|
2012-07-26 02:33:11 +00:00
|
|
|
|
ReloadComposableParts();
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(PluginsPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(PluginsPath);
|
|
|
|
|
}
|
2012-07-26 13:51:26 +00:00
|
|
|
|
|
2012-07-26 02:33:11 +00:00
|
|
|
|
var catalog = new AggregateCatalog(Directory.GetDirectories(PluginsPath, "*", SearchOption.TopDirectoryOnly).Select(f => new DirectoryCatalog(f)));
|
|
|
|
|
|
|
|
|
|
//catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
|
|
|
|
|
//catalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
plugin.ReloadConfiguration();
|
|
|
|
|
|
|
|
|
|
if (plugin.Enabled)
|
|
|
|
|
{
|
|
|
|
|
if (KernelContext == KernelContext.Server)
|
|
|
|
|
{
|
|
|
|
|
plugin.InitInServer();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
plugin.InitInUI();
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-26 13:51:26 +00:00
|
|
|
|
}
|
2012-07-21 18:39:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the path to the application's ProgramDataFolder
|
|
|
|
|
/// </summary>
|
|
|
|
|
private string GetProgramDataPath()
|
|
|
|
|
{
|
|
|
|
|
string programDataPath = ConfigurationManager.AppSettings["ProgramDataPath"];
|
|
|
|
|
|
|
|
|
|
// If it's a relative path, e.g. "..\"
|
|
|
|
|
if (!Path.IsPathRooted(programDataPath))
|
|
|
|
|
{
|
|
|
|
|
string path = Assembly.GetExecutingAssembly().Location;
|
|
|
|
|
path = Path.GetDirectoryName(path);
|
|
|
|
|
|
|
|
|
|
programDataPath = Path.Combine(path, programDataPath);
|
|
|
|
|
|
|
|
|
|
programDataPath = Path.GetFullPath(programDataPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(programDataPath))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(programDataPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return programDataPath;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reloads application configuration from the config file
|
|
|
|
|
/// </summary>
|
2012-07-21 18:39:47 +00:00
|
|
|
|
private void ReloadConfiguration()
|
|
|
|
|
{
|
|
|
|
|
// Deserialize config
|
2012-07-26 02:33:11 +00:00
|
|
|
|
if (!File.Exists(ConfigurationPath))
|
|
|
|
|
{
|
|
|
|
|
Configuration = new TConfigurationType();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Configuration = JsonSerializer.DeserializeFromFile<TConfigurationType>(ConfigurationPath);
|
|
|
|
|
}
|
2012-07-21 18:39:47 +00:00
|
|
|
|
|
2012-07-26 02:33:11 +00:00
|
|
|
|
Logger.LoggerInstance.LogSeverity = Configuration.LogSeverity;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the current application configuration to the config file
|
|
|
|
|
/// </summary>
|
2012-07-26 02:33:11 +00:00
|
|
|
|
public void SaveConfiguration()
|
|
|
|
|
{
|
|
|
|
|
JsonSerializer.SerializeToFile(Configuration, ConfigurationPath);
|
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()
|
|
|
|
|
{
|
|
|
|
|
if (HttpServer != null)
|
|
|
|
|
{
|
|
|
|
|
HttpServer.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 02:33:11 +00:00
|
|
|
|
HttpServer = new HttpServer("http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/");
|
2012-07-21 18:39:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
string dll = Directory.GetFiles(PluginsPath, "*.dll", SearchOption.AllDirectories)
|
|
|
|
|
.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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|