Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.PluginInfrastructure/3.3/SandboxApplicationManager.cs @ 18213

Last change on this file since 18213 was 17209, checked in by gkronber, 5 years ago

#2994: merged r17132:17198 from trunk to branch

File size: 18.3 KB
RevLine 
[6174]1#region License Information
2/* HeuristicLab
[17209]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6174]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 HeuristicLab.PluginInfrastructure.Manager;
28
29namespace HeuristicLab.PluginInfrastructure {
30
31  /// <summary>
32  /// The SandboxApplicationManager provides properties to retrieve the list of available plugins and applications.
33  /// It also provides methods for type discovery and instantiation for types declared in plugins.
34  /// The SandboxApplicationManager is used in sandboxed Application Domains where permissions are restricted and
35  /// only partially-trusted code can be executed.
36  /// </summary>
37  internal class SandboxApplicationManager : MarshalByRefObject, IApplicationManager {
38    /// <summary>
39    /// Fired when a plugin is loaded.
40    /// </summary>
41    internal event EventHandler<PluginInfrastructureEventArgs> PluginLoaded;
42    /// <summary>
43    /// Fired when a plugin is unloaded (when the application terminates).
44    /// </summary>
45    internal event EventHandler<PluginInfrastructureEventArgs> PluginUnloaded;
46
47    // cache for the AssemblyResolveEvent
48    // which must be handled when assemblies are loaded dynamically after the application start
49    protected internal Dictionary<string, Assembly> loadedAssemblies;
50
51    private List<IPlugin> loadedPlugins;
52
53    private List<PluginDescription> plugins;
54    /// <summary>
55    /// Gets all plugins.
56    /// </summary>
57    public IEnumerable<IPluginDescription> Plugins {
58      get { return plugins.Cast<IPluginDescription>(); }
59    }
60
61    private List<ApplicationDescription> applications;
62    /// <summary>
63    /// Gets all installed applications.
64    /// </summary>
65    public IEnumerable<IApplicationDescription> Applications {
66      get { return applications.Cast<IApplicationDescription>(); }
67    }
68
69    internal SandboxApplicationManager()
70      : base() {
71      loadedAssemblies = new Dictionary<string, Assembly>();
72      loadedPlugins = new List<IPlugin>();
[16911]73
74      AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload;
[6174]75    }
76
77    /// <summary>
[16911]78    /// The sandbox application manager can also be used to load plugins even when no actual application is executed.
79    /// In such cases, plugins that have been loaded also have to be unloaded again before the application domain is unloaded.
80    /// </summary>
81    private void CurrentDomain_DomainUnload(object sender, EventArgs e) {
82      UnloadPlugins();
83    }
84
85    /// <summary>
[6174]86    /// Prepares the application domain for the execution of an HL application.
87    /// Pre-loads all <paramref name="plugins"/>.
88    /// </summary>
89    /// <param name="apps">Enumerable of available HL applications.</param>
90    /// <param name="plugins">Enumerable of plugins that should be pre-loaded.</param> 
91    internal void PrepareApplicationDomain(IEnumerable<ApplicationDescription> apps, IEnumerable<PluginDescription> plugins) {
92      this.plugins = new List<PluginDescription>(plugins);
93      this.applications = new List<ApplicationDescription>(apps);
94      ApplicationManager.RegisterApplicationManager(this);
95      LoadPlugins(plugins);
96    }
97
98    /// <summary>
99    /// Loads the <paramref name="plugins"/> into this application domain.
100    /// </summary>
101    /// <param name="plugins">Enumerable of plugins that should be loaded.</param>
102    private void LoadPlugins(IEnumerable<PluginDescription> plugins) {
103      // load all loadable plugins (all dependencies available) into the execution context
104      foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(plugins.Where(x => x.PluginState != PluginState.Disabled))) {
105        foreach (string fileName in desc.AssemblyLocations) {
106          // load assembly reflection only first to get the full assembly name
107          var reflectionOnlyAssembly = Assembly.ReflectionOnlyLoadFrom(fileName);
108          // load the assembly into execution context using full assembly name
109          var asm = Assembly.Load(reflectionOnlyAssembly.FullName);
110          RegisterLoadedAssembly(asm);
111          // instantiate and load all plugins in this assembly
112          foreach (var plugin in GetInstances<IPlugin>(asm)) {
113            plugin.OnLoad();
114            loadedPlugins.Add(plugin);
115          }
116        }
117        OnPluginLoaded(new PluginInfrastructureEventArgs(desc));
118        desc.Load();
119      }
120    }
121
122    /// <summary>
[16911]123    /// Unloads the <paramref name="plugins"/> that were loaded into this application domain.
124    /// </summary>
125    private void UnloadPlugins() {
126      // unload plugins in reverse order
127      foreach (var plugin in loadedPlugins.Reverse<IPlugin>()) {
128        plugin.OnUnload();
129      }
130      loadedPlugins.Clear(); // remove all plugins once unloaded
131
132      foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(plugins.Where(x => x.PluginState != PluginState.Disabled))) {
133        desc.Unload();
134        OnPluginUnloaded(new PluginInfrastructureEventArgs(desc));
135      }
136      plugins.Clear(); // remove all plugin descriptions once unloaded
137    }
138
139    /// <summary>
[6174]140    /// Runs the application declared in <paramref name="appInfo"/>.
141    /// This is a synchronous call. When the application is terminated all plugins are unloaded.
142    /// </summary>
143    /// <param name="appInfo">Description of the application to run</param>
[8818]144    internal void Run(ApplicationDescription appInfo, ICommandLineArgument[] args) {
[6174]145      IApplication runnablePlugin = (IApplication)Activator.CreateInstance(appInfo.DeclaringAssemblyName, appInfo.DeclaringTypeName).Unwrap();
146      try {
[8818]147        runnablePlugin.Run(args);
[16911]148      } finally {
149        UnloadPlugins();
[7586]150      }
[6174]151    }
152
153    // register assembly in the assembly cache for the AssemblyResolveEvent
154    private void RegisterLoadedAssembly(Assembly asm) {
155      if (loadedAssemblies.ContainsKey(asm.FullName) || loadedAssemblies.ContainsKey(asm.GetName().Name)) {
156        throw new ArgumentException("An assembly with the name " + asm.GetName().Name + " has been registered already.", "asm");
157      }
158      loadedAssemblies.Add(asm.FullName, asm);
159      loadedAssemblies.Add(asm.GetName().Name, asm); // add short name
160    }
161
162    /// <summary>
163    /// Creates an instance of all types that are subtypes or the same type of the specified type and declared in <paramref name="plugin"/>
164    /// </summary>
165    /// <typeparam name="T">Most general type.</typeparam>
166    /// <returns>Enumerable of the created instances.</returns>
167    internal static IEnumerable<T> GetInstances<T>(IPluginDescription plugin) where T : class {
168      List<T> instances = new List<T>();
[7111]169      foreach (Type t in GetTypes(typeof(T), plugin, onlyInstantiable: true, includeGenericTypeDefinitions: false)) {
[6174]170        T instance = null;
[7586]171        try { instance = (T)Activator.CreateInstance(t); }
172        catch { }
[6174]173        if (instance != null) instances.Add(instance);
174      }
175      return instances;
176    }
177    /// <summary>
178    /// Creates an instance of all types declared in assembly <paramref name="asm"/> that are subtypes or the same type of the specified <typeparamref name="type"/>.
179    /// </summary>
180    /// <typeparam name="T">Most general type.</typeparam>
181    /// <param name="asm">Declaring assembly.</param>
182    /// <returns>Enumerable of the created instances.</returns>
183    private static IEnumerable<T> GetInstances<T>(Assembly asm) where T : class {
184      List<T> instances = new List<T>();
[7111]185      foreach (Type t in GetTypes(typeof(T), asm, onlyInstantiable: true, includeGenericTypeDefinitions: false)) {
[6174]186        T instance = null;
[7586]187        try { instance = (T)Activator.CreateInstance(t); }
188        catch { }
[6174]189        if (instance != null) instances.Add(instance);
190      }
191      return instances;
192    }
193    /// <summary>
194    /// Creates an instance of all types that are subtypes or the same type of the specified type
195    /// </summary>
196    /// <typeparam name="T">Most general type.</typeparam>
197    /// <returns>Enumerable of the created instances.</returns>
198    internal static IEnumerable<T> GetInstances<T>() where T : class {
199      return from i in GetInstances(typeof(T))
200             select (T)i;
201    }
202
203    /// <summary>
204    /// Creates an instance of all types that are subtypes or the same type of the specified type
205    /// </summary>
206    /// <param name="type">Most general type.</param>
207    /// <returns>Enumerable of the created instances.</returns>
208    internal static IEnumerable<object> GetInstances(Type type) {
209      List<object> instances = new List<object>();
[7111]210      foreach (Type t in GetTypes(type, onlyInstantiable: true, includeGenericTypeDefinitions: false)) {
[6174]211        object instance = null;
[7586]212        try { instance = Activator.CreateInstance(t); }
213        catch { }
[6174]214        if (instance != null) instances.Add(instance);
215      }
216      return instances;
217    }
218
219    /// <summary>
220    /// Finds all types that are subtypes or equal to the specified type.
221    /// </summary>
222    /// <param name="type">Most general type for which to find matching types.</param>
223    /// <param name="onlyInstantiable">Return only types that are instantiable
[7111]224    /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
[6174]225    /// (interfaces, abstract classes... are not returned)</param>
226    /// <returns>Enumerable of the discovered types.</returns>
[7111]227    internal static IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
[6174]228      return from asm in AppDomain.CurrentDomain.GetAssemblies()
[7111]229             from t in GetTypes(type, asm, onlyInstantiable, includeGenericTypeDefinitions)
[6174]230             select t;
231    }
232
[7111]233    internal static IEnumerable<Type> GetTypes(IEnumerable<Type> types, bool onlyInstantiable, bool includeGenericTypeDefinitions, bool assignableToAllTypes) {
234      IEnumerable<Type> result = GetTypes(types.First(), onlyInstantiable, includeGenericTypeDefinitions);
[6174]235      foreach (Type type in types.Skip(1)) {
[7111]236        IEnumerable<Type> discoveredTypes = GetTypes(type, onlyInstantiable, includeGenericTypeDefinitions);
[6174]237        if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
238        else result = result.Union(discoveredTypes);
239      }
240      return result;
241    }
242
243    /// <summary>
244    /// Finds all types that are subtypes or equal to the specified type if they are part of the given
245    /// <paramref name="pluginDescription"/>.
246    /// </summary>
247    /// <param name="type">Most general type for which to find matching types.</param>
248    /// <param name="pluginDescription">The plugin the subtypes must be part of.</param>
249    /// <param name="onlyInstantiable">Return only types that are instantiable
[7111]250    /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
[6174]251    /// (interfaces, abstract classes... are not returned)</param>
252    /// <returns>Enumerable of the discovered types.</returns>
[7111]253    internal static IEnumerable<Type> GetTypes(Type type, IPluginDescription pluginDescription, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
[6174]254      PluginDescription pluginDesc = (PluginDescription)pluginDescription;
255      return from asm in AppDomain.CurrentDomain.GetAssemblies()
[7586]256             where !asm.IsDynamic && !string.IsNullOrEmpty(asm.Location)
[6174]257             where pluginDesc.AssemblyLocations.Any(location => location.Equals(Path.GetFullPath(asm.Location), StringComparison.CurrentCultureIgnoreCase))
[7111]258             from t in GetTypes(type, asm, onlyInstantiable, includeGenericTypeDefinitions)
[6174]259             select t;
260    }
261
[7111]262    internal static IEnumerable<Type> GetTypes(IEnumerable<Type> types, IPluginDescription pluginDescription, bool onlyInstantiable, bool includeGenericTypeDefinitions, bool assignableToAllTypes) {
263      IEnumerable<Type> result = GetTypes(types.First(), pluginDescription, onlyInstantiable, includeGenericTypeDefinitions);
[6174]264      foreach (Type type in types.Skip(1)) {
[7111]265        IEnumerable<Type> discoveredTypes = GetTypes(type, pluginDescription, onlyInstantiable, includeGenericTypeDefinitions);
[6174]266        if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
267        else result = result.Union(discoveredTypes);
268      }
269      return result;
270    }
271
272    /// <summary>
273    /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
274    /// </summary>
275    /// <param name="type">Most general type we want to find.</param>
276    /// <param name="assembly">Assembly that should be searched for types.</param>
277    /// <param name="onlyInstantiable">Return only types that are instantiable
278    /// (interfaces, abstract classes...  are not returned)</param>
[7111]279    /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
[6174]280    /// <returns>Enumerable of the discovered types.</returns>
[11771]281    internal static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
[8571]282      var matchingTypes = from assemblyType in assembly.GetTypes()
283                          let t = assemblyType.BuildType(type)
284                          where t != null
285                          where t.IsSubTypeOf(type)
286                          where !t.IsNonDiscoverableType()
287                          where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
288                          where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition
289                          select t;
[7069]290
[8571]291      return matchingTypes;
[6174]292    }
293
[11771]294    /// <summary>
[11877]295    /// Discovers all types implementing or inheriting all or any type in <paramref name="types"/> (directly and indirectly) that are declared in the assembly <paramref name="assembly"/>.
[11771]296    /// </summary>
297    /// <param name="types">The types to discover.</param>
298    /// <param name="assembly">The declaring assembly.</param>
299    /// <param name="onlyInstantiable">Return only types that are instantiable (instance, abstract... are not returned)</param>
300    /// /// <param name="assignableToAllTypes">Specifies if discovered types must implement or inherit all given <paramref name="types"/>.</param>
301    /// <returns>An enumerable of discovered types.</returns>
302    internal static IEnumerable<Type> GetTypes(IEnumerable<Type> types, Assembly assembly, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false, bool assignableToAllTypes = true) {
303      IEnumerable<Type> result = GetTypes(types.First(), assembly, onlyInstantiable, includeGenericTypeDefinitions);
304      foreach (Type type in types.Skip(1)) {
305        IEnumerable<Type> discoveredTypes = GetTypes(type, assembly, onlyInstantiable, includeGenericTypeDefinitions);
306        if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
307        else result = result.Union(discoveredTypes);
308      }
309      return result;
310    }
311
[6174]312    private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
313      if (PluginLoaded != null) PluginLoaded(this, e);
314    }
315
316    private void OnPluginUnloaded(PluginInfrastructureEventArgs e) {
317      if (PluginUnloaded != null) PluginUnloaded(this, e);
318    }
319
320    #region IApplicationManager Members
321
322    IEnumerable<T> IApplicationManager.GetInstances<T>() {
323      return GetInstances<T>();
324    }
325
326    IEnumerable<object> IApplicationManager.GetInstances(Type type) {
327      return GetInstances(type);
328    }
329
[7111]330    IEnumerable<Type> IApplicationManager.GetTypes(Type type, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
331      return GetTypes(type, onlyInstantiable, includeGenericTypeDefinitions);
[6174]332    }
[7111]333    IEnumerable<Type> IApplicationManager.GetTypes(IEnumerable<Type> types, bool onlyInstantiable, bool includeGenericTypeDefinitions, bool assignableToAllTypes) {
334      return GetTypes(types, onlyInstantiable, includeGenericTypeDefinitions, assignableToAllTypes);
[6174]335    }
336
[7111]337    IEnumerable<Type> IApplicationManager.GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
338      return GetTypes(type, plugin, onlyInstantiable, includeGenericTypeDefinitions);
[6174]339    }
[7111]340    IEnumerable<Type> IApplicationManager.GetTypes(IEnumerable<Type> types, IPluginDescription plugin, bool onlyInstantiable, bool includeGenericTypeDefinitions, bool assignableToAllTypes) {
341      return GetTypes(types, plugin, onlyInstantiable, includeGenericTypeDefinitions, assignableToAllTypes);
[6174]342    }
343
[11781]344    IEnumerable<Type> IApplicationManager.GetTypes(Type type, Assembly assembly, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
[11771]345      return GetTypes(type, assembly, onlyInstantiable, includeGenericTypeDefinitions);
346    }
[11781]347    IEnumerable<Type> IApplicationManager.GetTypes(IEnumerable<Type> types, Assembly assembly, bool onlyInstantiable, bool includeGenericTypeDefinitions, bool assignableToAllTypes) {
348      return GetTypes(types, assembly, onlyInstantiable, includeGenericTypeDefinitions, assignableToAllTypes);
[11771]349    }
350
[6174]351    /// <summary>
352    /// Finds the plugin that declares the <paramref name="type">type</paramref>.
353    /// </summary>
354    /// <param name="type">The type of interest.</param>
355    /// <returns>The description of the plugin that declares the given type or null if the type has not been declared by a known plugin.</returns>
356    public IPluginDescription GetDeclaringPlugin(Type type) {
357      if (type == null) throw new ArgumentNullException("type");
358      foreach (PluginDescription info in Plugins) {
359        if (info.AssemblyLocations.Contains(Path.GetFullPath(type.Assembly.Location))) return info;
360      }
361      return null;
362    }
363    #endregion
364  }
365}
Note: See TracBrowser for help on using the repository browser.