Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Manager/PluginValidator.cs @ 5532

Last change on this file since 5532 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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