2 // System.Configuration.InternalConfigurationHost.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
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:
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
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.
26 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
31 using System.Security;
32 using System.Configuration.Internal;
33 using System.Runtime.CompilerServices;
35 namespace System.Configuration
37 abstract class InternalConfigurationHost: IInternalConfigHost
39 public virtual object CreateConfigurationContext (string configPath, string locationSubPath)
44 public virtual object CreateDeprecatedConfigContext (string configPath)
49 public virtual void DeleteStream (string streamName)
51 File.Delete (streamName);
54 string IInternalConfigHost.DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
56 return protectedSection.DecryptSection (encryptedXml, protectionProvider);
59 string IInternalConfigHost.EncryptSection (string clearXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
61 return protectedSection.EncryptSection (clearXml, protectionProvider);
64 public virtual string GetConfigPathFromLocationSubPath (string configPath, string locationSubPath)
69 public virtual Type GetConfigType (string typeName, bool throwOnError)
71 Type type = Type.GetType (typeName);
72 if (type == null && throwOnError)
73 throw new ConfigurationErrorsException ("Type '" + typeName + "' not found.");
77 public virtual string GetConfigTypeName (Type t)
79 return t.AssemblyQualifiedName;
82 public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
84 throw new NotImplementedException ();
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);
91 [MonoNotSupported ("mono does not support remote configuration")]
92 public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
94 throw new NotSupportedException ("mono does not support remote configuration");
97 public virtual object GetStreamVersion (string streamName)
99 throw new NotImplementedException ();
102 public virtual IDisposable Impersonate ()
104 throw new NotImplementedException ();
107 public virtual bool IsAboveApplication (string configPath)
109 throw new NotImplementedException ();
112 public virtual bool IsConfigRecordRequired (string configPath)
114 throw new NotImplementedException ();
117 public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
119 switch (allowDefinition) {
120 case ConfigurationAllowDefinition.MachineOnly:
121 return configPath == "machine";
122 case ConfigurationAllowDefinition.MachineToApplication:
123 return configPath == "machine" || configPath == "exe";
129 public virtual bool IsFile (string streamName)
131 throw new NotImplementedException ();
134 public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
136 throw new NotImplementedException ();
139 public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
141 throw new NotImplementedException ();
144 public virtual bool IsLocationApplicable (string configPath)
146 throw new NotImplementedException ();
149 public virtual bool IsRemote {
151 throw new NotImplementedException ();
155 public virtual bool IsSecondaryRoot (string configPath)
157 throw new NotImplementedException ();
160 public virtual bool IsTrustedConfigPath (string configPath)
162 throw new NotImplementedException ();
165 [MethodImplAttribute(MethodImplOptions.InternalCall)]
166 extern private static string get_bundled_machine_config ();
168 [MethodImplAttribute(MethodImplOptions.InternalCall)]
169 extern private static string get_bundled_app_config ();
172 public virtual Stream OpenStreamForRead (string streamName)
174 if (String.CompareOrdinal (streamName, System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile) == 0) {
176 return (Stream) vmw.common.IOUtils.getStreamForGHConfigs (streamName);
178 string bundle = get_bundled_machine_config ();
180 return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
184 if (String.CompareOrdinal (streamName, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) == 0) {
186 throw new NotImplementedException();
188 string bundle = get_bundled_app_config ();
190 return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
194 if (!File.Exists (streamName))
197 return new FileStream (streamName, FileMode.Open, FileAccess.Read);
200 public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
202 throw new NotImplementedException ();
205 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
207 string dir = Path.GetDirectoryName (streamName);
208 if (!String.IsNullOrEmpty (dir) && !Directory.Exists (dir))
209 Directory.CreateDirectory (dir);
210 return new FileStream (streamName, FileMode.Create, FileAccess.Write);
213 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
215 throw new NotImplementedException ();
218 public virtual bool PrefetchAll (string configPath, string streamName)
220 throw new NotImplementedException ();
223 public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
225 throw new NotImplementedException ();
228 public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
230 throw new NotImplementedException ();
233 public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
235 throw new NotImplementedException ();
238 public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
240 throw new NotImplementedException ();
243 public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
245 if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
246 throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
249 public virtual void WriteCompleted (string streamName, bool success, object writeContext)
253 public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
257 public virtual bool SupportsChangeNotifications {
258 get { return false; }
261 public virtual bool SupportsLocation {
262 get { return false; }
265 public virtual bool SupportsPath {
266 get { return false; }
269 public virtual bool SupportsRefresh {
270 get { return false; }
274 class ExeConfigurationHost: InternalConfigurationHost
276 ExeConfigurationFileMap map;
277 ConfigurationUserLevel level;
279 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
281 map = (ExeConfigurationFileMap) hostInitParams [0];
282 level = (ConfigurationUserLevel) hostInitParams [1];
283 CheckFileMap (level, map);
286 static void CheckFileMap (ConfigurationUserLevel level, ExeConfigurationFileMap map)
289 case ConfigurationUserLevel.None:
290 if (string.IsNullOrEmpty (map.ExeConfigFilename))
291 throw new ArgumentException (
292 "The 'ExeConfigFilename' argument cannot be null.");
294 case ConfigurationUserLevel.PerUserRoamingAndLocal:
295 if (string.IsNullOrEmpty (map.LocalUserConfigFilename))
296 throw new ArgumentException (
297 "The 'LocalUserConfigFilename' argument cannot be null.");
298 goto case ConfigurationUserLevel.PerUserRoaming;
299 case ConfigurationUserLevel.PerUserRoaming:
300 if (string.IsNullOrEmpty (map.RoamingUserConfigFilename))
301 throw new ArgumentException (
302 "The 'RoamingUserConfigFilename' argument cannot be null.");
303 goto case ConfigurationUserLevel.None;
307 public override string GetStreamName (string configPath)
309 switch (configPath) {
310 case "exe": return map.ExeConfigFilename;
311 case "local": return map.LocalUserConfigFilename;
312 case "roaming": return map.RoamingUserConfigFilename;
313 case "machine": return map.MachineConfigFilename;
314 default://return map.ExeConfigFilename;
316 case ConfigurationUserLevel.None:
317 return map.ExeConfigFilename;
318 case ConfigurationUserLevel.PerUserRoaming:
319 return map.RoamingUserConfigFilename;
320 case ConfigurationUserLevel.PerUserRoamingAndLocal:
321 return map.LocalUserConfigFilename;
323 return map.MachineConfigFilename;
328 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
330 map = (ExeConfigurationFileMap) hostInitConfigurationParams [0];
332 if (hostInitConfigurationParams.Length > 1 &&
333 hostInitConfigurationParams [1] is ConfigurationUserLevel)
334 level = (ConfigurationUserLevel) hostInitConfigurationParams [1];
336 CheckFileMap (level, map);
338 if (locationSubPath == null)
340 case ConfigurationUserLevel.PerUserRoaming:
341 if (map.RoamingUserConfigFilename == null)
342 throw new ArgumentException ("RoamingUserConfigFilename must be set correctly");
343 locationSubPath = "roaming";
345 case ConfigurationUserLevel.PerUserRoamingAndLocal:
346 if (map.LocalUserConfigFilename == null)
347 throw new ArgumentException ("LocalUserConfigFilename must be set correctly");
348 locationSubPath = "local";
355 locationConfigPath = null;
357 if (locationSubPath == "exe" || locationSubPath == null && map.ExeConfigFilename != null) {
360 locationConfigPath = map.ExeConfigFilename;
363 if (locationSubPath == "local" && map.LocalUserConfigFilename != null) {
364 configPath = "local";
366 locationConfigPath = map.LocalUserConfigFilename;
369 if (locationSubPath == "roaming" && map.RoamingUserConfigFilename != null) {
370 configPath = "roaming";
372 locationConfigPath = map.RoamingUserConfigFilename;
375 if ((locationSubPath == "machine" || configPath == null) && map.MachineConfigFilename != null) {
376 configPath = "machine";
379 locationSubPath = next;
383 class MachineConfigurationHost: InternalConfigurationHost
385 ConfigurationFileMap map;
387 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
389 map = (ConfigurationFileMap) hostInitParams [0];
392 public override string GetStreamName (string configPath)
394 return map.MachineConfigFilename;
397 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
399 map = (ConfigurationFileMap) hostInitConfigurationParams [0];
400 locationSubPath = null;
402 locationConfigPath = null;
405 public override bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)