#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Security.Policy;
using System.Reflection;
using System.Diagnostics;
using System.Security.Permissions;
using System.Security;
using System.Linq;
using HeuristicLab.PluginInfrastructure.Manager;
using System.IO;
namespace HeuristicLab.PluginInfrastructure {
///
/// The ApplicationManager has a reference to the application manager singleton.
/// The application manager provides
///
public sealed class ApplicationManager : MarshalByRefObject, IApplicationManager {
private static IApplicationManager appManager;
///
/// Gets the application manager singleton.
///
public static IApplicationManager Manager {
get { return appManager; }
}
internal event EventHandler PluginLoaded;
internal event EventHandler PluginUnloaded;
// cache for the AssemblyResolveEvent
// which must be handled when assemblies are loaded dynamically after the application start
private Dictionary loadedAssemblies;
private List loadedPlugins;
private List plugins;
///
/// Gets all plugins.
///
public IEnumerable Plugins {
get { return plugins.Cast(); }
}
private List applications;
///
/// Gets all installed applications.
///
public IEnumerable Applications {
get { return applications.Cast(); }
}
internal ApplicationManager()
: base() {
loadedAssemblies = new Dictionary();
loadedPlugins = new List();
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
if (loadedAssemblies.ContainsKey(args.Name)) {
return loadedAssemblies[args.Name];
}
return null;
};
}
internal void PrepareApplicationDomain(IEnumerable apps, IEnumerable plugins) {
this.plugins = new List(plugins);
this.applications = new List(apps);
RegisterApplicationManager((IApplicationManager)this);
LoadPlugins(plugins);
}
///
/// Registers a new application manager.
///
///
internal static void RegisterApplicationManager(IApplicationManager manager) {
if (appManager != null) throw new InvalidOperationException("The application manager has already been set.");
appManager = manager;
}
private void LoadPlugins(IEnumerable plugins) {
// load all loadable plugins (all dependencies available) into the execution context
foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(plugins.Where(x => x.PluginState != PluginState.Disabled))) {
foreach (string fileName in desc.AssemblyLocations) {
var asm = Assembly.LoadFrom(fileName);
RegisterLoadedAssembly(asm);
// instantiate and load all plugins in this assembly
foreach (var plugin in GetInstances(asm)) {
plugin.OnLoad();
loadedPlugins.Add(plugin);
}
}
OnPluginLoaded(new PluginInfrastructureEventArgs("Plugin loaded", desc));
desc.Load();
}
}
internal void Run(ApplicationDescription appInfo) {
IApplication runnablePlugin = (IApplication)Activator.CreateInstance(appInfo.DeclaringAssemblyName, appInfo.DeclaringTypeName).Unwrap();
try {
runnablePlugin.Run();
}
finally {
// unload plugins in reverse order
foreach (var plugin in loadedPlugins.Reverse()) {
plugin.OnUnload();
}
foreach (var desc in PluginDescriptionIterator.IterateDependenciesBottomUp(plugins.Where(x => x.PluginState != PluginState.Disabled))) {
desc.Unload();
OnPluginUnloaded(new PluginInfrastructureEventArgs("Plugin unloaded", desc));
}
}
}
///
/// Loads assemblies dynamically from a byte array
///
/// bytearray of all assemblies that should be loaded
internal void LoadAssemblies(IEnumerable assemblies) {
foreach (byte[] asm in assemblies) {
Assembly loadedAsm = Assembly.Load(asm);
RegisterLoadedAssembly(loadedAsm);
}
}
// register assembly in the assembly cache for the AssemblyResolveEvent
private void RegisterLoadedAssembly(Assembly asm) {
loadedAssemblies.Add(asm.FullName, asm);
loadedAssemblies.Add(asm.GetName().Name, asm); // add short name
}
///
/// Creates an instance of all types that are subtypes or the same type of the specified type and declared in
///
/// Most general type.
/// Enumerable of the created instances.
internal static IEnumerable GetInstances(IPluginDescription plugin) where T : class {
return from t in GetTypes(typeof(T), plugin, true)
select (T)Activator.CreateInstance(t);
}
///
/// Creates an instance of all types declared in assembly that are subtypes or the same type of the specified type and declared in
///
/// Most general type.
/// Declaring assembly.
/// Enumerable of the created instances.
private static IEnumerable GetInstances(Assembly asm) where T : class {
return from t in GetTypes(typeof(T), asm, true)
select (T)Activator.CreateInstance(t);
}
///
/// Creates an instance of all types that are subtypes or the same type of the specified type
///
/// Most general type.
/// Enumerable of the created instances.
internal static IEnumerable GetInstances() where T : class {
return from i in GetInstances(typeof(T))
select (T)i;
}
///
/// Creates an instance of all types that are subtypes or the same type of the specified type
///
/// Most general type.
/// Enumerable of the created instances.
internal static IEnumerable