From 8d7b2a6c8410b2eccf36aac0250eb99bc8b476eb Mon Sep 17 00:00:00 2001 From: Chris Koiak Date: Thu, 13 Feb 2014 09:25:33 +0000 Subject: [PATCH] Added support for explicitly loaded IGraphicsProcessors Former-commit-id: ecd76b82e62ccbe743091c30bc63f30a09f5e612 --- .../NET45/Config/ImageProcessingSection.cs | 26 +++++- .../NET45/Config/ImageProcessorConfig.cs | 88 ++++++++++++------- .../config/imageprocessor/processing.config | 33 ++++--- 3 files changed, 105 insertions(+), 42 deletions(-) diff --git a/src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs b/src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs index ec20a7d14..c2940e3e2 100644 --- a/src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs +++ b/src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs @@ -71,13 +71,25 @@ namespace ImageProcessor.Web.Config set { this["name"] = value; } } + /// + /// Gets or sets the type of the plugin file. + /// + /// The full Type definition of the plugin + [ConfigurationProperty("type", DefaultValue = "", IsRequired = true)] + public string Type + { + get { return (string)this["type"]; } + + set { this["type"] = value; } + } + /// /// Gets the . /// /// /// The . /// - [ConfigurationProperty("settings", IsRequired = true)] + [ConfigurationProperty("settings", IsRequired = false)] public SettingElementCollection Settings { get @@ -114,6 +126,18 @@ namespace ImageProcessor.Web.Config get { return "plugin"; } } + /// + /// Gets or sets the autoLoadPlugins of the plugin file. + /// + /// If True plugins are auto discovered and loaded from all assemblies otherwise they must be defined in the configuration file + [ConfigurationProperty("autoLoadPlugins", DefaultValue = true, IsRequired = false)] + public bool AutoLoadPlugins + { + get { return (bool)this["autoLoadPlugins"]; } + + set { this["autoLoadPlugins"] = value; } + } + /// /// Gets or sets the /// at the specified index within the collection. diff --git a/src/ImageProcessor.Web/NET45/Config/ImageProcessorConfig.cs b/src/ImageProcessor.Web/NET45/Config/ImageProcessorConfig.cs index 141538767..baa524e4e 100644 --- a/src/ImageProcessor.Web/NET45/Config/ImageProcessorConfig.cs +++ b/src/ImageProcessor.Web/NET45/Config/ImageProcessorConfig.cs @@ -9,6 +9,8 @@ // // -------------------------------------------------------------------------------------------------------------------- +using System.Collections; + namespace ImageProcessor.Web.Config { #region Using @@ -253,49 +255,73 @@ namespace ImageProcessor.Web.Config { if (this.GraphicsProcessors == null) { - try + if (GetImageProcessingSection().Plugins.AutoLoadPlugins) { - // Build a list of native IGraphicsProcessor instances. - Type type = typeof(IGraphicsProcessor); - IEnumerable types = - AppDomain.CurrentDomain.GetAssemblies() - .SelectMany(s => s.GetTypes()) - .Where(p => type.IsAssignableFrom(p) && p.IsClass && !p.IsAbstract) - .ToList(); - - // Create them and add. - this.GraphicsProcessors = - types.Select(x => (Activator.CreateInstance(x) as IGraphicsProcessor)).ToList(); - - // Add the available settings. - foreach (IGraphicsProcessor processor in this.GraphicsProcessors) + try { - processor.Settings = this.GetPluginSettings(processor.GetType().Name); + // Build a list of native IGraphicsProcessor instances. + Type type = typeof (IGraphicsProcessor); + IEnumerable types = + AppDomain.CurrentDomain.GetAssemblies() + .SelectMany(s => s.GetTypes()) + .Where(p => type.IsAssignableFrom(p) && p.IsClass && !p.IsAbstract) + .ToList(); + + // Create them and add. + this.GraphicsProcessors = + types.Select(x => (Activator.CreateInstance(x) as IGraphicsProcessor)).ToList(); + + // Add the available settings. + foreach (IGraphicsProcessor processor in this.GraphicsProcessors) + { + processor.Settings = this.GetPluginSettings(processor.GetType().Name); + } } - } - catch (ReflectionTypeLoadException ex) - { - StringBuilder sb = new StringBuilder(); - foreach (Exception exception in ex.LoaderExceptions) + catch (ReflectionTypeLoadException ex) { - sb.AppendLine(exception.Message); - if (exception is FileNotFoundException) + StringBuilder sb = new StringBuilder(); + foreach (Exception exception in ex.LoaderExceptions) { - FileNotFoundException fileNotFoundException = exception as FileNotFoundException; - if (!string.IsNullOrEmpty(fileNotFoundException.FusionLog)) + sb.AppendLine(exception.Message); + if (exception is FileNotFoundException) { - sb.AppendLine("Fusion Log:"); - sb.AppendLine(fileNotFoundException.FusionLog); + FileNotFoundException fileNotFoundException = exception as FileNotFoundException; + if (!string.IsNullOrEmpty(fileNotFoundException.FusionLog)) + { + sb.AppendLine("Fusion Log:"); + sb.AppendLine(fileNotFoundException.FusionLog); + } } + + sb.AppendLine(); } - sb.AppendLine(); + string errorMessage = sb.ToString(); + + // Display or log the error based on your application. + throw new Exception(errorMessage); } + } + else + { + var pluginConfigs = imageProcessingSection.Plugins; + this.GraphicsProcessors = new List(); + foreach (ImageProcessingSection.PluginElement pluginConfig in pluginConfigs) + { + var type = Type.GetType(pluginConfig.Type); - string errorMessage = sb.ToString(); + if (type == null) + { + throw new ArgumentException("Couldn't load IGraphicsProcessor: " + pluginConfig.Type); + } + this.GraphicsProcessors.Add((Activator.CreateInstance(type) as IGraphicsProcessor)); + } - // Display or log the error based on your application. - throw new Exception(errorMessage); + // Add the available settings. + foreach (IGraphicsProcessor processor in this.GraphicsProcessors) + { + processor.Settings = this.GetPluginSettings(processor.GetType().Name); + } } } } diff --git a/src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config b/src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config index fc33547f6..70821eeb5 100644 --- a/src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config +++ b/src/TestWebsites/NET45/Test_Website_NET45/config/imageprocessor/processing.config @@ -1,27 +1,40 @@  - - - - - - - - + + + + + + + + + - + - + + + + + + + + + + + + +