#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;
namespace HeuristicLab.PluginInfrastructure {
// must extend MarshalByRefObject because of event passing between Loader and PluginManager (each in it's own AppDomain)
public class PluginManager : MarshalByRefObject {
// singleton: only one manager allowed in each AppDomain
private static readonly PluginManager manager = PluginManager.manager==null?new PluginManager():PluginManager.manager;
static PluginManager() { Console.WriteLine("static ctor called "); }
public static PluginManager Manager {
get {
return manager;
}
}
// singleton: only one control manager allowed in each application (i.e. AppDomain)
private static IControlManager controlManager;
public static IControlManager ControlManager {
get { return controlManager; }
set { controlManager = value; }
}
public event PluginManagerActionEventHandler Action;
// holds a proxy for the loader in the special AppDomain for PluginManagament
private Loader remoteLoader;
private AppDomain pluginDomain;
private string pluginDir;
// singleton pattern
private PluginManager() {
this.pluginDir = HeuristicLab.PluginInfrastructure.Properties.Settings.Default.PluginDir;
}
public ICollection InstalledPlugins {
get { return remoteLoader.InstalledPlugins; }
}
public ICollection DisabledPlugins {
get { return remoteLoader.DisabledPlugins; }
}
public ICollection ActivePlugins {
get { return remoteLoader.ActivePlugins; }
}
public ICollection InstalledApplications {
get { return remoteLoader.InstalledApplications; }
}
private ICollection loadedPlugins;
public ICollection LoadedPlugins {
get {
Console.WriteLine(AppDomain.CurrentDomain.Id);
Console.WriteLine(this.GetHashCode());
if(loadedPlugins != null) Console.WriteLine(loadedPlugins.Count); else Console.WriteLine("Loaded plugins==null!");
return loadedPlugins;
}
internal set {
Console.WriteLine(AppDomain.CurrentDomain.Id);
Console.WriteLine(value.Count);
Console.WriteLine(this.GetHashCode());
loadedPlugins = new List(value);
}
}
///
/// Creates a dedicated AppDomain for loading all plugins and checking dependencies.
///
public void Initialize() {
NotifyListeners(PluginManagerAction.Initializing, "-");
AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
setup.PrivateBinPath = pluginDir;
pluginDomain = AppDomain.CreateDomain("plugin domain");
pluginDomain.SetupInformation.PrivateBinPath = pluginDir;
remoteLoader = (Loader)pluginDomain.CreateInstanceAndUnwrap("HeuristicLab.PluginInfraStructure", "HeuristicLab.PluginInfrastructure.Loader");
remoteLoader.PluginAction += new PluginManagerActionEventHandler(remoteLoader_PluginAction);
remoteLoader.Init();
NotifyListeners(PluginManagerAction.Initialized, "-");
}
public void remoteLoader_PluginAction(object sender, PluginManagerActionEventArgs e) {
if(Action != null) Action(this, e);
}
///
/// Creates a separate AppDomain.
/// Loads all active plugin assemblies and starts the application in the new AppDomain via a PluginRunner instance activated in the new AppDomain
///
/// application to run
public void Run(ApplicationInfo appInfo) {
// create a separate AppDomain for the application
// activate a PluginRunner instance in the application
// and remotely tell it to start the application
Console.WriteLine(appInfo);
NotifyListeners(PluginManagerAction.Starting, appInfo.Name);
AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
setup.PrivateBinPath = pluginDir;
AppDomain applicationDomain = AppDomain.CreateDomain(appInfo.Name + " AppDomain");
applicationDomain.SetupInformation.PrivateBinPath = pluginDir;
try {
Runner remoteRunner = (Runner)applicationDomain.CreateInstanceAndUnwrap("HeuristicLab.PluginInfrastructure", "HeuristicLab.PluginInfrastructure.Runner");
NotifyListeners(PluginManagerAction.Initializing, "All plugins");
remoteRunner.LoadPlugins(remoteLoader.ActivePlugins);
NotifyListeners(PluginManagerAction.Initialized, "All plugins");
remoteRunner.Run(appInfo.PluginAssembly, appInfo.PluginType);
} finally {
// make sure domain is unloaded in all cases
AppDomain.Unload(applicationDomain);
}
}
///
/// Calculates a set of plugins that directly or transitively depend on the plugin given in the argument.
///
///
/// a list of plugins that are directly of transitively dependent.
public List GetDependentPlugins(PluginInfo pluginInfo) {
List mergedList = new List();
foreach(PluginInfo plugin in InstalledPlugins) {
if(plugin.Dependencies.Contains(pluginInfo)) {
if(!mergedList.Contains(plugin)) {
mergedList.Add(plugin);
}
// for each of the dependent plugins add the list of transitively dependent plugins
// make sure that only one entry for each plugin is added to the merged list
GetDependentPlugins(plugin).ForEach(delegate(PluginInfo dependentPlugin) {
if(!mergedList.Contains(dependentPlugin)) {
mergedList.Add(dependentPlugin);
}
});
}
}
return mergedList;
}
public void UnloadAllPlugins() {
AppDomain.Unload(pluginDomain);
}
public void LoadAllPlugins() {
Initialize();
}
public void OnDelete(PluginInfo pluginInfo) {
remoteLoader.OnDelete(pluginInfo);
}
public void OnInstall(PluginInfo pluginInfo) {
remoteLoader.OnInstall(pluginInfo);
}
public void OnPreUpdate(PluginInfo pluginInfo) {
remoteLoader.OnPreUpdate(pluginInfo);
}
public void OnPostUpdate(PluginInfo pluginInfo) {
remoteLoader.OnPostUpdate(pluginInfo);
}
private void NotifyListeners(PluginManagerAction action, string text) {
if(Action != null) {
Action(this, new PluginManagerActionEventArgs(text, action));
}
}
}
}