[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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;
|
---|
[2488] | 29 | using System.Linq;
|
---|
[2] | 30 |
|
---|
[2481] | 31 | namespace HeuristicLab.PluginInfrastructure.Manager {
|
---|
[2] | 32 |
|
---|
| 33 | // must extend MarshalByRefObject because of event passing between Loader and PluginManager (each in it's own AppDomain)
|
---|
[1189] | 34 | /// <summary>
|
---|
| 35 | /// Class to manage different plugins.
|
---|
| 36 | /// </summary>
|
---|
[2504] | 37 | internal sealed class PluginManager : MarshalByRefObject {
|
---|
[2922] | 38 | internal event EventHandler<PluginInfrastructureEventArgs> PluginLoaded;
|
---|
| 39 | internal event EventHandler<PluginInfrastructureEventArgs> PluginUnloaded;
|
---|
| 40 | internal event EventHandler<PluginInfrastructureEventArgs> Initializing;
|
---|
| 41 | internal event EventHandler<PluginInfrastructureEventArgs> Initialized;
|
---|
| 42 | internal event EventHandler<PluginInfrastructureEventArgs> ApplicationStarting;
|
---|
| 43 | internal event EventHandler<PluginInfrastructureEventArgs> ApplicationStarted;
|
---|
[2] | 44 |
|
---|
| 45 | private string pluginDir;
|
---|
| 46 |
|
---|
[2481] | 47 | private List<PluginDescription> plugins;
|
---|
[2513] | 48 | /// <summary>
|
---|
| 49 | /// Gets all installed plugins.
|
---|
| 50 | /// </summary>
|
---|
| 51 | internal IEnumerable<PluginDescription> Plugins {
|
---|
| 52 | get { return plugins; }
|
---|
| 53 | }
|
---|
[29] | 54 |
|
---|
[2481] | 55 | private List<ApplicationDescription> applications;
|
---|
[1189] | 56 | /// <summary>
|
---|
| 57 | /// Gets all installed applications.
|
---|
| 58 | /// </summary>
|
---|
[2504] | 59 | internal IEnumerable<ApplicationDescription> Applications {
|
---|
[2481] | 60 | get { return applications; }
|
---|
[2] | 61 | }
|
---|
| 62 |
|
---|
[2503] | 63 | private object locker = new object();
|
---|
| 64 | private bool initialized;
|
---|
| 65 |
|
---|
[2504] | 66 | internal PluginManager(string pluginDir) {
|
---|
[2503] | 67 | this.pluginDir = pluginDir;
|
---|
| 68 | plugins = new List<PluginDescription>();
|
---|
| 69 | applications = new List<ApplicationDescription>();
|
---|
| 70 | initialized = false;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[1189] | 73 | /// <summary>
|
---|
[2481] | 74 | /// Determines installed plugins and checks if all plugins are loadable.
|
---|
[1189] | 75 | /// </summary>
|
---|
[2504] | 76 | internal void DiscoverAndCheckPlugins() {
|
---|
[2922] | 77 | OnInitializing(PluginInfrastructureEventArgs.Empty);
|
---|
[2] | 78 | AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
|
---|
| 79 | setup.PrivateBinPath = pluginDir;
|
---|
[2497] | 80 | AppDomain pluginDomain = null;
|
---|
| 81 | try {
|
---|
| 82 | pluginDomain = AppDomain.CreateDomain("plugin domain", null, setup);
|
---|
[2504] | 83 | Type pluginValidatorType = typeof(PluginValidator);
|
---|
| 84 | PluginValidator remoteValidator = (PluginValidator)pluginDomain.CreateInstanceAndUnwrap(pluginValidatorType.Assembly.FullName, pluginValidatorType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null, null);
|
---|
[2503] | 85 | remoteValidator.PluginDir = pluginDir;
|
---|
| 86 | // forward all events from the remoteValidator to listeners
|
---|
| 87 | remoteValidator.PluginLoaded +=
|
---|
| 88 | delegate(object sender, PluginInfrastructureEventArgs e) {
|
---|
[2922] | 89 | OnPluginLoaded(e);
|
---|
[2503] | 90 | };
|
---|
| 91 | // get list of plugins and applications from the validator
|
---|
| 92 | plugins.Clear(); applications.Clear();
|
---|
| 93 | plugins.AddRange(remoteValidator.Plugins);
|
---|
| 94 | applications.AddRange(remoteValidator.Applications);
|
---|
[2497] | 95 | }
|
---|
| 96 | finally {
|
---|
| 97 | // discard the AppDomain that was used for plugin discovery
|
---|
[2503] | 98 | AppDomain.Unload(pluginDomain);
|
---|
| 99 | // unload all plugins
|
---|
[2922] | 100 | foreach (var pluginDescription in plugins.Where(x => x.PluginState == PluginState.Loaded)) {
|
---|
[2503] | 101 | pluginDescription.Unload();
|
---|
[2922] | 102 | OnPluginUnloaded(new PluginInfrastructureEventArgs(pluginDescription));
|
---|
| 103 | }
|
---|
[2503] | 104 | initialized = true;
|
---|
[2922] | 105 | OnInitialized(PluginInfrastructureEventArgs.Empty);
|
---|
[2497] | 106 | }
|
---|
[2] | 107 | }
|
---|
| 108 |
|
---|
[2481] | 109 |
|
---|
[2] | 110 | /// <summary>
|
---|
[2592] | 111 | /// Starts an application in a separate AppDomain.
|
---|
| 112 | /// Loads all enabled plugins and starts the application via an ApplicationManager instance activated in the new AppDomain.
|
---|
[2] | 113 | /// </summary>
|
---|
| 114 | /// <param name="appInfo">application to run</param>
|
---|
[2504] | 115 | internal void Run(ApplicationDescription appInfo) {
|
---|
[2503] | 116 | if (!initialized) throw new InvalidOperationException("PluginManager is not initialized. DiscoverAndCheckPlugins() must be called before Run()");
|
---|
[2] | 117 | // create a separate AppDomain for the application
|
---|
[2488] | 118 | // initialize the static ApplicationManager in the AppDomain
|
---|
[2] | 119 | // and remotely tell it to start the application
|
---|
| 120 |
|
---|
[2922] | 121 | OnApplicationStarting(new PluginInfrastructureEventArgs(appInfo));
|
---|
[766] | 122 | AppDomain applicationDomain = null;
|
---|
[241] | 123 | try {
|
---|
[2481] | 124 | AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
|
---|
| 125 | setup.PrivateBinPath = pluginDir;
|
---|
[2738] | 126 | applicationDomain = AppDomain.CreateDomain(AppDomain.CurrentDomain.FriendlyName, null, setup);
|
---|
[2527] | 127 | Type applicationManagerType = typeof(ApplicationManager);
|
---|
| 128 | ApplicationManager applicationManager =
|
---|
| 129 | (ApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null, null);
|
---|
[2497] | 130 | applicationManager.PluginLoaded += applicationManager_PluginLoaded;
|
---|
[2503] | 131 | applicationManager.PluginUnloaded += applicationManager_PluginUnloaded;
|
---|
[2504] | 132 | applicationManager.PrepareApplicationDomain(applications, plugins);
|
---|
[2922] | 133 | OnApplicationStarted(new PluginInfrastructureEventArgs(appInfo));
|
---|
[2488] | 134 | applicationManager.Run(appInfo);
|
---|
[766] | 135 | }
|
---|
| 136 | finally {
|
---|
[241] | 137 | // make sure domain is unloaded in all cases
|
---|
[2503] | 138 | AppDomain.Unload(applicationDomain);
|
---|
[241] | 139 | }
|
---|
[2] | 140 | }
|
---|
| 141 |
|
---|
[2503] | 142 | private void applicationManager_PluginUnloaded(object sender, PluginInfrastructureEventArgs e) {
|
---|
| 143 | // unload the matching plugin description (
|
---|
[2497] | 144 | PluginDescription desc = (PluginDescription)e.Entity;
|
---|
[2503] | 145 |
|
---|
| 146 | // access to plugin descriptions has to be synchronized because multiple applications
|
---|
| 147 | // can be started or stopped at the same time
|
---|
| 148 | lock (locker) {
|
---|
[2922] | 149 | // also unload the matching plugin description in this AppDomain
|
---|
[2503] | 150 | plugins.First(x => x.Equals(desc)).Unload();
|
---|
[2497] | 151 | }
|
---|
[2922] | 152 | OnPluginUnloaded(e);
|
---|
[2497] | 153 | }
|
---|
[766] | 154 |
|
---|
[2503] | 155 | private void applicationManager_PluginLoaded(object sender, PluginInfrastructureEventArgs e) {
|
---|
| 156 | // load the matching plugin description (
|
---|
| 157 | PluginDescription desc = (PluginDescription)e.Entity;
|
---|
| 158 | // access to plugin descriptions has to be synchronized because multiple applications
|
---|
| 159 | // can be started or stopped at the same time
|
---|
| 160 | lock (locker) {
|
---|
[2922] | 161 | // also load the matching plugin description in this AppDomain
|
---|
[2503] | 162 | plugins.First(x => x.Equals(desc)).Load();
|
---|
| 163 | }
|
---|
[2922] | 164 | OnPluginLoaded(e);
|
---|
[2497] | 165 | }
|
---|
[882] | 166 |
|
---|
[2922] | 167 | #region event raising methods
|
---|
| 168 | private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
|
---|
| 169 | if (PluginLoaded != null) {
|
---|
| 170 | PluginLoaded(this, e);
|
---|
[2] | 171 | }
|
---|
| 172 | }
|
---|
[2615] | 173 |
|
---|
[2922] | 174 | private void OnPluginUnloaded(PluginInfrastructureEventArgs e) {
|
---|
| 175 | if (PluginUnloaded != null) {
|
---|
| 176 | PluginUnloaded(this, e);
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | private void OnInitializing(PluginInfrastructureEventArgs e) {
|
---|
| 181 | if (Initializing != null) {
|
---|
| 182 | Initializing(this, e);
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | private void OnInitialized(PluginInfrastructureEventArgs e) {
|
---|
| 187 | if (Initialized != null) {
|
---|
| 188 | Initialized(this, e);
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | private void OnApplicationStarting(PluginInfrastructureEventArgs e) {
|
---|
| 193 | if (ApplicationStarting != null) {
|
---|
| 194 | ApplicationStarting(this, e);
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | private void OnApplicationStarted(PluginInfrastructureEventArgs e) {
|
---|
| 199 | if (ApplicationStarted != null) {
|
---|
| 200 | ApplicationStarted(this, e);
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | #endregion
|
---|
| 204 |
|
---|
[2615] | 205 | // infinite lease time
|
---|
| 206 | /// <summary>
|
---|
[2922] | 207 | /// Make sure that the plugin manager is never disposed (necessary for cross-app-domain events)
|
---|
[2615] | 208 | /// </summary>
|
---|
| 209 | /// <returns><c>null</c>.</returns>
|
---|
| 210 | public override object InitializeLifetimeService() {
|
---|
| 211 | return null;
|
---|
| 212 | }
|
---|
[2] | 213 | }
|
---|
| 214 | }
|
---|