Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.PluginInfrastructure/3.3/SandboxApplicationManager.cs @ 11654

Last change on this file since 11654 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 16.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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) {
118      IApplication runnablePlugin = (IApplication)Activator.CreateInstance(appInfo.DeclaringAssemblyName, appInfo.DeclaringTypeName).Unwrap();
119      try {
120        runnablePlugin.Run();
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 !IsDynamicAssembly(asm)
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    private static bool IsDynamicAssembly(Assembly asm) {
254      return (asm is System.Reflection.Emit.AssemblyBuilder) || string.IsNullOrEmpty(asm.Location);
255    }
256
257    /// <summary>
258    /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
259    /// </summary>
260    /// <param name="type">Most general type we want to find.</param>
261    /// <param name="assembly">Assembly that should be searched for types.</param>
262    /// <param name="onlyInstantiable">Return only types that are instantiable
263    /// (interfaces, abstract classes...  are not returned)</param>
264    /// <param name="includeGenericTypeDefinitions">Specifies if generic type definitions shall be included</param>
265    /// <returns>Enumerable of the discovered types.</returns>
266    private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
267      var buildTypes = from t in assembly.GetTypes()
268                       where !IsNonDiscoverableType(t)
269                       where CheckTypeCompatibility(type, t)
270                       where onlyInstantiable == false ||
271                             (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
272                       select BuildType(t, type);
273
274      return from t in buildTypes
275             where includeGenericTypeDefinitions || !t.IsGenericTypeDefinition
276             select t;
277    }
278
279
280    private static bool IsNonDiscoverableType(Type t) {
281      return t.GetCustomAttributes(typeof(NonDiscoverableTypeAttribute), false).Any();
282    }
283
284    private static bool CheckTypeCompatibility(Type type, Type other) {
285      if (type.IsAssignableFrom(other))
286        return true;
287      if (type.IsGenericType && other.IsGenericType) {
288        try {
289          if (type.IsAssignableFrom(other.GetGenericTypeDefinition().MakeGenericType(type.GetGenericArguments())))
290            return true;
291        }
292        catch (Exception) { }
293      }
294      return false;
295    }
296    private static Type BuildType(Type type, Type protoType) {
297      if (type.IsGenericType && protoType.IsGenericType)
298        return type.GetGenericTypeDefinition().MakeGenericType(protoType.GetGenericArguments());
299      else
300        return type;
301    }
302
303    private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
304      if (PluginLoaded != null) PluginLoaded(this, e);
305    }
306
307    private void OnPluginUnloaded(PluginInfrastructureEventArgs e) {
308      if (PluginUnloaded != null) PluginUnloaded(this, e);
309    }
310
311    #region IApplicationManager Members
312
313    IEnumerable<T> IApplicationManager.GetInstances<T>() {
314      return GetInstances<T>();
315    }
316
317    IEnumerable<object> IApplicationManager.GetInstances(Type type) {
318      return GetInstances(type);
319    }
320
321    IEnumerable<Type> IApplicationManager.GetTypes(Type type, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
322      return GetTypes(type, onlyInstantiable, includeGenericTypeDefinitions);
323    }
324    IEnumerable<Type> IApplicationManager.GetTypes(IEnumerable<Type> types, bool onlyInstantiable, bool includeGenericTypeDefinitions, bool assignableToAllTypes) {
325      return GetTypes(types, onlyInstantiable, includeGenericTypeDefinitions, assignableToAllTypes);
326    }
327
328    IEnumerable<Type> IApplicationManager.GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable, bool includeGenericTypeDefinitions) {
329      return GetTypes(type, plugin, onlyInstantiable, includeGenericTypeDefinitions);
330    }
331    IEnumerable<Type> IApplicationManager.GetTypes(IEnumerable<Type> types, IPluginDescription plugin, bool onlyInstantiable, bool includeGenericTypeDefinitions, bool assignableToAllTypes) {
332      return GetTypes(types, plugin, onlyInstantiable, includeGenericTypeDefinitions, assignableToAllTypes);
333    }
334
335    /// <summary>
336    /// Finds the plugin that declares the <paramref name="type">type</paramref>.
337    /// </summary>
338    /// <param name="type">The type of interest.</param>
339    /// <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>
340    public IPluginDescription GetDeclaringPlugin(Type type) {
341      if (type == null) throw new ArgumentNullException("type");
342      foreach (PluginDescription info in Plugins) {
343        if (info.AssemblyLocations.Contains(Path.GetFullPath(type.Assembly.Location))) return info;
344      }
345      return null;
346    }
347    #endregion
348  }
349}
Note: See TracBrowser for help on using the repository browser.