2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / corlib / System / AppDomainSetup.cs
1 //
2 // System.AppDomainSetup.cs
3 //
4 // Authors:
5 //      Dietmar Maurer (dietmar@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2001 Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Known Problems:
12 //      Fix serialization compatibility with MS.NET.
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.IO;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Security;
38
39 #if NET_2_0
40 using System.Runtime.Hosting;
41 using System.Security.Policy;
42 #endif
43
44 namespace System
45 {
46         [Serializable]
47         [ClassInterface (ClassInterfaceType.None)]
48 #if NET_2_0
49         [ComVisible (true)]
50 #endif
51 #if NET_2_1
52         public sealed class AppDomainSetup
53 #else
54         public sealed class AppDomainSetup : IAppDomainSetup
55 #endif
56         {
57                 string application_base;
58                 string application_name;
59                 string cache_path;
60                 string configuration_file;
61                 string dynamic_base;
62                 string license_file;
63                 string private_bin_path;
64                 string private_bin_path_probe;
65                 string shadow_copy_directories;
66                 string shadow_copy_files;
67                 bool publisher_policy;
68                 private bool path_changed;
69                 private LoaderOptimization loader_optimization;
70                 bool disallow_binding_redirects;
71                 bool disallow_code_downloads;
72
73                 // those fields also exist in the runtime, so we need dummies in 1.x profile too.
74 #if NET_2_0
75                 private ActivationArguments _activationArguments;
76                 AppDomainInitializer domain_initializer;
77                 [NonSerialized]
78                 ApplicationTrust application_trust;
79 #else
80                 object _activationArguments;
81                 object domain_initializer; // always null
82 #endif
83                 string [] domain_initializer_args;
84                 SecurityElement application_trust_xml;
85                 bool disallow_appbase_probe;
86                 byte [] configuration_bytes;
87
88                 public AppDomainSetup ()
89                 {
90                 }
91
92                 internal AppDomainSetup (AppDomainSetup setup)
93                 {
94                         application_base = setup.application_base;
95                         application_name = setup.application_name;
96                         cache_path = setup.cache_path;
97                         configuration_file = setup.configuration_file;
98                         dynamic_base = setup.dynamic_base;
99                         license_file = setup.license_file;
100                         private_bin_path = setup.private_bin_path;
101                         private_bin_path_probe = setup.private_bin_path_probe;
102                         shadow_copy_directories = setup.shadow_copy_directories;
103                         shadow_copy_files = setup.shadow_copy_files;
104                         publisher_policy = setup.publisher_policy;
105                         path_changed = setup.path_changed;
106                         loader_optimization = setup.loader_optimization;
107                         disallow_binding_redirects = setup.disallow_binding_redirects;
108                         disallow_code_downloads = setup.disallow_code_downloads;
109 //#if NET_2_0
110                         _activationArguments = setup._activationArguments;
111                         domain_initializer = setup.domain_initializer;
112                         domain_initializer_args = setup.domain_initializer_args;
113                         application_trust_xml = setup.application_trust_xml;
114                         disallow_appbase_probe = setup.disallow_appbase_probe;
115                         configuration_bytes = setup.configuration_bytes;
116 //#endif
117                 }
118
119 #if NET_2_0
120                 public AppDomainSetup (ActivationArguments activationArguments)
121                 {
122                         _activationArguments = activationArguments;
123                 }
124
125                 public AppDomainSetup (ActivationContext activationContext)
126                 {
127                         _activationArguments = new ActivationArguments (activationContext);
128                 }
129 #endif
130
131                 static string GetAppBase (string appBase)
132                 {
133                         if (appBase == null)
134                                 return null;
135
136                         int len = appBase.Length;
137                         if (len >= 8 && appBase.ToLower ().StartsWith ("file://")) {
138                                 appBase = appBase.Substring (7);
139                                 if (Path.DirectorySeparatorChar != '/')
140                                         appBase = appBase.Replace ('/', Path.DirectorySeparatorChar);
141                                 if (Environment.IsRunningOnWindows) {
142                                         // Under Windows prepend "//" to indicate it's a local file
143                                         appBase = "//" + appBase;
144                                 }
145 #if NET_2_0
146                         } else {
147 #else
148                         // under 1.x the ":" gets a special treatment - but it doesn't make sense outside Windows
149                         } else if (!Environment.IsRunningOnWindows || (appBase.IndexOf (':') == -1)) {
150 #endif
151                                 appBase = Path.GetFullPath (appBase);
152                         }
153
154                         return appBase;
155                 }
156
157                 public string ApplicationBase {
158                         get { return GetAppBase (application_base); }
159                         set { application_base = value; } 
160                 }
161
162                 public string ApplicationName {
163                         get {
164                                 return application_name;
165                         }
166                         set {
167                                 application_name = value;
168                         }
169                 }
170
171                 public string CachePath {
172                         get {
173                                 return cache_path;
174                         }
175                         set {
176                                 cache_path = value;
177                         }
178                 }
179
180                 public string ConfigurationFile {
181                         get {
182                                 if (configuration_file == null)
183                                         return null;
184                                 if (Path.IsPathRooted(configuration_file))
185                                         return configuration_file;
186                                 if (ApplicationBase == null)
187                                         throw new MemberAccessException("The ApplicationBase must be set before retrieving this property.");
188                                 return Path.Combine(ApplicationBase, configuration_file);
189                         }
190                         set {
191                                 configuration_file = value;
192                         }
193                 }
194
195                 public bool DisallowPublisherPolicy {
196                         get {
197                                 return publisher_policy;
198                         }
199                         set {
200                                 publisher_policy = value;
201                         }
202                 }
203
204                 public string DynamicBase {
205                         get {
206                                 if (dynamic_base == null)
207                                         return null;
208
209                                 if (Path.IsPathRooted (dynamic_base))
210                                         return dynamic_base;
211
212                                 if (ApplicationBase == null)
213                                         throw new MemberAccessException ("The ApplicationBase must be set before retrieving this property.");
214                                 
215                                 return Path.Combine (ApplicationBase, dynamic_base);
216                         }
217                         set {
218                                 if (application_name == null)
219                                         throw new MemberAccessException ("ApplicationName must be set before the DynamicBase can be set.");
220                                 uint id = (uint) application_name.GetHashCode ();
221                                 dynamic_base = Path.Combine (value, id.ToString("x"));
222                         }
223                 }
224
225                 public string LicenseFile {
226                         get {
227                                 return license_file;
228                         }
229                         set {
230                                 license_file = value;
231                         }
232                 }
233
234                 [MonoLimitation ("In Mono this is controlled by the --share-code flag")]
235                 public LoaderOptimization LoaderOptimization {
236                         get {
237                                 return loader_optimization;
238                         }
239                         set {
240                                 loader_optimization = value;
241                         }
242                 }
243
244                 public string PrivateBinPath {
245                         get {
246                                 return private_bin_path;
247                         }
248                         set {
249                                 private_bin_path = value;
250                                 path_changed = true;
251                         }
252                 }
253
254                 public string PrivateBinPathProbe {
255                         get {
256                                 return private_bin_path_probe;
257                         }
258                         set {
259                                 private_bin_path_probe = value;
260                                 path_changed = true;
261                         }
262                 }
263
264                 public string ShadowCopyDirectories {
265                         get {
266                                 return shadow_copy_directories;
267                         }
268                         set {
269                                 shadow_copy_directories = value;
270                         }
271                 }
272
273                 public string ShadowCopyFiles {
274                         get {
275                                 return shadow_copy_files;
276                         }
277                         set {
278                                 shadow_copy_files = value;
279                         }
280                 }
281
282 #if NET_1_1
283                 public bool DisallowBindingRedirects {
284                         get {
285                                 return disallow_binding_redirects;
286                         }
287                         set {
288                                 disallow_binding_redirects = value;
289                         }
290                 }
291
292                 public bool DisallowCodeDownload {
293                         get {
294                                 return disallow_code_downloads;
295                         }
296                         set {
297                                 disallow_code_downloads = value;
298                         }
299                 }
300 #endif
301
302 #if NET_2_0
303                 public ActivationArguments ActivationArguments {
304                         get { return _activationArguments; }
305                         set { _activationArguments = value; }
306                 }
307
308                 [MonoLimitation ("it needs to be invoked within the created domain")]
309                 public AppDomainInitializer AppDomainInitializer {
310                         get { return domain_initializer; }
311                         set { domain_initializer = value; }
312                 }
313
314                 [MonoLimitation ("it needs to be used to invoke the initializer within the created domain")]
315                 public string [] AppDomainInitializerArguments {
316                         get { return domain_initializer_args; }
317                         set { domain_initializer_args = value; }
318                 }
319
320                 [MonoNotSupported ("This property exists but not considered.")]
321                 public ApplicationTrust ApplicationTrust {
322                         get {
323                                 if (application_trust_xml == null)
324                                         return null;
325                                 if (application_trust == null)
326                                         application_trust = new ApplicationTrust ();
327                                 return application_trust;
328                         }
329                         set {
330                                 application_trust = value;
331                                 if (value != null) {
332                                         application_trust_xml = value.ToXml ();
333                                         application_trust.FromXml (application_trust_xml);
334                                 }
335                                 else
336                                         application_trust_xml = null;
337                         }
338                 }
339
340                 [MonoNotSupported ("This property exists but not considered.")]
341                 public bool DisallowApplicationBaseProbing {
342                         get { return disallow_appbase_probe; }
343                         set { disallow_appbase_probe = value; }
344                 }
345
346                 [MonoNotSupported ("This method exists but not considered.")]
347                 public byte [] GetConfigurationBytes ()
348                 {
349                         return configuration_bytes != null ? configuration_bytes.Clone () as byte [] : null;
350                 }
351
352                 [MonoNotSupported ("This method exists but not considered.")]
353                 public void SetConfigurationBytes (byte [] value)
354                 {
355                         configuration_bytes = value;
356                 }
357 #endif
358         }
359 }