Merge pull request #556 from jack-pappas/ipproto-patch
[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.Collections.Generic;
35 using System.IO;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38 using System.Security;
39 using System.Runtime.Serialization.Formatters.Binary;
40
41 using System.Runtime.Hosting;
42 using System.Security.Policy;
43
44 namespace System
45 {
46         [Serializable]
47         [ClassInterface (ClassInterfaceType.None)]
48         [ComVisible (true)]
49         [StructLayout (LayoutKind.Sequential)]
50 #if NET_2_1
51         public sealed class AppDomainSetup
52 #else
53         public sealed class AppDomainSetup : IAppDomainSetup
54 #endif
55         {
56                 string application_base;
57                 string application_name;
58                 string cache_path;
59                 string configuration_file;
60                 string dynamic_base;
61                 string license_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;
71
72                 private ActivationArguments _activationArguments;
73                 AppDomainInitializer domain_initializer;
74                 [NonSerialized]
75                 ApplicationTrust application_trust;
76                 string [] domain_initializer_args;
77
78                 bool disallow_appbase_probe;
79                 byte [] configuration_bytes;
80
81                 byte [] serialized_non_primitives;
82
83                 public AppDomainSetup ()
84                 {
85                 }
86
87                 internal AppDomainSetup (AppDomainSetup setup)
88                 {
89                         application_base = setup.application_base;
90                         application_name = setup.application_name;
91                         cache_path = setup.cache_path;
92                         configuration_file = setup.configuration_file;
93                         dynamic_base = setup.dynamic_base;
94                         license_file = setup.license_file;
95                         private_bin_path = setup.private_bin_path;
96                         private_bin_path_probe = setup.private_bin_path_probe;
97                         shadow_copy_directories = setup.shadow_copy_directories;
98                         shadow_copy_files = setup.shadow_copy_files;
99                         publisher_policy = setup.publisher_policy;
100                         path_changed = setup.path_changed;
101                         loader_optimization = setup.loader_optimization;
102                         disallow_binding_redirects = setup.disallow_binding_redirects;
103                         disallow_code_downloads = setup.disallow_code_downloads;
104                         _activationArguments = setup._activationArguments;
105                         domain_initializer = setup.domain_initializer;
106                         application_trust = setup.application_trust;
107                         domain_initializer_args = setup.domain_initializer_args;
108                         disallow_appbase_probe = setup.disallow_appbase_probe;
109                         configuration_bytes = setup.configuration_bytes;
110                 }
111
112                 public AppDomainSetup (ActivationArguments activationArguments)
113                 {
114                         _activationArguments = activationArguments;
115                 }
116
117                 public AppDomainSetup (ActivationContext activationContext)
118                 {
119                         _activationArguments = new ActivationArguments (activationContext);
120                 }
121
122                 static string GetAppBase (string appBase)
123                 {
124                         if (appBase == null)
125                                 return null;
126
127                         int len = appBase.Length;
128                         if (len >= 8 && appBase.ToLower ().StartsWith ("file://")) {
129                                 appBase = appBase.Substring (7);
130                                 if (Path.DirectorySeparatorChar != '/')
131                                         appBase = appBase.Replace ('/', Path.DirectorySeparatorChar);
132                                 if (Environment.IsRunningOnWindows) {
133                                         // Under Windows prepend "//" to indicate it's a local file
134                                         appBase = "//" + appBase;
135                                 }
136                         } else {
137                                 appBase = Path.GetFullPath (appBase);
138                         }
139
140                         return appBase;
141                 }
142
143                 public string ApplicationBase {
144                         get { return GetAppBase (application_base); }
145                         set { application_base = value; } 
146                 }
147
148                 public string ApplicationName {
149                         get {
150                                 return application_name;
151                         }
152                         set {
153                                 application_name = value;
154                         }
155                 }
156
157                 public string CachePath {
158                         get {
159                                 return cache_path;
160                         }
161                         set {
162                                 cache_path = value;
163                         }
164                 }
165
166                 public string ConfigurationFile {
167                         get {
168                                 if (configuration_file == null)
169                                         return null;
170                                 if (Path.IsPathRooted(configuration_file))
171                                         return configuration_file;
172                                 if (ApplicationBase == null)
173                                         throw new MemberAccessException("The ApplicationBase must be set before retrieving this property.");
174                                 return Path.Combine(ApplicationBase, configuration_file);
175                         }
176                         set {
177                                 configuration_file = value;
178                         }
179                 }
180
181                 public bool DisallowPublisherPolicy {
182                         get {
183                                 return publisher_policy;
184                         }
185                         set {
186                                 publisher_policy = value;
187                         }
188                 }
189
190                 public string DynamicBase {
191                         get {
192                                 if (dynamic_base == null)
193                                         return null;
194
195                                 if (Path.IsPathRooted (dynamic_base))
196                                         return dynamic_base;
197
198                                 if (ApplicationBase == null)
199                                         throw new MemberAccessException ("The ApplicationBase must be set before retrieving this property.");
200                                 
201                                 return Path.Combine (ApplicationBase, dynamic_base);
202                         }
203                         set {
204                                 if (application_name == null)
205                                         throw new MemberAccessException ("ApplicationName must be set before the DynamicBase can be set.");
206                                 uint id = (uint) application_name.GetHashCode ();
207                                 dynamic_base = Path.Combine (value, id.ToString("x"));
208                         }
209                 }
210
211                 public string LicenseFile {
212                         get {
213                                 return license_file;
214                         }
215                         set {
216                                 license_file = value;
217                         }
218                 }
219
220                 [MonoLimitation ("In Mono this is controlled by the --share-code flag")]
221                 public LoaderOptimization LoaderOptimization {
222                         get {
223                                 return loader_optimization;
224                         }
225                         set {
226                                 loader_optimization = value;
227                         }
228                 }
229
230                 public string PrivateBinPath {
231                         get {
232                                 return private_bin_path;
233                         }
234                         set {
235                                 private_bin_path = value;
236                                 path_changed = true;
237                         }
238                 }
239
240                 public string PrivateBinPathProbe {
241                         get {
242                                 return private_bin_path_probe;
243                         }
244                         set {
245                                 private_bin_path_probe = value;
246                                 path_changed = true;
247                         }
248                 }
249
250                 public string ShadowCopyDirectories {
251                         get {
252                                 return shadow_copy_directories;
253                         }
254                         set {
255                                 shadow_copy_directories = value;
256                         }
257                 }
258
259                 public string ShadowCopyFiles {
260                         get {
261                                 return shadow_copy_files;
262                         }
263                         set {
264                                 shadow_copy_files = value;
265                         }
266                 }
267
268                 public bool DisallowBindingRedirects {
269                         get {
270                                 return disallow_binding_redirects;
271                         }
272                         set {
273                                 disallow_binding_redirects = value;
274                         }
275                 }
276
277                 public bool DisallowCodeDownload {
278                         get {
279                                 return disallow_code_downloads;
280                         }
281                         set {
282                                 disallow_code_downloads = value;
283                         }
284                 }
285
286                 public ActivationArguments ActivationArguments {
287                         get {
288                                 if (_activationArguments != null)
289                                         return _activationArguments;
290                                 DeserializeNonPrimitives ();
291                                 return _activationArguments;
292                         }
293                         set { _activationArguments = value; }
294                 }
295
296                 [MonoLimitation ("it needs to be invoked within the created domain")]
297                 public AppDomainInitializer AppDomainInitializer {
298                         get {
299                                 if (domain_initializer != null)
300                                         return domain_initializer;
301                                 DeserializeNonPrimitives ();
302                                 return domain_initializer;
303                         }
304                         set { domain_initializer = value; }
305                 }
306
307                 [MonoLimitation ("it needs to be used to invoke the initializer within the created domain")]
308                 public string [] AppDomainInitializerArguments {
309                         get { return domain_initializer_args; }
310                         set { domain_initializer_args = value; }
311                 }
312
313                 [MonoNotSupported ("This property exists but not considered.")]
314                 public ApplicationTrust ApplicationTrust {
315                         get {
316                                 if (application_trust != null)
317                                         return application_trust;
318                                 DeserializeNonPrimitives ();
319                                 if (application_trust == null)
320                                         application_trust = new ApplicationTrust ();
321                                 return application_trust;
322                         }
323                         set { application_trust = value; }
324                 }
325
326                 [MonoNotSupported ("This property exists but not considered.")]
327                 public bool DisallowApplicationBaseProbing {
328                         get { return disallow_appbase_probe; }
329                         set { disallow_appbase_probe = value; }
330                 }
331
332                 [MonoNotSupported ("This method exists but not considered.")]
333                 public byte [] GetConfigurationBytes ()
334                 {
335                         return configuration_bytes != null ? configuration_bytes.Clone () as byte [] : null;
336                 }
337
338                 [MonoNotSupported ("This method exists but not considered.")]
339                 public void SetConfigurationBytes (byte [] value)
340                 {
341                         configuration_bytes = value;
342                 }
343
344                 private void DeserializeNonPrimitives ()
345                 {
346                         lock (this) {
347                                 if (serialized_non_primitives == null)
348                                         return;
349
350                                 BinaryFormatter bf = new BinaryFormatter ();
351                                 MemoryStream ms = new MemoryStream (serialized_non_primitives);
352
353                                 object [] arr = (object []) bf.Deserialize (ms);
354
355                                 _activationArguments = (ActivationArguments) arr [0];
356                                 domain_initializer = (AppDomainInitializer) arr [1];
357                                 application_trust = (ApplicationTrust) arr [2];
358
359                                 serialized_non_primitives = null;
360                         }
361                 }
362
363                 internal void SerializeNonPrimitives ()
364                 {
365                         object [] arr = new object [3];
366
367                         arr [0] = _activationArguments;
368                         arr [1] = domain_initializer;
369                         arr [2] = application_trust;
370
371                         BinaryFormatter bf = new BinaryFormatter ();
372                         MemoryStream ms = new MemoryStream ();
373
374                         bf.Serialize (ms, arr);
375
376                         serialized_non_primitives = ms.ToArray ();
377                 }
378
379 #if NET_4_0
380                 [MonoTODO ("not implemented, does not throw because it's used in testing moonlight")]
381                 public void SetCompatibilitySwitches (IEnumerable<string> switches)
382                 {
383                 }
384 #endif
385         }
386 }