Kernel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.Kernel;
  3. using MediaBrowser.Common.ScheduledTasks;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Drawing;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.IO;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Localization;
  10. using MediaBrowser.Controller.MediaInfo;
  11. using MediaBrowser.Controller.Persistence;
  12. using MediaBrowser.Controller.Plugins;
  13. using MediaBrowser.Controller.Providers;
  14. using MediaBrowser.Controller.Updates;
  15. using MediaBrowser.Controller.Weather;
  16. using MediaBrowser.Model.Logging;
  17. using MediaBrowser.Model.Serialization;
  18. using MediaBrowser.Model.System;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Threading;
  23. using System.Threading.Tasks;
  24. namespace MediaBrowser.Controller
  25. {
  26. /// <summary>
  27. /// Class Kernel
  28. /// </summary>
  29. public class Kernel : BaseKernel, IDisposable
  30. {
  31. /// <summary>
  32. /// Gets the instance.
  33. /// </summary>
  34. /// <value>The instance.</value>
  35. public static Kernel Instance { get; private set; }
  36. /// <summary>
  37. /// Gets the image manager.
  38. /// </summary>
  39. /// <value>The image manager.</value>
  40. public ImageManager ImageManager { get; private set; }
  41. /// <summary>
  42. /// Gets the FFMPEG controller.
  43. /// </summary>
  44. /// <value>The FFMPEG controller.</value>
  45. public FFMpegManager FFMpegManager { get; private set; }
  46. /// <summary>
  47. /// Gets the installation manager.
  48. /// </summary>
  49. /// <value>The installation manager.</value>
  50. public InstallationManager InstallationManager { get; set; }
  51. /// <summary>
  52. /// Gets or sets the file system manager.
  53. /// </summary>
  54. /// <value>The file system manager.</value>
  55. public FileSystemManager FileSystemManager { get; private set; }
  56. /// <summary>
  57. /// Gets the provider manager.
  58. /// </summary>
  59. /// <value>The provider manager.</value>
  60. public ProviderManager ProviderManager { get; private set; }
  61. /// <summary>
  62. /// Gets the kernel context.
  63. /// </summary>
  64. /// <value>The kernel context.</value>
  65. public override KernelContext KernelContext
  66. {
  67. get { return KernelContext.Server; }
  68. }
  69. /// <summary>
  70. /// Gets the list of Localized string files
  71. /// </summary>
  72. /// <value>The string files.</value>
  73. public IEnumerable<LocalizedStringData> StringFiles { get; private set; }
  74. /// <summary>
  75. /// Gets the list of plugin configuration pages
  76. /// </summary>
  77. /// <value>The configuration pages.</value>
  78. public IEnumerable<IPluginConfigurationPage> PluginConfigurationPages { get; private set; }
  79. /// <summary>
  80. /// Gets the list of currently registered weather prvoiders
  81. /// </summary>
  82. /// <value>The weather providers.</value>
  83. public IEnumerable<IWeatherProvider> WeatherProviders { get; private set; }
  84. /// <summary>
  85. /// Gets the list of currently registered metadata prvoiders
  86. /// </summary>
  87. /// <value>The metadata providers enumerable.</value>
  88. public BaseMetadataProvider[] MetadataProviders { get; private set; }
  89. /// <summary>
  90. /// Gets the list of currently registered image processors
  91. /// Image processors are specialized metadata providers that run after the normal ones
  92. /// </summary>
  93. /// <value>The image enhancers.</value>
  94. public IEnumerable<IImageEnhancer> ImageEnhancers { get; private set; }
  95. /// <summary>
  96. /// Gets the list of available user repositories
  97. /// </summary>
  98. /// <value>The user repositories.</value>
  99. private IEnumerable<IUserRepository> UserRepositories { get; set; }
  100. /// <summary>
  101. /// Gets the active user repository
  102. /// </summary>
  103. /// <value>The user repository.</value>
  104. public IUserRepository UserRepository { get; private set; }
  105. /// <summary>
  106. /// Gets the active user repository
  107. /// </summary>
  108. /// <value>The display preferences repository.</value>
  109. public IDisplayPreferencesRepository DisplayPreferencesRepository { get; private set; }
  110. /// <summary>
  111. /// Gets the list of available item repositories
  112. /// </summary>
  113. /// <value>The item repositories.</value>
  114. private IEnumerable<IItemRepository> ItemRepositories { get; set; }
  115. /// <summary>
  116. /// Gets the active item repository
  117. /// </summary>
  118. /// <value>The item repository.</value>
  119. public IItemRepository ItemRepository { get; private set; }
  120. /// <summary>
  121. /// Gets the list of available DisplayPreferencesRepositories
  122. /// </summary>
  123. /// <value>The display preferences repositories.</value>
  124. private IEnumerable<IDisplayPreferencesRepository> DisplayPreferencesRepositories { get; set; }
  125. /// <summary>
  126. /// Gets the list of available item repositories
  127. /// </summary>
  128. /// <value>The user data repositories.</value>
  129. private IEnumerable<IUserDataRepository> UserDataRepositories { get; set; }
  130. /// <summary>
  131. /// Gets the active user data repository
  132. /// </summary>
  133. /// <value>The user data repository.</value>
  134. public IUserDataRepository UserDataRepository { get; private set; }
  135. /// <summary>
  136. /// Gets the UDP server port number.
  137. /// </summary>
  138. /// <value>The UDP server port number.</value>
  139. public override int UdpServerPortNumber
  140. {
  141. get { return 7359; }
  142. }
  143. private readonly IXmlSerializer _xmlSerializer;
  144. private readonly IServerConfigurationManager _configurationManager;
  145. private readonly ILogManager _logManager;
  146. /// <summary>
  147. /// Creates a kernel based on a Data path, which is akin to our current programdata path
  148. /// </summary>
  149. /// <param name="appHost">The app host.</param>
  150. /// <param name="xmlSerializer">The XML serializer.</param>
  151. /// <param name="logManager">The log manager.</param>
  152. /// <param name="configurationManager">The configuration manager.</param>
  153. /// <exception cref="System.ArgumentNullException">isoManager</exception>
  154. public Kernel(IApplicationHost appHost, IXmlSerializer xmlSerializer, ILogManager logManager, IServerConfigurationManager configurationManager)
  155. : base(appHost, logManager, configurationManager)
  156. {
  157. Instance = this;
  158. _configurationManager = configurationManager;
  159. _xmlSerializer = xmlSerializer;
  160. _logManager = logManager;
  161. // For now there's no real way to inject these properly
  162. BaseItem.Logger = logManager.GetLogger("BaseItem");
  163. User.XmlSerializer = _xmlSerializer;
  164. Ratings.ConfigurationManager = _configurationManager;
  165. LocalizedStrings.ApplicationPaths = _configurationManager.ApplicationPaths;
  166. BaseItem.ConfigurationManager = configurationManager;
  167. }
  168. /// <summary>
  169. /// Composes the parts with ioc container.
  170. /// </summary>
  171. protected void FindParts()
  172. {
  173. // For now there's no real way to inject these properly
  174. BaseItem.LibraryManager = ApplicationHost.Resolve<ILibraryManager>();
  175. User.UserManager = ApplicationHost.Resolve<IUserManager>();
  176. FFMpegManager = (FFMpegManager)ApplicationHost.CreateInstance(typeof(FFMpegManager));
  177. ImageManager = (ImageManager)ApplicationHost.CreateInstance(typeof(ImageManager));
  178. ProviderManager = (ProviderManager)ApplicationHost.CreateInstance(typeof(ProviderManager));
  179. UserDataRepositories = ApplicationHost.GetExports<IUserDataRepository>();
  180. UserRepositories = ApplicationHost.GetExports<IUserRepository>();
  181. DisplayPreferencesRepositories = ApplicationHost.GetExports<IDisplayPreferencesRepository>();
  182. ItemRepositories = ApplicationHost.GetExports<IItemRepository>();
  183. WeatherProviders = ApplicationHost.GetExports<IWeatherProvider>();
  184. PluginConfigurationPages = ApplicationHost.GetExports<IPluginConfigurationPage>();
  185. ImageEnhancers = ApplicationHost.GetExports<IImageEnhancer>().OrderBy(e => e.Priority).ToArray();
  186. StringFiles = ApplicationHost.GetExports<LocalizedStringData>();
  187. MetadataProviders = ApplicationHost.GetExports<BaseMetadataProvider>().OrderBy(e => e.Priority).ToArray();
  188. }
  189. /// <summary>
  190. /// Performs initializations that can be reloaded at anytime
  191. /// </summary>
  192. /// <returns>Task.</returns>
  193. protected override async void ReloadInternal()
  194. {
  195. base.ReloadInternal();
  196. FindParts();
  197. await LoadRepositories().ConfigureAwait(false);
  198. await ApplicationHost.Resolve<IUserManager>().RefreshUsersMetadata(CancellationToken.None).ConfigureAwait(false);
  199. foreach (var entryPoint in ApplicationHost.GetExports<IServerEntryPoint>())
  200. {
  201. entryPoint.Run();
  202. }
  203. ReloadFileSystemManager();
  204. }
  205. /// <summary>
  206. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  207. /// </summary>
  208. public void Dispose()
  209. {
  210. Dispose(true);
  211. GC.SuppressFinalize(this);
  212. }
  213. /// <summary>
  214. /// Releases unmanaged and - optionally - managed resources.
  215. /// </summary>
  216. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  217. protected virtual void Dispose(bool dispose)
  218. {
  219. if (dispose)
  220. {
  221. DisposeFileSystemManager();
  222. }
  223. }
  224. /// <summary>
  225. /// Called when [composable parts loaded].
  226. /// </summary>
  227. /// <returns>Task.</returns>
  228. protected Task LoadRepositories()
  229. {
  230. // Get the current item repository
  231. ItemRepository = GetRepository(ItemRepositories, _configurationManager.Configuration.ItemRepository);
  232. var itemRepoTask = ItemRepository.Initialize();
  233. // Get the current user repository
  234. UserRepository = GetRepository(UserRepositories, _configurationManager.Configuration.UserRepository);
  235. var userRepoTask = UserRepository.Initialize();
  236. // Get the current item repository
  237. UserDataRepository = GetRepository(UserDataRepositories, _configurationManager.Configuration.UserDataRepository);
  238. var userDataRepoTask = UserDataRepository.Initialize();
  239. // Get the current display preferences repository
  240. DisplayPreferencesRepository = GetRepository(DisplayPreferencesRepositories, _configurationManager.Configuration.DisplayPreferencesRepository);
  241. var displayPreferencesRepoTask = DisplayPreferencesRepository.Initialize();
  242. return Task.WhenAll(itemRepoTask, userRepoTask, userDataRepoTask, displayPreferencesRepoTask);
  243. }
  244. /// <summary>
  245. /// Gets a repository by name from a list, and returns the default if not found
  246. /// </summary>
  247. /// <typeparam name="T"></typeparam>
  248. /// <param name="repositories">The repositories.</param>
  249. /// <param name="name">The name.</param>
  250. /// <returns>``0.</returns>
  251. private T GetRepository<T>(IEnumerable<T> repositories, string name)
  252. where T : class, IRepository
  253. {
  254. var enumerable = repositories as T[] ?? repositories.ToArray();
  255. return enumerable.FirstOrDefault(r => string.Equals(r.Name, name, StringComparison.OrdinalIgnoreCase)) ??
  256. enumerable.FirstOrDefault();
  257. }
  258. /// <summary>
  259. /// Disposes the file system manager.
  260. /// </summary>
  261. private void DisposeFileSystemManager()
  262. {
  263. if (FileSystemManager != null)
  264. {
  265. FileSystemManager.Dispose();
  266. FileSystemManager = null;
  267. }
  268. }
  269. /// <summary>
  270. /// Reloads the file system manager.
  271. /// </summary>
  272. private void ReloadFileSystemManager()
  273. {
  274. DisposeFileSystemManager();
  275. FileSystemManager = new FileSystemManager(this, _logManager, ApplicationHost.Resolve<ITaskManager>(), ApplicationHost.Resolve<ILibraryManager>(), _configurationManager);
  276. FileSystemManager.StartWatchers();
  277. }
  278. /// <summary>
  279. /// Gets the system info.
  280. /// </summary>
  281. /// <returns>SystemInfo.</returns>
  282. public override SystemInfo GetSystemInfo()
  283. {
  284. var info = base.GetSystemInfo();
  285. if (InstallationManager != null)
  286. {
  287. info.InProgressInstallations = InstallationManager.CurrentInstallations.Select(i => i.Item1).ToArray();
  288. info.CompletedInstallations = InstallationManager.CompletedInstallations.ToArray();
  289. }
  290. return info;
  291. }
  292. }
  293. }