Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented changes as suggested by abeham after code review and simplified method PluginValidator.GetPluginDescription(). #863.

File size: 22.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using System.Reflection;
26using System.IO;
27using System.Diagnostics;
28using System.Linq;
29using System.Security;
30
31
32namespace HeuristicLab.PluginInfrastructure.Manager {
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 {
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
50    internal event EventHandler<PluginInfrastructureEventArgs> PluginLoaded;
51
52    private Dictionary<PluginDescription, IEnumerable<PluginDependency>> pluginDependencies;
53
54    private List<ApplicationDescription> applications;
55    internal IEnumerable<ApplicationDescription> Applications {
56      get {
57        if (string.IsNullOrEmpty(PluginDir)) throw new InvalidOperationException("PluginDir is not set.");
58        if (applications == null) DiscoverAndCheckPlugins();
59        return applications;
60      }
61    }
62
63    private IEnumerable<PluginDescription> plugins;
64    internal IEnumerable<PluginDescription> Plugins {
65      get {
66        if (string.IsNullOrEmpty(PluginDir)) throw new InvalidOperationException("PluginDir is not set.");
67        if (plugins == null) DiscoverAndCheckPlugins();
68        return plugins;
69      }
70    }
71
72    internal string PluginDir { get; set; }
73
74    internal PluginValidator() {
75      this.pluginDependencies = new Dictionary<PluginDescription, IEnumerable<PluginDependency>>();
76
77      // ReflectionOnlyAssemblyResolveEvent must be handled because we load assemblies from the plugin path
78      // (which is not listed in the default assembly lookup locations)
79      AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += ReflectionOnlyAssemblyResolveEventHandler;
80    }
81
82    private Dictionary<string, Assembly> reflectionOnlyAssemblies = new Dictionary<string, Assembly>();
83    private Assembly ReflectionOnlyAssemblyResolveEventHandler(object sender, ResolveEventArgs args) {
84      if (reflectionOnlyAssemblies.ContainsKey(args.Name))
85        return reflectionOnlyAssemblies[args.Name];
86      else
87        return Assembly.ReflectionOnlyLoad(args.Name);
88    }
89
90
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.
94    /// 2. The validator checks if all necessary files for each plugin are available.
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
101    ///    then one instance of each IPlugin type is activated and the OnLoad hook is called.
102    /// 9. All types implementing IApplication are discovered
103    /// </summary>
104    internal void DiscoverAndCheckPlugins() {
105      pluginDependencies.Clear();
106
107      IEnumerable<Assembly> reflectionOnlyAssemblies = ReflectionOnlyLoadDlls(PluginDir);
108      IEnumerable<PluginDescription> pluginDescriptions = GatherPluginDescriptions(reflectionOnlyAssemblies);
109      CheckPluginFiles(pluginDescriptions);
110
111      // check if all plugin assemblies can be loaded
112      CheckPluginAssemblies(pluginDescriptions);
113
114      // a full list of plugin descriptions is available now we can build the dependency tree
115      BuildDependencyTree(pluginDescriptions);
116
117      // check for dependency cycles
118      CheckPluginDependencyCycles(pluginDescriptions);
119
120      // recursively check if all necessary plugins are available and not disabled
121      // disable plugins with missing or disabled dependencies
122      CheckPluginDependencies(pluginDescriptions);
123
124      // mark all plugins as enabled that were not disabled in CheckPluginFiles, CheckPluginAssemblies,
125      // CheckCircularDependencies, or CheckPluginDependencies
126      foreach (var desc in pluginDescriptions)
127        if (desc.PluginState != PluginState.Disabled)
128          desc.Enable();
129
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>();
140
141      foreach (IApplication application in GetApplications()) {
142        Type appType = application.GetType();
143        ApplicationAttribute attr = (from x in appType.GetCustomAttributes(typeof(ApplicationAttribute), false)
144                                     select (ApplicationAttribute)x).Single();
145        ApplicationDescription info = new ApplicationDescription();
146        info.Name = application.Name;
147        info.Version = appType.Assembly.GetName().Version;
148        info.Description = application.Description;
149        info.AutoRestart = attr.RestartOnErrors;
150        info.DeclaringAssemblyName = appType.Assembly.GetName().Name;
151        info.DeclaringTypeName = appType.Namespace + "." + application.GetType().Name;
152
153        applications.Add(info);
154      }
155    }
156
157    private static IEnumerable<IApplication> GetApplications() {
158      return from asm in AppDomain.CurrentDomain.GetAssemblies()
159             from t in asm.GetTypes()
160             where typeof(IApplication).IsAssignableFrom(t) &&
161               !t.IsAbstract && !t.IsInterface && !t.HasElementType
162             select (IApplication)Activator.CreateInstance(t);
163    }
164
165    private IEnumerable<Assembly> ReflectionOnlyLoadDlls(string baseDir) {
166      List<Assembly> assemblies = new List<Assembly>();
167      // recursively load .dll files in subdirectories
168      foreach (string dirName in Directory.GetDirectories(baseDir)) {
169        assemblies.AddRange(ReflectionOnlyLoadDlls(dirName));
170      }
171      // try to load each .dll file in the plugin directory into the reflection only context
172      foreach (string filename in Directory.GetFiles(baseDir, "*.dll")) {
173        try {
174          Assembly asm = Assembly.ReflectionOnlyLoadFrom(filename);
175          RegisterLoadedAssembly(asm);
176          assemblies.Add(asm);
177        }
178        catch (BadImageFormatException) { } // just ignore the case that the .dll file is not a CLR assembly (e.g. a native dll)
179        catch (FileLoadException) { }
180        catch (SecurityException) { }
181      }
182      return assemblies;
183    }
184
185    /// <summary>
186    /// Checks if all plugin assemblies can be loaded. If an assembly can't be loaded the plugin is disabled.
187    /// </summary>
188    /// <param name="pluginDescriptions"></param>
189    private void CheckPluginAssemblies(IEnumerable<PluginDescription> pluginDescriptions) {
190      foreach (var desc in pluginDescriptions.Where(x => x.PluginState != PluginState.Disabled)) {
191        try {
192          foreach (var asmLocation in desc.AssemblyLocations) {
193            // the assembly must have been loaded in ReflectionOnlyDlls
194            // so we simply determine the name of the assembly and try to find it in the cache of loaded assemblies
195            var asmName = AssemblyName.GetAssemblyName(asmLocation);
196
197            if (!reflectionOnlyAssemblies.ContainsKey(asmName.FullName)) {
198              desc.Disable();
199              break; // as soon as one assembly is not available disable the plugin and check the next plugin description
200            }
201          }
202        }
203        catch (BadImageFormatException) {
204          // disable the plugin
205          desc.Disable();
206        }
207        catch (FileNotFoundException) {
208          // disable the plugin
209          desc.Disable();
210        }
211        catch (FileLoadException) {
212          // disable the plugin
213          desc.Disable();
214        }
215        catch (ArgumentException) {
216          // disable the plugin
217          desc.Disable();
218        }
219        catch (SecurityException) {
220          // disable the plugin
221          desc.Disable();
222        }
223      }
224    }
225
226
227    // find all types implementing IPlugin in the reflectionOnlyAssemblies and create a list of plugin descriptions
228    // the dependencies in the plugin descriptions are not yet set correctly because we need to create
229    // the full list of all plugin descriptions first
230    private IEnumerable<PluginDescription> GatherPluginDescriptions(IEnumerable<Assembly> assemblies) {
231      List<PluginDescription> pluginDescriptions = new List<PluginDescription>();
232      foreach (Assembly assembly in assemblies) {
233        // GetExportedTypes throws FileNotFoundException when a referenced assembly
234        // of the current assembly is missing.
235        try {
236          // if there is a type that implements IPlugin
237          // use AssemblyQualifiedName to compare the types because we can't directly
238          // compare ReflectionOnly types and execution types
239          var assemblyPluginDescriptions = from t in assembly.GetExportedTypes()
240                                           where !t.IsAbstract && t.GetInterfaces().Any(x => x.AssemblyQualifiedName == typeof(IPlugin).AssemblyQualifiedName)
241                                           select GetPluginDescription(t);
242          pluginDescriptions.AddRange(assemblyPluginDescriptions);
243        }
244        // ignore exceptions. Just don't yield a plugin description when an exception is thrown
245        catch (FileNotFoundException) {
246        }
247        catch (FileLoadException) {
248        }
249        catch (InvalidPluginException) {
250        }
251      }
252      return pluginDescriptions;
253    }
254
255    /// <summary>
256    /// Extracts plugin information for this type.
257    /// Reads plugin name, list and type of files and dependencies of the plugin. This information is necessary for
258    /// plugin dependency checking before plugin activation.
259    /// </summary>
260    /// <param name="t"></param>
261    private PluginDescription GetPluginDescription(Type pluginType) {
262
263      string pluginName, pluginDescription, pluginVersion;
264      GetPluginMetaData(pluginType, out pluginName, out pluginDescription, out pluginVersion);
265      var pluginFiles = GetPluginFilesMetaData(pluginType);
266      var pluginDependencies = GetPluginDependencyMetaData(pluginType);
267
268      // minimal sanity check of the attribute values
269      if (!string.IsNullOrEmpty(pluginName) &&
270          pluginFiles.Count() > 0 &&                                 // at least on file
271          pluginFiles.Any(f => f.Type == PluginFileType.Assembly)) { // at least on assembly
272        // create a temporary PluginDescription that contains the attribute values
273        PluginDescription info = new PluginDescription();
274        info.Name = pluginName;
275        info.Description = pluginDescription;
276        info.Version = new Version(pluginVersion);
277        info.AddFiles(pluginFiles);
278
279        this.pluginDependencies[info] = pluginDependencies;
280        return info;
281      } else {
282        throw new InvalidPluginException("Invalid metadata in plugin " + pluginType.ToString());
283      }
284    }
285
286    private static IEnumerable<PluginDependency> GetPluginDependencyMetaData(Type pluginType) {
287      // get all attributes of type PluginDependency
288      var dependencyAttributes = from attr in CustomAttributeData.GetCustomAttributes(pluginType)
289                                 where IsAttributeDataForType(attr, typeof(PluginDependencyAttribute))
290                                 select attr;
291
292      foreach (var dependencyAttr in dependencyAttributes) {
293        string name = (string)dependencyAttr.ConstructorArguments[0].Value;
294        Version version = new Version("0.0.0.0"); // default version
295        // check if version is given for now
296        // later when the constructor of PluginDependencyAttribute with only one argument has been removed
297        // this conditional can be removed as well
298        if (dependencyAttr.ConstructorArguments.Count > 1) {
299          try {
300            version = new Version((string)dependencyAttr.ConstructorArguments[1].Value); // might throw FormatException
301          }
302          catch (FormatException ex) {
303            throw new InvalidPluginException("Invalid version format of dependency " + name + " in plugin " + pluginType.ToString(), ex);
304          }
305        }
306        yield return new PluginDependency(name, version);
307      }
308    }
309
310    // not static because we need the PluginDir property
311    private IEnumerable<PluginFile> GetPluginFilesMetaData(Type pluginType) {
312      // get all attributes of type PluginFileAttribute
313      var pluginFileAttributes = from attr in CustomAttributeData.GetCustomAttributes(pluginType)
314                                 where IsAttributeDataForType(attr, typeof(PluginFileAttribute))
315                                 select attr;
316      foreach (var pluginFileAttribute in pluginFileAttributes) {
317        string pluginFileName = (string)pluginFileAttribute.ConstructorArguments[0].Value;
318        PluginFileType fileType = (PluginFileType)pluginFileAttribute.ConstructorArguments[1].Value;
319        yield return new PluginFile(Path.GetFullPath(Path.Combine(PluginDir, pluginFileName)), fileType);
320      }
321    }
322
323    private static void GetPluginMetaData(Type pluginType, out string pluginName, out string pluginDescription, out string pluginVersion) {
324      // there must be a single attribute of type PluginAttribute
325      var pluginMetaDataAttr = (from attr in CustomAttributeData.GetCustomAttributes(pluginType)
326                                where IsAttributeDataForType(attr, typeof(PluginAttribute))
327                                select attr).Single();
328
329      pluginName = (string)pluginMetaDataAttr.ConstructorArguments[0].Value;
330
331      // default description and version
332      pluginVersion = "0.0.0.0";
333      pluginDescription = pluginName;
334      if (pluginMetaDataAttr.ConstructorArguments.Count() == 2) {
335        // if two arguments are given the second argument is the version
336        pluginVersion = (string)pluginMetaDataAttr.ConstructorArguments[1].Value;
337      } else if (pluginMetaDataAttr.ConstructorArguments.Count() == 3) {
338        // if three arguments are given the second argument is the description and the third is the version
339        pluginDescription = (string)pluginMetaDataAttr.ConstructorArguments[1].Value;
340        pluginVersion = (string)pluginMetaDataAttr.ConstructorArguments[2].Value;
341      }
342    }
343
344    private static bool IsAttributeDataForType(CustomAttributeData attributeData, Type attributeType) {
345      return attributeData.Constructor.DeclaringType.AssemblyQualifiedName == attributeType.AssemblyQualifiedName;
346    }
347
348    // builds a dependency tree of all plugin descriptions
349    // searches matching plugin descriptions based on the list of dependency names for each plugin
350    // and sets the dependencies in the plugin descriptions
351    private void BuildDependencyTree(IEnumerable<PluginDescription> pluginDescriptions) {
352      foreach (var desc in pluginDescriptions) {
353        foreach (var dependency in pluginDependencies[desc]) {
354          var matchingDescriptions = from availablePlugin in pluginDescriptions
355                                     where availablePlugin.Name == dependency.Name
356                                     where IsCompatiblePluginVersion(availablePlugin.Version, dependency.Version)
357                                     select availablePlugin;
358          if (matchingDescriptions.Count() > 0) {
359            desc.AddDependency(matchingDescriptions.Single());
360          } else {
361            // no plugin description that matches the dependency name is available => plugin is disabled
362            desc.Disable(); break;
363          }
364        }
365      }
366    }
367
368    /// <summary>
369    /// Checks if version <paramref name="available"/> is compatible to version <paramref name="requested"/>.
370    /// Note: the compatibility relation is not bijective.
371    /// Compatibility rules:
372    ///  * major and minor number must be the same
373    ///  * build and revision number of <paramref name="available"/> must be larger or equal to <paramref name="requested"/>.
374    /// </summary>
375    /// <param name="available">The available version which should be compared to <paramref name="requested"/>.</param>
376    /// <param name="requested">The requested version that must be matched.</param>
377    /// <returns></returns>
378    private bool IsCompatiblePluginVersion(Version available, Version requested) {
379      // this condition must be removed after all plugins have been updated to declare plugin and dependency versions
380      if (
381        (requested.Major == 0 && requested.Minor == 0) ||
382        (available.Major == 0 && available.Minor == 0)) return true;
383      return
384        available.Major == requested.Major &&
385        available.Minor == requested.Minor &&
386        available.Build >= requested.Build &&
387        available.Revision >= requested.Revision;
388    }
389
390    private void CheckPluginDependencyCycles(IEnumerable<PluginDescription> pluginDescriptions) {
391      foreach (var plugin in pluginDescriptions) {
392        // if the plugin is not disabled anyway check if there are cycles
393        if (plugin.PluginState != PluginState.Disabled && HasCycleInDependencies(plugin, plugin.Dependencies)) {
394          plugin.Disable();
395        }
396      }
397    }
398
399    private bool HasCycleInDependencies(PluginDescription plugin, IEnumerable<PluginDescription> pluginDependencies) {
400      foreach (var dep in pluginDependencies) {
401        // if one of the dependencies is the original plugin we found a cycle and can return
402        // if the dependency is already disabled we can ignore the cycle detection because we will disable the plugin anyway
403        // if following one of the dependencies recursively leads to a cycle then we also return
404        if (dep == plugin || dep.PluginState == PluginState.Disabled || HasCycleInDependencies(plugin, dep.Dependencies)) return true;
405      }
406      // no cycle found and none of the direct and indirect dependencies is disabled
407      return false;
408    }
409
410    private void CheckPluginDependencies(IEnumerable<PluginDescription> pluginDescriptions) {
411      foreach (PluginDescription pluginDescription in pluginDescriptions.Where(x => x.PluginState != PluginState.Disabled)) {
412        if (IsAnyDependencyDisabled(pluginDescription)) {
413          pluginDescription.Disable();
414        }
415      }
416    }
417
418
419    private bool IsAnyDependencyDisabled(PluginDescription descr) {
420      if (descr.PluginState == PluginState.Disabled) return true;
421      foreach (PluginDescription dependency in descr.Dependencies) {
422        if (IsAnyDependencyDisabled(dependency)) return true;
423      }
424      return false;
425    }
426
427    private void LoadPlugins(IEnumerable<PluginDescription> pluginDescriptions) {
428      // load all loadable plugins (all dependencies available) into the execution context
429      foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(pluginDescriptions
430                                                                                .Where(x => x.PluginState != PluginState.Disabled))) {
431        List<Type> types = new List<Type>();
432        foreach (string assemblyLocation in desc.AssemblyLocations) {
433          // now load the assemblies into the execution context
434          var asm = Assembly.LoadFrom(assemblyLocation);
435          foreach (Type t in asm.GetTypes()) {
436            if (typeof(IPlugin).IsAssignableFrom(t)) {
437              types.Add(t);
438            }
439          }
440        }
441
442        foreach (Type pluginType in types) {
443          if (!pluginType.IsAbstract && !pluginType.IsInterface && !pluginType.HasElementType) {
444            IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
445            plugin.OnLoad();
446            OnPluginLoaded(new PluginInfrastructureEventArgs("Plugin loaded", plugin.Name));
447          }
448        }
449        desc.Load();
450      }
451    }
452
453    // checks if all declared plugin files are actually available and disables plugins with missing files
454    private void CheckPluginFiles(IEnumerable<PluginDescription> pluginDescriptions) {
455      foreach (PluginDescription desc in pluginDescriptions) {
456        if (!CheckPluginFiles(desc)) {
457          desc.Disable();
458        }
459      }
460    }
461
462    private bool CheckPluginFiles(PluginDescription pluginDescription) {
463      foreach (string filename in pluginDescription.Files.Select(x => x.Name)) {
464        if (!FileLiesInDirectory(PluginDir, filename) ||
465          !File.Exists(filename)) {
466          return false;
467        }
468      }
469      return true;
470    }
471
472    private static bool FileLiesInDirectory(string dir, string fileName) {
473      var basePath = Path.GetFullPath(dir);
474      return Path.GetFullPath(fileName).StartsWith(basePath);
475    }
476
477    // register assembly in the assembly cache for the ReflectionOnlyAssemblyResolveEvent
478    private void RegisterLoadedAssembly(Assembly asm) {
479      reflectionOnlyAssemblies.Add(asm.FullName, asm);
480      reflectionOnlyAssemblies.Add(asm.GetName().Name, asm); // add short name
481    }
482
483    private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
484      if (PluginLoaded != null)
485        PluginLoaded(this, e);
486    }
487
488    /// <summary>
489    /// Initializes the life time service with an infinite lease time.
490    /// </summary>
491    /// <returns><c>null</c>.</returns>
492    public override object InitializeLifetimeService() {
493      return null;
494    }
495  }
496}
Note: See TracBrowser for help on using the repository browser.