BaseKernel.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.Composition;
  4. using System.ComponentModel.Composition.Hosting;
  5. using System.Configuration;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using MediaBrowser.Common.Configuration;
  10. using MediaBrowser.Common.Json;
  11. using MediaBrowser.Common.Logging;
  12. using MediaBrowser.Common.Net;
  13. using MediaBrowser.Common.Plugins;
  14. namespace MediaBrowser.Common.Kernel
  15. {
  16. /// <summary>
  17. /// Represents a shared base kernel for both the UI and server apps
  18. /// </summary>
  19. public abstract class BaseKernel<TConfigurationType>
  20. where TConfigurationType : BaseConfiguration, new()
  21. {
  22. /// <summary>
  23. /// Gets the path to the program data folder
  24. /// </summary>
  25. public string ProgramDataPath { get; private set; }
  26. /// <summary>
  27. /// Gets the path to the plugin directory
  28. /// </summary>
  29. protected string PluginsPath
  30. {
  31. get
  32. {
  33. return Path.Combine(ProgramDataPath, "plugins");
  34. }
  35. }
  36. /// <summary>
  37. /// Gets the path to the application configuration file
  38. /// </summary>
  39. protected string ConfigurationPath
  40. {
  41. get
  42. {
  43. return Path.Combine(ProgramDataPath, "config.js");
  44. }
  45. }
  46. /// <summary>
  47. /// Gets the current configuration
  48. /// </summary>
  49. public TConfigurationType Configuration { get; private set; }
  50. /// <summary>
  51. /// Gets the list of currently loaded plugins
  52. /// </summary>
  53. [ImportMany(typeof(BasePlugin))]
  54. public IEnumerable<BasePlugin> Plugins { get; private set; }
  55. /// <summary>
  56. /// Both the UI and server will have a built-in HttpServer.
  57. /// People will inevitably want remote control apps so it's needed in the UI too.
  58. /// </summary>
  59. public HttpServer HttpServer { get; private set; }
  60. /// <summary>
  61. /// Gets the kernel context. The UI kernel will have to override this.
  62. /// </summary>
  63. protected KernelContext KernelContext { get { return KernelContext.Server; } }
  64. public BaseKernel()
  65. {
  66. ProgramDataPath = GetProgramDataPath();
  67. Logger.LoggerInstance = new FileLogger(Path.Combine(ProgramDataPath, "Logs"));
  68. }
  69. public virtual void Init()
  70. {
  71. ReloadConfiguration();
  72. ReloadHttpServer();
  73. ReloadComposableParts();
  74. }
  75. /// <summary>
  76. /// Uses MEF to locate plugins
  77. /// Subclasses can use this to locate types within plugins
  78. /// </summary>
  79. protected void ReloadComposableParts()
  80. {
  81. if (!Directory.Exists(PluginsPath))
  82. {
  83. Directory.CreateDirectory(PluginsPath);
  84. }
  85. var catalog = new AggregateCatalog(Directory.GetDirectories(PluginsPath, "*", SearchOption.TopDirectoryOnly).Select(f => new DirectoryCatalog(f)));
  86. //catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
  87. //catalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));
  88. var container = new CompositionContainer(catalog);
  89. container.ComposeParts(this);
  90. OnComposablePartsLoaded();
  91. catalog.Dispose();
  92. container.Dispose();
  93. }
  94. /// <summary>
  95. /// Fires after MEF finishes finding composable parts within plugin assemblies
  96. /// </summary>
  97. protected virtual void OnComposablePartsLoaded()
  98. {
  99. // This event handler will allow any plugin to reference another
  100. AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(CurrentDomain_AssemblyResolve);
  101. AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
  102. StartPlugins();
  103. }
  104. /// <summary>
  105. /// Initializes all plugins
  106. /// </summary>
  107. private void StartPlugins()
  108. {
  109. foreach (BasePlugin plugin in Plugins)
  110. {
  111. plugin.ReloadConfiguration();
  112. if (plugin.Enabled)
  113. {
  114. if (KernelContext == KernelContext.Server)
  115. {
  116. plugin.InitInServer();
  117. }
  118. else
  119. {
  120. plugin.InitInUI();
  121. }
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// Gets the path to the application's ProgramDataFolder
  127. /// </summary>
  128. private string GetProgramDataPath()
  129. {
  130. string programDataPath = ConfigurationManager.AppSettings["ProgramDataPath"];
  131. // If it's a relative path, e.g. "..\"
  132. if (!Path.IsPathRooted(programDataPath))
  133. {
  134. string path = Assembly.GetExecutingAssembly().Location;
  135. path = Path.GetDirectoryName(path);
  136. programDataPath = Path.Combine(path, programDataPath);
  137. programDataPath = Path.GetFullPath(programDataPath);
  138. }
  139. if (!Directory.Exists(programDataPath))
  140. {
  141. Directory.CreateDirectory(programDataPath);
  142. }
  143. return programDataPath;
  144. }
  145. /// <summary>
  146. /// Reloads application configuration from the config file
  147. /// </summary>
  148. private void ReloadConfiguration()
  149. {
  150. // Deserialize config
  151. if (!File.Exists(ConfigurationPath))
  152. {
  153. Configuration = new TConfigurationType();
  154. }
  155. else
  156. {
  157. Configuration = JsonSerializer.DeserializeFromFile<TConfigurationType>(ConfigurationPath);
  158. }
  159. Logger.LoggerInstance.LogSeverity = Configuration.LogSeverity;
  160. }
  161. /// <summary>
  162. /// Saves the current application configuration to the config file
  163. /// </summary>
  164. public void SaveConfiguration()
  165. {
  166. JsonSerializer.SerializeToFile(Configuration, ConfigurationPath);
  167. }
  168. /// <summary>
  169. /// Restarts the Http Server, or starts it if not currently running
  170. /// </summary>
  171. private void ReloadHttpServer()
  172. {
  173. if (HttpServer != null)
  174. {
  175. HttpServer.Dispose();
  176. }
  177. HttpServer = new HttpServer("http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/");
  178. }
  179. /// <summary>
  180. /// This snippet will allow any plugin to reference another
  181. /// </summary>
  182. Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
  183. {
  184. AssemblyName assemblyName = new AssemblyName(args.Name);
  185. // Look for the .dll recursively within the plugins directory
  186. string dll = Directory.GetFiles(PluginsPath, "*.dll", SearchOption.AllDirectories)
  187. .FirstOrDefault(f => Path.GetFileNameWithoutExtension(f) == assemblyName.Name);
  188. // If we found a matching assembly, load it now
  189. if (!string.IsNullOrEmpty(dll))
  190. {
  191. return Assembly.Load(File.ReadAllBytes(dll));
  192. }
  193. return null;
  194. }
  195. }
  196. }