2 // System.AppDomainSetup.cs
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Fix serialization compatibility with MS.NET.
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Security;
38 using System.Runtime.Serialization.Formatters.Binary;
41 using System.Runtime.Hosting;
42 using System.Security.Policy;
48 [ClassInterface (ClassInterfaceType.None)]
51 public sealed class AppDomainSetup
53 public sealed class AppDomainSetup : IAppDomainSetup
56 string application_base;
57 string application_name;
59 string configuration_file;
62 string private_bin_path;
63 string private_bin_path_probe;
64 string shadow_copy_directories;
65 string shadow_copy_files;
66 bool publisher_policy;
67 private bool path_changed;
68 private LoaderOptimization loader_optimization;
69 bool disallow_binding_redirects;
70 bool disallow_code_downloads;
73 private ActivationArguments _activationArguments;
74 AppDomainInitializer domain_initializer;
76 ApplicationTrust application_trust;
77 string [] domain_initializer_args;
79 object _activationArguments;
80 object domain_initializer; // always null
82 object application_trust; // dummy, always null
83 object domain_initializer_args;
85 bool disallow_appbase_probe;
86 byte [] configuration_bytes;
88 byte [] serialized_non_primitives;
90 public AppDomainSetup ()
94 internal AppDomainSetup (AppDomainSetup setup)
96 application_base = setup.application_base;
97 application_name = setup.application_name;
98 cache_path = setup.cache_path;
99 configuration_file = setup.configuration_file;
100 dynamic_base = setup.dynamic_base;
101 license_file = setup.license_file;
102 private_bin_path = setup.private_bin_path;
103 private_bin_path_probe = setup.private_bin_path_probe;
104 shadow_copy_directories = setup.shadow_copy_directories;
105 shadow_copy_files = setup.shadow_copy_files;
106 publisher_policy = setup.publisher_policy;
107 path_changed = setup.path_changed;
108 loader_optimization = setup.loader_optimization;
109 disallow_binding_redirects = setup.disallow_binding_redirects;
110 disallow_code_downloads = setup.disallow_code_downloads;
111 _activationArguments = setup._activationArguments;
112 domain_initializer = setup.domain_initializer;
113 application_trust = setup.application_trust;
114 domain_initializer_args = setup.domain_initializer_args;
115 disallow_appbase_probe = setup.disallow_appbase_probe;
116 configuration_bytes = setup.configuration_bytes;
120 public AppDomainSetup (ActivationArguments activationArguments)
122 _activationArguments = activationArguments;
125 public AppDomainSetup (ActivationContext activationContext)
127 _activationArguments = new ActivationArguments (activationContext);
131 static string GetAppBase (string appBase)
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;
146 appBase = Path.GetFullPath (appBase);
152 public string ApplicationBase {
153 get { return GetAppBase (application_base); }
154 set { application_base = value; }
157 public string ApplicationName {
159 return application_name;
162 application_name = value;
166 public string CachePath {
175 public string ConfigurationFile {
177 if (configuration_file == null)
179 if (Path.IsPathRooted(configuration_file))
180 return configuration_file;
181 if (ApplicationBase == null)
182 throw new MemberAccessException("The ApplicationBase must be set before retrieving this property.");
183 return Path.Combine(ApplicationBase, configuration_file);
186 configuration_file = value;
190 public bool DisallowPublisherPolicy {
192 return publisher_policy;
195 publisher_policy = value;
199 public string DynamicBase {
201 if (dynamic_base == null)
204 if (Path.IsPathRooted (dynamic_base))
207 if (ApplicationBase == null)
208 throw new MemberAccessException ("The ApplicationBase must be set before retrieving this property.");
210 return Path.Combine (ApplicationBase, dynamic_base);
213 if (application_name == null)
214 throw new MemberAccessException ("ApplicationName must be set before the DynamicBase can be set.");
215 uint id = (uint) application_name.GetHashCode ();
216 dynamic_base = Path.Combine (value, id.ToString("x"));
220 public string LicenseFile {
225 license_file = value;
229 [MonoLimitation ("In Mono this is controlled by the --share-code flag")]
230 public LoaderOptimization LoaderOptimization {
232 return loader_optimization;
235 loader_optimization = value;
239 public string PrivateBinPath {
241 return private_bin_path;
244 private_bin_path = value;
249 public string PrivateBinPathProbe {
251 return private_bin_path_probe;
254 private_bin_path_probe = value;
259 public string ShadowCopyDirectories {
261 return shadow_copy_directories;
264 shadow_copy_directories = value;
268 public string ShadowCopyFiles {
270 return shadow_copy_files;
273 shadow_copy_files = value;
277 public bool DisallowBindingRedirects {
279 return disallow_binding_redirects;
282 disallow_binding_redirects = value;
286 public bool DisallowCodeDownload {
288 return disallow_code_downloads;
291 disallow_code_downloads = value;
295 public ActivationArguments ActivationArguments {
297 if (_activationArguments != null)
298 return _activationArguments;
299 DeserializeNonPrimitives ();
300 return _activationArguments;
302 set { _activationArguments = value; }
305 [MonoLimitation ("it needs to be invoked within the created domain")]
306 public AppDomainInitializer AppDomainInitializer {
308 if (domain_initializer != null)
309 return domain_initializer;
310 DeserializeNonPrimitives ();
311 return domain_initializer;
313 set { domain_initializer = value; }
316 [MonoLimitation ("it needs to be used to invoke the initializer within the created domain")]
317 public string [] AppDomainInitializerArguments {
318 get { return domain_initializer_args; }
319 set { domain_initializer_args = value; }
322 [MonoNotSupported ("This property exists but not considered.")]
323 public ApplicationTrust ApplicationTrust {
325 if (application_trust != null)
326 return application_trust;
327 DeserializeNonPrimitives ();
328 if (application_trust == null)
329 application_trust = new ApplicationTrust ();
330 return application_trust;
332 set { application_trust = value; }
335 [MonoNotSupported ("This property exists but not considered.")]
336 public bool DisallowApplicationBaseProbing {
337 get { return disallow_appbase_probe; }
338 set { disallow_appbase_probe = value; }
341 [MonoNotSupported ("This method exists but not considered.")]
342 public byte [] GetConfigurationBytes ()
344 return configuration_bytes != null ? configuration_bytes.Clone () as byte [] : null;
347 [MonoNotSupported ("This method exists but not considered.")]
348 public void SetConfigurationBytes (byte [] value)
350 configuration_bytes = value;
353 private void DeserializeNonPrimitives ()
356 if (serialized_non_primitives == null)
359 BinaryFormatter bf = new BinaryFormatter ();
360 MemoryStream ms = new MemoryStream (serialized_non_primitives);
362 object [] arr = (object []) bf.Deserialize (ms);
364 _activationArguments = (ActivationArguments) arr [0];
365 domain_initializer = (AppDomainInitializer) arr [1];
366 application_trust = (ApplicationTrust) arr [2];
368 serialized_non_primitives = null;
372 internal void SerializeNonPrimitives ()
374 object [] arr = new object [3];
376 arr [0] = _activationArguments;
377 arr [1] = domain_initializer;
378 arr [2] = application_trust;
380 BinaryFormatter bf = new BinaryFormatter ();
381 MemoryStream ms = new MemoryStream ();
383 bf.Serialize (ms, arr);
385 serialized_non_primitives = ms.ToArray ();