Kernel.cs 13 KB

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