Kernel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using MediaBrowser.Common;
  2. using MediaBrowser.Common.ScheduledTasks;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Drawing;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.IO;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.Localization;
  9. using MediaBrowser.Controller.MediaInfo;
  10. using MediaBrowser.Controller.Persistence;
  11. using MediaBrowser.Controller.Plugins;
  12. using MediaBrowser.Controller.Providers;
  13. using MediaBrowser.Controller.Weather;
  14. using MediaBrowser.Model.Logging;
  15. using MediaBrowser.Model.Serialization;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. namespace MediaBrowser.Controller
  22. {
  23. /// <summary>
  24. /// Class Kernel
  25. /// </summary>
  26. public class Kernel : IDisposable
  27. {
  28. /// <summary>
  29. /// Gets the instance.
  30. /// </summary>
  31. /// <value>The instance.</value>
  32. public static Kernel Instance { get; private set; }
  33. /// <summary>
  34. /// Gets the image manager.
  35. /// </summary>
  36. /// <value>The image manager.</value>
  37. public ImageManager ImageManager { get; private set; }
  38. /// <summary>
  39. /// Gets the FFMPEG controller.
  40. /// </summary>
  41. /// <value>The FFMPEG controller.</value>
  42. public FFMpegManager FFMpegManager { get; private set; }
  43. /// <summary>
  44. /// Gets or sets the file system manager.
  45. /// </summary>
  46. /// <value>The file system manager.</value>
  47. public FileSystemManager FileSystemManager { get; private set; }
  48. /// <summary>
  49. /// Gets the provider manager.
  50. /// </summary>
  51. /// <value>The provider manager.</value>
  52. public ProviderManager ProviderManager { get; private set; }
  53. /// <summary>
  54. /// Gets the name of the web application that can be used for url building.
  55. /// All api urls will be of the form {protocol}://{host}:{port}/{appname}/...
  56. /// </summary>
  57. /// <value>The name of the web application.</value>
  58. public string WebApplicationName
  59. {
  60. get { return "mediabrowser"; }
  61. }
  62. /// <summary>
  63. /// Gets the HTTP server URL prefix.
  64. /// </summary>
  65. /// <value>The HTTP server URL prefix.</value>
  66. public virtual string HttpServerUrlPrefix
  67. {
  68. get
  69. {
  70. return "http://+:" + _configurationManager.Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/";
  71. }
  72. }
  73. /// <summary>
  74. /// Gets the list of Localized string files
  75. /// </summary>
  76. /// <value>The string files.</value>
  77. public IEnumerable<LocalizedStringData> StringFiles { 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 int UdpServerPortNumber
  139. {
  140. get { return 7359; }
  141. }
  142. private readonly IXmlSerializer _xmlSerializer;
  143. private readonly IServerConfigurationManager _configurationManager;
  144. private readonly ILogManager _logManager;
  145. private IApplicationHost ApplicationHost { get; set; }
  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. {
  156. Instance = this;
  157. ApplicationHost = appHost;
  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. ImageEnhancers = ApplicationHost.GetExports<IImageEnhancer>().OrderBy(e => e.Priority).ToArray();
  185. StringFiles = ApplicationHost.GetExports<LocalizedStringData>();
  186. MetadataProviders = ApplicationHost.GetExports<BaseMetadataProvider>().OrderBy(e => e.Priority).ToArray();
  187. }
  188. /// <summary>
  189. /// Performs initializations that can be reloaded at anytime
  190. /// </summary>
  191. /// <returns>Task.</returns>
  192. public async Task Init()
  193. {
  194. FindParts();
  195. await LoadRepositories().ConfigureAwait(false);
  196. await ApplicationHost.Resolve<IUserManager>().RefreshUsersMetadata(CancellationToken.None).ConfigureAwait(false);
  197. foreach (var entryPoint in ApplicationHost.GetExports<IServerEntryPoint>())
  198. {
  199. entryPoint.Run();
  200. }
  201. ReloadFileSystemManager();
  202. }
  203. /// <summary>
  204. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  205. /// </summary>
  206. public void Dispose()
  207. {
  208. Dispose(true);
  209. GC.SuppressFinalize(this);
  210. }
  211. /// <summary>
  212. /// Releases unmanaged and - optionally - managed resources.
  213. /// </summary>
  214. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  215. protected virtual void Dispose(bool dispose)
  216. {
  217. if (dispose)
  218. {
  219. DisposeFileSystemManager();
  220. }
  221. }
  222. /// <summary>
  223. /// Called when [composable parts loaded].
  224. /// </summary>
  225. /// <returns>Task.</returns>
  226. protected Task LoadRepositories()
  227. {
  228. // Get the current item repository
  229. ItemRepository = GetRepository(ItemRepositories, _configurationManager.Configuration.ItemRepository);
  230. var itemRepoTask = ItemRepository.Initialize();
  231. // Get the current user repository
  232. UserRepository = GetRepository(UserRepositories, _configurationManager.Configuration.UserRepository);
  233. var userRepoTask = UserRepository.Initialize();
  234. // Get the current item repository
  235. UserDataRepository = GetRepository(UserDataRepositories, _configurationManager.Configuration.UserDataRepository);
  236. var userDataRepoTask = UserDataRepository.Initialize();
  237. // Get the current display preferences repository
  238. DisplayPreferencesRepository = GetRepository(DisplayPreferencesRepositories, _configurationManager.Configuration.DisplayPreferencesRepository);
  239. var displayPreferencesRepoTask = DisplayPreferencesRepository.Initialize();
  240. return Task.WhenAll(itemRepoTask, userRepoTask, userDataRepoTask, displayPreferencesRepoTask);
  241. }
  242. /// <summary>
  243. /// Gets a repository by name from a list, and returns the default if not found
  244. /// </summary>
  245. /// <typeparam name="T"></typeparam>
  246. /// <param name="repositories">The repositories.</param>
  247. /// <param name="name">The name.</param>
  248. /// <returns>``0.</returns>
  249. private T GetRepository<T>(IEnumerable<T> repositories, string name)
  250. where T : class, IRepository
  251. {
  252. var enumerable = repositories as T[] ?? repositories.ToArray();
  253. return enumerable.FirstOrDefault(r => string.Equals(r.Name, name, StringComparison.OrdinalIgnoreCase)) ??
  254. enumerable.FirstOrDefault();
  255. }
  256. /// <summary>
  257. /// Disposes the file system manager.
  258. /// </summary>
  259. private void DisposeFileSystemManager()
  260. {
  261. if (FileSystemManager != null)
  262. {
  263. FileSystemManager.Dispose();
  264. FileSystemManager = null;
  265. }
  266. }
  267. /// <summary>
  268. /// Reloads the file system manager.
  269. /// </summary>
  270. private void ReloadFileSystemManager()
  271. {
  272. DisposeFileSystemManager();
  273. FileSystemManager = new FileSystemManager(_logManager, ApplicationHost.Resolve<ITaskManager>(), ApplicationHost.Resolve<ILibraryManager>(), _configurationManager);
  274. FileSystemManager.StartWatchers();
  275. }
  276. }
  277. }