Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.PluginInfrastructure/3.3/Manager/PluginManager.cs @ 13401

Last change on this file since 13401 was 9363, checked in by spimming, 12 years ago

#1888:

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