Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Sandboxing/SandboxManager.cs @ 4414

Last change on this file since 4414 was 4414, checked in by cneumuel, 14 years ago

Changes to PluginInfrastructure for Hive compatibility (#1191)

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Diagnostics;
25using System.Linq;
26using System.Reflection;
27using System.Security;
28using System.Security.Permissions;
29using System.Security.Policy;
30using HeuristicLab.PluginInfrastructure.Manager;
31
32namespace HeuristicLab.PluginInfrastructure.Sandboxing {
33  public class SandboxManager {
34
35    private static StrongName CreateStrongName(Assembly assembly) {
36      if (assembly == null)
37        throw new ArgumentNullException("assembly");
38
39      AssemblyName assemblyName = assembly.GetName();
40      Trace.Assert(assemblyName != null, "Could not get assembly name");
41
42      // get the public key blob
43      byte[] publicKey = assemblyName.GetPublicKey();
44      if (publicKey == null || publicKey.Length == 0)
45        throw new InvalidOperationException("Assembly is not strongly named");
46
47      StrongNamePublicKeyBlob keyBlob = new StrongNamePublicKeyBlob(publicKey);
48
49      // and create the StrongName
50      return new StrongName(keyBlob, assemblyName.Name, assemblyName.Version);
51    }
52
53    #region ISandboxManager Members
54
55    public static AppDomain CreateAndInitSandbox(string name) {
56      return CreateAndInitSandbox(name, Enumerable.Empty<byte[]>());
57    }
58
59
60    public static AppDomain CreateAndInitSandbox(string name, IEnumerable<byte[]> files) {
61      PermissionSet pset;
62
63      #region permission set for sandbox
64      // Uncomment code for sandboxed appdomain
65      //pset = new PermissionSet(PermissionState.None);
66      //pset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
67      //pset.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess));
68      //FileIOPermission fPerm = new FileIOPermission(PermissionState.None);
69
70      //foreach (IPluginDescription plugin in ApplicationManager.Manager.Plugins) {
71      //  fPerm.AddPathList(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery, plugin.Files.ToArray());
72      //}
73
74      //pset.AddPermission(fPerm);
75      #endregion
76
77      #region permission set of unrestricted appdomain
78      // unrestricted appdomain
79      pset = new PermissionSet(PermissionState.Unrestricted);
80      #endregion
81
82      AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
83      //setup.PrivateBinPath = pluginDir;
84      setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
85      AppDomain applicationDomain = AppDomain.CreateDomain(name, AppDomain.CurrentDomain.Evidence, setup, pset, CreateStrongName(Assembly.GetExecutingAssembly()));
86      Type applicationManagerType = typeof(DefaultApplicationManager);
87      DefaultApplicationManager applicationManager =
88        (DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null, null);
89      PluginManager pm = new PluginManager(name);
90      pm.DiscoverAndCheckPlugins();
91      ApplicationDescription[] apps = ApplicationManager.Manager.Applications.Cast<ApplicationDescription>().ToArray();
92      PluginDescription[] plugins = ApplicationManager.Manager.Plugins.Cast<PluginDescription>().ToArray();
93      applicationManager.PrepareApplicationDomain(apps, plugins);
94      //if (files != null && files.Count() > 0)
95      //applicationManager.LoadAssemblies(files);
96      return applicationDomain;
97    }
98
99    #endregion
100  }
101}
Note: See TracBrowser for help on using the repository browser.