Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2213_irace/HeuristicLab.PluginInfrastructure/3.3/Manager/PluginManager.cs @ 16147

Last change on this file since 16147 was 16102, checked in by abeham, 6 years ago

#2213: some pending changes exploring the topic

File size: 8.7 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[15583]3 * Copyright (C) 2002-2018 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>
[8818]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));
[16102]131        CrossDomainTracer.StartListening(applicationDomain);
[8818]132        applicationManager.Run(appInfo, args);
[766]133      }
134      finally {
[241]135        // make sure domain is unloaded in all cases
[2503]136        AppDomain.Unload(applicationDomain);
[241]137      }
[2]138    }
139
[2503]140    private void applicationManager_PluginUnloaded(object sender, PluginInfrastructureEventArgs e) {
141      // unload the matching plugin description (
[2497]142      PluginDescription desc = (PluginDescription)e.Entity;
[2503]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) {
[2922]147        // also unload the matching plugin description in this AppDomain
[2503]148        plugins.First(x => x.Equals(desc)).Unload();
[2497]149      }
[2922]150      OnPluginUnloaded(e);
[2497]151    }
[766]152
[2503]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) {
[2922]159        // also load the matching plugin description in this AppDomain
[2503]160        plugins.First(x => x.Equals(desc)).Load();
161      }
[2922]162      OnPluginLoaded(e);
[2497]163    }
[882]164
[2922]165    #region event raising methods
166    private void OnPluginLoaded(PluginInfrastructureEventArgs e) {
167      if (PluginLoaded != null) {
168        PluginLoaded(this, e);
[2]169      }
170    }
[2615]171
[2922]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
[2615]203    // infinite lease time
204    /// <summary>
[2922]205    /// Make sure that the plugin manager is never disposed (necessary for cross-app-domain events)
[2615]206    /// </summary>
207    /// <returns><c>null</c>.</returns>
[6998]208    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
[2615]209    public override object InitializeLifetimeService() {
210      return null;
211    }
[2]212  }
213}
Note: See TracBrowser for help on using the repository browser.