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