Kernel.cs 14 KB

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