using MediaBrowser.Common.Kernel;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Common.Updates;
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Localization;
using MediaBrowser.Controller.MediaInfo;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Controller.Playback;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Controller.Updates;
using MediaBrowser.Controller.Weather;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller
{
///
/// Class Kernel
///
public class Kernel : BaseKernel
{
///
/// The MB admin URL
///
public const string MBAdminUrl = "http://mb3admin.com/admin/";
///
/// Gets the instance.
///
/// The instance.
public static Kernel Instance { get; private set; }
///
/// Gets the library manager.
///
/// The library manager.
public LibraryManager LibraryManager { get; private set; }
///
/// Gets the image manager.
///
/// The image manager.
public ImageManager ImageManager { get; private set; }
///
/// Gets the user manager.
///
/// The user manager.
public UserManager UserManager { get; private set; }
///
/// Gets the FFMPEG controller.
///
/// The FFMPEG controller.
public FFMpegManager FFMpegManager { get; private set; }
///
/// Gets the installation manager.
///
/// The installation manager.
public IInstallationManager InstallationManager { get; private set; }
///
/// Gets or sets the file system manager.
///
/// The file system manager.
public FileSystemManager FileSystemManager { get; private set; }
///
/// Gets the provider manager.
///
/// The provider manager.
public ProviderManager ProviderManager { get; private set; }
///
/// Gets the user data manager.
///
/// The user data manager.
public UserDataManager UserDataManager { get; private set; }
///
/// Gets the plug-in security manager.
///
/// The plug-in security manager.
public PluginSecurityManager PluginSecurityManager { get; private set; }
///
/// The _users
///
private IEnumerable _users;
///
/// The _user lock
///
private object _usersSyncLock = new object();
///
/// The _users initialized
///
private bool _usersInitialized;
///
/// Gets the users.
///
/// The users.
public IEnumerable Users
{
get
{
// Call ToList to exhaust the stream because we'll be iterating over this multiple times
LazyInitializer.EnsureInitialized(ref _users, ref _usersInitialized, ref _usersSyncLock, UserManager.LoadUsers);
return _users;
}
internal set
{
_users = value;
if (value == null)
{
_usersInitialized = false;
}
}
}
///
/// The _root folder
///
private AggregateFolder _rootFolder;
///
/// The _root folder sync lock
///
private object _rootFolderSyncLock = new object();
///
/// The _root folder initialized
///
private bool _rootFolderInitialized;
///
/// Gets the root folder.
///
/// The root folder.
public AggregateFolder RootFolder
{
get
{
LazyInitializer.EnsureInitialized(ref _rootFolder, ref _rootFolderInitialized, ref _rootFolderSyncLock, LibraryManager.CreateRootFolder);
return _rootFolder;
}
private set
{
_rootFolder = value;
if (value == null)
{
_rootFolderInitialized = false;
}
}
}
///
/// Gets the kernel context.
///
/// The kernel context.
public override KernelContext KernelContext
{
get { return KernelContext.Server; }
}
///
/// Gets the list of Localized string files
///
/// The string files.
public IEnumerable StringFiles { get; private set; }
///
/// Gets the list of plugin configuration pages
///
/// The configuration pages.
public IEnumerable PluginConfigurationPages { get; private set; }
///
/// Gets the intro providers.
///
/// The intro providers.
public IEnumerable IntroProviders { get; private set; }
///
/// Gets the list of currently registered weather prvoiders
///
/// The weather providers.
public IEnumerable WeatherProviders { get; private set; }
///
/// Gets the list of currently registered metadata prvoiders
///
/// The metadata providers enumerable.
public BaseMetadataProvider[] MetadataProviders { get; private set; }
///
/// Gets the list of currently registered image processors
/// Image processors are specialized metadata providers that run after the normal ones
///
/// The image enhancers.
public IEnumerable ImageEnhancers { get; private set; }
///
/// Gets the list of currently registered entity resolvers
///
/// The entity resolvers enumerable.
internal IEnumerable EntityResolvers { get; private set; }
///
/// Gets the list of BasePluginFolders added by plugins
///
/// The plugin folders.
internal IEnumerable PluginFolderCreators { get; private set; }
///
/// Gets the list of available user repositories
///
/// The user repositories.
private IEnumerable UserRepositories { get; set; }
///
/// Gets the active user repository
///
/// The user repository.
public IUserRepository UserRepository { get; private set; }
///
/// Gets the active user repository
///
/// The display preferences repository.
public IDisplayPreferencesRepository DisplayPreferencesRepository { get; private set; }
///
/// Gets the list of available item repositories
///
/// The item repositories.
private IEnumerable ItemRepositories { get; set; }
///
/// Gets the active item repository
///
/// The item repository.
public IItemRepository ItemRepository { get; private set; }
///
/// Gets the list of available item repositories
///
/// The user data repositories.
private IEnumerable UserDataRepositories { get; set; }
///
/// Gets the list of available DisplayPreferencesRepositories
///
/// The display preferences repositories.
private IEnumerable DisplayPreferencesRepositories { get; set; }
///
/// Gets the list of entity resolution ignore rules
///
/// The entity resolution ignore rules.
internal IEnumerable EntityResolutionIgnoreRules { get; private set; }
///
/// Gets the active user data repository
///
/// The user data repository.
public IUserDataRepository UserDataRepository { get; private set; }
///
/// Gets the UDP server port number.
///
/// The UDP server port number.
public override int UdpServerPortNumber
{
get { return 7359; }
}
///
/// Creates a kernel based on a Data path, which is akin to our current programdata path
///
/// The app host.
/// The app paths.
/// The XML serializer.
/// The task manager.
/// The logger.
/// isoManager
public Kernel(IApplicationHost appHost, IServerApplicationPaths appPaths, IXmlSerializer xmlSerializer, ILogger logger)
: base(appHost, appPaths, xmlSerializer, logger)
{
Instance = this;
// For now there's no real way to inject this properly
BaseItem.Logger = logger;
Ratings.Logger = logger;
LocalizedStrings.Logger = logger;
// For now, until this can become an interface
BaseMetadataProvider.Logger = logger;
}
///
/// Composes the parts with ioc container.
///
protected override void FindParts()
{
InstallationManager = (InstallationManager)ApplicationHost.CreateInstance(typeof(InstallationManager));
FFMpegManager = (FFMpegManager)ApplicationHost.CreateInstance(typeof(FFMpegManager));
LibraryManager = (LibraryManager)ApplicationHost.CreateInstance(typeof(LibraryManager));
UserManager = (UserManager)ApplicationHost.CreateInstance(typeof(UserManager));
ImageManager = (ImageManager)ApplicationHost.CreateInstance(typeof(ImageManager));
ProviderManager = (ProviderManager)ApplicationHost.CreateInstance(typeof(ProviderManager));
UserDataManager = (UserDataManager)ApplicationHost.CreateInstance(typeof(UserDataManager));
PluginSecurityManager = (PluginSecurityManager)ApplicationHost.CreateInstance(typeof(PluginSecurityManager));
base.FindParts();
EntityResolutionIgnoreRules = ApplicationHost.GetExports();
UserDataRepositories = ApplicationHost.GetExports();
UserRepositories = ApplicationHost.GetExports();
DisplayPreferencesRepositories = ApplicationHost.GetExports();
ItemRepositories = ApplicationHost.GetExports();
WeatherProviders = ApplicationHost.GetExports();
IntroProviders = ApplicationHost.GetExports();
PluginConfigurationPages = ApplicationHost.GetExports();
ImageEnhancers = ApplicationHost.GetExports().OrderBy(e => e.Priority).ToArray();
PluginFolderCreators = ApplicationHost.GetExports();
StringFiles = ApplicationHost.GetExports();
EntityResolvers = ApplicationHost.GetExports().OrderBy(e => e.Priority).ToArray();
MetadataProviders = ApplicationHost.GetExports().OrderBy(e => e.Priority).ToArray();
}
///
/// Performs initializations that can be reloaded at anytime
///
/// Task.
protected override async Task ReloadInternal()
{
// Reset these so that they can be lazy loaded again
Users = null;
RootFolder = null;
await base.ReloadInternal().ConfigureAwait(false);
ReloadResourcePools();
ReloadFileSystemManager();
await UserManager.RefreshUsersMetadata(CancellationToken.None).ConfigureAwait(false);
}
///
/// Releases unmanaged and - optionally - managed resources.
///
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
protected override void Dispose(bool dispose)
{
if (dispose)
{
DisposeResourcePools();
DisposeFileSystemManager();
}
base.Dispose(dispose);
}
///
/// Disposes the resource pools.
///
private void DisposeResourcePools()
{
if (ResourcePools != null)
{
ResourcePools.Dispose();
ResourcePools = null;
}
}
///
/// Reloads the resource pools.
///
private void ReloadResourcePools()
{
DisposeResourcePools();
ResourcePools = new ResourcePool();
}
///
/// Called when [composable parts loaded].
///
/// Task.
protected override async Task OnComposablePartsLoaded()
{
// The base class will start up all the plugins
await base.OnComposablePartsLoaded().ConfigureAwait(false);
// Get the current item repository
ItemRepository = GetRepository(ItemRepositories, Configuration.ItemRepository);
var itemRepoTask = ItemRepository.Initialize();
// Get the current user repository
UserRepository = GetRepository(UserRepositories, Configuration.UserRepository);
var userRepoTask = UserRepository.Initialize();
// Get the current item repository
UserDataRepository = GetRepository(UserDataRepositories, Configuration.UserDataRepository);
var userDataRepoTask = UserDataRepository.Initialize();
// Get the current display preferences repository
DisplayPreferencesRepository = GetRepository(DisplayPreferencesRepositories, Configuration.DisplayPreferencesRepository);
var displayPreferencesRepoTask = DisplayPreferencesRepository.Initialize();
await Task.WhenAll(itemRepoTask, userRepoTask, userDataRepoTask, displayPreferencesRepoTask).ConfigureAwait(false);
}
///
/// Gets a repository by name from a list, and returns the default if not found
///
///
/// The repositories.
/// The name.
/// ``0.
private T GetRepository(IEnumerable repositories, string name)
where T : class, IRepository
{
var enumerable = repositories as T[] ?? repositories.ToArray();
return enumerable.FirstOrDefault(r => string.Equals(r.Name, name, StringComparison.OrdinalIgnoreCase)) ??
enumerable.FirstOrDefault();
}
///
/// Disposes the file system manager.
///
private void DisposeFileSystemManager()
{
if (FileSystemManager != null)
{
FileSystemManager.Dispose();
FileSystemManager = null;
}
}
///
/// Reloads the file system manager.
///
private void ReloadFileSystemManager()
{
DisposeFileSystemManager();
FileSystemManager = new FileSystemManager(this, Logger, ApplicationHost.Resolve());
FileSystemManager.StartWatchers();
}
///
/// Gets a User by Id
///
/// The id.
/// User.
///
public User GetUserById(Guid id)
{
if (id == Guid.Empty)
{
throw new ArgumentNullException();
}
return Users.FirstOrDefault(u => u.Id == id);
}
///
/// Finds a library item by Id and UserId.
///
/// The id.
/// The user id.
/// BaseItem.
/// id
public BaseItem GetItemById(Guid id, Guid userId)
{
if (id == Guid.Empty)
{
throw new ArgumentNullException("id");
}
if (userId == Guid.Empty)
{
throw new ArgumentNullException("userId");
}
var user = GetUserById(userId);
var userRoot = user.RootFolder;
return userRoot.FindItemById(id, user);
}
///
/// Gets the item by id.
///
/// The id.
/// BaseItem.
/// id
public BaseItem GetItemById(Guid id)
{
if (id == Guid.Empty)
{
throw new ArgumentNullException("id");
}
return RootFolder.FindItemById(id, null);
}
///
/// Completely overwrites the current configuration with a new copy
///
/// The config.
public void UpdateConfiguration(ServerConfiguration config)
{
Configuration = config;
SaveConfiguration();
// Validate currently executing providers, in the background
Task.Run(() =>
{
ProviderManager.ValidateCurrentlyRunningProviders();
});
}
///
/// Gets the system info.
///
/// SystemInfo.
public override SystemInfo GetSystemInfo()
{
var info = base.GetSystemInfo();
if (InstallationManager != null)
{
info.InProgressInstallations = InstallationManager.CurrentInstallations.Select(i => i.Item1).ToArray();
info.CompletedInstallations = InstallationManager.CompletedInstallations.ToArray();
}
return info;
}
}
}