Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.PluginInfrastructure/3.3/Manager/PluginValidator.cs @ 8697

Last change on this file since 8697 was 4534, checked in by gkronber, 14 years ago

Implemented fix for #1220 (Plugin infrastructure throws a BadImageFormatException when x86 and x64 plugins are installed side by side.)

File size: 29.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
25using System.Linq;
26using System.Reflection;
27using System.Security;
28using System.Text;
29
30
31namespace HeuristicLab.PluginInfrastructure.Manager {
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 {
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
49    internal event EventHandler<PluginInfrastructureEventArgs> PluginLoaded;
50
51    private Dictionary<PluginDescription, IEnumerable<PluginDependency>> pluginDependencies;
52
53    private List<ApplicationDescription> applications;
54    internal IEnumerable<ApplicationDescription> Applications {
55      get {
56        if (string.IsNullOrEmpty(PluginDir)) throw new InvalidOperationException("PluginDir is not set.");
57        if (applications == null) DiscoverAndCheckPlugins();
58        return applications;
59      }
60    }
61
62    private IEnumerable<PluginDescription> plugins;
63    internal IEnumerable<PluginDescription> Plugins {
64      get {
65        if (string.IsNullOrEmpty(PluginDir)) throw new InvalidOperationException("PluginDir is not set.");
66        if (plugins == null) DiscoverAndCheckPlugins();
67        return plugins;
68      }
69    }
70
71    internal string PluginDir { get; set; }
72
73    internal PluginValidator() {
74      this.pluginDependencies = new Dictionary<PluginDescription, IEnumerable<PluginDependency>>();
75
76      // ReflectionOnlyAssemblyResolveEvent must be handled because we load assemblies from the plugin path
77      // (which is not listed in the default assembly lookup locations)
78      AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += ReflectionOnlyAssemblyResolveEventHandler;
79    }
80
81    private Dictionary<string, Assembly> reflectionOnlyAssemblies = new Dictionary<string, Assembly>();
82    private Assembly ReflectionOnlyAssemblyResolveEventHandler(object sender, ResolveEventArgs args) {
83      if (reflectionOnlyAssemblies.ContainsKey(args.Name))
84        return reflectionOnlyAssemblies[args.Name];
85      else
86        return Assembly.ReflectionOnlyLoad(args.Name);
87    }
88
89
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.
93    /// 2. The validator checks if all necessary files for each plugin are available.
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
100    ///    then one instance of each IPlugin type is activated and the OnLoad hook is called.
101    /// 9. All types implementing IApplication are discovered
102    /// </summary>
103    internal void DiscoverAndCheckPlugins() {
104      pluginDependencies.Clear();
105
106      IEnumerable<Assembly> reflectionOnlyAssemblies = ReflectionOnlyLoadDlls(PluginDir);
107      IEnumerable<PluginDescription> pluginDescriptions = GatherPluginDescriptions(reflectionOnlyAssemblies);
108      CheckPluginFiles(pluginDescriptions);
109
110      // check if all plugin assemblies can be loaded
111      CheckPluginAssemblies(pluginDescriptions);
112
113      // a full list of plugin descriptions is available now we can build the dependency tree
114      BuildDependencyTree(pluginDescriptions);
115
116      // check for dependency cycles
117      CheckPluginDependencyCycles(pluginDescriptions);
118
119      // recursively check if all necessary plugins are available and not disabled
120      // disable plugins with missing or disabled dependencies
121      CheckPluginDependencies(pluginDescriptions);
122
123      // test full loading (in contrast to reflection only loading) of plugins
124      // disables plugins that are not loaded correctly
125      CheckExecutionContextLoad(pluginDescriptions);
126
127      // mark all plugins as enabled that were not disabled in CheckPluginFiles, CheckPluginAssemblies,
128      // CheckCircularDependencies, CheckPluginDependencies and CheckExecutionContextLoad
129      foreach (var desc in pluginDescriptions)
130        if (desc.PluginState != PluginState.Disabled)
131          desc.Enable();
132
133      // load the enabled plugins
134      LoadPlugins(pluginDescriptions);
135
136      plugins = pluginDescriptions;
137      DiscoverApplications();
138    }
139
140    private void DiscoverApplications() {
141      applications = new List<ApplicationDescription>();
142
143      foreach (IApplication application in GetApplications()) {
144        Type appType = application.GetType();
145        ApplicationAttribute attr = (from x in appType.GetCustomAttributes(typeof(ApplicationAttribute), false)
146                                     select (ApplicationAttribute)x).Single();
147        var declaringPlugin = GetDeclaringPlugin(appType, plugins);
148        ApplicationDescription info = new ApplicationDescription();
149        info.Name = application.Name;
150        info.Version = declaringPlugin.Version;
151        info.Description = application.Description;
152        info.AutoRestart = attr.RestartOnErrors;
153        info.DeclaringAssemblyName = appType.Assembly.GetName().Name;
154        info.DeclaringTypeName = appType.Namespace + "." + application.GetType().Name;
155
156        applications.Add(info);
157      }
158    }
159
160    private static IEnumerable<IApplication> GetApplications() {
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
168    private IEnumerable<Assembly> ReflectionOnlyLoadDlls(string baseDir) {
169      List<Assembly> assemblies = new List<Assembly>();
170      // recursively load .dll files in subdirectories
171      foreach (string dirName in Directory.GetDirectories(baseDir)) {
172        assemblies.AddRange(ReflectionOnlyLoadDlls(dirName));
173      }
174      // try to load each .dll file in the plugin directory into the reflection only context
175      foreach (string filename in Directory.GetFiles(baseDir, "*.dll")) {
176        try {
177          Assembly asm = Assembly.ReflectionOnlyLoadFrom(filename);
178          RegisterLoadedAssembly(asm);
179          assemblies.Add(asm);
180        }
181        catch (BadImageFormatException) { } // just ignore the case that the .dll file is not a CLR assembly (e.g. a native dll)
182        catch (FileLoadException) { }
183        catch (SecurityException) { }
184        catch (ReflectionTypeLoadException) { } // referenced assemblies are missing
185      }
186      return assemblies;
187    }
188
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 {
196          var missingAssemblies = new List<string>();
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)) {
202              missingAssemblies.Add(asmName.FullName);
203            }
204          }
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          }
213        }
214        catch (BadImageFormatException ex) {
215          // disable the plugin
216          desc.Disable("Problem while loading plugin assemblies:" + Environment.NewLine + "BadImageFormatException: " + ex.Message);
217        }
218        catch (FileNotFoundException ex) {
219          // disable the plugin
220          desc.Disable("Problem while loading plugin assemblies:" + Environment.NewLine + "FileNotFoundException: " + ex.Message);
221        }
222        catch (FileLoadException ex) {
223          // disable the plugin
224          desc.Disable("Problem while loading plugin assemblies:" + Environment.NewLine + "FileLoadException: " + ex.Message);
225        }
226        catch (ArgumentException ex) {
227          // disable the plugin
228          desc.Disable("Problem while loading plugin assemblies:" + Environment.NewLine + "ArgumentException: " + ex.Message);
229        }
230        catch (SecurityException ex) {
231          // disable the plugin
232          desc.Disable("Problem while loading plugin assemblies:" + Environment.NewLine + "SecurityException: " + ex.Message);
233        }
234      }
235    }
236
237
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>();
243      foreach (Assembly assembly in assemblies) {
244        // GetExportedTypes throws FileNotFoundException when a referenced assembly
245        // of the current assembly is missing.
246        try {
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);
254        }
255        // ignore exceptions. Just don't yield a plugin description when an exception is thrown
256        catch (FileNotFoundException) {
257        }
258        catch (FileLoadException) {
259        }
260        catch (InvalidPluginException) {
261        }
262        catch (TypeLoadException) {
263        }
264        catch (MissingMemberException) {
265        }
266      }
267      return pluginDescriptions;
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>
275    /// <param name="pluginType"></param>
276    private PluginDescription GetPluginDescription(Type pluginType) {
277
278      string pluginName, pluginDescription, pluginVersion;
279      string contactName, contactAddress;
280      GetPluginMetaData(pluginType, out pluginName, out pluginDescription, out pluginVersion);
281      GetPluginContactMetaData(pluginType, out contactName, out contactAddress);
282      var pluginFiles = GetPluginFilesMetaData(pluginType);
283      var pluginDependencies = GetPluginDependencyMetaData(pluginType);
284
285      // minimal sanity check of the attribute values
286      if (!string.IsNullOrEmpty(pluginName) &&
287          pluginFiles.Count() > 0 &&                                 // at least one file
288          pluginFiles.Any(f => f.Type == PluginFileType.Assembly)) { // at least one assembly
289        // create a temporary PluginDescription that contains the attribute values
290        PluginDescription info = new PluginDescription();
291        info.Name = pluginName;
292        info.Description = pluginDescription;
293        info.Version = new Version(pluginVersion);
294        info.ContactName = contactName;
295        info.ContactEmail = contactAddress;
296        info.LicenseText = ReadLicenseFiles(pluginFiles);
297        info.AddFiles(pluginFiles);
298
299        this.pluginDependencies[info] = pluginDependencies;
300        return info;
301      } else {
302        throw new InvalidPluginException("Invalid metadata in plugin " + pluginType.ToString());
303      }
304    }
305
306    private static string ReadLicenseFiles(IEnumerable<PluginFile> pluginFiles) {
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
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
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
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";
383      pluginDescription = string.Empty;
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
394    private static bool IsAttributeDataForType(CustomAttributeData attributeData, Type attributeType) {
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) {
403        var missingDependencies = new List<PluginDependency>();
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;
409          if (matchingDescriptions.Count() > 0) {
410            desc.AddDependency(matchingDescriptions.Single());
411          } else {
412            missingDependencies.Add(dependency);
413          }
414        }
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        }
424      }
425    }
426
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>
437    private static bool IsCompatiblePluginVersion(Version available, Version requested) {
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
449    private void CheckPluginDependencyCycles(IEnumerable<PluginDescription> pluginDescriptions) {
450      foreach (var plugin in pluginDescriptions) {
451        // if the plugin is not disabled check if there are cycles
452        if (plugin.PluginState != PluginState.Disabled && HasCycleInDependencies(plugin, plugin.Dependencies)) {
453          plugin.Disable("Dependency graph has a cycle.");
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
469    private void CheckPluginDependencies(IEnumerable<PluginDescription> pluginDescriptions) {
470      foreach (PluginDescription pluginDescription in pluginDescriptions.Where(x => x.PluginState != PluginState.Disabled)) {
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());
479        }
480      }
481    }
482
483
484    private bool IsAnyDependencyDisabled(PluginDescription descr, List<PluginDescription> disabledPlugins) {
485      if (descr.PluginState == PluginState.Disabled) {
486        disabledPlugins.Add(descr);
487        return true;
488      }
489      foreach (PluginDescription dependency in descr.Dependencies) {
490        IsAnyDependencyDisabled(dependency, disabledPlugins);
491      }
492      return disabledPlugins.Count > 0;
493    }
494
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) {
498      // load all loadable plugins (all dependencies available) into the execution context
499      foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(pluginDescriptions
500                                                                                .Where(x => x.PluginState != PluginState.Disabled))) {
501        foreach (string assemblyLocation in desc.AssemblyLocations) {
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);
507          }
508          catch (BadImageFormatException) {
509            desc.Disable(Path.GetFileName(assemblyLocation) + " is not a valid assembly.");
510          }
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          }
520        }
521      }
522    }
523
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
552    // checks if all declared plugin files are actually available and disables plugins with missing files
553    private void CheckPluginFiles(IEnumerable<PluginDescription> pluginDescriptions) {
554      foreach (PluginDescription desc in pluginDescriptions) {
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());
563        }
564      }
565    }
566
567    private bool ArePluginFilesMissing(PluginDescription pluginDescription, out IEnumerable<string> missingFiles) {
568      List<string> missing = new List<string>();
569      foreach (string filename in pluginDescription.Files.Select(x => x.Name)) {
570        if (!FileLiesInDirectory(PluginDir, filename) ||
571          !File.Exists(filename)) {
572          missing.Add(filename);
573        }
574      }
575      missingFiles = missing;
576      return missing.Count > 0;
577    }
578
579    private static bool FileLiesInDirectory(string dir, string fileName) {
580      var basePath = Path.GetFullPath(dir);
581      return Path.GetFullPath(fileName).StartsWith(basePath);
582    }
583
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
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
597    private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
598      if (PluginLoaded != null)
599        PluginLoaded(this, e);
600    }
601
602    /// <summary>
603    /// Initializes the life time service with an infinite lease time.
604    /// </summary>
605    /// <returns><c>null</c>.</returns>
606    public override object InitializeLifetimeService() {
607      return null;
608    }
609  }
610}
Note: See TracBrowser for help on using the repository browser.