Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure.Manager/ApplicationManager.cs @ 2488

Last change on this file since 2488 was 2488, checked in by gkronber, 14 years ago

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

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Security.Policy;
25using System.Reflection;
26using System.Diagnostics;
27using System.Security.Permissions;
28using System.Security;
29using System.Linq;
30
31namespace HeuristicLab.PluginInfrastructure.Manager {
32
33  public class ApplicationManager : MarshalByRefObject, IApplicationManager {
34    private List<IPluginDescription> plugins;
35    /// <summary>
36    /// Gets all plugins.
37    /// </summary>
38    public IEnumerable<IPluginDescription> Plugins {
39      get { return plugins; }
40    }
41
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;
152    }
153
154
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;
162    }
163  }
164}
165
Note: See TracBrowser for help on using the repository browser.