[Cleanup] Removed TARGET_JVM
[mono.git] / mcs / class / System.Configuration / System.Configuration / InternalConfigurationHost.cs
1 //
2 // System.Configuration.InternalConfigurationHost.cs
3 //
4 // Authors:
5 //  Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 // 
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 // 
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
27 //
28
29 using System;
30 using System.IO;
31 using System.Security;
32 using System.Configuration.Internal;
33 using System.Runtime.CompilerServices;
34
35 namespace System.Configuration
36 {
37         abstract class InternalConfigurationHost: IInternalConfigHost
38         {
39                 public virtual object CreateConfigurationContext (string configPath, string locationSubPath)
40                 {
41                         return null;
42                 }
43                 
44                 public virtual object CreateDeprecatedConfigContext (string configPath)
45                 {
46                         return null;
47                 }
48                 
49                 public virtual void DeleteStream (string streamName)
50                 {
51                         File.Delete (streamName);
52                 }
53                 
54                 string IInternalConfigHost.DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
55                 {
56                         return protectedSection.DecryptSection (encryptedXml, protectionProvider);
57                 }
58                 
59                 string IInternalConfigHost.EncryptSection (string clearXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
60                 {
61                         return protectedSection.EncryptSection (clearXml, protectionProvider);
62                 }
63                 
64                 public virtual string GetConfigPathFromLocationSubPath (string configPath, string locationSubPath)
65                 {
66                         return configPath;
67                 }
68                 
69                 public virtual Type GetConfigType (string typeName, bool throwOnError)
70                 {
71                         Type type = Type.GetType (typeName);
72                         if (type == null && throwOnError)
73                                 throw new ConfigurationErrorsException ("Type '" + typeName + "' not found.");
74                         return type;
75                 }
76                 
77                 public virtual string GetConfigTypeName (Type t)
78                 {
79                         return t.AssemblyQualifiedName;
80                 }
81                 
82                 public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
83                 {
84                         throw new NotImplementedException ();
85                 }
86                 
87                 public abstract string GetStreamName (string configPath);
88                 public abstract void Init (IInternalConfigRoot root, params object[] hostInitParams);
89                 public abstract void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams);
90
91                 [MonoNotSupported ("mono does not support remote configuration")]
92                 public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
93                 {
94                         throw new NotSupportedException ("mono does not support remote configuration");
95                 }
96                 
97                 public virtual object GetStreamVersion (string streamName)
98                 {
99                         throw new NotImplementedException ();
100                 }
101                 
102                 public virtual IDisposable Impersonate ()
103                 {
104                         throw new NotImplementedException ();
105                 }
106                 
107                 public virtual bool IsAboveApplication (string configPath)
108                 {
109                         throw new NotImplementedException ();
110                 }
111                 
112                 public virtual bool IsConfigRecordRequired (string configPath)
113                 {
114                         throw new NotImplementedException ();
115                 }
116                 
117                 public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
118                 {
119                         switch (allowDefinition) {
120                                 case ConfigurationAllowDefinition.MachineOnly:
121                                         return configPath == "machine";
122                                 case ConfigurationAllowDefinition.MachineToApplication:
123                                         return configPath == "machine" || configPath == "exe";
124                                 default:
125                                         return true;
126                         }
127                 }
128                 
129                 public virtual bool IsFile (string streamName)
130                 {
131                         throw new NotImplementedException ();
132                 }
133
134                 public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
135                 {
136                         throw new NotImplementedException ();
137                 }
138
139                 public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
140                 {
141                         throw new NotImplementedException ();
142                 }
143
144                 public virtual bool IsLocationApplicable (string configPath)
145                 {
146                         throw new NotImplementedException ();
147                 }
148
149                 public virtual bool IsRemote {
150                         get {
151                                 throw new NotImplementedException ();
152                         }
153                 }
154
155                 public virtual bool IsSecondaryRoot (string configPath)
156                 {
157                         throw new NotImplementedException ();
158                 }
159
160                 public virtual bool IsTrustedConfigPath (string configPath)
161                 {
162                         throw new NotImplementedException ();
163                 }
164                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
165                 extern private static string get_bundled_machine_config ();
166
167                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
168                 extern private static string get_bundled_app_config ();
169                 
170                 public virtual Stream OpenStreamForRead (string streamName)
171                 {
172                         if (String.CompareOrdinal (streamName, System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile) == 0) {
173                                 string bundle = get_bundled_machine_config ();
174                                 if (bundle != null)
175                                         return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
176                         }
177
178                         if (String.CompareOrdinal (streamName, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) == 0) {
179                                 string bundle = get_bundled_app_config ();
180                                 if (bundle != null)
181                                         return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
182                         }
183
184                         if (!File.Exists (streamName))
185                                 return null;
186                                 
187                         return new FileStream (streamName, FileMode.Open, FileAccess.Read);
188                 }
189
190                 public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
191                 {
192                         throw new NotImplementedException ();
193                 }
194                 
195                 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
196                 {
197                         string dir = Path.GetDirectoryName (streamName);
198                         if (!String.IsNullOrEmpty (dir) && !Directory.Exists (dir))
199                                 Directory.CreateDirectory (dir);
200                         return new FileStream (streamName, FileMode.Create, FileAccess.Write);
201                 }
202
203                 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
204                 {
205                         throw new NotImplementedException ();
206                 }
207                 
208                 public virtual bool PrefetchAll (string configPath, string streamName)
209                 {
210                         throw new NotImplementedException ();
211                 }
212                 
213                 public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
214                 {
215                         throw new NotImplementedException ();
216                 }
217
218                 public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
219                 {
220                         throw new NotImplementedException ();
221                 }
222
223                 public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
224                 {
225                         throw new NotImplementedException ();
226                 }
227                 
228                 public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
229                 {
230                         throw new NotImplementedException ();
231                 }
232                 
233                 public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
234                 {
235                         if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
236                                 throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
237                 }
238                 
239                 public virtual void WriteCompleted (string streamName, bool success, object writeContext)
240                 {
241                 }
242
243                 public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
244                 {
245                 }
246                 
247                 public virtual bool SupportsChangeNotifications {
248                         get { return false; }
249                 }
250                 
251                 public virtual bool SupportsLocation {
252                         get { return false; }
253                 }
254                 
255                 public virtual bool SupportsPath {
256                         get { return false; }
257                 }
258                 
259                 public virtual bool SupportsRefresh {
260                         get { return false; }
261                 }
262         }
263         
264         class ExeConfigurationHost: InternalConfigurationHost
265         {
266                 ExeConfigurationFileMap map;
267                 ConfigurationUserLevel level;
268                 
269                 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
270                 {
271                         map = (ExeConfigurationFileMap) hostInitParams [0];
272                         level = (ConfigurationUserLevel) hostInitParams [1];
273                         CheckFileMap (level, map);
274                 }
275
276                 static void CheckFileMap (ConfigurationUserLevel level, ExeConfigurationFileMap map)
277                 {
278                         switch (level) {
279                         case ConfigurationUserLevel.None:
280                                 if (string.IsNullOrEmpty (map.ExeConfigFilename))
281                                         throw new ArgumentException (
282                                                 "The 'ExeConfigFilename' argument cannot be null.");
283                                 break;
284                         case ConfigurationUserLevel.PerUserRoamingAndLocal:
285                                 if (string.IsNullOrEmpty (map.LocalUserConfigFilename))
286                                         throw new ArgumentException (
287                                                 "The 'LocalUserConfigFilename' argument cannot be null.");
288                                 goto case ConfigurationUserLevel.PerUserRoaming;
289                         case ConfigurationUserLevel.PerUserRoaming:
290                                 if (string.IsNullOrEmpty (map.RoamingUserConfigFilename))
291                                         throw new ArgumentException (
292                                                 "The 'RoamingUserConfigFilename' argument cannot be null.");
293                                 goto case ConfigurationUserLevel.None;
294                         }
295                 }
296                 
297                 public override string GetStreamName (string configPath)
298                 {
299                         switch (configPath) {
300                                 case "exe": return map.ExeConfigFilename; 
301                                 case "local": return map.LocalUserConfigFilename;
302                                 case "roaming": return map.RoamingUserConfigFilename;
303                                 case "machine": return map.MachineConfigFilename;
304                                 default://return map.ExeConfigFilename;
305                                 switch (level) {
306                                         case ConfigurationUserLevel.None:
307                                                 return map.ExeConfigFilename; 
308                                         case ConfigurationUserLevel.PerUserRoaming:
309                                                 return map.RoamingUserConfigFilename;
310                                         case ConfigurationUserLevel.PerUserRoamingAndLocal:
311                                                 return map.LocalUserConfigFilename;
312                                         default:
313                                                 return map.MachineConfigFilename;
314                                 }
315                         }
316                 }
317                 
318                 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
319                 {
320                         map = (ExeConfigurationFileMap) hostInitConfigurationParams [0];
321
322                         if (hostInitConfigurationParams.Length > 1 && 
323                             hostInitConfigurationParams [1] is ConfigurationUserLevel)
324                                 level = (ConfigurationUserLevel) hostInitConfigurationParams [1];
325
326                         CheckFileMap (level, map);
327
328                         if (locationSubPath == null)
329                                 switch (level) {
330                                 case ConfigurationUserLevel.PerUserRoaming:
331                                         if (map.RoamingUserConfigFilename == null)
332                                                 throw new ArgumentException ("RoamingUserConfigFilename must be set correctly");
333                                         locationSubPath = "roaming";
334                                         break;
335                                 case ConfigurationUserLevel.PerUserRoamingAndLocal:
336                                         if (map.LocalUserConfigFilename == null)
337                                                 throw new ArgumentException ("LocalUserConfigFilename must be set correctly");
338                                         locationSubPath = "local";
339                                         break;
340                                 }
341
342                         configPath = null;
343                         string next = null;
344
345                         locationConfigPath = null;
346
347                         if (locationSubPath == "exe" || locationSubPath == null && map.ExeConfigFilename != null) {
348                                 configPath = "exe";
349                                 next = "machine";
350                                 locationConfigPath = map.ExeConfigFilename;
351                         }
352                         
353                         if (locationSubPath == "local" && map.LocalUserConfigFilename != null) {
354                                 configPath = "local";
355                                 next = "roaming";
356                                 locationConfigPath = map.LocalUserConfigFilename;
357                         }
358                         
359                         if (locationSubPath == "roaming" && map.RoamingUserConfigFilename != null) {
360                                 configPath = "roaming";
361                                 next = "exe";
362                                 locationConfigPath = map.RoamingUserConfigFilename;
363                         }
364                         
365                         if ((locationSubPath == "machine" || configPath == null) && map.MachineConfigFilename != null) {
366                                 configPath = "machine";
367                                 next = null;
368                         }
369                         locationSubPath = next;
370                 }
371         }
372         
373         class MachineConfigurationHost: InternalConfigurationHost
374         {
375                 ConfigurationFileMap map;
376                 
377                 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
378                 {
379                         map = (ConfigurationFileMap) hostInitParams [0];
380                 }
381                 
382                 public override string GetStreamName (string configPath)
383                 {
384                         return map.MachineConfigFilename;
385                 }
386                 
387                 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
388                 {
389                         map = (ConfigurationFileMap) hostInitConfigurationParams [0];
390                         locationSubPath = null;
391                         configPath = null;
392                         locationConfigPath = null;
393                 }
394
395                 public override bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
396                 {
397                         return true;
398                 }
399         }
400 }
401