From b8d5c718429f1325111834b8b95698fc9c9ba47d Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Sat, 23 Feb 2013 02:57:11 -0500 Subject: type discovery without attributes --- MediaBrowser.Common/Kernel/BaseKernel.cs | 115 +++++++------------------------ 1 file changed, 24 insertions(+), 91 deletions(-) (limited to 'MediaBrowser.Common/Kernel/BaseKernel.cs') diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs index 85954cb82..2b9550496 100644 --- a/MediaBrowser.Common/Kernel/BaseKernel.cs +++ b/MediaBrowser.Common/Kernel/BaseKernel.cs @@ -9,9 +9,6 @@ using MediaBrowser.Model.Logging; using MediaBrowser.Model.System; using System; using System.Collections.Generic; -using System.ComponentModel.Composition; -using System.ComponentModel.Composition.Hosting; -using System.ComponentModel.Composition.Primitives; using System.Diagnostics; using System.IO; using System.Linq; @@ -182,28 +179,14 @@ namespace MediaBrowser.Common.Kernel /// Gets the list of currently loaded plugins /// /// The plugins. - [ImportMany(typeof(IPlugin))] public IEnumerable Plugins { get; protected set; } - /// - /// Gets the list of Scheduled Tasks - /// - /// The scheduled tasks. - [ImportMany(typeof(IScheduledTask))] - public IEnumerable ScheduledTasks { get; private set; } - /// /// Gets the web socket listeners. /// /// The web socket listeners. public IEnumerable WebSocketListeners { get; private set; } - /// - /// Gets the MEF CompositionContainer - /// - /// The composition container. - private CompositionContainer CompositionContainer { get; set; } - /// /// The _HTTP manager /// @@ -216,12 +199,6 @@ namespace MediaBrowser.Common.Kernel /// The TCP manager. public TcpManager TcpManager { get; private set; } - /// - /// Gets the task manager. - /// - /// The task manager. - public TaskManager TaskManager { get; private set; } - /// /// Gets the rest services. /// @@ -324,11 +301,17 @@ namespace MediaBrowser.Common.Kernel /// The application host. protected IApplicationHost ApplicationHost { get; private set; } + /// + /// Gets or sets the task manager. + /// + /// The task manager. + protected ITaskManager TaskManager { get; set; } + /// /// Gets the assemblies. /// /// The assemblies. - public Assembly[] Assemblies { get; private set; } + protected Assembly[] Assemblies { get; private set; } /// /// Gets all types. @@ -407,7 +390,7 @@ namespace MediaBrowser.Common.Kernel await OnConfigurationLoaded().ConfigureAwait(false); DisposeTaskManager(); - TaskManager = new TaskManager(this, Logger); + TaskManager = new TaskManager(Logger); Logger.Info("Loading Plugins"); await ReloadComposableParts().ConfigureAwait(false); @@ -453,8 +436,6 @@ namespace MediaBrowser.Common.Kernel ComposeParts(AllTypes); await OnComposablePartsLoaded().ConfigureAwait(false); - - CompositionContainer.Catalog.Dispose(); } /// @@ -465,11 +446,7 @@ namespace MediaBrowser.Common.Kernel { var concreteTypes = allTypes.Where(t => t.IsClass && !t.IsAbstract && !t.IsInterface && !t.IsGenericType).ToArray(); - CompositionContainer = GetSafeCompositionContainer(concreteTypes.Select(i => new TypeCatalog(i))); - - RegisterExportedValues(CompositionContainer); - - CompositionContainer.ComposeParts(this); + RegisterExportedValues(); FindParts(concreteTypes); } @@ -482,6 +459,11 @@ namespace MediaBrowser.Common.Kernel { RestServices = GetExports(allTypes); WebSocketListeners = GetExports(allTypes); + Plugins = GetExports(allTypes); + + var tasks = GetExports(allTypes, false); + + TaskManager.AddTasks(tasks); } /// @@ -489,8 +471,9 @@ namespace MediaBrowser.Common.Kernel /// /// /// All types. + /// if set to true [manage liftime]. /// IEnumerable{``0}. - protected IEnumerable GetExports(Type[] allTypes) + protected IEnumerable GetExports(Type[] allTypes, bool manageLiftime = true) { var currentType = typeof(T); @@ -498,7 +481,10 @@ namespace MediaBrowser.Common.Kernel var parts = allTypes.Where(currentType.IsAssignableFrom).Select(Instantiate).Cast().ToArray(); - _disposableParts.AddRange(parts.OfType()); + if (manageLiftime) + { + _disposableParts.AddRange(parts.OfType()); + } return parts; } @@ -517,13 +503,10 @@ namespace MediaBrowser.Common.Kernel /// Composes the exported values. /// /// The container. - protected virtual void RegisterExportedValues(CompositionContainer container) + protected virtual void RegisterExportedValues() { ApplicationHost.Register(this); - - container.ComposeExportedValue("logger", Logger); - container.ComposeExportedValue("appHost", ApplicationHost); - container.ComposeExportedValue("isoManager", ApplicationHost.Resolve()); + ApplicationHost.Register(TaskManager); } /// @@ -590,46 +573,6 @@ namespace MediaBrowser.Common.Kernel yield return GetType().Assembly; } - /// - /// Plugins that live on both the server and UI are going to have references to assemblies from both sides. - /// But looks for Parts on one side, it will throw an exception when it seems Types from the other side that it doesn't have a reference to. - /// For example, a plugin provides a Resolver. When MEF runs in the UI, it will throw an exception when it sees the resolver because there won't be a reference to the base class. - /// This method will catch those exceptions while retining the list of Types that MEF is able to resolve. - /// - /// The catalogs. - /// CompositionContainer. - /// catalogs - private static CompositionContainer GetSafeCompositionContainer(IEnumerable catalogs) - { - if (catalogs == null) - { - throw new ArgumentNullException("catalogs"); - } - - var newList = new List(); - - // Go through each Catalog - foreach (var catalog in catalogs) - { - try - { - // Try to have MEF find Parts - catalog.Parts.ToArray(); - - // If it succeeds we can use the entire catalog - newList.Add(catalog); - } - catch (ReflectionTypeLoadException ex) - { - // If it fails we can still get a list of the Types it was able to resolve and create TypeCatalogs - var typeCatalogs = ex.Types.Where(t => t != null).Select(t => new TypeCatalog(t)); - newList.AddRange(typeCatalogs); - } - } - - return new CompositionContainer(new AggregateCatalog(newList)); - } - /// /// Gets a list of types within an assembly /// This will handle situations that would normally throw an exception - such as a type within the assembly that depends on some other non-existant reference @@ -663,11 +606,6 @@ namespace MediaBrowser.Common.Kernel { return Task.Run(() => { - foreach (var task in ScheduledTasks) - { - task.Initialize(this, Logger); - } - // Start-up each plugin Parallel.ForEach(Plugins, plugin => { @@ -722,11 +660,6 @@ namespace MediaBrowser.Common.Kernel DisposeComposableParts(); - foreach (var part in _disposableParts) - { - part.Dispose(); - } - _disposableParts.Clear(); } } @@ -772,9 +705,9 @@ namespace MediaBrowser.Common.Kernel /// protected virtual void DisposeComposableParts() { - if (CompositionContainer != null) + foreach (var part in _disposableParts) { - CompositionContainer.Dispose(); + part.Dispose(); } } -- cgit v1.2.3