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