Kernel.cs 13 KB

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