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