[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.IO;
|
---|
[2481] | 25 | using System.Linq;
|
---|
[4068] | 26 | using System.Reflection;
|
---|
[2527] | 27 | using System.Security;
|
---|
[4068] | 28 | using System.Text;
|
---|
[2] | 29 |
|
---|
[2481] | 30 |
|
---|
| 31 | namespace HeuristicLab.PluginInfrastructure.Manager {
|
---|
[2503] | 32 | /// <summary>
|
---|
| 33 | /// Discovers all installed plugins in the plugin directory. Checks correctness of plugin meta-data and if
|
---|
| 34 | /// all plugin files are available and checks plugin dependencies.
|
---|
| 35 | /// </summary>
|
---|
| 36 | internal sealed class PluginValidator : MarshalByRefObject {
|
---|
[2750] | 37 | // private class to store plugin dependency declarations while reflecting over plugins
|
---|
| 38 | private class PluginDependency {
|
---|
| 39 | public string Name { get; private set; }
|
---|
| 40 | public Version Version { get; private set; }
|
---|
| 41 |
|
---|
| 42 | public PluginDependency(string name, Version version) {
|
---|
| 43 | this.Name = name;
|
---|
| 44 | this.Version = version;
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 |
|
---|
[2489] | 49 | internal event EventHandler<PluginInfrastructureEventArgs> PluginLoaded;
|
---|
[2] | 50 |
|
---|
[2750] | 51 | private Dictionary<PluginDescription, IEnumerable<PluginDependency>> pluginDependencies;
|
---|
[2] | 52 |
|
---|
[2481] | 53 | private List<ApplicationDescription> applications;
|
---|
| 54 | internal IEnumerable<ApplicationDescription> Applications {
|
---|
[2] | 55 | get {
|
---|
[2503] | 56 | if (string.IsNullOrEmpty(PluginDir)) throw new InvalidOperationException("PluginDir is not set.");
|
---|
[2497] | 57 | if (applications == null) DiscoverAndCheckPlugins();
|
---|
[2481] | 58 | return applications;
|
---|
[2] | 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[2481] | 62 | private IEnumerable<PluginDescription> plugins;
|
---|
| 63 | internal IEnumerable<PluginDescription> Plugins {
|
---|
[2] | 64 | get {
|
---|
[2503] | 65 | if (string.IsNullOrEmpty(PluginDir)) throw new InvalidOperationException("PluginDir is not set.");
|
---|
[2497] | 66 | if (plugins == null) DiscoverAndCheckPlugins();
|
---|
[2481] | 67 | return plugins;
|
---|
[29] | 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[2504] | 71 | internal string PluginDir { get; set; }
|
---|
[2] | 72 |
|
---|
[2504] | 73 | internal PluginValidator() {
|
---|
[2750] | 74 | this.pluginDependencies = new Dictionary<PluginDescription, IEnumerable<PluginDependency>>();
|
---|
[2488] | 75 |
|
---|
[2497] | 76 | // ReflectionOnlyAssemblyResolveEvent must be handled because we load assemblies from the plugin path
|
---|
| 77 | // (which is not listed in the default assembly lookup locations)
|
---|
[2488] | 78 | AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += ReflectionOnlyAssemblyResolveEventHandler;
|
---|
[2] | 79 | }
|
---|
| 80 |
|
---|
[2690] | 81 | private Dictionary<string, Assembly> reflectionOnlyAssemblies = new Dictionary<string, Assembly>();
|
---|
[2488] | 82 | private Assembly ReflectionOnlyAssemblyResolveEventHandler(object sender, ResolveEventArgs args) {
|
---|
[2690] | 83 | if (reflectionOnlyAssemblies.ContainsKey(args.Name))
|
---|
| 84 | return reflectionOnlyAssemblies[args.Name];
|
---|
| 85 | else
|
---|
| 86 | return Assembly.ReflectionOnlyLoad(args.Name);
|
---|
[2488] | 87 | }
|
---|
[2] | 88 |
|
---|
[2488] | 89 |
|
---|
[2] | 90 | /// <summary>
|
---|
| 91 | /// Init first clears all internal datastructures (including plugin lists)
|
---|
| 92 | /// 1. All assemblies in the plugins directory are loaded into the reflection only context.
|
---|
[2503] | 93 | /// 2. The validator checks if all necessary files for each plugin are available.
|
---|
[2536] | 94 | /// 3. The validator checks if all declared plugin assemblies can be loaded.
|
---|
| 95 | /// 4. The validator builds the tree of plugin descriptions (dependencies)
|
---|
| 96 | /// 5. The validator checks if there are any cycles in the plugin dependency graph and disables plugin with circular dependencies
|
---|
| 97 | /// 6. The validator checks for each plugin if any dependency is disabled.
|
---|
| 98 | /// 7. All plugins that are not disabled are loaded into the execution context.
|
---|
| 99 | /// 8. Each loaded plugin (all assemblies) is searched for a types that implement IPlugin
|
---|
[2481] | 100 | /// then one instance of each IPlugin type is activated and the OnLoad hook is called.
|
---|
[2536] | 101 | /// 9. All types implementing IApplication are discovered
|
---|
[2] | 102 | /// </summary>
|
---|
[2503] | 103 | internal void DiscoverAndCheckPlugins() {
|
---|
[2] | 104 | pluginDependencies.Clear();
|
---|
| 105 |
|
---|
[2527] | 106 | IEnumerable<Assembly> reflectionOnlyAssemblies = ReflectionOnlyLoadDlls(PluginDir);
|
---|
[2481] | 107 | IEnumerable<PluginDescription> pluginDescriptions = GatherPluginDescriptions(reflectionOnlyAssemblies);
|
---|
| 108 | CheckPluginFiles(pluginDescriptions);
|
---|
[2] | 109 |
|
---|
[2536] | 110 | // check if all plugin assemblies can be loaded
|
---|
[2527] | 111 | CheckPluginAssemblies(pluginDescriptions);
|
---|
| 112 |
|
---|
[2481] | 113 | // a full list of plugin descriptions is available now we can build the dependency tree
|
---|
| 114 | BuildDependencyTree(pluginDescriptions);
|
---|
| 115 |
|
---|
[2536] | 116 | // check for dependency cycles
|
---|
| 117 | CheckPluginDependencyCycles(pluginDescriptions);
|
---|
| 118 |
|
---|
[5628] | 119 | // 1st time recursively check if all necessary plugins are available and not disabled
|
---|
| 120 | // disable plugins with missing or disabled dependencies
|
---|
| 121 | // to prevent that plugins with missing dependencies are loaded into the execution context
|
---|
| 122 | // in the next step
|
---|
[2481] | 123 | CheckPluginDependencies(pluginDescriptions);
|
---|
| 124 |
|
---|
[4534] | 125 | // test full loading (in contrast to reflection only loading) of plugins
|
---|
| 126 | // disables plugins that are not loaded correctly
|
---|
| 127 | CheckExecutionContextLoad(pluginDescriptions);
|
---|
| 128 |
|
---|
[5628] | 129 | // 2nd time recursively check if all necessary plugins have been loaded successfully and not disabled
|
---|
| 130 | // disable plugins with for which dependencies could not be loaded successfully
|
---|
| 131 | CheckPluginDependencies(pluginDescriptions);
|
---|
| 132 |
|
---|
[2536] | 133 | // mark all plugins as enabled that were not disabled in CheckPluginFiles, CheckPluginAssemblies,
|
---|
[4534] | 134 | // CheckCircularDependencies, CheckPluginDependencies and CheckExecutionContextLoad
|
---|
[2488] | 135 | foreach (var desc in pluginDescriptions)
|
---|
| 136 | if (desc.PluginState != PluginState.Disabled)
|
---|
| 137 | desc.Enable();
|
---|
| 138 |
|
---|
[4534] | 139 | // load the enabled plugins
|
---|
[2481] | 140 | LoadPlugins(pluginDescriptions);
|
---|
| 141 |
|
---|
| 142 | plugins = pluginDescriptions;
|
---|
[5631] | 143 | DiscoverApplications(pluginDescriptions);
|
---|
[2481] | 144 | }
|
---|
| 145 |
|
---|
[5631] | 146 | private void DiscoverApplications(IEnumerable<PluginDescription> pluginDescriptions) {
|
---|
[2481] | 147 | applications = new List<ApplicationDescription>();
|
---|
[5631] | 148 | foreach (IApplication application in GetApplications(pluginDescriptions)) {
|
---|
[2504] | 149 | Type appType = application.GetType();
|
---|
| 150 | ApplicationAttribute attr = (from x in appType.GetCustomAttributes(typeof(ApplicationAttribute), false)
|
---|
| 151 | select (ApplicationAttribute)x).Single();
|
---|
[2481] | 152 | ApplicationDescription info = new ApplicationDescription();
|
---|
[5631] | 153 | PluginDescription declaringPlugin = GetDeclaringPlugin(appType, pluginDescriptions);
|
---|
[2] | 154 | info.Name = application.Name;
|
---|
[3178] | 155 | info.Version = declaringPlugin.Version;
|
---|
[2] | 156 | info.Description = application.Description;
|
---|
[2504] | 157 | info.AutoRestart = attr.RestartOnErrors;
|
---|
| 158 | info.DeclaringAssemblyName = appType.Assembly.GetName().Name;
|
---|
| 159 | info.DeclaringTypeName = appType.Namespace + "." + application.GetType().Name;
|
---|
[2] | 160 |
|
---|
[29] | 161 | applications.Add(info);
|
---|
[2] | 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[5631] | 165 | private static IEnumerable<IApplication> GetApplications(IEnumerable<PluginDescription> pluginDescriptions) {
|
---|
[2488] | 166 | return from asm in AppDomain.CurrentDomain.GetAssemblies()
|
---|
| 167 | from t in asm.GetTypes()
|
---|
| 168 | where typeof(IApplication).IsAssignableFrom(t) &&
|
---|
| 169 | !t.IsAbstract && !t.IsInterface && !t.HasElementType
|
---|
[5631] | 170 | where GetDeclaringPlugin(t, pluginDescriptions).PluginState != PluginState.Disabled
|
---|
[2488] | 171 | select (IApplication)Activator.CreateInstance(t);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[2690] | 174 | private IEnumerable<Assembly> ReflectionOnlyLoadDlls(string baseDir) {
|
---|
[2] | 175 | List<Assembly> assemblies = new List<Assembly>();
|
---|
[2527] | 176 | // recursively load .dll files in subdirectories
|
---|
| 177 | foreach (string dirName in Directory.GetDirectories(baseDir)) {
|
---|
| 178 | assemblies.AddRange(ReflectionOnlyLoadDlls(dirName));
|
---|
| 179 | }
|
---|
[2481] | 180 | // try to load each .dll file in the plugin directory into the reflection only context
|
---|
[5652] | 181 | foreach (string filename in Directory.GetFiles(baseDir, "*.dll").Union(Directory.GetFiles(baseDir, "*.exe"))) {
|
---|
[535] | 182 | try {
|
---|
[2690] | 183 | Assembly asm = Assembly.ReflectionOnlyLoadFrom(filename);
|
---|
| 184 | RegisterLoadedAssembly(asm);
|
---|
| 185 | assemblies.Add(asm);
|
---|
[1229] | 186 | }
|
---|
[2503] | 187 | catch (BadImageFormatException) { } // just ignore the case that the .dll file is not a CLR assembly (e.g. a native dll)
|
---|
[2527] | 188 | catch (FileLoadException) { }
|
---|
| 189 | catch (SecurityException) { }
|
---|
[3081] | 190 | catch (ReflectionTypeLoadException) { } // referenced assemblies are missing
|
---|
[2] | 191 | }
|
---|
| 192 | return assemblies;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[2527] | 195 | /// <summary>
|
---|
| 196 | /// Checks if all plugin assemblies can be loaded. If an assembly can't be loaded the plugin is disabled.
|
---|
| 197 | /// </summary>
|
---|
| 198 | /// <param name="pluginDescriptions"></param>
|
---|
| 199 | private void CheckPluginAssemblies(IEnumerable<PluginDescription> pluginDescriptions) {
|
---|
| 200 | foreach (var desc in pluginDescriptions.Where(x => x.PluginState != PluginState.Disabled)) {
|
---|
| 201 | try {
|
---|
[2779] | 202 | var missingAssemblies = new List<string>();
|
---|
[2690] | 203 | foreach (var asmLocation in desc.AssemblyLocations) {
|
---|
| 204 | // the assembly must have been loaded in ReflectionOnlyDlls
|
---|
| 205 | // so we simply determine the name of the assembly and try to find it in the cache of loaded assemblies
|
---|
| 206 | var asmName = AssemblyName.GetAssemblyName(asmLocation);
|
---|
| 207 | if (!reflectionOnlyAssemblies.ContainsKey(asmName.FullName)) {
|
---|
[2779] | 208 | missingAssemblies.Add(asmName.FullName);
|
---|
[2690] | 209 | }
|
---|
[2527] | 210 | }
|
---|
[2779] | 211 | if (missingAssemblies.Count > 0) {
|
---|
| 212 | StringBuilder errorStrBuiler = new StringBuilder();
|
---|
| 213 | errorStrBuiler.AppendLine("Missing assemblies:");
|
---|
| 214 | foreach (string missingAsm in missingAssemblies) {
|
---|
| 215 | errorStrBuiler.AppendLine(missingAsm);
|
---|
| 216 | }
|
---|
| 217 | desc.Disable(errorStrBuiler.ToString());
|
---|
| 218 | }
|
---|
[2527] | 219 | }
|
---|
[2779] | 220 | catch (BadImageFormatException ex) {
|
---|
[2527] | 221 | // disable the plugin
|
---|
[2779] | 222 | desc.Disable("Problem while loading plugin assemblies:" + Environment.NewLine + "BadImageFormatException: " + ex.Message);
|
---|
[2527] | 223 | }
|
---|
[2779] | 224 | catch (FileNotFoundException ex) {
|
---|
[2527] | 225 | // disable the plugin
|
---|
[2779] | 226 | desc.Disable("Problem while loading plugin assemblies:" + Environment.NewLine + "FileNotFoundException: " + ex.Message);
|
---|
[2527] | 227 | }
|
---|
[2779] | 228 | catch (FileLoadException ex) {
|
---|
[2527] | 229 | // disable the plugin
|
---|
[2779] | 230 | desc.Disable("Problem while loading plugin assemblies:" + Environment.NewLine + "FileLoadException: " + ex.Message);
|
---|
[2527] | 231 | }
|
---|
[2779] | 232 | catch (ArgumentException ex) {
|
---|
[2527] | 233 | // disable the plugin
|
---|
[2779] | 234 | desc.Disable("Problem while loading plugin assemblies:" + Environment.NewLine + "ArgumentException: " + ex.Message);
|
---|
[2527] | 235 | }
|
---|
[2779] | 236 | catch (SecurityException ex) {
|
---|
[2527] | 237 | // disable the plugin
|
---|
[2779] | 238 | desc.Disable("Problem while loading plugin assemblies:" + Environment.NewLine + "SecurityException: " + ex.Message);
|
---|
[2527] | 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 |
|
---|
[2481] | 244 | // find all types implementing IPlugin in the reflectionOnlyAssemblies and create a list of plugin descriptions
|
---|
| 245 | // the dependencies in the plugin descriptions are not yet set correctly because we need to create
|
---|
| 246 | // the full list of all plugin descriptions first
|
---|
| 247 | private IEnumerable<PluginDescription> GatherPluginDescriptions(IEnumerable<Assembly> assemblies) {
|
---|
| 248 | List<PluginDescription> pluginDescriptions = new List<PluginDescription>();
|
---|
[1229] | 249 | foreach (Assembly assembly in assemblies) {
|
---|
[2] | 250 | // GetExportedTypes throws FileNotFoundException when a referenced assembly
|
---|
| 251 | // of the current assembly is missing.
|
---|
| 252 | try {
|
---|
[2527] | 253 | // if there is a type that implements IPlugin
|
---|
| 254 | // use AssemblyQualifiedName to compare the types because we can't directly
|
---|
| 255 | // compare ReflectionOnly types and execution types
|
---|
| 256 | var assemblyPluginDescriptions = from t in assembly.GetExportedTypes()
|
---|
| 257 | where !t.IsAbstract && t.GetInterfaces().Any(x => x.AssemblyQualifiedName == typeof(IPlugin).AssemblyQualifiedName)
|
---|
| 258 | select GetPluginDescription(t);
|
---|
| 259 | pluginDescriptions.AddRange(assemblyPluginDescriptions);
|
---|
[1229] | 260 | }
|
---|
[2497] | 261 | // ignore exceptions. Just don't yield a plugin description when an exception is thrown
|
---|
[2489] | 262 | catch (FileNotFoundException) {
|
---|
[1229] | 263 | }
|
---|
[2489] | 264 | catch (FileLoadException) {
|
---|
[2] | 265 | }
|
---|
[2489] | 266 | catch (InvalidPluginException) {
|
---|
[1395] | 267 | }
|
---|
[3508] | 268 | catch (TypeLoadException) {
|
---|
| 269 | }
|
---|
[3547] | 270 | catch (MissingMemberException) {
|
---|
| 271 | }
|
---|
[2] | 272 | }
|
---|
[2481] | 273 | return pluginDescriptions;
|
---|
[2] | 274 | }
|
---|
| 275 |
|
---|
| 276 | /// <summary>
|
---|
| 277 | /// Extracts plugin information for this type.
|
---|
| 278 | /// Reads plugin name, list and type of files and dependencies of the plugin. This information is necessary for
|
---|
| 279 | /// plugin dependency checking before plugin activation.
|
---|
| 280 | /// </summary>
|
---|
[3046] | 281 | /// <param name="pluginType"></param>
|
---|
[2481] | 282 | private PluginDescription GetPluginDescription(Type pluginType) {
|
---|
[2] | 283 |
|
---|
[2763] | 284 | string pluginName, pluginDescription, pluginVersion;
|
---|
[2778] | 285 | string contactName, contactAddress;
|
---|
[2763] | 286 | GetPluginMetaData(pluginType, out pluginName, out pluginDescription, out pluginVersion);
|
---|
[2778] | 287 | GetPluginContactMetaData(pluginType, out contactName, out contactAddress);
|
---|
[2763] | 288 | var pluginFiles = GetPluginFilesMetaData(pluginType);
|
---|
| 289 | var pluginDependencies = GetPluginDependencyMetaData(pluginType);
|
---|
| 290 |
|
---|
[29] | 291 | // minimal sanity check of the attribute values
|
---|
[2517] | 292 | if (!string.IsNullOrEmpty(pluginName) &&
|
---|
[2778] | 293 | pluginFiles.Count() > 0 && // at least one file
|
---|
| 294 | pluginFiles.Any(f => f.Type == PluginFileType.Assembly)) { // at least one assembly
|
---|
[2481] | 295 | // create a temporary PluginDescription that contains the attribute values
|
---|
| 296 | PluginDescription info = new PluginDescription();
|
---|
[29] | 297 | info.Name = pluginName;
|
---|
[2513] | 298 | info.Description = pluginDescription;
|
---|
[2750] | 299 | info.Version = new Version(pluginVersion);
|
---|
[2778] | 300 | info.ContactName = contactName;
|
---|
| 301 | info.ContactEmail = contactAddress;
|
---|
[2815] | 302 | info.LicenseText = ReadLicenseFiles(pluginFiles);
|
---|
[2481] | 303 | info.AddFiles(pluginFiles);
|
---|
| 304 |
|
---|
[29] | 305 | this.pluginDependencies[info] = pluginDependencies;
|
---|
[2481] | 306 | return info;
|
---|
[2] | 307 | } else {
|
---|
[2481] | 308 | throw new InvalidPluginException("Invalid metadata in plugin " + pluginType.ToString());
|
---|
[2] | 309 | }
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[4482] | 312 | private static string ReadLicenseFiles(IEnumerable<PluginFile> pluginFiles) {
|
---|
[2815] | 313 | // combine the contents of all plugin files
|
---|
| 314 | var licenseFiles = from file in pluginFiles
|
---|
| 315 | where file.Type == PluginFileType.License
|
---|
| 316 | select file;
|
---|
| 317 | if (licenseFiles.Count() == 0) return string.Empty;
|
---|
| 318 | StringBuilder licenseTextBuilder = new StringBuilder();
|
---|
| 319 | licenseTextBuilder.AppendLine(File.ReadAllText(licenseFiles.First().Name));
|
---|
| 320 | foreach (var licenseFile in licenseFiles.Skip(1)) {
|
---|
| 321 | licenseTextBuilder.AppendLine().AppendLine(); // leave some empty space between multiple license files
|
---|
| 322 | licenseTextBuilder.AppendLine(File.ReadAllText(licenseFile.Name));
|
---|
| 323 | }
|
---|
| 324 | return licenseTextBuilder.ToString();
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[2763] | 327 | private static IEnumerable<PluginDependency> GetPluginDependencyMetaData(Type pluginType) {
|
---|
| 328 | // get all attributes of type PluginDependency
|
---|
| 329 | var dependencyAttributes = from attr in CustomAttributeData.GetCustomAttributes(pluginType)
|
---|
| 330 | where IsAttributeDataForType(attr, typeof(PluginDependencyAttribute))
|
---|
| 331 | select attr;
|
---|
| 332 |
|
---|
| 333 | foreach (var dependencyAttr in dependencyAttributes) {
|
---|
| 334 | string name = (string)dependencyAttr.ConstructorArguments[0].Value;
|
---|
| 335 | Version version = new Version("0.0.0.0"); // default version
|
---|
| 336 | // check if version is given for now
|
---|
| 337 | // later when the constructor of PluginDependencyAttribute with only one argument has been removed
|
---|
| 338 | // this conditional can be removed as well
|
---|
| 339 | if (dependencyAttr.ConstructorArguments.Count > 1) {
|
---|
| 340 | try {
|
---|
| 341 | version = new Version((string)dependencyAttr.ConstructorArguments[1].Value); // might throw FormatException
|
---|
| 342 | }
|
---|
| 343 | catch (FormatException ex) {
|
---|
| 344 | throw new InvalidPluginException("Invalid version format of dependency " + name + " in plugin " + pluginType.ToString(), ex);
|
---|
| 345 | }
|
---|
| 346 | }
|
---|
| 347 | yield return new PluginDependency(name, version);
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
| 350 |
|
---|
[2778] | 351 | private static void GetPluginContactMetaData(Type pluginType, out string contactName, out string contactAddress) {
|
---|
| 352 | // get attribute of type ContactInformation if there is any
|
---|
| 353 | var contactInfoAttribute = (from attr in CustomAttributeData.GetCustomAttributes(pluginType)
|
---|
| 354 | where IsAttributeDataForType(attr, typeof(ContactInformationAttribute))
|
---|
| 355 | select attr).SingleOrDefault();
|
---|
| 356 |
|
---|
| 357 | if (contactInfoAttribute != null) {
|
---|
| 358 | contactName = (string)contactInfoAttribute.ConstructorArguments[0].Value;
|
---|
| 359 | contactAddress = (string)contactInfoAttribute.ConstructorArguments[1].Value;
|
---|
| 360 | } else {
|
---|
| 361 | contactName = string.Empty;
|
---|
| 362 | contactAddress = string.Empty;
|
---|
| 363 | }
|
---|
| 364 | }
|
---|
| 365 |
|
---|
[2763] | 366 | // not static because we need the PluginDir property
|
---|
| 367 | private IEnumerable<PluginFile> GetPluginFilesMetaData(Type pluginType) {
|
---|
| 368 | // get all attributes of type PluginFileAttribute
|
---|
| 369 | var pluginFileAttributes = from attr in CustomAttributeData.GetCustomAttributes(pluginType)
|
---|
| 370 | where IsAttributeDataForType(attr, typeof(PluginFileAttribute))
|
---|
| 371 | select attr;
|
---|
| 372 | foreach (var pluginFileAttribute in pluginFileAttributes) {
|
---|
| 373 | string pluginFileName = (string)pluginFileAttribute.ConstructorArguments[0].Value;
|
---|
| 374 | PluginFileType fileType = (PluginFileType)pluginFileAttribute.ConstructorArguments[1].Value;
|
---|
| 375 | yield return new PluginFile(Path.GetFullPath(Path.Combine(PluginDir, pluginFileName)), fileType);
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | private static void GetPluginMetaData(Type pluginType, out string pluginName, out string pluginDescription, out string pluginVersion) {
|
---|
| 380 | // there must be a single attribute of type PluginAttribute
|
---|
| 381 | var pluginMetaDataAttr = (from attr in CustomAttributeData.GetCustomAttributes(pluginType)
|
---|
| 382 | where IsAttributeDataForType(attr, typeof(PluginAttribute))
|
---|
| 383 | select attr).Single();
|
---|
| 384 |
|
---|
| 385 | pluginName = (string)pluginMetaDataAttr.ConstructorArguments[0].Value;
|
---|
| 386 |
|
---|
| 387 | // default description and version
|
---|
| 388 | pluginVersion = "0.0.0.0";
|
---|
[3573] | 389 | pluginDescription = string.Empty;
|
---|
[2763] | 390 | if (pluginMetaDataAttr.ConstructorArguments.Count() == 2) {
|
---|
| 391 | // if two arguments are given the second argument is the version
|
---|
| 392 | pluginVersion = (string)pluginMetaDataAttr.ConstructorArguments[1].Value;
|
---|
| 393 | } else if (pluginMetaDataAttr.ConstructorArguments.Count() == 3) {
|
---|
| 394 | // if three arguments are given the second argument is the description and the third is the version
|
---|
| 395 | pluginDescription = (string)pluginMetaDataAttr.ConstructorArguments[1].Value;
|
---|
| 396 | pluginVersion = (string)pluginMetaDataAttr.ConstructorArguments[2].Value;
|
---|
| 397 | }
|
---|
| 398 | }
|
---|
| 399 |
|
---|
[2504] | 400 | private static bool IsAttributeDataForType(CustomAttributeData attributeData, Type attributeType) {
|
---|
[2481] | 401 | return attributeData.Constructor.DeclaringType.AssemblyQualifiedName == attributeType.AssemblyQualifiedName;
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | // builds a dependency tree of all plugin descriptions
|
---|
| 405 | // searches matching plugin descriptions based on the list of dependency names for each plugin
|
---|
| 406 | // and sets the dependencies in the plugin descriptions
|
---|
| 407 | private void BuildDependencyTree(IEnumerable<PluginDescription> pluginDescriptions) {
|
---|
| 408 | foreach (var desc in pluginDescriptions) {
|
---|
[2779] | 409 | var missingDependencies = new List<PluginDependency>();
|
---|
[2750] | 410 | foreach (var dependency in pluginDependencies[desc]) {
|
---|
| 411 | var matchingDescriptions = from availablePlugin in pluginDescriptions
|
---|
| 412 | where availablePlugin.Name == dependency.Name
|
---|
| 413 | where IsCompatiblePluginVersion(availablePlugin.Version, dependency.Version)
|
---|
| 414 | select availablePlugin;
|
---|
[2481] | 415 | if (matchingDescriptions.Count() > 0) {
|
---|
[2517] | 416 | desc.AddDependency(matchingDescriptions.Single());
|
---|
[2481] | 417 | } else {
|
---|
[2779] | 418 | missingDependencies.Add(dependency);
|
---|
[2481] | 419 | }
|
---|
[29] | 420 | }
|
---|
[2779] | 421 | // no plugin description that matches the dependencies are available => plugin is disabled
|
---|
| 422 | if (missingDependencies.Count > 0) {
|
---|
| 423 | StringBuilder errorStrBuilder = new StringBuilder();
|
---|
| 424 | errorStrBuilder.AppendLine("Missing dependencies:");
|
---|
| 425 | foreach (var missingDep in missingDependencies) {
|
---|
| 426 | errorStrBuilder.AppendLine(missingDep.Name + " " + missingDep.Version);
|
---|
| 427 | }
|
---|
| 428 | desc.Disable(errorStrBuilder.ToString());
|
---|
| 429 | }
|
---|
[2481] | 430 | }
|
---|
| 431 | }
|
---|
[37] | 432 |
|
---|
[2750] | 433 | /// <summary>
|
---|
| 434 | /// Checks if version <paramref name="available"/> is compatible to version <paramref name="requested"/>.
|
---|
| 435 | /// Note: the compatibility relation is not bijective.
|
---|
| 436 | /// Compatibility rules:
|
---|
| 437 | /// * major and minor number must be the same
|
---|
| 438 | /// * build and revision number of <paramref name="available"/> must be larger or equal to <paramref name="requested"/>.
|
---|
| 439 | /// </summary>
|
---|
| 440 | /// <param name="available">The available version which should be compared to <paramref name="requested"/>.</param>
|
---|
| 441 | /// <param name="requested">The requested version that must be matched.</param>
|
---|
| 442 | /// <returns></returns>
|
---|
[4482] | 443 | private static bool IsCompatiblePluginVersion(Version available, Version requested) {
|
---|
[2750] | 444 | // this condition must be removed after all plugins have been updated to declare plugin and dependency versions
|
---|
| 445 | if (
|
---|
| 446 | (requested.Major == 0 && requested.Minor == 0) ||
|
---|
| 447 | (available.Major == 0 && available.Minor == 0)) return true;
|
---|
| 448 | return
|
---|
| 449 | available.Major == requested.Major &&
|
---|
| 450 | available.Minor == requested.Minor &&
|
---|
| 451 | available.Build >= requested.Build &&
|
---|
| 452 | available.Revision >= requested.Revision;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
[2536] | 455 | private void CheckPluginDependencyCycles(IEnumerable<PluginDescription> pluginDescriptions) {
|
---|
| 456 | foreach (var plugin in pluginDescriptions) {
|
---|
[2779] | 457 | // if the plugin is not disabled check if there are cycles
|
---|
[2536] | 458 | if (plugin.PluginState != PluginState.Disabled && HasCycleInDependencies(plugin, plugin.Dependencies)) {
|
---|
[2779] | 459 | plugin.Disable("Dependency graph has a cycle.");
|
---|
[2536] | 460 | }
|
---|
| 461 | }
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | private bool HasCycleInDependencies(PluginDescription plugin, IEnumerable<PluginDescription> pluginDependencies) {
|
---|
| 465 | foreach (var dep in pluginDependencies) {
|
---|
| 466 | // if one of the dependencies is the original plugin we found a cycle and can return
|
---|
| 467 | // if the dependency is already disabled we can ignore the cycle detection because we will disable the plugin anyway
|
---|
| 468 | // if following one of the dependencies recursively leads to a cycle then we also return
|
---|
| 469 | if (dep == plugin || dep.PluginState == PluginState.Disabled || HasCycleInDependencies(plugin, dep.Dependencies)) return true;
|
---|
| 470 | }
|
---|
| 471 | // no cycle found and none of the direct and indirect dependencies is disabled
|
---|
| 472 | return false;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[2481] | 475 | private void CheckPluginDependencies(IEnumerable<PluginDescription> pluginDescriptions) {
|
---|
| 476 | foreach (PluginDescription pluginDescription in pluginDescriptions.Where(x => x.PluginState != PluginState.Disabled)) {
|
---|
[2779] | 477 | List<PluginDescription> disabledPlugins = new List<PluginDescription>();
|
---|
| 478 | if (IsAnyDependencyDisabled(pluginDescription, disabledPlugins)) {
|
---|
| 479 | StringBuilder errorStrBuilder = new StringBuilder();
|
---|
| 480 | errorStrBuilder.AppendLine("Dependencies are disabled:");
|
---|
| 481 | foreach (var disabledPlugin in disabledPlugins) {
|
---|
| 482 | errorStrBuilder.AppendLine(disabledPlugin.Name + " " + disabledPlugin.Version);
|
---|
| 483 | }
|
---|
| 484 | pluginDescription.Disable(errorStrBuilder.ToString());
|
---|
[29] | 485 | }
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 |
|
---|
[2779] | 490 | private bool IsAnyDependencyDisabled(PluginDescription descr, List<PluginDescription> disabledPlugins) {
|
---|
| 491 | if (descr.PluginState == PluginState.Disabled) {
|
---|
| 492 | disabledPlugins.Add(descr);
|
---|
| 493 | return true;
|
---|
| 494 | }
|
---|
[2481] | 495 | foreach (PluginDescription dependency in descr.Dependencies) {
|
---|
[2779] | 496 | IsAnyDependencyDisabled(dependency, disabledPlugins);
|
---|
[2] | 497 | }
|
---|
[2779] | 498 | return disabledPlugins.Count > 0;
|
---|
[2] | 499 | }
|
---|
| 500 |
|
---|
[4534] | 501 | // tries to load all plugin assemblies into the execution context
|
---|
| 502 | // if an assembly of a plugin cannot be loaded the plugin is disabled
|
---|
| 503 | private void CheckExecutionContextLoad(IEnumerable<PluginDescription> pluginDescriptions) {
|
---|
[2] | 504 | // load all loadable plugins (all dependencies available) into the execution context
|
---|
[2517] | 505 | foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(pluginDescriptions
|
---|
[2488] | 506 | .Where(x => x.PluginState != PluginState.Disabled))) {
|
---|
[2690] | 507 | foreach (string assemblyLocation in desc.AssemblyLocations) {
|
---|
[5628] | 508 | if (desc.PluginState != PluginState.Disabled) {
|
---|
| 509 | try {
|
---|
[5629] | 510 | string assemblyName = (from assembly in AppDomain.CurrentDomain.ReflectionOnlyGetAssemblies()
|
---|
| 511 | where string.Equals(Path.GetFullPath(assembly.Location), Path.GetFullPath(assemblyLocation), StringComparison.CurrentCultureIgnoreCase)
|
---|
| 512 | select assembly.FullName).Single();
|
---|
[5628] | 513 | // now load the assemblies into the execution context
|
---|
| 514 | // this can still lead to an exception
|
---|
[5629] | 515 | // even when the assemby was successfully loaded into the reflection only context before
|
---|
| 516 | var asm = Assembly.Load(assemblyName);
|
---|
[5628] | 517 | }
|
---|
| 518 | catch (BadImageFormatException) {
|
---|
| 519 | desc.Disable(Path.GetFileName(assemblyLocation) + " is not a valid assembly.");
|
---|
| 520 | }
|
---|
| 521 | catch (FileLoadException) {
|
---|
| 522 | desc.Disable("Can't load file " + Path.GetFileName(assemblyLocation));
|
---|
| 523 | }
|
---|
| 524 | catch (FileNotFoundException) {
|
---|
| 525 | desc.Disable("File " + Path.GetFileName(assemblyLocation) + " is missing.");
|
---|
| 526 | }
|
---|
| 527 | catch (SecurityException) {
|
---|
| 528 | desc.Disable("File " + Path.GetFileName(assemblyLocation) + " can't be loaded because of security constraints.");
|
---|
| 529 | }
|
---|
[5629] | 530 | catch (NotSupportedException ex) {
|
---|
| 531 | // disable the plugin
|
---|
| 532 | desc.Disable("Problem while loading plugin assemblies:" + Environment.NewLine + "NotSupportedException: " + ex.Message);
|
---|
| 533 | }
|
---|
[2] | 534 | }
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
| 537 | }
|
---|
[91] | 538 |
|
---|
[4534] | 539 | // assumes that all plugin assemblies have been loaded into the execution context via CheckExecutionContextLoad
|
---|
| 540 | // for each enabled plugin:
|
---|
| 541 | // calls OnLoad method of the plugin
|
---|
| 542 | // and raises the PluginLoaded event
|
---|
| 543 | private void LoadPlugins(IEnumerable<PluginDescription> pluginDescriptions) {
|
---|
| 544 | List<Assembly> assemblies = new List<Assembly>(AppDomain.CurrentDomain.GetAssemblies());
|
---|
| 545 | foreach (var desc in pluginDescriptions) {
|
---|
| 546 | if (desc.PluginState == PluginState.Enabled) {
|
---|
| 547 | // cannot use ApplicationManager to retrieve types because it is not yet instantiated
|
---|
| 548 | foreach (string assemblyLocation in desc.AssemblyLocations) {
|
---|
| 549 | var asm = (from assembly in assemblies
|
---|
[5628] | 550 | where string.Equals(Path.GetFullPath(assembly.Location), Path.GetFullPath(assemblyLocation), StringComparison.CurrentCultureIgnoreCase)
|
---|
| 551 | select assembly)
|
---|
[4534] | 552 | .Single();
|
---|
| 553 |
|
---|
| 554 | foreach (Type pluginType in asm.GetTypes()) {
|
---|
| 555 | if (typeof(IPlugin).IsAssignableFrom(pluginType) && !pluginType.IsAbstract && !pluginType.IsInterface && !pluginType.HasElementType) {
|
---|
| 556 | IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
|
---|
| 557 | plugin.OnLoad();
|
---|
| 558 | OnPluginLoaded(new PluginInfrastructureEventArgs(desc));
|
---|
| 559 | }
|
---|
| 560 | }
|
---|
| 561 | } // end foreach assembly in plugin
|
---|
| 562 | desc.Load();
|
---|
| 563 | }
|
---|
| 564 | } // end foreach plugin description
|
---|
| 565 | }
|
---|
| 566 |
|
---|
[2481] | 567 | // checks if all declared plugin files are actually available and disables plugins with missing files
|
---|
[2527] | 568 | private void CheckPluginFiles(IEnumerable<PluginDescription> pluginDescriptions) {
|
---|
[2481] | 569 | foreach (PluginDescription desc in pluginDescriptions) {
|
---|
[2779] | 570 | IEnumerable<string> missingFiles;
|
---|
| 571 | if (ArePluginFilesMissing(desc, out missingFiles)) {
|
---|
| 572 | StringBuilder errorStrBuilder = new StringBuilder();
|
---|
| 573 | errorStrBuilder.AppendLine("Missing files:");
|
---|
| 574 | foreach (string fileName in missingFiles) {
|
---|
| 575 | errorStrBuilder.AppendLine(fileName);
|
---|
| 576 | }
|
---|
| 577 | desc.Disable(errorStrBuilder.ToString());
|
---|
[29] | 578 | }
|
---|
[2] | 579 | }
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[2779] | 582 | private bool ArePluginFilesMissing(PluginDescription pluginDescription, out IEnumerable<string> missingFiles) {
|
---|
| 583 | List<string> missing = new List<string>();
|
---|
[2688] | 584 | foreach (string filename in pluginDescription.Files.Select(x => x.Name)) {
|
---|
[2527] | 585 | if (!FileLiesInDirectory(PluginDir, filename) ||
|
---|
| 586 | !File.Exists(filename)) {
|
---|
[2779] | 587 | missing.Add(filename);
|
---|
[2] | 588 | }
|
---|
| 589 | }
|
---|
[2779] | 590 | missingFiles = missing;
|
---|
| 591 | return missing.Count > 0;
|
---|
[2] | 592 | }
|
---|
| 593 |
|
---|
[2527] | 594 | private static bool FileLiesInDirectory(string dir, string fileName) {
|
---|
| 595 | var basePath = Path.GetFullPath(dir);
|
---|
| 596 | return Path.GetFullPath(fileName).StartsWith(basePath);
|
---|
| 597 | }
|
---|
| 598 |
|
---|
[5631] | 599 | private static PluginDescription GetDeclaringPlugin(Type appType, IEnumerable<PluginDescription> plugins) {
|
---|
[3178] | 600 | return (from p in plugins
|
---|
| 601 | from asmLocation in p.AssemblyLocations
|
---|
| 602 | where Path.GetFullPath(asmLocation).Equals(Path.GetFullPath(appType.Assembly.Location), StringComparison.CurrentCultureIgnoreCase)
|
---|
| 603 | select p).Single();
|
---|
| 604 | }
|
---|
| 605 |
|
---|
[2690] | 606 | // register assembly in the assembly cache for the ReflectionOnlyAssemblyResolveEvent
|
---|
| 607 | private void RegisterLoadedAssembly(Assembly asm) {
|
---|
[6021] | 608 | if (reflectionOnlyAssemblies.ContainsKey(asm.FullName) || reflectionOnlyAssemblies.ContainsKey(asm.GetName().Name)) {
|
---|
| 609 | throw new ArgumentException("An assembly with the name " + asm.GetName().Name + " has been registered already.", "asm");
|
---|
| 610 | }
|
---|
[2690] | 611 | reflectionOnlyAssemblies.Add(asm.FullName, asm);
|
---|
| 612 | reflectionOnlyAssemblies.Add(asm.GetName().Name, asm); // add short name
|
---|
| 613 | }
|
---|
| 614 |
|
---|
[2763] | 615 | private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
|
---|
[2489] | 616 | if (PluginLoaded != null)
|
---|
[2503] | 617 | PluginLoaded(this, e);
|
---|
[2489] | 618 | }
|
---|
| 619 |
|
---|
[1189] | 620 | /// <summary>
|
---|
[2497] | 621 | /// Initializes the life time service with an infinite lease time.
|
---|
[1189] | 622 | /// </summary>
|
---|
| 623 | /// <returns><c>null</c>.</returns>
|
---|
[2] | 624 | public override object InitializeLifetimeService() {
|
---|
| 625 | return null;
|
---|
| 626 | }
|
---|
| 627 | }
|
---|
| 628 | }
|
---|