Esempio n. 1
0
 /// <summary>
 /// Returns a unique hash for the combination of FileInfo objects passed in
 /// </summary>
 /// <param name="filesAndFolders"></param>
 /// <returns></returns>
 internal static long GetAssembliesHash(IEnumerable <FileSystemInfo> filesAndFolders)
 {
     using (DisposableTimer.TraceDuration <PluginManager>("Determining hash of code files on disk", "Hash determined"))
     {
         var hashCombiner = new HashCodeCombiner();
         //add each unique folder to the hash
         foreach (var i in filesAndFolders.DistinctBy(x => x.FullName))
         {
             hashCombiner.AddFileSystemItem(i);
         }
         return(ConvertPluginsHashFromHex(hashCombiner.GetCombinedHashCode()));
     }
 }
Esempio n. 2
0
        public virtual IBootManager Initialize()
        {
            if (_isInitialized)
            {
                throw new InvalidOperationException("The boot manager has already been initialized");
            }

            LogHelper.Info <CoreBootManager>("Umbraco application starting");
            _timer = DisposableTimer.Start(x => LogHelper.Info <CoreBootManager>("Umbraco application startup complete" + " (took " + x + "ms)"));

            //create the ApplicationContext
            ApplicationContext = ApplicationContext.Current = new ApplicationContext();

            InitializeResolvers();

            _isInitialized = true;

            return(this);
        }
Esempio n. 3
0
        public virtual IBootManager Initialize()
        {
            if (_isInitialized)
            {
                throw new InvalidOperationException("The boot manager has already been initialized");
            }

            InitializeProfilerResolver();

            _timer = DisposableTimer.DebugDuration <CoreBootManager>("Umbraco application starting", "Umbraco application startup complete");

            //create database and service contexts for the app context
            var dbFactory = new DefaultDatabaseFactory(GlobalSettings.UmbracoConnectionName);

            Database.Mapper = new PetaPocoMapper();
            var dbContext      = new DatabaseContext(dbFactory);
            var serviceContext = new ServiceContext(
                new PetaPocoUnitOfWorkProvider(dbFactory),
                new FileUnitOfWorkProvider(),
                new PublishingStrategy());

            CreateApplicationContext(dbContext, serviceContext);

            InitializeApplicationEventsResolver();

            InitializeResolvers();

            //initialize the DatabaseContext
            dbContext.Initialize();

            //now we need to call the initialize methods
            ApplicationEventsResolver.Current.ApplicationEventHandlers
            .ForEach(x => x.OnApplicationInitialized(UmbracoApplication, ApplicationContext));

            _isInitialized = true;

            return(this);
        }
Esempio n. 4
0
        private IEnumerable <Type> ResolveTypes <T>(
            Func <IEnumerable <Type> > finder,
            TypeResolutionKind resolutionType,
            bool cacheResult)
        {
            using (var readLock = new UpgradeableReadLock(_lock))
            {
                using (DisposableTimer.TraceDuration <PluginManager>(
                           String.Format("Starting resolution types of {0}", typeof(T).FullName),
                           String.Format("Completed resolution of types of {0}", typeof(T).FullName)))
                {
                    //check if the TypeList already exists, if so return it, if not we'll create it
                    var typeList = _types.SingleOrDefault(x => x.IsTypeList <T>(resolutionType));
                    //if we're not caching the result then proceed, or if the type list doesn't exist then proceed
                    if (!cacheResult || typeList == null)
                    {
                        //upgrade to a write lock since we're adding to the collection
                        readLock.UpgradeToWriteLock();

                        typeList = new TypeList <T>(resolutionType);

                        foreach (var t in finder())
                        {
                            typeList.AddType(t);
                        }

                        //only add the cache if we are to cache the results
                        if (cacheResult)
                        {
                            //add the type list to the collection
                            _types.Add(typeList);
                        }
                    }
                    return(typeList.GetTypes());
                }
            }
        }
Esempio n. 5
0
        public virtual IBootManager Initialize()
        {
            if (_isInitialized)
            {
                throw new InvalidOperationException("The boot manager has already been initialized");
            }

            InitializeLoggerResolver();
            InitializeProfilerResolver();

            ProfilingLogger = ProfilingLogger ?? new ProfilingLogger(LoggerResolver.Current.Logger, ProfilerResolver.Current.Profiler);

            _timer = ProfilingLogger.TraceDuration <CoreBootManager>(
                string.Format("Umbraco {0} application starting on {1}", UmbracoVersion.GetSemanticVersion().ToSemanticString(), NetworkHelper.MachineName),
                "Umbraco application startup complete");

            ApplicationCache = CreateApplicationCache();

            //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.Current = PluginManager;

            //Create the legacy prop-eds mapping
            LegacyPropertyEditorIdToAliasConverter.CreateMappingsForCoreEditors();
            LegacyParameterEditorAliasConverter.CreateMappingsForCoreEditors();

            //create database and service contexts for the app context
            var dbFactory = new DefaultDatabaseFactory(Constants.System.UmbracoConnectionName, ProfilingLogger.Logger);

            Database.Mapper = new PetaPocoMapper();

            var scopeProvider = new ScopeProvider(dbFactory);

            dbFactory.ScopeProvider = scopeProvider;

            var dbContext = new DatabaseContext(
                scopeProvider,
                ProfilingLogger.Logger,
                SqlSyntaxProviders.CreateDefault(ProfilingLogger.Logger));

            //initialize the DatabaseContext
            dbContext.Initialize();

            //get the service context
            var serviceContext = CreateServiceContext(dbContext, scopeProvider);

            //set property and singleton from response
            ApplicationContext.Current = ApplicationContext = CreateApplicationContext(dbContext, serviceContext);

            InitializeApplicationEventsResolver();

            InitializeResolvers();

            InitializeModelMappers();

            using (ProfilingLogger.DebugDuration <CoreBootManager>(
                       string.Format("Executing {0} IApplicationEventHandler.OnApplicationInitialized", ApplicationEventsResolver.Current.ApplicationEventHandlers.Count()),
                       "Finished executing IApplicationEventHandler.OnApplicationInitialized"))
            {
                //now we need to call the initialize methods
                ApplicationEventsResolver.Current.ApplicationEventHandlers
                .ForEach(x =>
                {
                    try
                    {
                        using (ProfilingLogger.DebugDuration <CoreBootManager>(string.Format("Executing {0} in ApplicationInitialized", x.GetType())))
                        {
                            x.OnApplicationInitialized(UmbracoApplication, ApplicationContext);
                        }
                    }
                    catch (Exception ex)
                    {
                        ProfilingLogger.Logger.Error <CoreBootManager>("An error occurred running OnApplicationInitialized for handler " + x.GetType(), ex);
                        throw;
                    }
                });
            }

            _isInitialized = true;

            return(this);
        }
Esempio n. 6
0
        private IEnumerable <Type> ResolveTypes <T>(
            Func <IEnumerable <Type> > finder,
            TypeResolutionKind resolutionType,
            bool cacheResult)
        {
            using (var readLock = new UpgradeableReadLock(_lock))
            {
                var typesFound = new List <Type>();

                using (DisposableTimer.TraceDuration <PluginManager>(
                           () => String.Format("Starting resolution types of {0}", typeof(T).FullName),
                           () => String.Format("Completed resolution of types of {0}, found {1}", typeof(T).FullName, typesFound.Count)))
                {
                    //check if the TypeList already exists, if so return it, if not we'll create it
                    var typeList = _types.SingleOrDefault(x => x.IsTypeList <T>(resolutionType));
                    //if we're not caching the result then proceed, or if the type list doesn't exist then proceed
                    if (!cacheResult || typeList == null)
                    {
                        //upgrade to a write lock since we're adding to the collection
                        readLock.UpgradeToWriteLock();

                        typeList = new TypeList <T>(resolutionType);

                        //we first need to look into our cache file (this has nothing to do with the 'cacheResult' parameter which caches in memory).
                        //if assemblies have not changed and the cache file actually exists, then proceed to try to lookup by the cache file.
                        if (!HaveAssembliesChanged && File.Exists(GetPluginListFilePath()))
                        {
                            var fileCacheResult = TryGetCachedPluginsFromFile <T>(resolutionType);

                            //here we need to identify if the CachedPluginNotFoundInFile was the exception, if it was then we need to re-scan
                            //in some cases the plugin will not have been scanned for on application startup, but the assemblies haven't changed
                            //so in this instance there will never be a result.
                            if (fileCacheResult.Error != null && fileCacheResult.Error is CachedPluginNotFoundInFile)
                            {
                                //we don't have a cache for this so proceed to look them up by scanning
                                LoadViaScanningAndUpdateCacheFile <T>(typeList, resolutionType, finder);
                            }
                            else
                            {
                                if (fileCacheResult.Success)
                                {
                                    var successfullyLoadedFromCache = true;
                                    //we have a previous cache for this so we don't need to scan we just load what has been found in the file
                                    foreach (var t in fileCacheResult.Result)
                                    {
                                        try
                                        {
                                            //we use the build manager to ensure we get all types loaded, this is slightly slower than
                                            //Type.GetType but if the types in the assembly aren't loaded yet then we have problems with that.
                                            var type = BuildManager.GetType(t, true);
                                            typeList.AddType(type);
                                        }
                                        catch (Exception ex)
                                        {
                                            //if there are any exceptions loading types, we have to exist, this should never happen so
                                            //we will need to revert to scanning for types.
                                            successfullyLoadedFromCache = false;
                                            LogHelper.Error <PluginManager>("Could not load a cached plugin type: " + t + " now reverting to re-scanning assemblies for the base type: " + typeof(T).FullName, ex);
                                            break;
                                        }
                                    }
                                    if (!successfullyLoadedFromCache)
                                    {
                                        //we need to manually load by scanning if loading from the file was not successful.
                                        LoadViaScanningAndUpdateCacheFile <T>(typeList, resolutionType, finder);
                                    }
                                    else
                                    {
                                        LogHelper.Debug <PluginManager>("Loaded plugin types " + typeof(T).FullName + " from persisted cache");
                                    }
                                }
                            }
                        }
                        else
                        {
                            //we don't have a cache for this so proceed to look them up by scanning
                            LoadViaScanningAndUpdateCacheFile <T>(typeList, resolutionType, finder);
                        }

                        //only add the cache if we are to cache the results
                        if (cacheResult)
                        {
                            //add the type list to the collection
                            _types.Add(typeList);
                        }
                    }
                    typesFound = typeList.GetTypes().ToList();
                }

                return(typesFound);
            }
        }