[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[882] | 24 | using System.Security.Policy;
|
---|
| 25 | using System.Reflection;
|
---|
| 26 | using System.Diagnostics;
|
---|
| 27 | using System.Security.Permissions;
|
---|
| 28 | using System.Security;
|
---|
[2] | 29 |
|
---|
| 30 | namespace HeuristicLab.PluginInfrastructure {
|
---|
| 31 |
|
---|
| 32 | // must extend MarshalByRefObject because of event passing between Loader and PluginManager (each in it's own AppDomain)
|
---|
[1189] | 33 | /// <summary>
|
---|
| 34 | /// Class to manage different plugins.
|
---|
| 35 | /// </summary>
|
---|
[2] | 36 | public class PluginManager : MarshalByRefObject {
|
---|
| 37 |
|
---|
| 38 | // singleton: only one manager allowed in each AppDomain
|
---|
| 39 | private static PluginManager manager = new PluginManager();
|
---|
[1189] | 40 | /// <summary>
|
---|
| 41 | /// Gets the plugin manager (is a singleton).
|
---|
| 42 | /// </summary>
|
---|
[2] | 43 | public static PluginManager Manager {
|
---|
| 44 | get { return manager; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | // singleton: only one control manager allowed in each applicatoin (i.e. AppDomain)
|
---|
| 48 | private static IControlManager controlManager;
|
---|
[1189] | 49 | /// <summary>
|
---|
| 50 | /// Gets or sets the control manager (is a singleton).
|
---|
| 51 | /// </summary>
|
---|
[2] | 52 | public static IControlManager ControlManager {
|
---|
| 53 | get { return controlManager; }
|
---|
| 54 | set { controlManager = value; }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[1189] | 57 | /// <summary>
|
---|
| 58 | /// Event handler for actions in the plugin manager.
|
---|
| 59 | /// </summary>
|
---|
[2] | 60 | public event PluginManagerActionEventHandler Action;
|
---|
| 61 |
|
---|
| 62 | // holds a proxy for the loader in the special AppDomain for PluginManagament
|
---|
| 63 | private Loader remoteLoader;
|
---|
| 64 | private AppDomain pluginDomain;
|
---|
| 65 | private string pluginDir;
|
---|
| 66 |
|
---|
| 67 | // singleton pattern
|
---|
| 68 | private PluginManager() {
|
---|
| 69 | this.pluginDir = HeuristicLab.PluginInfrastructure.Properties.Settings.Default.PluginDir;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[1189] | 72 | /// <summary>
|
---|
| 73 | /// Gets all installed plugins.
|
---|
| 74 | /// </summary>
|
---|
| 75 | /// <remarks>This information is provided by a <see cref="Loader"/>.</remarks>
|
---|
[29] | 76 | public ICollection<PluginInfo> InstalledPlugins {
|
---|
[2] | 77 | get { return remoteLoader.InstalledPlugins; }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[1189] | 80 | /// <summary>
|
---|
| 81 | /// Gets all disabled plugins.
|
---|
| 82 | /// </summary>
|
---|
| 83 | /// <remarks>This information is provided by a <see cref="Loader"/>.</remarks>
|
---|
[29] | 84 | public ICollection<PluginInfo> DisabledPlugins {
|
---|
| 85 | get { return remoteLoader.DisabledPlugins; }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[1189] | 88 | /// <summary>
|
---|
| 89 | /// Gets all active plugins.
|
---|
| 90 | /// </summary>
|
---|
| 91 | /// <remarks>This information is provided by a <see cref="Loader"/>.</remarks>
|
---|
[29] | 92 | public ICollection<PluginInfo> ActivePlugins {
|
---|
| 93 | get { return remoteLoader.ActivePlugins; }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[1189] | 96 | /// <summary>
|
---|
| 97 | /// Gets all installed applications.
|
---|
| 98 | /// </summary>
|
---|
| 99 | /// <remarks>This information is provided by a <see cref="Loader"/>.</remarks>
|
---|
[29] | 100 | public ICollection<ApplicationInfo> InstalledApplications {
|
---|
[2] | 101 | get { return remoteLoader.InstalledApplications; }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[29] | 104 | private ICollection<PluginInfo> loadedPlugins;
|
---|
[1189] | 105 | /// <summary>
|
---|
| 106 | /// Gets or (internally) sets the loaded plugins.
|
---|
| 107 | /// </summary>
|
---|
[29] | 108 | public ICollection<PluginInfo> LoadedPlugins {
|
---|
[2] | 109 | get { return loadedPlugins; }
|
---|
| 110 | internal set { loadedPlugins = value; }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /// <summary>
|
---|
| 114 | /// Creates a dedicated AppDomain for loading all plugins and checking dependencies.
|
---|
| 115 | /// </summary>
|
---|
| 116 | public void Initialize() {
|
---|
| 117 | NotifyListeners(PluginManagerAction.Initializing, "-");
|
---|
| 118 | AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
|
---|
| 119 | setup.PrivateBinPath = pluginDir;
|
---|
| 120 | pluginDomain = AppDomain.CreateDomain("plugin domain", null, setup);
|
---|
| 121 | remoteLoader = (Loader)pluginDomain.CreateInstanceAndUnwrap("HeuristicLab.PluginInfraStructure", "HeuristicLab.PluginInfrastructure.Loader");
|
---|
[766] | 122 | remoteLoader.PluginAction += delegate(object sender, PluginManagerActionEventArgs args) { if (Action != null) Action(this, args); };
|
---|
[2] | 123 | remoteLoader.Init();
|
---|
| 124 | NotifyListeners(PluginManagerAction.Initialized, "-");
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /// <summary>
|
---|
| 128 | /// Creates a separate AppDomain.
|
---|
| 129 | /// Loads all active plugin assemblies and starts the application in the new AppDomain via a PluginRunner instance activated in the new AppDomain
|
---|
| 130 | /// </summary>
|
---|
| 131 | /// <param name="appInfo">application to run</param>
|
---|
| 132 | public void Run(ApplicationInfo appInfo) {
|
---|
| 133 | // create a separate AppDomain for the application
|
---|
| 134 | // activate a PluginRunner instance in the application
|
---|
| 135 | // and remotely tell it to start the application
|
---|
| 136 |
|
---|
| 137 | NotifyListeners(PluginManagerAction.Starting, appInfo.Name);
|
---|
[766] | 138 | AppDomain applicationDomain = null;
|
---|
[241] | 139 | try {
|
---|
[1470] | 140 | applicationDomain = CreateAndInitAppDomain(appInfo.Name);
|
---|
[766] | 141 | Runner remoteRunner = (Runner)applicationDomain.CreateInstanceAndUnwrap(typeof(Runner).Assembly.GetName().Name, typeof(Runner).FullName);
|
---|
[241] | 142 | remoteRunner.Run(appInfo);
|
---|
[766] | 143 | }
|
---|
| 144 | finally {
|
---|
[241] | 145 | // make sure domain is unloaded in all cases
|
---|
[766] | 146 | if (applicationDomain != null) AppDomain.Unload(applicationDomain);
|
---|
[241] | 147 | }
|
---|
[2] | 148 | }
|
---|
| 149 |
|
---|
| 150 | /// <summary>
|
---|
[766] | 151 | /// Creates a new AppDomain with all plugins preloaded.
|
---|
| 152 | /// </summary>
|
---|
| 153 | /// <param name="friendlyName">Name of the new AppDomain</param>
|
---|
| 154 | /// <returns>the new AppDomain with all plugins preloaded.</returns>
|
---|
| 155 | public AppDomain CreateAndInitAppDomain(string friendlyName) {
|
---|
| 156 | AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
|
---|
| 157 | setup.PrivateBinPath = pluginDir;
|
---|
| 158 | AppDomain applicationDomain = AppDomain.CreateDomain(friendlyName, null, setup);
|
---|
| 159 | Runner remoteRunner = (Runner)applicationDomain.CreateInstanceAndUnwrap(typeof(Runner).Assembly.GetName().Name, typeof(Runner).FullName);
|
---|
| 160 | NotifyListeners(PluginManagerAction.Initializing, "All plugins");
|
---|
| 161 | if (remoteLoader != null) {
|
---|
| 162 | remoteRunner.LoadPlugins(remoteLoader.ActivePlugins);
|
---|
| 163 | } else if (LoadedPlugins != null && LoadedPlugins.Count > 0) {
|
---|
| 164 | remoteRunner.LoadPlugins(LoadedPlugins);
|
---|
| 165 | }
|
---|
| 166 | NotifyListeners(PluginManagerAction.Initialized, "All plugins");
|
---|
| 167 | return applicationDomain;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[882] | 170 | /// <summary>
|
---|
[1189] | 171 | /// Creates a new AppDomain with all plugins preloaded and Sandboxing capability.
|
---|
[882] | 172 | /// </summary>
|
---|
| 173 | /// <param name="assembly">Assembly reference</param>
|
---|
| 174 | /// <returns>the strongname of the assembly</returns>
|
---|
| 175 | private StrongName CreateStrongName(Assembly assembly) {
|
---|
| 176 | if (assembly == null)
|
---|
| 177 | throw new ArgumentNullException("assembly");
|
---|
[766] | 178 |
|
---|
[882] | 179 | AssemblyName assemblyName = assembly.GetName();
|
---|
| 180 | Debug.Assert(assemblyName != null, "Could not get assembly name");
|
---|
| 181 |
|
---|
| 182 | // get the public key blob
|
---|
| 183 | byte[] publicKey = assemblyName.GetPublicKey();
|
---|
| 184 | if (publicKey == null || publicKey.Length == 0)
|
---|
| 185 | throw new InvalidOperationException("Assembly is not strongly named");
|
---|
| 186 |
|
---|
| 187 | StrongNamePublicKeyBlob keyBlob = new StrongNamePublicKeyBlob(publicKey);
|
---|
| 188 |
|
---|
| 189 | // and create the StrongName
|
---|
| 190 | return new StrongName(keyBlob, assemblyName.Name, assemblyName.Version);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[1602] | 193 | public AppDomain CreateAndInitAppDomainWithSandbox(string friendlyName, bool sandboxed, Type jobType, ICollection<byte[]> assemblyFiles) {
|
---|
[997] | 194 | PermissionSet pset;
|
---|
[882] | 195 |
|
---|
[1655] | 196 |
|
---|
| 197 |
|
---|
[1715] | 198 | //DiscoveryService dService = new DiscoveryService();
|
---|
[997] | 199 | //get the declaring plugin of the job
|
---|
[1715] | 200 | //PluginInfo jobPlugin = dService.GetDeclaringPlugin(jobType);
|
---|
[997] | 201 |
|
---|
| 202 | //get all the plugins that have dependencies with the jobplugin
|
---|
[1715] | 203 | //List<PluginInfo> depPlugins = GetDependentPluginsRec(jobPlugin);
|
---|
[997] | 204 | //insert all jobs into one list
|
---|
[1715] | 205 | //depPlugins.Add(jobPlugin);
|
---|
[997] | 206 |
|
---|
[882] | 207 | if (sandboxed) {
|
---|
| 208 | pset = new PermissionSet(PermissionState.None);
|
---|
[997] | 209 | pset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
|
---|
| 210 | pset.AddPermission(new ReflectionPermission(PermissionState.Unrestricted));
|
---|
[1199] | 211 | FileIOPermission fPerm = new FileIOPermission(PermissionState.None);
|
---|
| 212 |
|
---|
[1715] | 213 | /*foreach (PluginInfo plugin in depPlugins) {
|
---|
[997] | 214 | foreach(String assemblies in plugin.Assemblies)
|
---|
[1199] | 215 | fPerm.AddPathList(FileIOPermissionAccess.AllAccess, assemblies);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[1715] | 218 | pset.AddPermission(fPerm);*/
|
---|
[1199] | 219 |
|
---|
[882] | 220 | } else {
|
---|
| 221 | pset = new PermissionSet(PermissionState.Unrestricted);
|
---|
| 222 | }
|
---|
| 223 | AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
|
---|
| 224 | setup.PrivateBinPath = pluginDir;
|
---|
| 225 | setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
|
---|
| 226 | AppDomain applicationDomain = AppDomain.CreateDomain(friendlyName, AppDomain.CurrentDomain.Evidence, setup, pset, CreateStrongName(Assembly.GetExecutingAssembly()));
|
---|
| 227 | Runner remoteRunner = (Runner)applicationDomain.CreateInstanceAndUnwrap(typeof(Runner).Assembly.GetName().Name, typeof(Runner).FullName);
|
---|
| 228 | NotifyListeners(PluginManagerAction.Initializing, "All plugins");
|
---|
[886] | 229 |
|
---|
[1936] | 230 | if (assemblyFiles != null && assemblyFiles.Count > 0)
|
---|
[1990] | 231 | remoteRunner.LoadAssemblies(assemblyFiles);
|
---|
[1655] | 232 |
|
---|
[1755] | 233 | //if (depPlugins != null && depPlugins.Count > 0) {
|
---|
[1936] | 234 | //remoteRunner.LoadPlugins(ActivePlugins);
|
---|
[1755] | 235 | //}
|
---|
[1936] | 236 | NotifyListeners(PluginManagerAction.Initialized, "All plugins");
|
---|
[882] | 237 | return applicationDomain;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[766] | 240 | /// <summary>
|
---|
[2] | 241 | /// Calculates a set of plugins that directly or transitively depend on the plugin given in the argument.
|
---|
| 242 | /// </summary>
|
---|
[1189] | 243 | /// <param name="pluginInfo">The plugin the other depend on.</param>
|
---|
[2] | 244 | /// <returns>a list of plugins that are directly of transitively dependent.</returns>
|
---|
| 245 | public List<PluginInfo> GetDependentPlugins(PluginInfo pluginInfo) {
|
---|
| 246 | List<PluginInfo> mergedList = new List<PluginInfo>();
|
---|
[1501] | 247 | foreach (PluginInfo plugin in InstalledPlugins) {
|
---|
| 248 | if (plugin.Dependencies.Contains(pluginInfo)) {
|
---|
| 249 | if (!mergedList.Contains(plugin)) {
|
---|
| 250 | mergedList.Add(plugin);
|
---|
| 251 | }
|
---|
| 252 | // for each of the dependent plugins add the list of transitively dependent plugins
|
---|
| 253 | // make sure that only one entry for each plugin is added to the merged list
|
---|
| 254 | GetDependentPlugins(plugin).ForEach(delegate(PluginInfo dependentPlugin) {
|
---|
| 255 | if (!mergedList.Contains(dependentPlugin)) {
|
---|
| 256 | mergedList.Add(dependentPlugin);
|
---|
| 257 | }
|
---|
| 258 | });
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 | return mergedList;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | /// <summary>
|
---|
[1189] | 265 | /// Unloads all plugins.
|
---|
| 266 | /// </summary>
|
---|
[2] | 267 | public void UnloadAllPlugins() {
|
---|
| 268 | AppDomain.Unload(pluginDomain);
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[1189] | 271 | /// <summary>
|
---|
| 272 | /// Loads all plugins.
|
---|
| 273 | /// </summary>
|
---|
[2] | 274 | public void LoadAllPlugins() {
|
---|
| 275 | Initialize();
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[1189] | 278 | /// <inheritdoc cref="Loader.OnDelete"/>
|
---|
[2] | 279 | public void OnDelete(PluginInfo pluginInfo) {
|
---|
| 280 | remoteLoader.OnDelete(pluginInfo);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[1189] | 283 | /// <inheritdoc cref="Loader.OnInstall"/>
|
---|
[2] | 284 | public void OnInstall(PluginInfo pluginInfo) {
|
---|
| 285 | remoteLoader.OnInstall(pluginInfo);
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[1189] | 288 | /// <inheritdoc cref="Loader.OnPreUpdate"/>
|
---|
[2] | 289 | public void OnPreUpdate(PluginInfo pluginInfo) {
|
---|
| 290 | remoteLoader.OnPreUpdate(pluginInfo);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[1189] | 293 | /// <inheritdoc cref="Loader.OnPostUpdate"/>
|
---|
[2] | 294 | public void OnPostUpdate(PluginInfo pluginInfo) {
|
---|
| 295 | remoteLoader.OnPostUpdate(pluginInfo);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | private void NotifyListeners(PluginManagerAction action, string text) {
|
---|
[766] | 299 | if (Action != null) {
|
---|
[2] | 300 | Action(this, new PluginManagerActionEventArgs(text, action));
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 | }
|
---|