New test.
[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 using System.Runtime.Serialization.Formatters.Binary;
39
40 #if(!MOONLIGHT)
41 using System.Runtime.Hosting;
42 using System.Security.Policy;
43 #endif
44
45 namespace System
46 {
47         [Serializable]
48         [ClassInterface (ClassInterfaceType.None)]
49         [ComVisible (true)]
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 #if (!MOONLIGHT)
73                 private ActivationArguments _activationArguments;
74                 AppDomainInitializer domain_initializer;
75                 [NonSerialized]
76                 ApplicationTrust application_trust;
77                 string [] domain_initializer_args;
78 #else
79                 object _activationArguments;
80                 object domain_initializer; // always null
81                 [NonSerialized]
82                 object application_trust;  // dummy, always null
83                 object domain_initializer_args;
84 #endif
85                 bool disallow_appbase_probe;
86                 byte [] configuration_bytes;
87
88                 byte [] serialized_non_primitives;
89
90                 public AppDomainSetup ()
91                 {
92                 }
93
94                 internal AppDomainSetup (AppDomainSetup setup)
95                 {
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;
117                 }
118
119 #if (!MOONLIGHT)
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                         } else {
146                                 appBase = Path.GetFullPath (appBase);
147                         }
148
149                         return appBase;
150                 }
151
152                 public string ApplicationBase {
153                         get { return GetAppBase (application_base); }
154                         set { application_base = value; } 
155                 }
156
157                 public string ApplicationName {
158                         get {
159                                 return application_name;
160                         }
161                         set {
162                                 application_name = value;
163                         }
164                 }
165 #if !MOONLIGHT
166                 public string CachePath {
167                         get {
168                                 return cache_path;
169                         }
170                         set {
171                                 cache_path = value;
172                         }
173                 }
174
175                 public string ConfigurationFile {
176                         get {
177                                 if (configuration_file == null)
178                                         return 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);
184                         }
185                         set {
186                                 configuration_file = value;
187                         }
188                 }
189
190                 public bool DisallowPublisherPolicy {
191                         get {
192                                 return publisher_policy;
193                         }
194                         set {
195                                 publisher_policy = value;
196                         }
197                 }
198
199                 public string DynamicBase {
200                         get {
201                                 if (dynamic_base == null)
202                                         return null;
203
204                                 if (Path.IsPathRooted (dynamic_base))
205                                         return dynamic_base;
206
207                                 if (ApplicationBase == null)
208                                         throw new MemberAccessException ("The ApplicationBase must be set before retrieving this property.");
209                                 
210                                 return Path.Combine (ApplicationBase, dynamic_base);
211                         }
212                         set {
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"));
217                         }
218                 }
219
220                 public string LicenseFile {
221                         get {
222                                 return license_file;
223                         }
224                         set {
225                                 license_file = value;
226                         }
227                 }
228 #endif
229                 [MonoLimitation ("In Mono this is controlled by the --share-code flag")]
230                 public LoaderOptimization LoaderOptimization {
231                         get {
232                                 return loader_optimization;
233                         }
234                         set {
235                                 loader_optimization = value;
236                         }
237                 }
238 #if !MOONLIGHT
239                 public string PrivateBinPath {
240                         get {
241                                 return private_bin_path;
242                         }
243                         set {
244                                 private_bin_path = value;
245                                 path_changed = true;
246                         }
247                 }
248
249                 public string PrivateBinPathProbe {
250                         get {
251                                 return private_bin_path_probe;
252                         }
253                         set {
254                                 private_bin_path_probe = value;
255                                 path_changed = true;
256                         }
257                 }
258
259                 public string ShadowCopyDirectories {
260                         get {
261                                 return shadow_copy_directories;
262                         }
263                         set {
264                                 shadow_copy_directories = value;
265                         }
266                 }
267
268                 public string ShadowCopyFiles {
269                         get {
270                                 return shadow_copy_files;
271                         }
272                         set {
273                                 shadow_copy_files = value;
274                         }
275                 }
276
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
295                 public ActivationArguments ActivationArguments {
296                         get {
297                                 if (_activationArguments != null)
298                                         return _activationArguments;
299                                 DeserializeNonPrimitives ();
300                                 return _activationArguments;
301                         }
302                         set { _activationArguments = value; }
303                 }
304
305                 [MonoLimitation ("it needs to be invoked within the created domain")]
306                 public AppDomainInitializer AppDomainInitializer {
307                         get {
308                                 if (domain_initializer != null)
309                                         return domain_initializer;
310                                 DeserializeNonPrimitives ();
311                                 return domain_initializer;
312                         }
313                         set { domain_initializer = value; }
314                 }
315
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; }
320                 }
321
322                 [MonoNotSupported ("This property exists but not considered.")]
323                 public ApplicationTrust ApplicationTrust {
324                         get {
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;
331                         }
332                         set { application_trust = value; }
333                 }
334
335                 [MonoNotSupported ("This property exists but not considered.")]
336                 public bool DisallowApplicationBaseProbing {
337                         get { return disallow_appbase_probe; }
338                         set { disallow_appbase_probe = value; }
339                 }
340
341                 [MonoNotSupported ("This method exists but not considered.")]
342                 public byte [] GetConfigurationBytes ()
343                 {
344                         return configuration_bytes != null ? configuration_bytes.Clone () as byte [] : null;
345                 }
346
347                 [MonoNotSupported ("This method exists but not considered.")]
348                 public void SetConfigurationBytes (byte [] value)
349                 {
350                         configuration_bytes = value;
351                 }
352
353                 private void DeserializeNonPrimitives ()
354                 {
355                         lock (this) {
356                                 if (serialized_non_primitives == null)
357                                         return;
358
359                                 BinaryFormatter bf = new BinaryFormatter ();
360                                 MemoryStream ms = new MemoryStream (serialized_non_primitives);
361
362                                 object [] arr = (object []) bf.Deserialize (ms);
363
364                                 _activationArguments = (ActivationArguments) arr [0];
365                                 domain_initializer = (AppDomainInitializer) arr [1];
366                                 application_trust = (ApplicationTrust) arr [2];
367
368                                 serialized_non_primitives = null;
369                         }
370                 }
371
372                 internal void SerializeNonPrimitives ()
373                 {
374                         object [] arr = new object [3];
375
376                         arr [0] = _activationArguments;
377                         arr [1] = domain_initializer;
378                         arr [2] = application_trust;
379
380                         BinaryFormatter bf = new BinaryFormatter ();
381                         MemoryStream ms = new MemoryStream ();
382
383                         bf.Serialize (ms, arr);
384
385                         serialized_non_primitives = ms.ToArray ();
386                 }
387 #endif // !NET_2_1
388         }
389 }