Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.PluginInfrastructure/DefaultApplicationManager.cs @ 7581

Last change on this file since 7581 was 3247, checked in by gkronber, 15 years ago

Implemented a LightweightApplicationManager for unit testing and extracted the DefaultApplicationManager out of class ApplicationManager. #954 (ApplicationManager.Manager should have a default instance to enable unit testing)

File size: 13.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Security.Policy;
25using System.Reflection;
26using System.Diagnostics;
27using System.Security.Permissions;
28using System.Security;
29using System.Linq;
30using HeuristicLab.PluginInfrastructure.Manager;
31using System.IO;
32
33namespace HeuristicLab.PluginInfrastructure {
34
35  /// <summary>
36  /// The DefaultApplicationManager provides properties to retrieve the list of available plugins and applications.
37  /// It also provides methods for type discovery and instantiation for types declared in plugins.
38  /// The DefaultApplicationManager is registered as ApplicationManager.Manager singleton for each HL application
39  /// started via the plugin infrastructure.
40  /// </summary>
41  internal sealed class DefaultApplicationManager : MarshalByRefObject, IApplicationManager {
42    /// <summary>
43    /// Fired when a plugin is loaded.
44    /// </summary>
45    internal event EventHandler<PluginInfrastructureEventArgs> PluginLoaded;
46    /// <summary>
47    /// Fired when a plugin is unloaded (when the application terminates).
48    /// </summary>
49    internal event EventHandler<PluginInfrastructureEventArgs> PluginUnloaded;
50
51    // cache for the AssemblyResolveEvent
52    // which must be handled when assemblies are loaded dynamically after the application start
53    private Dictionary<string, Assembly> loadedAssemblies;
54
55    private List<IPlugin> loadedPlugins;
56
57    private List<PluginDescription> plugins;
58    /// <summary>
59    /// Gets all plugins.
60    /// </summary>
61    public IEnumerable<IPluginDescription> Plugins {
62      get { return plugins.Cast<IPluginDescription>(); }
63    }
64
65    private List<ApplicationDescription> applications;
66    /// <summary>
67    /// Gets all installed applications.
68    /// </summary>
69    public IEnumerable<IApplicationDescription> Applications {
70      get { return applications.Cast<IApplicationDescription>(); }
71    }
72
73    internal DefaultApplicationManager()
74      : base() {
75      loadedAssemblies = new Dictionary<string, Assembly>();
76      loadedPlugins = new List<IPlugin>();
77      AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
78        if (loadedAssemblies.ContainsKey(args.Name)) {
79          return loadedAssemblies[args.Name];
80        }
81        return null;
82      };
83    }
84
85    /// <summary>
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          var asm = Assembly.LoadFrom(fileName);
107          RegisterLoadedAssembly(asm);
108          // instantiate and load all plugins in this assembly
109          foreach (var plugin in GetInstances<IPlugin>(asm)) {
110            plugin.OnLoad();
111            loadedPlugins.Add(plugin);
112          }
113        }
114        OnPluginLoaded(new PluginInfrastructureEventArgs(desc));
115        desc.Load();
116      }
117    }
118
119    /// <summary>
120    /// Runs the application declared in <paramref name="appInfo"/>.
121    /// This is a synchronous call. When the application is terminated all plugins are unloaded.
122    /// </summary>
123    /// <param name="appInfo">Description of the application to run</param>
124    internal void Run(ApplicationDescription appInfo) {
125      IApplication runnablePlugin = (IApplication)Activator.CreateInstance(appInfo.DeclaringAssemblyName, appInfo.DeclaringTypeName).Unwrap();
126      try {
127        runnablePlugin.Run();
128      }
129      finally {
130        // unload plugins in reverse order
131        foreach (var plugin in loadedPlugins.Reverse<IPlugin>()) {
132          plugin.OnUnload();
133        }
134        foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(plugins.Where(x => x.PluginState != PluginState.Disabled))) {
135          desc.Unload();
136          OnPluginUnloaded(new PluginInfrastructureEventArgs(desc));
137        }
138      }
139    }
140
141    /// <summary>
142    /// Loads raw assemblies dynamically from a byte array
143    /// </summary>
144    /// <param name="assemblies">bytearray of all raw assemblies that should be loaded</param>
145    internal void LoadAssemblies(IEnumerable<byte[]> assemblies) {
146      foreach (byte[] asm in assemblies) {
147        Assembly loadedAsm = Assembly.Load(asm);
148        RegisterLoadedAssembly(loadedAsm);
149      }
150    }
151
152    // register assembly in the assembly cache for the AssemblyResolveEvent
153    private void RegisterLoadedAssembly(Assembly asm) {
154      loadedAssemblies.Add(asm.FullName, asm);
155      loadedAssemblies.Add(asm.GetName().Name, asm); // add short name
156    }
157
158    /// <summary>
159    /// Creates an instance of all types that are subtypes or the same type of the specified type and declared in <paramref name="plugin"/>
160    /// </summary>
161    /// <typeparam name="T">Most general type.</typeparam>
162    /// <returns>Enumerable of the created instances.</returns>
163    internal static IEnumerable<T> GetInstances<T>(IPluginDescription plugin) where T : class {
164      return from t in GetTypes(typeof(T), plugin, true)
165             select (T)Activator.CreateInstance(t);
166    }
167    /// <summary>
168    /// 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"/>.
169    /// </summary>
170    /// <typeparam name="T">Most general type.</typeparam>
171    /// <param name="asm">Declaring assembly.</param>
172    /// <returns>Enumerable of the created instances.</returns>
173    private static IEnumerable<T> GetInstances<T>(Assembly asm) where T : class {
174      return from t in GetTypes(typeof(T), asm, true)
175             select (T)Activator.CreateInstance(t);
176    }
177    /// <summary>
178    /// Creates an instance of all types that are subtypes or the same type of the specified type
179    /// </summary>
180    /// <typeparam name="T">Most general type.</typeparam>
181    /// <returns>Enumerable of the created instances.</returns>
182    internal static IEnumerable<T> GetInstances<T>() where T : class {
183      return from i in GetInstances(typeof(T))
184             select (T)i;
185    }
186
187    /// <summary>
188    /// Creates an instance of all types that are subtypes or the same type of the specified type
189    /// </summary>
190    /// <param name="type">Most general type.</param>
191    /// <returns>Enumerable of the created instances.</returns>
192    internal static IEnumerable<object> GetInstances(Type type) {
193      return (from t in GetTypes(type, true)
194              select Activator.CreateInstance(t)).ToList();
195    }
196
197    /// <summary>
198    /// Finds all types that are subtypes or equal to the specified type.
199    /// </summary>
200    /// <param name="type">Most general type for which to find matching types.</param>
201    /// <param name="onlyInstantiable">Return only types that are instantiable
202    /// (interfaces, abstract classes... are not returned)</param>
203    /// <returns>Enumerable of the discovered types.</returns>
204    internal static IEnumerable<Type> GetTypes(Type type, bool onlyInstantiable) {
205      return from asm in AppDomain.CurrentDomain.GetAssemblies()
206             from t in GetTypes(type, asm, onlyInstantiable)
207             select t;
208    }
209
210    /// <summary>
211    /// Finds all types that are subtypes or equal to the specified type if they are part of the given
212    /// <paramref name="pluginDescription"/>.
213    /// </summary>
214    /// <param name="type">Most general type for which to find matching types.</param>
215    /// <param name="pluginDescription">The plugin the subtypes must be part of.</param>
216    /// <param name="onlyInstantiable">Return only types that are instantiable
217    /// (interfaces, abstract classes... are not returned)</param>
218    /// <returns>Enumerable of the discovered types.</returns>
219    internal static IEnumerable<Type> GetTypes(Type type, IPluginDescription pluginDescription, bool onlyInstantiable) {
220      PluginDescription pluginDesc = (PluginDescription)pluginDescription;
221      return from asm in AppDomain.CurrentDomain.GetAssemblies()
222             where !IsDynamicAssembly(asm)
223             where pluginDesc.AssemblyLocations.Any(location => location.Equals(Path.GetFullPath(asm.Location), StringComparison.CurrentCultureIgnoreCase))
224             from t in GetTypes(type, asm, onlyInstantiable)
225             select t;
226    }
227
228    private static bool IsDynamicAssembly(Assembly asm) {
229      return (asm is System.Reflection.Emit.AssemblyBuilder) || string.IsNullOrEmpty(asm.Location);
230    }
231
232    /// <summary>
233    /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
234    /// </summary>
235    /// <param name="type">Most general type we want to find.</param>
236    /// <param name="assembly">Assembly that should be searched for types.</param>
237    /// <param name="onlyInstantiable">Return only types that are instantiable
238    /// (interfaces, abstract classes...  are not returned)</param>
239    /// <returns>Enumerable of the discovered types.</returns>
240    private static IEnumerable<Type> GetTypes(Type type, Assembly assembly, bool onlyInstantiable) {
241      return from t in assembly.GetTypes()
242             where type.IsAssignableFrom(t)
243             where onlyInstantiable == false || (!t.IsAbstract && !t.IsInterface && !t.HasElementType)
244             select t;
245    }
246
247    private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
248      if (PluginLoaded != null) PluginLoaded(this, e);
249    }
250
251    private void OnPluginUnloaded(PluginInfrastructureEventArgs e) {
252      if (PluginUnloaded != null) PluginUnloaded(this, e);
253    }
254
255    // infinite lease time
256    /// <summary>
257    /// Initializes the life time service with infinite lease time.
258    /// </summary>
259    /// <returns><c>null</c>.</returns>
260    public override object InitializeLifetimeService() {
261      return null;
262    }
263
264    #region IApplicationManager Members
265
266    IEnumerable<T> IApplicationManager.GetInstances<T>() {
267      return GetInstances<T>();
268    }
269
270    IEnumerable<object> IApplicationManager.GetInstances(Type type) {
271      return GetInstances(type);
272    }
273
274    IEnumerable<Type> IApplicationManager.GetTypes(Type type) {
275      return GetTypes(type, true);
276    }
277
278    IEnumerable<Type> IApplicationManager.GetTypes(Type type, bool onlyInstantiable) {
279      return GetTypes(type, onlyInstantiable);
280    }
281
282    IEnumerable<Type> IApplicationManager.GetTypes(Type type, IPluginDescription plugin) {
283      return GetTypes(type, plugin, true);
284    }
285
286    IEnumerable<Type> IApplicationManager.GetTypes(Type type, IPluginDescription plugin, bool onlyInstantiable) {
287      return GetTypes(type, plugin, onlyInstantiable);
288    }
289
290
291    /// <summary>
292    /// Finds the plugin that declares the <paramref name="type">type</paramref>.
293    /// </summary>
294    /// <param name="type">The type of interest.</param>
295    /// <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>
296    public IPluginDescription GetDeclaringPlugin(Type type) {
297      foreach (PluginDescription info in Plugins) {
298        if (info.AssemblyLocations.Contains(Path.GetFullPath(type.Assembly.Location))) return info;
299      }
300      return null;
301    }
302    #endregion
303  }
304}
Note: See TracBrowser for help on using the repository browser.