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                         foreach (Assembly a in getDefaultAssemblies ())
37                                 loaded_assemblies [a.FullName] = a;
38                 }
39                 
40                 public AppDomainSetup SetupInformation {
41
42                         get {
43                                 return adsetup;
44                         }
45                 }
46
47                 public string BaseDirectory {
48
49                         get {
50                                 return adsetup.ApplicationBase;
51                         }
52                 }
53
54                 public string RelativeSearchPath {
55
56                         get {
57                                 return adsetup.PrivateBinPath;
58                         }
59                 }
60
61                 public string DynamicDirectory {
62
63                         get {
64                                 // fixme: dont know what to return here
65                                 return null;
66                         }
67                 }
68
69                 public string FriendlyName {
70
71                         get {
72                                 return friendly_name;
73                         }
74                 }
75
76                 public Evidence Evidence {
77
78                         get {
79                                 return evidence;
80                         }
81                 }
82
83                 
84                 public static AppDomain CreateDomain (string friendlyName)
85                 {
86                         return CreateDomain (friendlyName, new Evidence (), new AppDomainSetup ());
87                 }
88                 
89                 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo)
90                 {
91                         return CreateDomain (friendlyName, securityInfo, new AppDomainSetup ());
92                 }
93                 
94                 public static AppDomain CreateDomain (string friendlyName,
95                                                       Evidence securityInfo,
96                                                       AppDomainSetup info)
97                 {
98                         if (friendlyName == null || securityInfo == null || info == null)
99                                 throw new System.ArgumentNullException();
100
101                         AppDomain ad = new AppDomain ();
102
103                         ad.friendly_name = friendlyName;
104                         ad.evidence = securityInfo;
105                         ad.adsetup = info;
106
107                         return ad;
108                 }
109
110                 public static AppDomain CreateDomain (string friendlyName, Evidence securityInfo,
111                                                       string appBasePath, string appRelativeSearchPath,
112                                                       bool shadowCopyFiles)
113                 {
114                         AppDomainSetup info = new AppDomainSetup ();
115
116                         info.ApplicationBase = appBasePath;
117                         info.PrivateBinPath = appRelativeSearchPath;
118
119                         if (shadowCopyFiles)
120                                 info.ShadowCopyFiles = "true";
121                         else
122                                 info.ShadowCopyFiles = "false";
123
124                         return CreateDomain (friendlyName, securityInfo, info);
125                 }
126                 
127                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
128                 public static extern Assembly LoadFrom (String assemblyFile, Evidence securityEvidence);
129
130                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
131                 private static extern AppDomain getCurDomain ();
132                 
133                 public static AppDomain CurrentDomain
134                 {
135                         get {
136                                 return getCurDomain ();
137                         }
138                 }
139
140                 public AssemblyBuilder DefineDynamicAssembly (AssemblyName name,
141                                                               AssemblyBuilderAccess access)
142                 {
143                         AssemblyBuilder ab = new AssemblyBuilder (name, access);
144                         return ab;
145                 }
146
147                 public Assembly Load (AssemblyName assemblyRef)
148                 {
149                         return Load (assemblyRef, new Evidence ());
150                 }
151
152                 public Assembly Load (AssemblyName assemblyRef, Evidence assemblySecurity)
153                 {
154                         Assembly res;
155                         
156                         if ((res = (Assembly)loaded_assemblies [assemblyRef]) != null)
157                                 return res;
158
159                         // fixme: we should pass the whole assemblyRef instead of the name,
160                         // and maybe also the adsetup
161                         res = LoadFrom (assemblyRef.Name, assemblySecurity);
162
163                         loaded_assemblies [assemblyRef] = res;
164                         
165                         return res;
166                 }
167
168                 public Assembly Load (string assemblyString)
169                 {
170                         AssemblyName an = new AssemblyName ();
171                         an.Name = assemblyString;
172                         
173                         return Load (an, new Evidence ());                      
174                 }
175
176                 public Assembly Load (string assemblyString, Evidence assemblySecurity)
177                 {
178                         AssemblyName an = new AssemblyName ();
179                         an.Name = assemblyString;
180                         
181                         return Load (an, assemblySecurity);                     
182                 }
183
184                 public Assembly Load (byte[] rawAssembly)
185                 {
186                         return Load (rawAssembly, null, new Evidence ());
187                 }
188
189                 public Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore)
190                 {
191                         return Load (rawAssembly, rawSymbolStore, new Evidence ());
192                 }
193
194                 public Assembly Load (byte[] rawAssembly, byte[] rawSymbolStore, Evidence securityEvidence)
195                 {
196                         throw new NotImplementedException ();
197                 }
198
199                 //
200                 // This returns a list of the assemblies that were loaded in behalf
201                 // of this AppDomain
202                 //
203                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
204                 static private extern Assembly [] getDefaultAssemblies ();
205                         
206                 public Assembly[] GetAssemblies ()
207                 {
208                         int x = loaded_assemblies.Count;
209                         Assembly[] res = new Assembly [loaded_assemblies.Count];
210
211                         int i = 0;
212                         foreach (DictionaryEntry de in loaded_assemblies)
213                                 res [i++] = (Assembly) de.Value;
214                                 
215                         return res;
216                 }
217
218                 // fixme: how does marshalling work ?
219                 public object GetData (string name)
220                 {
221                         switch (name) {
222                         case "APPBASE":
223                                 return adsetup.ApplicationBase;
224                         case "APP_CONFIG_FILE":
225                                 return adsetup.ConfigurationFile;
226                         case "DYNAMIC_BASE":
227                                 return adsetup.DynamicBase;
228                         case "APP_NAME":
229                                 return adsetup.ApplicationName;
230                         case "CACHE_BASE":
231                                 return adsetup.CachePath;
232                         case "PRIVATE_BINPATH":
233                                 return adsetup.PrivateBinPath;
234                         case "BINPATH_PROBE_ONLY":
235                                 return adsetup.PrivateBinPathProbe;
236                         case "SHADOW_COPY_DIRS":
237                                 return adsetup.ShadowCopyDirectories;
238                         case "FORCE_CACHE_INSTALL":
239                                 return adsetup.ShadowCopyFiles;
240                         }
241
242                         return data_hash [name];
243                 }
244
245                 // fixme: how does marshalling work ?
246                 public void SetData (string name, object data)
247                 {
248                         // LAMESPEC: why can't we set adsetup properties ??
249
250                         data_hash [name] = data;
251                 }
252
253         }
254 }