* AppDomainSetup.cs: If configuration file is not an absolute path,
[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 #endif
41
42 namespace System
43 {
44         [Serializable]
45         [ClassInterface (ClassInterfaceType.None)]
46 #if NET_2_0
47         [ComVisible (true)]
48 #endif
49         public sealed class AppDomainSetup : IAppDomainSetup
50         {
51                 string application_base;
52                 string application_name;
53                 string cache_path;
54                 string configuration_file;
55                 string dynamic_base;
56                 string license_file;
57                 string private_bin_path;
58                 string private_bin_path_probe;
59                 string shadow_copy_directories;
60                 string shadow_copy_files;
61                 bool publisher_policy;
62                 private bool path_changed;
63                 private LoaderOptimization loader_optimization;
64                 bool disallow_binding_redirects;
65                 bool disallow_code_downloads;
66 #if NET_2_0
67                 private ActivationArguments _activationArguments;
68 #endif
69
70                 public AppDomainSetup ()
71                 {
72                 }
73
74                 internal AppDomainSetup (AppDomainSetup setup)
75                 {
76                         application_base = setup.application_base;
77                         application_name = setup.application_name;
78                         cache_path = setup.cache_path;
79                         configuration_file = setup.configuration_file;
80                         dynamic_base = setup.dynamic_base;
81                         license_file = setup.license_file;
82                         private_bin_path = setup.private_bin_path;
83                         private_bin_path_probe = setup.private_bin_path_probe;
84                         shadow_copy_directories = setup.shadow_copy_directories;
85                         shadow_copy_files = setup.shadow_copy_files;
86                         publisher_policy = setup.publisher_policy;
87                         path_changed = setup.path_changed;
88                         loader_optimization = setup.loader_optimization;
89                         disallow_binding_redirects = setup.disallow_binding_redirects;
90                         disallow_code_downloads = setup.disallow_code_downloads;
91                 }
92
93 #if NET_2_0
94                 public AppDomainSetup (ActivationArguments activationArguments)
95                 {
96                         _activationArguments = activationArguments;
97                 }
98
99                 public AppDomainSetup (ActivationContext activationContext)
100                 {
101                         _activationArguments = new ActivationArguments (activationContext);
102                 }
103 #endif
104
105                 static string GetAppBase (string appBase)
106                 {
107                         if (appBase == null)
108                                 return null;
109
110                         int len = appBase.Length;
111                         if (len >= 8 && appBase.ToLower ().StartsWith ("file://")) {
112                                 appBase = appBase.Substring (7);
113                                 if (Path.DirectorySeparatorChar != '/')
114                                         appBase = appBase.Replace ('/', Path.DirectorySeparatorChar);
115                                 if (Environment.IsRunningOnWindows) {
116                                         // Under Windows prepend "//" to indicate it's a local file
117                                         appBase = "//" + appBase;
118                                 }
119 #if NET_2_0
120                         } else {
121 #else
122                         // under 1.x the ":" gets a special treatment - but it doesn't make sense outside Windows
123                         } else if (!Environment.IsRunningOnWindows || (appBase.IndexOf (':') == -1)) {
124 #endif
125                                 appBase = Path.GetFullPath (appBase);
126                         }
127
128                         return appBase;
129                 }
130                 
131                 public string ApplicationBase {
132                         get { return GetAppBase (application_base); }
133                         set { application_base = value; } 
134                 }
135
136                 public string ApplicationName {
137                         get {
138                                 return application_name;
139                         }
140                         set {
141                                 application_name = value;
142                         }
143                 }
144
145                 public string CachePath {
146                         get {
147                                 return cache_path;
148                         }
149                         set {
150                                 cache_path = value;
151                         }
152                 }
153
154                 public string ConfigurationFile {
155                         get {
156                                 if (configuration_file == null)
157                                         return null;
158                                 if (Path.IsPathRooted(configuration_file))
159                                         return configuration_file;
160                                 if (ApplicationBase == null)
161                                         throw new MemberAccessException("The ApplicationBase must be set before retrieving this property.");
162                                 return Path.Combine(ApplicationBase, configuration_file);
163                         }
164                         set {
165                                 configuration_file = value;
166                         }
167                 }
168
169                 public bool DisallowPublisherPolicy {
170                         get {
171                                 return publisher_policy;
172                         }
173                         set {
174                                 publisher_policy = value;
175                         }
176                 }
177
178                 public string DynamicBase {
179                         get {
180                                 if (dynamic_base == null)
181                                         return null;
182
183                                 if (Path.IsPathRooted (dynamic_base))
184                                         return dynamic_base;
185
186                                 if (ApplicationBase == null)
187                                         throw new MemberAccessException ("The ApplicationBase must be set before retrieving this property.");
188                                 
189                                 return Path.Combine (ApplicationBase, dynamic_base);
190                         }
191                         set {
192                                 if (application_name == null)
193                                         throw new MemberAccessException ("ApplicationName must be set before the DynamicBase can be set.");
194                                 uint id = (uint) application_name.GetHashCode ();
195                                 dynamic_base = Path.Combine (value, id.ToString("x"));
196                         }
197                 }
198
199                 public string LicenseFile {
200                         get {
201                                 return license_file;
202                         }
203                         set {
204                                 license_file = value;
205                         }
206                 }
207
208                 [MonoTODO ("In Mono this is controlled by the --share-code flag")]
209                 public LoaderOptimization LoaderOptimization {
210                         get {
211                                 return loader_optimization;
212                         }
213                         set {
214                                 loader_optimization = value;
215                         }
216                 }
217
218                 public string PrivateBinPath {
219                         get {
220                                 return private_bin_path;
221                         }
222                         set {
223                                 private_bin_path = value;
224                                 path_changed = true;
225                         }
226                 }
227
228                 public string PrivateBinPathProbe {
229                         get {
230                                 return private_bin_path_probe;
231                         }
232                         set {
233                                 private_bin_path_probe = value;
234                                 path_changed = true;
235                         }
236                 }
237
238                 public string ShadowCopyDirectories {
239                         get {
240                                 return shadow_copy_directories;
241                         }
242                         set {
243                                 shadow_copy_directories = value;
244                         }
245                 }
246
247                 public string ShadowCopyFiles {
248                         get {
249                                 return shadow_copy_files;
250                         }
251                         set {
252                                 shadow_copy_files = value;
253                         }
254                 }
255
256 #if NET_1_1
257                 public bool DisallowBindingRedirects {
258                         get {
259                                 return disallow_binding_redirects;
260                         }
261                         set {
262                                 disallow_binding_redirects = value;
263                         }
264                 }
265
266                 public bool DisallowCodeDownload {
267                         get {
268                                 return disallow_code_downloads;
269                         }
270                         set {
271                                 disallow_code_downloads = value;
272                         }
273                 }
274 #endif
275
276 #if NET_2_0
277                 public ActivationArguments ActivationArguments {
278                         get { return _activationArguments; }
279                         set { _activationArguments = value; }
280                 }
281 #endif
282         }
283 }