Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EnhancedProgress/HeuristicLab.PluginInfrastructure/3.3/SandboxApplicationManager.cs @ 15418

Last change on this file since 15418 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 17.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 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>();
73    }
74
75    /// <summary>
76    /// Prepares the application domain for the execution of an HL application.
77    /// Pre-loads all <paramref name="plugins"/>.
78    /// </summary>
79    /// <param name="apps">Enumerable of available HL applications.</param>
80    /// <param name="plugins">Enumerable of plugins that should be pre-loaded.</param> 
81    internal void PrepareApplicationDomain(IEnumerable<ApplicationDescription> apps, IEnumerable<PluginDescription> plugins) {
82      this.plugins = new List<PluginDescription>(plugins);
83      this.applications = new List<ApplicationDescription>(apps);
84      ApplicationManager.RegisterApplicationManager(this);
85      LoadPlugins(plugins);
86    }
87
88    /// <summary>
89    /// Loads the <paramref name="plugins"/> into this application domain.
90    /// </summary>
91    /// <param name="plugins">Enumerable of plugins that should be loaded.</param>
92    private void LoadPlugins(IEnumerable<PluginDescription> plugins) {
93      // load all loadable plugins (all dependencies available) into the execution context
94      foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(plugins.Where(x => x.PluginState != PluginState.Disabled))) {
95        foreach (string fileName in desc.AssemblyLocations) {
96          // load assembly reflection only first to get the full assembly name
97          var reflectionOnlyAssembly = Assembly.ReflectionOnlyLoadFrom(fileName);
98          // load the assembly into execution context using full assembly name
99          var asm = Assembly.Load(reflectionOnlyAssembly.FullName);
100          RegisterLoadedAssembly(asm);
101          // instantiate and load all plugins in this assembly
102          foreach (var plugin in GetInstances<IPlugin>(asm)) {
103            plugin.OnLoad();
104            loadedPlugins.Add(plugin);
105          }
106        }
107        OnPluginLoaded(new PluginInfrastructureEventArgs(desc));
108        desc.Load();
109      }
110    }
111
112    /// <summary>
113    /// Runs the application declared in <paramref name="appInfo"/>.
114    /// This is a synchronous call. When the application is terminated all plugins are unloaded.
115    /// </summary>
116    /// <param name="appInfo">Description of the application to run</param>
117    internal void Run(ApplicationDescription appInfo, ICommandLineArgument[] args) {
118      IApplication runnablePlugin = (IApplication)Activator.CreateInstance(appInfo.DeclaringAssemblyName, appInfo.DeclaringTypeName).Unwrap();
119      try {
120        runnablePlugin.Run(args);
121      }
122      finally {
123        // unload plugins in reverse order
124        foreach (var plugin in loadedPlugins.Reverse<IPlugin>()) {
125          plugin.OnUnload();
126        }
127        foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(plugins.Where(x => x.PluginState != PluginState.Disabled))) {
128          desc.Unload();
129          OnPluginUnloaded(new PluginInfrastructureEventArgs(desc));
130        }
131      }
132    }
133
134    // register assembly in the assembly cache for the AssemblyResolveEvent
135    private void RegisterLoadedAssembly(Assembly asm) {
136      if (loadedAssemblies.ContainsKey(asm.FullName) || loadedAssemblies.ContainsKey(asm.GetName().Name)) {
137        throw new ArgumentException("An assembly with the name " + asm.GetName().Name + " has been registered already.", "asm");
138      }
139      loadedAssemblies.Add(asm.FullName, asm);
140      loadedAssemblies.Add(asm.GetName().Name, asm); // add short name
141    }
142
143    /// <summary>
144    /// Creates an instance of all types that are subtypes or the same type of the specified type and declared in <paramref name="plugin"/>
145    /// </summary>
146    /// <typeparam name="T">Most general type.</typeparam>
147    /// <returns>Enumerable of the created instances.</returns>
148    internal static IEnumerable<T> GetInstances<T>(IPluginDescription plugin) where T : class {
149      List<T> instances = new List<T>();
150      foreach (Type t in GetTypes(typeof(T), plugin, onlyInstantiable: true, includeGenericTypeDefinitions: false)) {
151        T instance = null;
152        try { instance = (T)Activator.CreateInstance(t); }
153        catch { }
154        if (instance != null) instances.Add(instance);
155      }
156      return instances;
157    }
158    /// <summary>
159    /// 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"/>.
160    /// </summary>
161    /// <typeparam name="T">Most general type.</typeparam>
162    /// <param name="asm">Declaring assembly.</param>
163    /// <returns>Enumerable of the created instances.</returns>
164    private static IEnumerable<T> GetInstances<T>(Assembly asm) where T : class {
165      List<T> instances = new List<T>();
166      foreach (Type t in GetTypes(typeof(T), asm, onlyInstantiable: true, includeGenericTypeDefinitions: false)) {
167        T instance = null;
168        try { instance = (T)Activator.CreateInstance(t); }
169        catch { }
170        if (instance != null) instances.Add(instance);
171      }
172      return instances;
173    }
174    /// <summary>
175    /// Creates an instance of all types that are subtypes or the same type of the specified type
176    /// </summary>
177    /// <typeparam name="T">Most general type.</typeparam>
178    /// <returns>Enumerable of the created instances.</returns>
179    internal static IEnumerable<T> GetInstances<T>() where T : class {
180      return from i in GetInstances(typeof(T))
181             select (T)i;
182    }
183
184    /// <summary>
185    /// Creates an instance of all types that are subtypes or the same type of the specified type
186    /// </summary>
187    /// <param name="type">Most general type.</param>
188    /// <returns>Enumerable of the created instances.</returns>
189    internal static IEnumerable<object> GetInstances(Type type) {
190      List<object> instances = new List<object>();
191      foreach (Type t in GetTypes(type, onlyInstantiable: true, includeGenericTypeDefinitions: false)) {
192        object instance = null;
193        try { instance = Activator.CreateInstance(t); }
194        catch { }
195        if (instance != null) instances.Add(instance);
196      }
197      return instances;
198    }
199
200    /// <summary>
201    /// Finds all types that are subtypes or equal to the specified type.
202    /// </summary>
203    /// <param name="type">Most general type for which to find matching types.</param>
204    /// <param name="onlyInstantiable">Return only types that are instantiable
205    /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
206    /// (interfaces, abstract classes... are not returned)</param>
207    /// <returns>Enumerable of the discovered types.</returns>
208    internal static IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
209      return from asm in AppDomain.CurrentDomain.GetAssemblies()
210             from t in GetTypes(type, asm, onlyInstantiable, includeGenericTypeDefinitions)
211             select t;
212    }
213
214    internal static IEnumerable<Type> GetTypes(IEnumerable<Type> types, bool onlyInstantiable, bool includeGenericTypeDefinitions, bool assignableToAllTypes) {
215      IEnumerable<Type> result = GetTypes(types.First(), onlyInstantiable, includeGenericTypeDefinitions);
216      foreach (Type type in types.Skip(1)) {
217        IEnumerable<Type> discoveredTypes = GetTypes(type, onlyInstantiable, includeGenericTypeDefinitions);
218        if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
219        else result = result.Union(discoveredTypes);
220      }
221      return result;
222    }
223
224    /// <summary>
225    /// Finds all types that are subtypes or equal to the specified type if they are part of the given
226    /// <paramref name="pluginDescription"/>.
227    /// </summary>
228    /// <param name="type">Most general type for which to find matching types.</param>
229    /// <param name="pluginDescription">The plugin the subtypes must be part of.</param>
230    /// <param name="onlyInstantiable">Return only types that are instantiable
231    /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
232    /// (interfaces, abstract classes... are not returned)</param>
233    /// <returns>Enumerable of the discovered types.</returns>
234    internal static IEnumerable<Type> GetTypes(Type type, IPluginDescription pluginDescription, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
235      PluginDescription pluginDesc = (PluginDescription)pluginDescription;
236      return from asm in AppDomain.CurrentDomain.GetAssemblies()
237             where !asm.IsDynamic && !string.IsNullOrEmpty(asm.Location)
238             where pluginDesc.AssemblyLocations.Any(location => location.Equals(Path.GetFullPath(asm.Location), StringComparison.CurrentCultureIgnoreCase))
239             from t in GetTypes(type, asm, onlyInstantiable, includeGenericTypeDefinitions)
240             select t;
241    }
242
243    internal static IEnumerable<Type> GetTypes(IEnumerable<Type> types, IPluginDescription pluginDescription, bool onlyInstantiable, bool includeGenericTypeDefinitions, bool assignableToAllTypes) {
244      IEnumerable<Type> result = GetTypes(types.First(), pluginDescription, onlyInstantiable, includeGenericTypeDefinitions);
245      foreach (Type type in types.Skip(1)) {
246        IEnumerable<Type> discoveredTypes = GetTypes(type, pluginDescription, onlyInstantiable, includeGenericTypeDefinitions);
247        if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
248        else result = result.Union(discoveredTypes);
249      }
250      return result;
251    }
252
253    /// <summary>
254    /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
255    /// </summary>
256    /// <param name="type">Most general type we want to find.</param>
257    /// <param name="assembly">Assembly that should be searched for types.</param>
258    /// <param name="onlyInstantiable">Return only types that are instantiable
259    /// (interfaces, abstract classes...  are not returned)</param>
260    /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
261    /// <returns>Enumerable of the discovered types.</returns>
262    internal static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
263      var matchingTypes = from assemblyType in assembly.GetTypes()
264                          let t = assemblyType.BuildType(type)
265                          where t != null
266                          where t.IsSubTypeOf(type)
267                          where !t.IsNonDiscoverableType()
268                          where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
269                          where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition
270                          select t;
271
272      return matchingTypes;
273    }
274
275    /// <summary>
276    /// 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"/>.
277    /// </summary>
278    /// <param name="types">The types to discover.</param>
279    /// <param name="assembly">The declaring assembly.</param>
280    /// <param name="onlyInstantiable">Return only types that are instantiable (instance, abstract... are not returned)</param>
281    /// /// <param name="assignableToAllTypes">Specifies if discovered types must implement or inherit all given <paramref name="types"/>.</param>
282    /// <returns>An enumerable of discovered types.</returns>
283    internal static IEnumerable<Type> GetTypes(IEnumerable<Type> types, Assembly assembly, bool onlyInstantiable = true, bool includeGenericTypeDefinitions = false, bool assignableToAllTypes = true) {
284      IEnumerable<Type> result = GetTypes(types.First(), assembly, onlyInstantiable, includeGenericTypeDefinitions);
285      foreach (Type type in types.Skip(1)) {
286        IEnumerable<Type> discoveredTypes = GetTypes(type, assembly, onlyInstantiable, includeGenericTypeDefinitions);
287        if (assignableToAllTypes) result = result.Intersect(discoveredTypes);
288        else result = result.Union(discoveredTypes);
289      }
290      return result;
291    }
292
293    private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
294      if (PluginLoaded != null) PluginLoaded(this, e);
295    }
296
297    private void OnPluginUnloaded(PluginInfrastructureEventArgs e) {
298      if (PluginUnloaded != null) PluginUnloaded(this, e);
299    }
300
301    #region IApplicationManager Members
302
303    IEnumerable<T> IApplicationManager.GetInstances<T>() {
304      return GetInstances<T>();
305    }
306
307    IEnumerable<object> IApplicationManager.GetInstances(Type type) {
308      return GetInstances(type);
309    }
310
311    IEnumerable<Type> IApplicationManager.GetTypes(Type type, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
312      return GetTypes(type, onlyInstantiable, includeGenericTypeDefinitions);
313    }
314    IEnumerable<Type> IApplicationManager.GetTypes(IEnumerable<Type> types, bool onlyInstantiable, bool includeGenericTypeDefinitions, bool assignableToAllTypes) {
315      return GetTypes(types, onlyInstantiable, includeGenericTypeDefinitions, assignableToAllTypes);
316    }
317
318    IEnumerable<Type> IApplicationManager.GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
319      return GetTypes(type, plugin, onlyInstantiable, includeGenericTypeDefinitions);
320    }
321    IEnumerable<Type> IApplicationManager.GetTypes(IEnumerable<Type> types, IPluginDescription plugin, bool onlyInstantiable, bool includeGenericTypeDefinitions, bool assignableToAllTypes) {
322      return GetTypes(types, plugin, onlyInstantiable, includeGenericTypeDefinitions, assignableToAllTypes);
323    }
324
325    IEnumerable<Type> IApplicationManager.GetTypes(Type type, Assembly assembly, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
326      return GetTypes(type, assembly, onlyInstantiable, includeGenericTypeDefinitions);
327    }
328    IEnumerable<Type> IApplicationManager.GetTypes(IEnumerable<Type> types, Assembly assembly, bool onlyInstantiable, bool includeGenericTypeDefinitions, bool assignableToAllTypes) {
329      return GetTypes(types, assembly, onlyInstantiable, includeGenericTypeDefinitions, assignableToAllTypes);
330    }
331
332    /// <summary>
333    /// Finds the plugin that declares the <paramref name="type">type</paramref>.
334    /// </summary>
335    /// <param name="type">The type of interest.</param>
336    /// <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>
337    public IPluginDescription GetDeclaringPlugin(Type type) {
338      if (type == null) throw new ArgumentNullException("type");
339      foreach (PluginDescription info in Plugins) {
340        if (info.AssemblyLocations.Contains(Path.GetFullPath(type.Assembly.Location))) return info;
341      }
342      return null;
343    }
344    #endregion
345  }
346}
Note: See TracBrowser for help on using the repository browser.