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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Reflection;
|
---|
26 | using System.Security.Permissions;
|
---|
27 | using System.Security;
|
---|
28 | using System.Linq;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.PluginInfrastructure.Manager {
|
---|
31 | public class Runner : MarshalByRefObject {
|
---|
32 | internal event PluginManagerActionEventHandler PluginAction;
|
---|
33 |
|
---|
34 | private Dictionary<string, Assembly> loadedAssemblies;
|
---|
35 |
|
---|
36 | public Runner() {
|
---|
37 | loadedAssemblies = new Dictionary<string, Assembly>();
|
---|
38 | AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
|
---|
39 | if (loadedAssemblies.ContainsKey(args.Name)) {
|
---|
40 | return loadedAssemblies[args.Name];
|
---|
41 | }
|
---|
42 | return null;
|
---|
43 | };
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void LoadPlugins(IEnumerable<IPluginDescription> pluginDescriptions) {
|
---|
47 | //FileIOPermission fileperm = new FileIOPermission(FileIOPermissionAccess.AllAccess, @"C:\Program Files\HeuristicLab 3.0\plugins\");
|
---|
48 | //fileperm.Assert();
|
---|
49 |
|
---|
50 | // load all loadable plugins (all dependencies available) into the execution context
|
---|
51 | foreach (var desc in PluginDescriptionIterator.IterateInDependencyOrder(pluginDescriptions.Where(x => x.PluginState != PluginState.Disabled))) {
|
---|
52 | List<Type> types = new List<Type>();
|
---|
53 | foreach (string assembly in desc.Assemblies) {
|
---|
54 | var asm = Assembly.LoadFrom(assembly);
|
---|
55 | foreach (Type t in asm.GetTypes()) {
|
---|
56 | if (typeof(IPlugin).IsAssignableFrom(t)) {
|
---|
57 | types.Add(t);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | foreach (Type pluginType in types) {
|
---|
63 | if (!pluginType.IsAbstract && !pluginType.IsInterface && !pluginType.HasElementType) {
|
---|
64 | IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
|
---|
65 | plugin.OnLoad();
|
---|
66 | desc.Load();
|
---|
67 | PluginAction(this, new PluginManagerActionEventArgs(plugin.Name, PluginManagerAction.PluginLoaded));
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | //CodeAccessPermission.RevertAssert();
|
---|
72 | }
|
---|
73 |
|
---|
74 | /// <summary>
|
---|
75 | /// Loads assemblies from a byte array
|
---|
76 | /// </summary>
|
---|
77 | /// <param name="plugins">bytearray of all assemblies that should be loaded</param>
|
---|
78 | public void LoadAssemblies(IEnumerable<byte[]> assemblies) {
|
---|
79 | foreach (byte[] asm in assemblies) {
|
---|
80 | Assembly loadedAsm = Assembly.Load(asm);
|
---|
81 | RegisterLoadedAssembly(loadedAsm);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void RegisterLoadedAssembly(Assembly asm) {
|
---|
86 | loadedAssemblies.Add(asm.FullName, asm);
|
---|
87 | loadedAssemblies.Add(asm.GetName().Name, asm); // add short name
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void Run(ApplicationDescription appInfo) {
|
---|
91 | IApplication runnablePlugin = (IApplication)Activator.CreateInstance(appInfo.DeclaringAssemblyName, appInfo.DeclaringTypeName).Unwrap();
|
---|
92 | try {
|
---|
93 | runnablePlugin.Run();
|
---|
94 | }
|
---|
95 | catch (Exception e) {
|
---|
96 | throw new Exception(String.Format(
|
---|
97 | "Unexpected exception caught: \"{0}\"\r\n" +
|
---|
98 | "Type: {1}\r\n" +
|
---|
99 | "Plugin {2}:\r\n{3}",
|
---|
100 | e.Message,
|
---|
101 | e.GetType().FullName,
|
---|
102 | appInfo.Name,
|
---|
103 | e.ToString()));
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | // infinite lease time
|
---|
108 | /// <summary>
|
---|
109 | /// Initializes the life time service with infinite lease time.
|
---|
110 | /// </summary>
|
---|
111 | /// <returns><c>null</c>.</returns>
|
---|
112 | public override object InitializeLifetimeService() {
|
---|
113 | return null;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|