Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Manager/PluginManager.cs @ 6410

Last change on this file since 6410 was 5703, checked in by cneumuel, 14 years ago

#1429

  • ensure that assemblies are only searched in PrivateBinPath and not in ApplicationBase. This is important for a hive slave creating a AppDomain with assemblies from a path different from the path of the executing AppDomain
File size: 8.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Reflection;
26using System.Security.Permissions;
27
28namespace 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.PrivateBinPath = pluginDir;
77      // probing should only occur in PrivateBinPath (not in ApplicationBase). This is enforced by the value string.Empty
78      setup.PrivateBinPathProbe = string.Empty;
79      AppDomain pluginDomain = null;
80      try {
81        pluginDomain = AppDomain.CreateDomain("plugin domain", null, setup);
82        Type pluginValidatorType = typeof(PluginValidator);
83        PluginValidator remoteValidator = (PluginValidator)pluginDomain.CreateInstanceAndUnwrap(pluginValidatorType.Assembly.FullName, pluginValidatorType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null);
84        remoteValidator.PluginDir = pluginDir;
85        // forward all events from the remoteValidator to listeners
86        remoteValidator.PluginLoaded +=
87          delegate(object sender, PluginInfrastructureEventArgs e) {
88            OnPluginLoaded(e);
89          };
90        // get list of plugins and applications from the validator
91        plugins.Clear(); applications.Clear();
92        plugins.AddRange(remoteValidator.Plugins);
93        applications.AddRange(remoteValidator.Applications);
94      }
95      finally {
96        // discard the AppDomain that was used for plugin discovery
97        AppDomain.Unload(pluginDomain);
98        // unload all plugins
99        foreach (var pluginDescription in plugins.Where(x => x.PluginState == PluginState.Loaded)) {
100          pluginDescription.Unload();
101          OnPluginUnloaded(new PluginInfrastructureEventArgs(pluginDescription));
102        }
103        initialized = true;
104        OnInitialized(PluginInfrastructureEventArgs.Empty);
105      }
106    }
107
108
109    /// <summary>
110    /// Starts an application in a separate AppDomain.
111    /// Loads all enabled plugins and starts the application via an ApplicationManager instance activated in the new AppDomain.
112    /// </summary>
113    /// <param name="appInfo">application to run</param>
114    public void Run(ApplicationDescription appInfo) {
115      if (!initialized) throw new InvalidOperationException("PluginManager is not initialized. DiscoverAndCheckPlugins() must be called before Run()");
116      // create a separate AppDomain for the application
117      // initialize the static ApplicationManager in the AppDomain
118      // and remotely tell it to start the application
119
120      OnApplicationStarting(new PluginInfrastructureEventArgs(appInfo));
121      AppDomain applicationDomain = null;
122      try {
123        AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
124        setup.PrivateBinPath = pluginDir;
125        applicationDomain = AppDomain.CreateDomain(AppDomain.CurrentDomain.FriendlyName, null, setup);
126        Type applicationManagerType = typeof(DefaultApplicationManager);
127        DefaultApplicationManager applicationManager =
128          (DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null);
129        applicationManager.PluginLoaded += applicationManager_PluginLoaded;
130        applicationManager.PluginUnloaded += applicationManager_PluginUnloaded;
131        applicationManager.PrepareApplicationDomain(applications, plugins);
132        OnApplicationStarted(new PluginInfrastructureEventArgs(appInfo));
133        applicationManager.Run(appInfo);
134      }
135      finally {
136        // make sure domain is unloaded in all cases
137        AppDomain.Unload(applicationDomain);
138      }
139    }
140
141    private void applicationManager_PluginUnloaded(object sender, PluginInfrastructureEventArgs e) {
142      // unload the matching plugin description (
143      PluginDescription desc = (PluginDescription)e.Entity;
144
145      // access to plugin descriptions has to be synchronized because multiple applications
146      // can be started or stopped at the same time
147      lock (locker) {
148        // also unload the matching plugin description in this AppDomain
149        plugins.First(x => x.Equals(desc)).Unload();
150      }
151      OnPluginUnloaded(e);
152    }
153
154    private void applicationManager_PluginLoaded(object sender, PluginInfrastructureEventArgs e) {
155      // load the matching plugin description (
156      PluginDescription desc = (PluginDescription)e.Entity;
157      // access to plugin descriptions has to be synchronized because multiple applications
158      // can be started or stopped at the same time
159      lock (locker) {
160        // also load the matching plugin description in this AppDomain
161        plugins.First(x => x.Equals(desc)).Load();
162      }
163      OnPluginLoaded(e);
164    }
165
166    #region event raising methods
167    private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
168      if (PluginLoaded != null) {
169        PluginLoaded(this, e);
170      }
171    }
172
173    private void OnPluginUnloaded(PluginInfrastructureEventArgs e) {
174      if (PluginUnloaded != null) {
175        PluginUnloaded(this, e);
176      }
177    }
178
179    private void OnInitializing(PluginInfrastructureEventArgs e) {
180      if (Initializing != null) {
181        Initializing(this, e);
182      }
183    }
184
185    private void OnInitialized(PluginInfrastructureEventArgs e) {
186      if (Initialized != null) {
187        Initialized(this, e);
188      }
189    }
190
191    private void OnApplicationStarting(PluginInfrastructureEventArgs e) {
192      if (ApplicationStarting != null) {
193        ApplicationStarting(this, e);
194      }
195    }
196
197    private void OnApplicationStarted(PluginInfrastructureEventArgs e) {
198      if (ApplicationStarted != null) {
199        ApplicationStarted(this, e);
200      }
201    }
202    #endregion
203
204    // infinite lease time
205    /// <summary>
206    /// Make sure that the plugin manager is never disposed (necessary for cross-app-domain events)
207    /// </summary>
208    /// <returns><c>null</c>.</returns>
209    [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
210    public override object InitializeLifetimeService() {
211      return null;
212    }
213  }
214}
Note: See TracBrowser for help on using the repository browser.