Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/Runner.cs @ 2576

Last change on this file since 2576 was 1990, checked in by gkronber, 16 years ago
  • Refactoring: renamed method LoadPlugins to LoadAssemblies in class Runner.
  • Added cache of loaded assemblies in the Runner.
  • Added an AssemblyResolveEvent that returns already loaded assemblies from the cache.

#658 (For the execution of jobs assemblies have to be loaded dynamically in the correct order)

File size: 3.5 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.Text;
25using System.Reflection;
26using System.Security.Permissions;
27using System.Security;
28
29namespace HeuristicLab.PluginInfrastructure {
30  public class Runner : MarshalByRefObject {
31
32    private Dictionary<string, Assembly> loadedAssemblies;
33
34    public Runner() {
35      loadedAssemblies = new Dictionary<string, Assembly>();
36      AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
37        if(loadedAssemblies.ContainsKey(args.Name)) {
38          return loadedAssemblies[args.Name];
39        }
40        return null;
41      };
42    }
43
44    public void LoadPlugins(ICollection<PluginInfo> plugins) {
45      //FileIOPermission fileperm = new FileIOPermission(FileIOPermissionAccess.AllAccess, @"C:\Program Files\HeuristicLab 3.0\plugins\");
46      //fileperm.Assert();
47      foreach(PluginInfo pluginInfo in plugins) {
48        foreach(string assemblyName in pluginInfo.Assemblies) {
49          Assembly.LoadFrom(assemblyName);
50        }
51      }
52      //CodeAccessPermission.RevertAssert();
53      FireOnLoad();
54      PluginManager.Manager.LoadedPlugins = plugins;
55    }
56    /// <summary>
57    /// Loads assemblies from a byte array
58    /// </summary>
59    /// <param name="plugins">bytearray of all assemblies that should be loaded</param>
60    public void LoadAssemblies(ICollection<byte[]> assemblies) {
61      foreach (byte[] asm in assemblies) {
62        Assembly loadedAsm = Assembly.Load(asm);
63        RegisterLoadedAssembly(loadedAsm);
64      }
65    }
66
67    private void RegisterLoadedAssembly(Assembly asm) {
68      loadedAssemblies.Add(asm.FullName, asm);
69      loadedAssemblies.Add(asm.GetName().Name, asm); // add short name
70    }
71
72    public void Run(ApplicationInfo appInfo) {
73      IApplication runnablePlugin = (IApplication)Activator.CreateInstance(appInfo.PluginAssembly, appInfo.PluginType).Unwrap();
74      try {
75        runnablePlugin.Run();
76      } catch (Exception e) {
77        throw new Exception(String.Format(
78          "Unexpected exception caught: \"{0}\"\r\n" +
79          "Type: {1}\r\n" +
80          "Plugin {2}:\r\n{3}",
81          e.Message,
82          e.GetType().FullName,
83          appInfo.Name,
84          e.ToString()));
85      }
86    }
87
88    private void FireOnLoad() {
89      DiscoveryService service = new DiscoveryService();
90      Array.ForEach(service.GetInstances<IPlugin>(), p => p.OnLoad());
91    }
92
93    // infinite lease time
94    /// <summary>
95    /// Initializes the life time service with infinite lease time.
96    /// </summary>
97    /// <returns><c>null</c>.</returns>
98    public override object InitializeLifetimeService() {
99      return null;
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.