Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3580 was 3573, checked in by gkronber, 15 years ago

Implemented a list of review comments by swagner. #989 (Implement review comments in plugin infrastructure)

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