2007-07-20 Atsushi Enomoto <atsushi@ximian.com>
[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         public sealed class AppDomainSetup : IAppDomainSetup
52         {
53                 string application_base;
54                 string application_name;
55                 string cache_path;
56                 string configuration_file;
57                 string dynamic_base;
58                 string license_file;
59                 string private_bin_path;
60                 string private_bin_path_probe;
61                 string shadow_copy_directories;
62                 string shadow_copy_files;
63                 bool publisher_policy;
64                 private bool path_changed;
65                 private LoaderOptimization loader_optimization;
66                 bool disallow_binding_redirects;
67                 bool disallow_code_downloads;
68
69                 // those fields also exist in the runtime, so we need dummies in 1.x profile too.
70 #if NET_2_0
71                 private ActivationArguments _activationArguments;
72                 AppDomainInitializer domain_initializer;
73                 [NonSerialized]
74                 ApplicationTrust application_trust;
75 #else
76                 object _activationArguments;
77                 object domain_initializer; // always null
78 #endif
79                 string [] domain_initializer_args;
80                 SecurityElement application_trust_xml;
81                 bool disallow_appbase_probe;
82                 byte [] configuration_bytes;
83
84                 public AppDomainSetup ()
85                 {
86                 }
87
88                 internal AppDomainSetup (AppDomainSetup setup)
89                 {
90                         application_base = setup.application_base;
91                         application_name = setup.application_name;
92                         cache_path = setup.cache_path;
93                         configuration_file = setup.configuration_file;
94                         dynamic_base = setup.dynamic_base;
95                         license_file = setup.license_file;
96                         private_bin_path = setup.private_bin_path;
97                         private_bin_path_probe = setup.private_bin_path_probe;
98                         shadow_copy_directories = setup.shadow_copy_directories;
99                         shadow_copy_files = setup.shadow_copy_files;
100                         publisher_policy = setup.publisher_policy;
101                         path_changed = setup.path_changed;
102                         loader_optimization = setup.loader_optimization;
103                         disallow_binding_redirects = setup.disallow_binding_redirects;
104                         disallow_code_downloads = setup.disallow_code_downloads;
105 //#if NET_2_0
106                         _activationArguments = setup._activationArguments;
107                         domain_initializer = setup.domain_initializer;
108                         domain_initializer_args = setup.domain_initializer_args;
109                         application_trust_xml = setup.application_trust_xml;
110                         disallow_appbase_probe = setup.disallow_appbase_probe;
111                         configuration_bytes = setup.configuration_bytes;
112 //#endif
113                 }
114
115 #if NET_2_0
116                 public AppDomainSetup (ActivationArguments activationArguments)
117                 {
118                         _activationArguments = activationArguments;
119                 }
120
121                 public AppDomainSetup (ActivationContext activationContext)
122                 {
123                         _activationArguments = new ActivationArguments (activationContext);
124                 }
125 #endif
126
127                 static string GetAppBase (string appBase)
128                 {
129                         if (appBase == null)
130                                 return null;
131
132                         int len = appBase.Length;
133                         if (len >= 8 && appBase.ToLower ().StartsWith ("file://")) {
134                                 appBase = appBase.Substring (7);
135                                 if (Path.DirectorySeparatorChar != '/')
136                                         appBase = appBase.Replace ('/', Path.DirectorySeparatorChar);
137                                 if (Environment.IsRunningOnWindows) {
138                                         // Under Windows prepend "//" to indicate it's a local file
139                                         appBase = "//" + appBase;
140                                 }
141 #if NET_2_0
142                         } else {
143 #else
144                         // under 1.x the ":" gets a special treatment - but it doesn't make sense outside Windows
145                         } else if (!Environment.IsRunningOnWindows || (appBase.IndexOf (':') == -1)) {
146 #endif
147                                 appBase = Path.GetFullPath (appBase);
148                         }
149
150                         return appBase;
151                 }
152
153                 public string ApplicationBase {
154                         get { return GetAppBase (application_base); }
155                         set { application_base = value; } 
156                 }
157
158                 public string ApplicationName {
159                         get {
160                                 return application_name;
161                         }
162                         set {
163                                 application_name = value;
164                         }
165                 }
166
167                 public string CachePath {
168                         get {
169                                 return cache_path;
170                         }
171                         set {
172                                 cache_path = value;
173                         }
174                 }
175
176                 public string ConfigurationFile {
177                         get {
178                                 if (configuration_file == null)
179                                         return null;
180                                 if (Path.IsPathRooted(configuration_file))
181                                         return configuration_file;
182                                 if (ApplicationBase == null)
183                                         throw new MemberAccessException("The ApplicationBase must be set before retrieving this property.");
184                                 return Path.Combine(ApplicationBase, configuration_file);
185                         }
186                         set {
187                                 configuration_file = value;
188                         }
189                 }
190
191                 public bool DisallowPublisherPolicy {
192                         get {
193                                 return publisher_policy;
194                         }
195                         set {
196                                 publisher_policy = value;
197                         }
198                 }
199
200                 public string DynamicBase {
201                         get {
202                                 if (dynamic_base == null)
203                                         return null;
204
205                                 if (Path.IsPathRooted (dynamic_base))
206                                         return dynamic_base;
207
208                                 if (ApplicationBase == null)
209                                         throw new MemberAccessException ("The ApplicationBase must be set before retrieving this property.");
210                                 
211                                 return Path.Combine (ApplicationBase, dynamic_base);
212                         }
213                         set {
214                                 if (application_name == null)
215                                         throw new MemberAccessException ("ApplicationName must be set before the DynamicBase can be set.");
216                                 uint id = (uint) application_name.GetHashCode ();
217                                 dynamic_base = Path.Combine (value, id.ToString("x"));
218                         }
219                 }
220
221                 public string LicenseFile {
222                         get {
223                                 return license_file;
224                         }
225                         set {
226                                 license_file = value;
227                         }
228                 }
229
230                 [MonoTODO ("In Mono this is controlled by the --share-code flag")]
231                 public LoaderOptimization LoaderOptimization {
232                         get {
233                                 return loader_optimization;
234                         }
235                         set {
236                                 loader_optimization = value;
237                         }
238                 }
239
240                 public string PrivateBinPath {
241                         get {
242                                 return private_bin_path;
243                         }
244                         set {
245                                 private_bin_path = value;
246                                 path_changed = true;
247                         }
248                 }
249
250                 public string PrivateBinPathProbe {
251                         get {
252                                 return private_bin_path_probe;
253                         }
254                         set {
255                                 private_bin_path_probe = value;
256                                 path_changed = true;
257                         }
258                 }
259
260                 public string ShadowCopyDirectories {
261                         get {
262                                 return shadow_copy_directories;
263                         }
264                         set {
265                                 shadow_copy_directories = value;
266                         }
267                 }
268
269                 public string ShadowCopyFiles {
270                         get {
271                                 return shadow_copy_files;
272                         }
273                         set {
274                                 shadow_copy_files = value;
275                         }
276                 }
277
278 #if NET_1_1
279                 public bool DisallowBindingRedirects {
280                         get {
281                                 return disallow_binding_redirects;
282                         }
283                         set {
284                                 disallow_binding_redirects = value;
285                         }
286                 }
287
288                 public bool DisallowCodeDownload {
289                         get {
290                                 return disallow_code_downloads;
291                         }
292                         set {
293                                 disallow_code_downloads = value;
294                         }
295                 }
296 #endif
297
298 #if NET_2_0
299                 public ActivationArguments ActivationArguments {
300                         get { return _activationArguments; }
301                         set { _activationArguments = value; }
302                 }
303
304                 [MonoLimitation ("it needs to be invoked within the created domain")]
305                 public AppDomainInitializer AppDomainInitializer {
306                         get { return domain_initializer; }
307                         set { domain_initializer = value; }
308                 }
309
310                 [MonoLimitation ("it needs to be used to invoke the initializer within the created domain")]
311                 public string [] AppDomainInitializerArguments {
312                         get { return domain_initializer_args; }
313                         set { domain_initializer_args = value; }
314                 }
315
316                 [MonoNotSupported ("This property exists but not considered.")]
317                 public ApplicationTrust ApplicationTrust {
318                         get {
319                                 if (application_trust_xml == null)
320                                         return null;
321                                 if (application_trust == null)
322                                         application_trust = new ApplicationTrust ();
323                                 return application_trust;
324                         }
325                         set {
326                                 application_trust = value;
327                                 if (value != null) {
328                                         application_trust_xml = value.ToXml ();
329                                         application_trust.FromXml (application_trust_xml);
330                                 }
331                                 else
332                                         application_trust_xml = null;
333                         }
334                 }
335
336                 [MonoNotSupported ("This property exists but not considered.")]
337                 public bool DisallowApplicationBaseProbing {
338                         get { return disallow_appbase_probe; }
339                         set { disallow_appbase_probe = value; }
340                 }
341
342                 [MonoNotSupported ("This method exists but not considered.")]
343                 public byte [] GetConfigurationBytes ()
344                 {
345                         return configuration_bytes != null ? configuration_bytes.Clone () as byte [] : null;
346                 }
347
348                 [MonoNotSupported ("This method exists but not considered.")]
349                 public void SetConfigurationBytes (byte [] bytes)
350                 {
351                         configuration_bytes = bytes;
352                 }
353 #endif
354         }
355 }