Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[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 #if NET_2_0
30
31 using System;
32 using System.IO;
33 using System.Security;
34 using System.Configuration.Internal;
35 using System.Runtime.CompilerServices;
36
37 namespace System.Configuration
38 {
39         abstract class InternalConfigurationHost: IInternalConfigHost
40         {
41                 public virtual object CreateConfigurationContext (string configPath, string locationSubPath)
42                 {
43                         return null;
44                 }
45                 
46                 public virtual object CreateDeprecatedConfigContext (string configPath)
47                 {
48                         return null;
49                 }
50                 
51                 public virtual void DeleteStream (string streamName)
52                 {
53                         File.Delete (streamName);
54                 }
55                 
56                 string IInternalConfigHost.DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
57                 {
58                         return protectedSection.DecryptSection (encryptedXml, protectionProvider);
59                 }
60                 
61                 string IInternalConfigHost.EncryptSection (string clearXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
62                 {
63                         return protectedSection.EncryptSection (clearXml, protectionProvider);
64                 }
65                 
66                 public virtual string GetConfigPathFromLocationSubPath (string configPath, string locationSubPath)
67                 {
68                         return configPath;
69                 }
70                 
71                 public virtual Type GetConfigType (string typeName, bool throwOnError)
72                 {
73                         Type type = Type.GetType (typeName);
74                         if (type == null && throwOnError)
75                                 throw new ConfigurationErrorsException ("Type '" + typeName + "' not found.");
76                         return type;
77                 }
78                 
79                 public virtual string GetConfigTypeName (Type t)
80                 {
81                         return t.AssemblyQualifiedName;
82                 }
83                 
84                 public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
85                 {
86                         throw new NotImplementedException ();
87                 }
88                 
89                 public abstract string GetStreamName (string configPath);
90                 public abstract void Init (IInternalConfigRoot root, params object[] hostInitParams);
91                 public abstract void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams);
92
93                 [MonoNotSupported ("mono does not support remote configuration")]
94                 public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
95                 {
96                         throw new NotSupportedException ("mono does not support remote configuration");
97                 }
98                 
99                 public virtual object GetStreamVersion (string streamName)
100                 {
101                         throw new NotImplementedException ();
102                 }
103                 
104                 public virtual IDisposable Impersonate ()
105                 {
106                         throw new NotImplementedException ();
107                 }
108                 
109                 public virtual bool IsAboveApplication (string configPath)
110                 {
111                         throw new NotImplementedException ();
112                 }
113                 
114                 public virtual bool IsConfigRecordRequired (string configPath)
115                 {
116                         throw new NotImplementedException ();
117                 }
118                 
119                 public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
120                 {
121                         switch (allowDefinition) {
122                                 case ConfigurationAllowDefinition.MachineOnly:
123                                         return configPath == "machine";
124                                 case ConfigurationAllowDefinition.MachineToApplication:
125                                         return configPath == "machine" || configPath == "exe";
126                                 default:
127                                         return true;
128                         }
129                 }
130                 
131                 public virtual bool IsFile (string streamName)
132                 {
133                         throw new NotImplementedException ();
134                 }
135
136                 public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
137                 {
138                         throw new NotImplementedException ();
139                 }
140
141                 public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
142                 {
143                         throw new NotImplementedException ();
144                 }
145
146                 public virtual bool IsLocationApplicable (string configPath)
147                 {
148                         throw new NotImplementedException ();
149                 }
150
151                 public virtual bool IsRemote {
152                         get {
153                                 throw new NotImplementedException ();
154                         }
155                 }
156
157                 public virtual bool IsSecondaryRoot (string configPath)
158                 {
159                         throw new NotImplementedException ();
160                 }
161
162                 public virtual bool IsTrustedConfigPath (string configPath)
163                 {
164                         throw new NotImplementedException ();
165                 }
166 #if !TARGET_JVM
167                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
168                 extern private static string get_bundled_machine_config ();
169
170                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
171                 extern private static string get_bundled_app_config ();
172 #endif
173                 
174                 public virtual Stream OpenStreamForRead (string streamName)
175                 {
176                         if (String.CompareOrdinal (streamName, System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile) == 0) {
177 #if TARGET_JVM
178                                 return (Stream) vmw.common.IOUtils.getStreamForGHConfigs (streamName);
179 #else
180                                 string bundle = get_bundled_machine_config ();
181                                 if (bundle != null)
182                                         return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
183 #endif
184                         }
185
186                         if (String.CompareOrdinal (streamName, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) == 0) {
187 #if TARGET_JVM
188                                 throw new NotImplementedException();
189 #else
190                                 string bundle = get_bundled_app_config ();
191                                 if (bundle != null)
192                                         return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
193 #endif
194                         }
195
196                         if (!File.Exists (streamName))
197                                 return null;
198                                 
199                         return new FileStream (streamName, FileMode.Open, FileAccess.Read);
200                 }
201
202                 public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
203                 {
204                         throw new NotImplementedException ();
205                 }
206                 
207                 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
208                 {
209                         string dir = Path.GetDirectoryName (streamName);
210                         if (!String.IsNullOrEmpty (dir) && !Directory.Exists (dir))
211                                 Directory.CreateDirectory (dir);
212                         return new FileStream (streamName, FileMode.Create, FileAccess.Write);
213                 }
214
215                 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
216                 {
217                         throw new NotImplementedException ();
218                 }
219                 
220                 public virtual bool PrefetchAll (string configPath, string streamName)
221                 {
222                         throw new NotImplementedException ();
223                 }
224                 
225                 public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
226                 {
227                         throw new NotImplementedException ();
228                 }
229
230                 public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
231                 {
232                         throw new NotImplementedException ();
233                 }
234
235                 public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
236                 {
237                         throw new NotImplementedException ();
238                 }
239                 
240                 public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
241                 {
242                         throw new NotImplementedException ();
243                 }
244                 
245                 public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
246                 {
247                         if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
248                                 throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
249                 }
250                 
251                 public virtual void WriteCompleted (string streamName, bool success, object writeContext)
252                 {
253                 }
254
255                 public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
256                 {
257                 }
258                 
259                 public virtual bool SupportsChangeNotifications {
260                         get { return false; }
261                 }
262                 
263                 public virtual bool SupportsLocation {
264                         get { return false; }
265                 }
266                 
267                 public virtual bool SupportsPath {
268                         get { return false; }
269                 }
270                 
271                 public virtual bool SupportsRefresh {
272                         get { return false; }
273                 }
274         }
275         
276         class ExeConfigurationHost: InternalConfigurationHost
277         {
278                 ExeConfigurationFileMap map;
279                 ConfigurationUserLevel level;
280                 
281                 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
282                 {
283                         map = (ExeConfigurationFileMap) hostInitParams [0];
284                         level = (ConfigurationUserLevel) hostInitParams [1];
285                 }
286                 
287                 public override string GetStreamName (string configPath)
288                 {
289                         switch (configPath) {
290                                 case "exe": return map.ExeConfigFilename; 
291                                 case "local": return map.LocalUserConfigFilename;
292                                 case "roaming": return map.RoamingUserConfigFilename;
293                                 case "machine": return map.MachineConfigFilename;
294                                 default://return map.ExeConfigFilename;
295                                 switch (level) {
296                                         case ConfigurationUserLevel.None:
297                                                 return map.ExeConfigFilename; 
298                                         case ConfigurationUserLevel.PerUserRoaming:
299                                                 return map.RoamingUserConfigFilename;
300                                         case ConfigurationUserLevel.PerUserRoamingAndLocal:
301                                                 return map.LocalUserConfigFilename;
302                                         default:
303                                                 return map.MachineConfigFilename;
304                                 }
305                         }
306                 }
307                 
308                 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
309                 {
310                         map = (ExeConfigurationFileMap) hostInitConfigurationParams [0];
311
312                         if (hostInitConfigurationParams.Length > 1 && 
313                             hostInitConfigurationParams [1] is ConfigurationUserLevel)
314                                 level = (ConfigurationUserLevel) hostInitConfigurationParams [1];
315                         if (locationSubPath == null)
316                                 switch (level) {
317                                 case ConfigurationUserLevel.PerUserRoaming:
318                                         if (map.RoamingUserConfigFilename == null)
319                                                 throw new ArgumentException ("RoamingUserConfigFilename must be set correctly");
320                                         locationSubPath = "roaming";
321                                         break;
322                                 case ConfigurationUserLevel.PerUserRoamingAndLocal:
323                                         if (map.LocalUserConfigFilename == null)
324                                                 throw new ArgumentException ("LocalUserConfigFilename must be set correctly");
325                                         locationSubPath = "local";
326                                         break;
327                                 }
328
329                         configPath = null;
330                         string next = null;
331
332                         locationConfigPath = null;
333
334                         if (locationSubPath == "exe" || locationSubPath == null && map.ExeConfigFilename != null) {
335                                 configPath = "exe";
336                                 next = "local";
337                                 locationConfigPath = map.ExeConfigFilename;
338                         }
339                         
340                         if (locationSubPath == "local" && map.LocalUserConfigFilename != null) {
341                                 configPath = "local";
342                                 next = "roaming";
343                                 locationConfigPath = map.LocalUserConfigFilename;
344                         }
345                         
346                         if (locationSubPath == "roaming" && map.RoamingUserConfigFilename != null) {
347                                 configPath = "roaming";
348                                 next = "machine";
349                                 locationConfigPath = map.RoamingUserConfigFilename;
350                         }
351                         
352                         if ((locationSubPath == "machine" || configPath == null) && map.MachineConfigFilename != null) {
353                                 configPath = "machine";
354                                 next = null;
355                         }
356                         locationSubPath = next;
357                 }
358         }
359         
360         class MachineConfigurationHost: InternalConfigurationHost
361         {
362                 ConfigurationFileMap map;
363                 
364                 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
365                 {
366                         map = (ConfigurationFileMap) hostInitParams [0];
367                 }
368                 
369                 public override string GetStreamName (string configPath)
370                 {
371                         return map.MachineConfigFilename;
372                 }
373                 
374                 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
375                 {
376                         map = (ConfigurationFileMap) hostInitConfigurationParams [0];
377                         locationSubPath = null;
378                         configPath = null;
379                         locationConfigPath = null;
380                 }
381
382                 public override bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
383                 {
384                         return true;
385                 }
386         }
387 }
388
389 #endif