From dbe43a0918fd85a1fe4f2e71589197f53aa72202 Mon Sep 17 00:00:00 2001 From: "shannon@ShandemVaio" Date: Fri, 27 Jul 2012 01:01:34 +0600 Subject: [PATCH] Updated the PluginTypeResolver to have a generic way of instantiating the plugin types it has already found. Have added some notes to some classes that need to be fixed up. Removed the PluginTypeResolver from the ApplicationContext since it is a utility and isn't exposing application objects. --- src/Umbraco.Core/ApplicationContext.cs | 9 ++---- src/Umbraco.Core/PluginTypeResolver.cs | 27 +++++++++++++++- src/Umbraco.Core/Resolving/ResolverBase.cs | 2 ++ .../PluginTypeResolverExtensions.cs | 2 +- .../PluginTypeResolverExtensions.cs | 31 ++----------------- .../Routing/DocumentLookupsResolver.cs | 14 ++++++--- src/Umbraco.Web/Routing/DocumentRequest.cs | 2 +- src/Umbraco.Web/UmbracoApplication.cs | 6 ++-- 8 files changed, 49 insertions(+), 44 deletions(-) diff --git a/src/Umbraco.Core/ApplicationContext.cs b/src/Umbraco.Core/ApplicationContext.cs index 586bf5e628..db50c4e853 100644 --- a/src/Umbraco.Core/ApplicationContext.cs +++ b/src/Umbraco.Core/ApplicationContext.cs @@ -17,10 +17,9 @@ namespace Umbraco.Core /// /// Constructor /// - /// - public ApplicationContext(PluginTypeResolver pluginResolver) + public ApplicationContext() { - PluginTypes = pluginResolver; + } /// @@ -45,10 +44,6 @@ namespace Umbraco.Core } } - /// - /// Gets the plugin resolver for the application - /// - public PluginTypeResolver PluginTypes { get; private set; } // notes // GlobalSettings.ConfigurationStatus returns the value that's in the web.config, so it's the "configured version" diff --git a/src/Umbraco.Core/PluginTypeResolver.cs b/src/Umbraco.Core/PluginTypeResolver.cs index b187fb8baa..426029de61 100644 --- a/src/Umbraco.Core/PluginTypeResolver.cs +++ b/src/Umbraco.Core/PluginTypeResolver.cs @@ -18,7 +18,7 @@ namespace Umbraco.Core /// This class can expose extension methods to resolve custom plugins /// /// - public class PluginTypeResolver + internal class PluginTypeResolver { private PluginTypeResolver() @@ -50,6 +50,31 @@ namespace Umbraco.Core private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(); private readonly HashSet _types = new HashSet(); + /// + /// Used to create instances of the specified type based on the resolved/cached plugin types + /// + /// + /// + internal IEnumerable CreateInstances() + { + var types = ResolveTypes(); + var instances = new List(); + foreach(var t in types) + { + try + { + var typeInstance = (T)Activator.CreateInstance(t); + instances.Add(typeInstance); + } + catch (Exception ex) + { + //TODO: Need to fix logging so this doesn't bork if no SQL connection + //Log.Add(LogTypes.Error, -1, "Error loading ILookup: " + ex.ToString()); + } + } + return instances; + } + /// /// Generic method to find the specified type and cache the result /// diff --git a/src/Umbraco.Core/Resolving/ResolverBase.cs b/src/Umbraco.Core/Resolving/ResolverBase.cs index a89c696db6..d7a62860a8 100644 --- a/src/Umbraco.Core/Resolving/ResolverBase.cs +++ b/src/Umbraco.Core/Resolving/ResolverBase.cs @@ -6,6 +6,8 @@ namespace Umbraco.Core.Resolving internal abstract class ResolverBase where TResolver : class { static TResolver _resolver; + + //TODO: This is not correct, this will be the same lock for all ResolverBase classes!! static readonly ReaderWriterLockSlim ResolversLock = new ReaderWriterLockSlim(); public static TResolver Current diff --git a/src/Umbraco.Tests/PluginTypeResolverExtensions.cs b/src/Umbraco.Tests/PluginTypeResolverExtensions.cs index da4f60020d..91fd0b9010 100644 --- a/src/Umbraco.Tests/PluginTypeResolverExtensions.cs +++ b/src/Umbraco.Tests/PluginTypeResolverExtensions.cs @@ -7,7 +7,7 @@ namespace Umbraco.Tests /// /// Used for PluginTypeResolverTests /// - public static class PluginTypeResolverExtensions + internal static class PluginTypeResolverExtensions { public static IEnumerable ResolveFindMeTypes(this PluginTypeResolver resolver) { diff --git a/src/Umbraco.Web/PluginTypeResolverExtensions.cs b/src/Umbraco.Web/PluginTypeResolverExtensions.cs index 628959bcf2..fa8ac22980 100644 --- a/src/Umbraco.Web/PluginTypeResolverExtensions.cs +++ b/src/Umbraco.Web/PluginTypeResolverExtensions.cs @@ -12,40 +12,15 @@ namespace Umbraco.Web /// public static class PluginTypeResolverExtensions { - private static IEnumerable _lookups; - private static readonly ReaderWriterLockSlim LookupsLocker = new ReaderWriterLockSlim(); /// - /// Returns all available ILookup objects + /// Returns all IDocumentLookup types /// /// /// - internal static IEnumerable ResolveLookups(this PluginTypeResolver resolver) + internal static IEnumerable ResolveLookups(this PluginTypeResolver resolver) { - if (_lookups == null) - { - using (new WriteLock(LookupsLocker)) - { - var lookupTypes = resolver.TypeFinder.FindClassesOfType(); - var lookups = new List(); - foreach (var l in lookupTypes) - { - try - { - var typeInstance = Activator.CreateInstance(l) as IDocumentLookup; - lookups.Add(typeInstance); - } - catch (Exception ex) - { - //TODO: Need to fix logging so this doesn't bork if no SQL connection - //Log.Add(LogTypes.Error, -1, "Error loading ILookup: " + ex.ToString()); - } - } - //set the global - _lookups = lookups; - } - } - return _lookups; + return resolver.ResolveTypes(); } } diff --git a/src/Umbraco.Web/Routing/DocumentLookupsResolver.cs b/src/Umbraco.Web/Routing/DocumentLookupsResolver.cs index 3e10a6e4d6..0e6cae28d2 100644 --- a/src/Umbraco.Web/Routing/DocumentLookupsResolver.cs +++ b/src/Umbraco.Web/Routing/DocumentLookupsResolver.cs @@ -11,9 +11,13 @@ namespace Umbraco.Web.Routing { class DocumentLookupsResolver : ResolverBase { - internal DocumentLookupsResolver(IEnumerable resolvers, IRequestDocumentLastChanceResolver lastChanceResolver) + internal DocumentLookupsResolver(IEnumerable resolvers, IRequestDocumentLastChanceResolver lastChanceResolver) { - _resolvers.AddRange(resolvers); + //TODO: I've changed this to resolve types but the intances are not created yet! + // I've created a method on the PluginTypeResolver to create types: PluginTypesResolver.Current.CreateInstances() + + + _resolverTypes.AddRange(resolvers); _lastChanceResolver.Value = lastChanceResolver; } @@ -31,14 +35,16 @@ namespace Umbraco.Web.Routing #region Resolvers + private readonly List _resolverTypes = new List(); readonly ManyWeightedResolved _resolvers = new ManyWeightedResolved(); - public IEnumerable RequestDocumentResolvers + public IEnumerable GetDocumentLookups { get { return _resolvers.Values; } } - public ManyWeightedResolved RequestDocumentResolversResolution + //why do we have this? + public ManyWeightedResolved GetDocumentLookupResolution { get { return _resolvers; } } diff --git a/src/Umbraco.Web/Routing/DocumentRequest.cs b/src/Umbraco.Web/Routing/DocumentRequest.cs index badad3746b..4aef634980 100644 --- a/src/Umbraco.Web/Routing/DocumentRequest.cs +++ b/src/Umbraco.Web/Routing/DocumentRequest.cs @@ -202,7 +202,7 @@ namespace Umbraco.Web.Routing // the first successful resolver, if any, will set this.Node, and may also set this.Template // some lookups may implement caching Trace.TraceInformation("{0}Begin resolvers", tracePrefix); - var lookups = RoutingContext.DocumentLookupsResolver.RequestDocumentResolvers; + var lookups = RoutingContext.DocumentLookupsResolver.GetDocumentLookups; lookups.Any(lookup => lookup.TrySetDocument(this)); Trace.TraceInformation("{0}End resolvers, {1}", tracePrefix, (this.HasNode ? "a document was found" : "no document was found")); diff --git a/src/Umbraco.Web/UmbracoApplication.cs b/src/Umbraco.Web/UmbracoApplication.cs index 9f31229ab1..fa8c20e49b 100644 --- a/src/Umbraco.Web/UmbracoApplication.cs +++ b/src/Umbraco.Web/UmbracoApplication.cs @@ -34,7 +34,7 @@ namespace Umbraco.Web ClientDependency.Core.CompositeFiles.Providers.BaseCompositeFileProcessingProvider.UrlTypeDefault = ClientDependency.Core.CompositeFiles.Providers.CompositeUrlType.Base64QueryStrings; //create the ApplicationContext - ApplicationContext.Current = new ApplicationContext(PluginTypeResolver.Current) + ApplicationContext.Current = new ApplicationContext() { IsReady = true // fixme }; @@ -43,7 +43,9 @@ namespace Umbraco.Web ApplicationStartupHandler.RegisterHandlers(); // create the resolvers - DocumentLookupsResolver.Current = new DocumentLookupsResolver(ApplicationContext.Current.PluginTypes.ResolveLookups(), new ResolveLastChance()); + DocumentLookupsResolver.Current = new DocumentLookupsResolver( + PluginTypeResolver.Current.ResolveLookups(), + new ResolveLastChance()); RoutesCacheResolver.Current = new RoutesCacheResolver(new DefaultRoutesCache()); OnApplicationStarting(sender, e);