Kernel.cs 15 KB

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