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