From 44c3140b20b2aa28927ccfee81937aa7bac1883b Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 22 Jul 2015 12:44:29 +0200 Subject: [PATCH] Updates CoreBootManager to be provided a logger which decouples it a little bit, updates CoreBootManagerTests to ensure resolution is reset on teardown, updates the ApplicationEventsResolver inner resolver to not use obsolete ctor's --- src/Umbraco.Core/CoreBootManager.cs | 72 ++++++++++--------- .../ApplicationEventsResolver.cs | 5 +- .../BootManagers/CoreBootManagerTests.cs | 21 ++++-- .../Routing/RenderRouteHandlerTests.cs | 3 +- .../TestHelpers/BaseUmbracoApplicationTest.cs | 3 +- src/Umbraco.Web/WebBootManager.cs | 14 ++-- 6 files changed, 70 insertions(+), 48 deletions(-) diff --git a/src/Umbraco.Core/CoreBootManager.cs b/src/Umbraco.Core/CoreBootManager.cs index 0ebe05565b..2b1f14cded 100644 --- a/src/Umbraco.Core/CoreBootManager.cs +++ b/src/Umbraco.Core/CoreBootManager.cs @@ -41,7 +41,7 @@ namespace Umbraco.Core /// public class CoreBootManager : IBootManager { - private ProfilingLogger _profilingLogger; + protected ProfilingLogger ProfilingLogger { get; private set; } private DisposableTimer _timer; private bool _isInitialized = false; private bool _isStarted = false; @@ -68,6 +68,14 @@ namespace Umbraco.Core _umbracoApplication = umbracoApplication; } + internal CoreBootManager(UmbracoApplicationBase umbracoApplication, ProfilingLogger logger) + { + if (umbracoApplication == null) throw new ArgumentNullException("umbracoApplication"); + if (logger == null) throw new ArgumentNullException(nameof(logger)); + _umbracoApplication = umbracoApplication; + ProfilingLogger = logger; + } + public virtual IBootManager Initialize() { if (_isInitialized) @@ -76,9 +84,9 @@ namespace Umbraco.Core InitializeLoggerResolver(); InitializeProfilerResolver(); - _profilingLogger = new ProfilingLogger(LoggerResolver.Current.Logger, ProfilerResolver.Current.Profiler); + ProfilingLogger = ProfilingLogger?? new ProfilingLogger(LoggerResolver.Current.Logger, ProfilerResolver.Current.Profiler); - _timer = _profilingLogger.TraceDuration( + _timer = ProfilingLogger.TraceDuration( string.Format("Umbraco {0} application starting on {1}", UmbracoVersion.GetSemanticVersion().ToSemanticString(), NetworkHelper.MachineName), "Umbraco application startup complete"); @@ -86,7 +94,7 @@ namespace Umbraco.Core //create and set the plugin manager (I'd much prefer to not use this singleton anymore but many things are using it unfortunately and // the way that it is setup, there must only ever be one per app so without IoC it would be hard to make this not a singleton) - PluginManager = new PluginManager(ServiceProvider, ApplicationCache.RuntimeCache, _profilingLogger); + PluginManager = new PluginManager(ServiceProvider, ApplicationCache.RuntimeCache, ProfilingLogger); PluginManager.Current = PluginManager; //Create the legacy prop-eds mapping @@ -94,24 +102,24 @@ namespace Umbraco.Core LegacyParameterEditorAliasConverter.CreateMappingsForCoreEditors(); //create database and service contexts for the app context - var dbFactory = new DefaultDatabaseFactory(GlobalSettings.UmbracoConnectionName, LoggerResolver.Current.Logger); + var dbFactory = new DefaultDatabaseFactory(GlobalSettings.UmbracoConnectionName, ProfilingLogger.Logger); Database.Mapper = new PetaPocoMapper(); var dbContext = new DatabaseContext( dbFactory, - LoggerResolver.Current.Logger, - SqlSyntaxProviders.CreateDefault(LoggerResolver.Current.Logger)); + ProfilingLogger.Logger, + SqlSyntaxProviders.CreateDefault(ProfilingLogger.Logger)); //initialize the DatabaseContext dbContext.Initialize(); var serviceContext = new ServiceContext( - new RepositoryFactory(ApplicationCache, LoggerResolver.Current.Logger, dbContext.SqlSyntax, UmbracoConfig.For.UmbracoSettings()), + new RepositoryFactory(ApplicationCache, ProfilingLogger.Logger, dbContext.SqlSyntax, UmbracoConfig.For.UmbracoSettings()), new PetaPocoUnitOfWorkProvider(dbFactory), new FileUnitOfWorkProvider(), new PublishingStrategy(), ApplicationCache, - LoggerResolver.Current.Logger); + ProfilingLogger.Logger); CreateApplicationContext(dbContext, serviceContext); @@ -123,7 +131,7 @@ namespace Umbraco.Core InitializeModelMappers(); - using (_profilingLogger.DebugDuration( + using (ProfilingLogger.DebugDuration( string.Format("Executing {0} IApplicationEventHandler.OnApplicationInitialized", ApplicationEventsResolver.Current.ApplicationEventHandlers.Count()), "Finished executing IApplicationEventHandler.OnApplicationInitialized")) { @@ -137,7 +145,7 @@ namespace Umbraco.Core } catch (Exception ex) { - _profilingLogger.Logger.Error("An error occurred running OnApplicationInitialized for handler " + x.GetType(), ex); + ProfilingLogger.Logger.Error("An error occurred running OnApplicationInitialized for handler " + x.GetType(), ex); throw; } }); @@ -156,7 +164,7 @@ namespace Umbraco.Core protected virtual void CreateApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext) { //create the ApplicationContext - ApplicationContext = ApplicationContext.Current = new ApplicationContext(dbContext, serviceContext, ApplicationCache, _profilingLogger); + ApplicationContext = ApplicationContext.Current = new ApplicationContext(dbContext, serviceContext, ApplicationCache, ProfilingLogger); } /// @@ -196,7 +204,7 @@ namespace Umbraco.Core /// protected virtual void InitializeLoggerResolver() { - LoggerResolver.Current = new LoggerResolver(Logger.CreateWithDefaultLog4NetConfiguration()) + LoggerResolver.Current = new LoggerResolver(ProfilingLogger == null ? Logger.CreateWithDefaultLog4NetConfiguration() : ProfilingLogger.Logger) { //This is another special resolver that needs to be resolvable before resolution is frozen //since it is used for profiling the application startup @@ -210,7 +218,7 @@ namespace Umbraco.Core protected virtual void InitializeProfilerResolver() { //By default we'll initialize the Log profiler (in the web project, we'll override with the web profiler) - ProfilerResolver.Current = new ProfilerResolver(new LogProfiler(LoggerResolver.Current.Logger)) + ProfilerResolver.Current = new ProfilerResolver(ProfilingLogger == null ? new LogProfiler(LoggerResolver.Current.Logger) : ProfilingLogger.Profiler) { //This is another special resolver that needs to be resolvable before resolution is frozen //since it is used for profiling the application startup @@ -231,7 +239,7 @@ namespace Umbraco.Core //... and set the special flag to let us resolve before frozen resolution ApplicationEventsResolver.Current = new ApplicationEventsResolver( ServiceProvider, - LoggerResolver.Current.Logger, + ProfilingLogger.Logger, PluginManager.ResolveApplicationStartupHandlers()) { CanResolveBeforeFrozen = true @@ -260,7 +268,7 @@ namespace Umbraco.Core if (_isStarted) throw new InvalidOperationException("The boot manager has already been initialized"); - using (_profilingLogger.DebugDuration( + using (ProfilingLogger.DebugDuration( string.Format("Executing {0} IApplicationEventHandler.OnApplicationStarting", ApplicationEventsResolver.Current.ApplicationEventHandlers.Count()), "Finished executing IApplicationEventHandler.OnApplicationStarting")) { @@ -274,7 +282,7 @@ namespace Umbraco.Core } catch (Exception ex) { - _profilingLogger.Logger.Error("An error occurred running OnApplicationStarting for handler " + x.GetType(), ex); + ProfilingLogger.Logger.Error("An error occurred running OnApplicationStarting for handler " + x.GetType(), ex); throw; } }); @@ -305,7 +313,7 @@ namespace Umbraco.Core //Here we need to make sure the db can be connected to EnsureDatabaseConnection(); - using (_profilingLogger.DebugDuration( + using (ProfilingLogger.DebugDuration( string.Format("Executing {0} IApplicationEventHandler.OnApplicationStarted", ApplicationEventsResolver.Current.ApplicationEventHandlers.Count()), "Finished executing IApplicationEventHandler.OnApplicationStarted")) { @@ -319,7 +327,7 @@ namespace Umbraco.Core } catch (Exception ex) { - _profilingLogger.Logger.Error("An error occurred running OnApplicationStarted for handler " + x.GetType(), ex); + ProfilingLogger.Logger.Error("An error occurred running OnApplicationStarted for handler " + x.GetType(), ex); throw; } }); @@ -386,12 +394,12 @@ namespace Umbraco.Core ApplicationCache.RuntimeCache, new ManifestParser(new DirectoryInfo(IOHelper.MapPath("~/App_Plugins")), ApplicationCache.RuntimeCache)); - PropertyEditorResolver.Current = new PropertyEditorResolver(ServiceProvider, LoggerResolver.Current.Logger, () => PluginManager.ResolvePropertyEditors(), builder); - ParameterEditorResolver.Current = new ParameterEditorResolver(ServiceProvider, LoggerResolver.Current.Logger, () => PluginManager.ResolveParameterEditors(), builder); + PropertyEditorResolver.Current = new PropertyEditorResolver(ServiceProvider, ProfilingLogger.Logger, () => PluginManager.ResolvePropertyEditors(), builder); + ParameterEditorResolver.Current = new ParameterEditorResolver(ServiceProvider, ProfilingLogger.Logger, () => PluginManager.ResolveParameterEditors(), builder); //setup the validators resolver with our predefined validators ValidatorsResolver.Current = new ValidatorsResolver( - ServiceProvider, LoggerResolver.Current.Logger, new[] + ServiceProvider, ProfilingLogger.Logger, new[] { new Lazy(() => typeof (RequiredManifestValueValidator)), new Lazy(() => typeof (RegexValidator)), @@ -421,7 +429,7 @@ namespace Umbraco.Core new DatabaseServerMessenger(ApplicationContext, true, new DatabaseServerMessengerOptions())); MappingResolver.Current = new MappingResolver( - ServiceProvider, LoggerResolver.Current.Logger, + ServiceProvider, ProfilingLogger.Logger, () => PluginManager.ResolveAssignedMapperTypes()); @@ -429,38 +437,38 @@ namespace Umbraco.Core // new RepositoryFactory(ApplicationCache)); CacheRefreshersResolver.Current = new CacheRefreshersResolver( - ServiceProvider, LoggerResolver.Current.Logger, + ServiceProvider, ProfilingLogger.Logger, () => PluginManager.ResolveCacheRefreshers()); DataTypesResolver.Current = new DataTypesResolver( - ServiceProvider, LoggerResolver.Current.Logger, + ServiceProvider, ProfilingLogger.Logger, () => PluginManager.ResolveDataTypes()); MacroFieldEditorsResolver.Current = new MacroFieldEditorsResolver( - ServiceProvider, LoggerResolver.Current.Logger, + ServiceProvider, ProfilingLogger.Logger, () => PluginManager.ResolveMacroRenderings()); PackageActionsResolver.Current = new PackageActionsResolver( - ServiceProvider, LoggerResolver.Current.Logger, + ServiceProvider, ProfilingLogger.Logger, () => PluginManager.ResolvePackageActions()); ActionsResolver.Current = new ActionsResolver( - ServiceProvider, LoggerResolver.Current.Logger, + ServiceProvider, ProfilingLogger.Logger, () => PluginManager.ResolveActions()); //the database migration objects MigrationResolver.Current = new MigrationResolver( - LoggerResolver.Current.Logger, + ProfilingLogger.Logger, () => PluginManager.ResolveTypes()); // todo: remove once we drop IPropertyEditorValueConverter support. PropertyEditorValueConvertersResolver.Current = new PropertyEditorValueConvertersResolver( - ServiceProvider, LoggerResolver.Current.Logger, + ServiceProvider, ProfilingLogger.Logger, PluginManager.ResolvePropertyEditorValueConverters()); // need to filter out the ones we dont want!! PropertyValueConvertersResolver.Current = new PropertyValueConvertersResolver( - ServiceProvider, LoggerResolver.Current.Logger, + ServiceProvider, ProfilingLogger.Logger, PluginManager.ResolveTypes()); // use the new DefaultShortStringHelper @@ -469,7 +477,7 @@ namespace Umbraco.Core new DefaultShortStringHelper(UmbracoConfig.For.UmbracoSettings()).WithDefaultConfig()); UrlSegmentProviderResolver.Current = new UrlSegmentProviderResolver( - ServiceProvider, LoggerResolver.Current.Logger, + ServiceProvider, ProfilingLogger.Logger, typeof(DefaultUrlSegmentProvider)); // by default, no factory is activated diff --git a/src/Umbraco.Core/ObjectResolution/ApplicationEventsResolver.cs b/src/Umbraco.Core/ObjectResolution/ApplicationEventsResolver.cs index 1b55a89a9c..78c8f483f2 100644 --- a/src/Umbraco.Core/ObjectResolution/ApplicationEventsResolver.cs +++ b/src/Umbraco.Core/ObjectResolution/ApplicationEventsResolver.cs @@ -29,6 +29,7 @@ namespace Umbraco.Core.ObjectResolution { //create the legacy resolver and only include the legacy types _legacyResolver = new LegacyStartupHandlerResolver( + serviceProvider, logger, applicationEventHandlers.Where(x => !TypeHelper.IsTypeAssignableFrom(x))); } @@ -70,8 +71,8 @@ namespace Umbraco.Core.ObjectResolution private class LegacyStartupHandlerResolver : ManyObjectsResolverBase, IDisposable { - internal LegacyStartupHandlerResolver(IEnumerable legacyStartupHandlers) - : base(legacyStartupHandlers) + internal LegacyStartupHandlerResolver(IServiceProvider serviceProvider, ILogger logger, IEnumerable legacyStartupHandlers) + : base(serviceProvider, logger, legacyStartupHandlers) { } diff --git a/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs b/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs index fcd43bbcd8..d2e5a9db12 100644 --- a/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs +++ b/src/Umbraco.Tests/BootManagers/CoreBootManagerTests.cs @@ -13,6 +13,7 @@ using Umbraco.Core.Persistence.SqlSyntax; using Umbraco.Tests.TestHelpers; using umbraco.interfaces; using Umbraco.Core.Persistence; +using Umbraco.Core.Profiling; using Umbraco.Core.Services; namespace Umbraco.Tests.BootManagers @@ -35,7 +36,8 @@ namespace Umbraco.Tests.BootManagers { base.TearDown(); - _testApp = null; + _testApp = null; + ResolverCollection.ResetAll(); } @@ -46,7 +48,7 @@ namespace Umbraco.Tests.BootManagers { protected override IBootManager GetBootManager() { - return new TestBootManager(this); + return new TestBootManager(this, new ProfilingLogger(Mock.Of(), Mock.Of())); } } @@ -55,8 +57,8 @@ namespace Umbraco.Tests.BootManagers /// public class TestBootManager : CoreBootManager { - public TestBootManager(UmbracoApplicationBase umbracoApplication) - : base(umbracoApplication) + public TestBootManager(UmbracoApplicationBase umbracoApplication, ProfilingLogger logger) + : base(umbracoApplication, logger) { } @@ -69,7 +71,7 @@ namespace Umbraco.Tests.BootManagers { base.CreateApplicationContext(dbContext, serviceContext); - var dbContextMock = new Mock(Mock.Of(), Mock.Of(), Mock.Of(), "test"); + var dbContextMock = new Mock(Mock.Of(), ProfilingLogger.Logger, Mock.Of(), "test"); dbContextMock.Setup(x => x.CanConnect).Returns(true); ApplicationContext.DatabaseContext = dbContextMock.Object; } @@ -78,7 +80,7 @@ namespace Umbraco.Tests.BootManagers { //create an empty resolver so we can add our own custom ones (don't type find) ApplicationEventsResolver.Current = new ApplicationEventsResolver( - new ActivatorServiceProvider(), Mock.Of(), + new ActivatorServiceProvider(), ProfilingLogger.Logger, new Type[] { typeof(LegacyStartupHandler), @@ -89,6 +91,13 @@ namespace Umbraco.Tests.BootManagers }; } + protected override void InitializeLoggerResolver() + { + } + + protected override void InitializeProfilerResolver() + { + } } /// diff --git a/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs b/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs index 9744f189aa..f7dc8c160c 100644 --- a/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs +++ b/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs @@ -13,6 +13,7 @@ using Umbraco.Web.Mvc; using Umbraco.Web.Routing; using Umbraco.Web.WebApi; using umbraco.BusinessLogic; +using Umbraco.Core.Profiling; using Umbraco.Core.Strings; namespace Umbraco.Tests.Routing @@ -28,7 +29,7 @@ namespace Umbraco.Tests.Routing SettingsForTests.UmbracoPath = "~/umbraco"; - var webBoot = new WebBootManager(new UmbracoApplication(), true); + var webBoot = new WebBootManager(new UmbracoApplication(), new ProfilingLogger(Mock.Of(), Mock.Of()), true); //webBoot.Initialize(); //webBoot.Startup(null); -> don't call startup, we don't want any other application event handlers to bind for this test. //webBoot.Complete(null); diff --git a/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs b/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs index 23c3391e84..3639a7709c 100644 --- a/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs +++ b/src/Umbraco.Tests/TestHelpers/BaseUmbracoApplicationTest.cs @@ -63,8 +63,7 @@ namespace Umbraco.Tests.TestHelpers public override void TearDown() { base.TearDown(); - - LoggerResolver.Reset(); + //reset settings SettingsForTests.Reset(); UmbracoContext.Current = null; diff --git a/src/Umbraco.Web/WebBootManager.cs b/src/Umbraco.Web/WebBootManager.cs index 497056a518..fa2a0e2fc7 100644 --- a/src/Umbraco.Web/WebBootManager.cs +++ b/src/Umbraco.Web/WebBootManager.cs @@ -54,18 +54,19 @@ namespace Umbraco.Web private readonly List _indexesToRebuild = new List(); public WebBootManager(UmbracoApplicationBase umbracoApplication) - : this(umbracoApplication, false) + : base(umbracoApplication) { - + _isForTesting = false; } /// /// Constructor for unit tests, ensures some resolvers are not initialized /// /// + /// /// - internal WebBootManager(UmbracoApplicationBase umbracoApplication, bool isForTesting) - : base(umbracoApplication) + internal WebBootManager(UmbracoApplicationBase umbracoApplication, ProfilingLogger logger, bool isForTesting) + : base(umbracoApplication, logger) { _isForTesting = isForTesting; } @@ -140,7 +141,10 @@ namespace Umbraco.Web UmbracoContext.EnsureContext( httpContext, ApplicationContext, - new WebSecurity(httpContext, ApplicationContext)); + new WebSecurity(httpContext, ApplicationContext), + UmbracoConfig.For.UmbracoSettings(), + UrlProviderResolver.Current.Providers, + false); } ///