Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/01/08 14:42:57 (16 years ago)
Author:
kgrading
Message:

moved the appdomain creator (#410)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.PluginInfrastructure/PluginManager.cs

    r766 r882  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Security.Policy;
     25using System.Reflection;
     26using System.Diagnostics;
     27using System.Security.Permissions;
     28using System.Security;
    2429
    2530namespace HeuristicLab.PluginInfrastructure {
     
    132137    }
    133138
     139    /// <summary>
     140    /// Creates a new AppDomain with all plugins preloaded and Sandboxing capability
     141    /// </summary>
     142    /// <param name="assembly">Assembly reference</param>
     143    /// <returns>the strongname of the assembly</returns>
     144    private StrongName CreateStrongName(Assembly assembly) {
     145      if (assembly == null)
     146        throw new ArgumentNullException("assembly");
     147
     148      AssemblyName assemblyName = assembly.GetName();
     149      Debug.Assert(assemblyName != null, "Could not get assembly name");
     150
     151      // get the public key blob
     152      byte[] publicKey = assemblyName.GetPublicKey();
     153      if (publicKey == null || publicKey.Length == 0)
     154        throw new InvalidOperationException("Assembly is not strongly named");
     155
     156      StrongNamePublicKeyBlob keyBlob = new StrongNamePublicKeyBlob(publicKey);
     157
     158      // and create the StrongName
     159      return new StrongName(keyBlob, assemblyName.Name, assemblyName.Version);
     160    }
     161
     162    public AppDomain CreateAndInitAppDomainWithSandbox(string friendlyName, bool sandboxed) {
     163
     164      PermissionSet pset;
     165      if (sandboxed) {
     166        pset = new PermissionSet(PermissionState.None);
     167        pset.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));       
     168      } else {
     169        pset = new PermissionSet(PermissionState.Unrestricted);
     170      }
     171      AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
     172      setup.PrivateBinPath = pluginDir;
     173      setup.ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;     
     174      AppDomain applicationDomain = AppDomain.CreateDomain(friendlyName, AppDomain.CurrentDomain.Evidence, setup, pset, CreateStrongName(Assembly.GetExecutingAssembly()));
     175                     
     176      Runner remoteRunner = (Runner)applicationDomain.CreateInstanceAndUnwrap(typeof(Runner).Assembly.GetName().Name, typeof(Runner).FullName);
     177      NotifyListeners(PluginManagerAction.Initializing, "All plugins");
     178      if (remoteLoader != null) {
     179        remoteRunner.LoadPlugins(remoteLoader.ActivePlugins);
     180      } else if (LoadedPlugins != null && LoadedPlugins.Count > 0) {
     181        remoteRunner.LoadPlugins(LoadedPlugins);
     182      }
     183      NotifyListeners(PluginManagerAction.Initialized, "All plugins");
     184      return applicationDomain;
     185    }
    134186
    135187    /// <summary>
Note: See TracChangeset for help on using the changeset viewer.