Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RefactorPluginInfrastructure-2522/HeuristicLab.PluginInfrastructure/3.3/PluginValidator.cs @ 13344

Last change on this file since 13344 was 13344, checked in by gkronber, 8 years ago

#2522: removed unused interface IControlManager, removed usage of restartApplication field (see r13343)

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