Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/12/09 17:45:45 (14 years ago)
Author:
gkronber
Message:

Worked on plugin infrastructure refactoring. (Fully functional revision). #799

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure.Manager/ApplicationManager.cs

    r2481 r2488  
    2727using System.Security.Permissions;
    2828using System.Security;
     29using System.Linq;
    2930
    3031namespace HeuristicLab.PluginInfrastructure.Manager {
    3132
    32   public class ApplicationManager {
     33  public class ApplicationManager : MarshalByRefObject, IApplicationManager {
     34    private List<IPluginDescription> plugins;
    3335    /// <summary>
    34     /// Event handler for actions in the application manager.
     36    /// Gets all plugins.
    3537    /// </summary>
    36     public event PluginManagerActionEventHandler Action;
    37 
    38     // singleton pattern
    39     public ApplicationManager() {
    40       applications = new List<ApplicationDescription>();
     38    public IEnumerable<IPluginDescription> Plugins {
     39      get { return plugins; }
    4140    }
    4241
    43     private ApplicationManager singleton = new ApplicationManager();
    44     public ApplicationManager Manager {
    45       get { return singleton; }
     42    private List<IApplicationDescription> applications;
     43    /// <summary>
     44    /// Gets all installed applications.
     45    /// </summary>
     46    public IEnumerable<IApplicationDescription> Applications {
     47      get { return applications; }
     48    }
     49
     50    public ApplicationManager() : base() { }
     51
     52    internal void PrepareApplicationDomain(IEnumerable<IApplicationDescription> apps, IEnumerable<IPluginDescription> plugins) {
     53      this.plugins = new List<IPluginDescription>(plugins);
     54      this.applications = new List<IApplicationDescription>(apps);
     55      PluginInfrastructure.ApplicationManager.RegisterApplicationManager(this);
     56      LoadPlugins(plugins);
     57    }
     58
     59    private void LoadPlugins(IEnumerable<IPluginDescription> plugins) {
     60      // load all loadable plugins (all dependencies available) into the execution context
     61      foreach (var desc in PluginDescriptionIterator.IterateInDependencyOrder(plugins.Where(x => x.PluginState != PluginState.Disabled))) {
     62        foreach (var plugin in GetInstances<IPlugin>(desc)) {
     63          plugin.OnLoad();
     64        }
     65        desc.Load();
     66      }
     67    }
     68
     69    internal void Run(IApplicationDescription appInfo) {
     70      IApplication runnablePlugin = (IApplication)Activator.CreateInstance(appInfo.DeclaringAssemblyName, appInfo.DeclaringTypeName).Unwrap();
     71      try {
     72        runnablePlugin.Run();
     73      }
     74      catch (Exception e) {
     75        throw new Exception(String.Format(
     76          "Unexpected exception caught: \"{0}\"\r\n" +
     77          "Type: {1}\r\n" +
     78          "Plugin {2}:\r\n{3}",
     79          e.Message,
     80          e.GetType().FullName,
     81          appInfo.Name,
     82          e.ToString()));
     83      }
     84    }
     85
     86    /// <summary>
     87    /// Creates an instance of all types that are subtypes or the same type of the specified type and declared in <paramref name="plugin"/>
     88    /// </summary>
     89    /// <typeparam name="T">Most general type.</typeparam>
     90    /// <returns>Enumerable of the created instances.</returns>
     91    public IEnumerable<T> GetInstances<T>(IPluginDescription plugin) where T : class {
     92      return from t in GetTypes(typeof(T), plugin)
     93             where !t.IsAbstract && !t.IsInterface && !t.HasElementType
     94             select (T)Activator.CreateInstance(t);
     95    }
     96    /// <summary>
     97    /// Creates an instance of all types that are subtypes or the same type of the specified type
     98    /// </summary>
     99    /// <typeparam name="T">Most general type.</typeparam>
     100    /// <returns>Enumerable of the created instances.</returns>
     101    public IEnumerable<T> GetInstances<T>() where T : class {
     102      return from i in GetInstances(typeof(T))
     103             select (T)i;
     104    }
     105
     106    /// <summary>
     107    /// Creates an instance of all types that are subtypes or the same type of the specified type
     108    /// </summary>
     109    /// <typeparam name="type">Most general type.</typeparam>
     110    /// <returns>Enumerable of the created instances.</returns>
     111    public IEnumerable<object> GetInstances(Type type) {
     112      return from t in GetTypes(type)
     113             where !t.IsAbstract && !t.IsInterface && !t.HasElementType
     114             select Activator.CreateInstance(t);
     115    }
     116
     117    /// <summary>
     118    /// Finds all types that are subtypes or equal to the specified type.
     119    /// </summary>
     120    /// <param name="type">Most general type for which to find matching types.</param>
     121    /// <returns>Enumerable of the discovered types.</returns>
     122    public IEnumerable<Type> GetTypes(Type type) {
     123      return from asm in AppDomain.CurrentDomain.GetAssemblies()
     124             from t in GetTypes(type, asm)
     125             select t;
     126    }
     127
     128    /// <summary>
     129    /// Finds all types that are subtypes or equal to the specified type if they are part of the given
     130    /// <paramref name="plugin"/>.
     131    /// </summary>
     132    /// <param name="type">Most general type for which to find matching types.</param>
     133    /// <param name="plugin">The plugin the subtypes must be part of.</param>
     134    /// <returns>Enumerable of the discovered types.</returns>
     135    public IEnumerable<Type> GetTypes(Type type, IPluginDescription pluginDescription) {
     136      return from asm in AppDomain.CurrentDomain.GetAssemblies()
     137             where pluginDescription.Assemblies.Contains(asm.Location)
     138             from t in GetTypes(type, asm)
     139             select t;
     140    }
     141
     142    /// <summary>
     143    /// Gets types that are assignable (same of subtype) to the specified type only from the given assembly.
     144    /// </summary>
     145    /// <param name="type">Most general type we want to find.</param>
     146    /// <param name="assembly">Assembly that should be searched for types.</param>
     147    /// <returns>Enumerable of the discovered types.</returns>
     148    private IEnumerable<Type> GetTypes(Type type, Assembly assembly) {
     149      return from t in assembly.GetTypes()
     150             where type.IsAssignableFrom(t)
     151             select t;
    46152    }
    47153
    48154
    49     private void NotifyListeners(string action, string text) {
    50       if (Action != null) {
    51         Action(this, new PluginManagerActionEventArgs(text, action));
    52       }
     155    // infinite lease time
     156    /// <summary>
     157    /// Initializes the life time service with infinite lease time.
     158    /// </summary>
     159    /// <returns><c>null</c>.</returns>
     160    public override object InitializeLifetimeService() {
     161      return null;
    53162    }
    54163  }
    55164}
     165
Note: See TracChangeset for help on using the changeset viewer.