2002-01-23 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / class / corlib / System / AppDomain.cs
1
2 //
3 // System/AppDomain.cs
4 //
5 // Authors:
6 //   Paolo Molaro (lupus@ximian.com)
7 //   Dietmar Maurer (dietmar@ximian.com)
8 //   Miguel de Icaza (miguel@ximian.com)
9 //
10 // (C) 2001, 2002 Ximian, Inc.  http://www.ximian.com
11 //
12
13 using System;
14 using System.Collections;
15 using System.Reflection;
16 using System.Reflection.Emit;
17 using System.Runtime.CompilerServices;
18 using System.Security.Policy;
19
20 namespace System {
21
22         public sealed class AppDomain /* : MarshalByRefObject , _AppDomain, IEvidenceFactory */ {
23
24                 private Hashtable loaded_assemblies = new Hashtable ();
25                 private Hashtable data_hash = new Hashtable ();
26                 private AppDomainSetup adsetup;
27                 private string friendly_name;
28                 private Evidence evidence;
29
30                 private AppDomain ()
31                 {
32                         //
33                         // Prime the loaded assemblies with the assemblies that were loaded
34                         // by the runtime in our behalf
35                         //
36
37                         //
38                         // TODO: Maybe we can lazily do this, as loaded_assemblies
39                         // will not be used all the time, we can just compute this on
40                         // demand.
41                         //
42                         foreach (Assembly a in getDefaultAssemblies ())
43                                 loaded_assemblies [a.FullName] = a;
44                 }
45                 
46                 public AppDomainSetup SetupInformation {
47
48                         get {
49                                 return adsetup;
50                         }
51                 }
52
53                 public string BaseDirectory {
54
55                         get {
56                                 return adsetup.ApplicationBase;
57                         }
58                 }
59
60                 public string RelativeSearchPath {
61
62                         get {
63                                 return adsetup.PrivateBinPath;
64                         }
65                 }
66
67                 public string DynamicDirectory {
68
69                         get {
70                                 // fixme: dont know what to return here
71                                 return null;
72                         }
73                 }
74
75                 public string FriendlyName {
76
77                         get {
78                                 return friendly_name;
79                         }
80                 }
81
82                 public Evidence Evidence {
83
84                         get {
85                                 return evidence;
86                         }
87                 }
88
89                 
90                 public static AppDomain CreateDomain (string friendlyName)
91                 {
92                         return CreateDomain (friendlyName, new Evidence (), new AppDomainSetup ());
93                 }
94                 
95                 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo)
96                 {
97                         return CreateDomain (friendlyName, securityInfo, new AppDomainSetup ());
98                 }
99                 
100                 public static AppDomain CreateDomain (string friendlyName,
101                                                       Evidence securityInfo,
102                                                       AppDomainSetup info)
103                 {
104                         if (friendlyName == null || securityInfo == null || info == null)
105                                 throw new System.ArgumentNullException();
106
107                         AppDomain ad = new AppDomain ();
108
109                         ad.friendly_name = friendlyName;
110                         ad.evidence = securityInfo;
111                         ad.adsetup = info;
112
113                         return ad;
114                 }
115
116                 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo,
117                                                       string appBasePath, string appRelativeSearchPath,
118                                                       bool shadowCopyFiles)
119                 {
120                         AppDomainSetup info = new AppDomainSetup ();
121
122                         info.ApplicationBase = appBasePath;
123                         info.PrivateBinPath = appRelativeSearchPath;
124
125                         if (shadowCopyFiles)
126                                 info.ShadowCopyFiles = "true";
127                         else
128                                 info.ShadowCopyFiles = "false";
129
130                         return CreateDomain (friendlyName, securityInfo, info);
131                 }
132                 
133                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
134                 public static extern Assembly LoadFrom (String assemblyFile, Evidence securityEvidence);
135
136                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
137                 private static extern AppDomain getCurDomain ();
138                 
139                 public static AppDomain CurrentDomain
140                 {
141                         get {
142                                 return getCurDomain ();
143                         }
144                 }
145
146                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name,
147                                                               AssemblyBuilderAccess access)
148                 {
149                         AssemblyBuilder ab = new AssemblyBuilder (name, access);
150                         return ab;
151                 }
152
153                 public Assembly Load (AssemblyName assemblyRef)
154                 {
155                         return Load (assemblyRef, new Evidence ());
156                 }
157
158                 public Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
159                 {
160                         Assembly res;
161                         
162                         if ((res = (Assembly)loaded_assemblies [assemblyRef]) != null)
163                                 return res;
164
165                         // fixme: we should pass the whole assemblyRef instead of the name,
166                         // and maybe also the adsetup
167                         res = LoadFrom (assemblyRef.Name, assemblySecurity);
168
169                         loaded_assemblies [assemblyRef] = res;
170                         
171                         return res;
172                 }
173
174                 public Assembly Load (string assemblyString)
175                 {
176                         AssemblyName an = new AssemblyName ();
177                         an.Name = assemblyString;
178                         
179                         return Load (an, new Evidence ());                      
180                 }
181
182                 public Assembly Load (string assemblyString, Evidence assemblySecurity)
183                 {
184                         AssemblyName an = new AssemblyName ();
185                         an.Name = assemblyString;
186                         
187                         return Load (an, assemblySecurity);                     
188                 }
189
190                 public Assembly Load (byte[] rawAssembly)
191                 {
192                         return Load (rawAssembly, null, new Evidence ());
193                 }
194
195                 public Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore)
196                 {
197                         return Load (rawAssembly, rawSymbolStore, new Evidence ());
198                 }
199
200                 public Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, Evidence securityEvidence)
201                 {
202                         throw new NotImplementedException ();
203                 }
204
205                 //
206                 // This returns a list of the assemblies that were loaded in behalf
207                 // of this AppDomain
208                 //
209                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
210                 static private extern Assembly [] getDefaultAssemblies ();
211                         
212                 public Assembly[] GetAssemblies ()
213                 {
214                         int x = loaded_assemblies.Count;
215                         Assembly[] res = new Assembly [loaded_assemblies.Count];
216
217                         int i = 0;
218                         foreach (DictionaryEntry de in loaded_assemblies)
219                                 res [i++] = (Assembly) de.Value;
220                                 
221                         return res;
222                 }
223
224                 // fixme: how does marshalling work ?
225                 public object GetData (string name)
226                 {
227                         switch (name) {
228                         case "APPBASE":
229                                 return adsetup.ApplicationBase;
230                         case "APP_CONFIG_FILE":
231                                 return adsetup.ConfigurationFile;
232                         case "DYNAMIC_BASE":
233                                 return adsetup.DynamicBase;
234                         case "APP_NAME":
235                                 return adsetup.ApplicationName;
236                         case "CACHE_BASE":
237                                 return adsetup.CachePath;
238                         case "PRIVATE_BINPATH":
239                                 return adsetup.PrivateBinPath;
240                         case "BINPATH_PROBE_ONLY":
241                                 return adsetup.PrivateBinPathProbe;
242                         case "SHADOW_COPY_DIRS":
243                                 return adsetup.ShadowCopyDirectories;
244                         case "FORCE_CACHE_INSTALL":
245                                 return adsetup.ShadowCopyFiles;
246                         }
247
248                         return data_hash [name];
249                 }
250
251                 // fixme: how does marshalling work ?
252                 public void SetData (string name, object data)
253                 {
254                         // LAMESPEC: why can't we set adsetup properties ??
255
256                         data_hash [name] = data;
257                 }
258
259         }
260 }