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)
33 using System.Security;
34 using System.Configuration.Internal;
35 using System.Runtime.CompilerServices;
37 namespace System.Configuration
39 abstract class InternalConfigurationHost: IInternalConfigHost
41 public virtual object CreateConfigurationContext (string configPath, string locationSubPath)
46 public virtual object CreateDeprecatedConfigContext (string configPath)
51 public virtual void DeleteStream (string streamName)
53 File.Delete (streamName);
56 string IInternalConfigHost.DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
58 return protectedSection.DecryptSection (encryptedXml, protectionProvider);
61 string IInternalConfigHost.EncryptSection (string clearXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
63 return protectedSection.EncryptSection (clearXml, protectionProvider);
66 public virtual string GetConfigPathFromLocationSubPath (string configPath, string locationSubPath)
71 public virtual Type GetConfigType (string typeName, bool throwOnError)
73 Type type = Type.GetType (typeName);
74 if (type == null && throwOnError)
75 throw new ConfigurationErrorsException ("Type '" + typeName + "' not found.");
79 public virtual string GetConfigTypeName (Type t)
81 return t.AssemblyQualifiedName;
84 public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
86 throw new NotImplementedException ();
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);
93 [MonoNotSupported ("mono does not support remote configuration")]
94 public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
96 throw new NotSupportedException ("mono does not support remote configuration");
99 public virtual object GetStreamVersion (string streamName)
101 throw new NotImplementedException ();
104 public virtual IDisposable Impersonate ()
106 throw new NotImplementedException ();
109 public virtual bool IsAboveApplication (string configPath)
111 throw new NotImplementedException ();
114 public virtual bool IsConfigRecordRequired (string configPath)
116 throw new NotImplementedException ();
119 public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
121 switch (allowDefinition) {
122 case ConfigurationAllowDefinition.MachineOnly:
123 return configPath == "machine";
124 case ConfigurationAllowDefinition.MachineToApplication:
125 return configPath == "machine" || configPath == "exe";
131 public virtual bool IsFile (string streamName)
133 throw new NotImplementedException ();
136 public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
138 throw new NotImplementedException ();
141 public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
143 throw new NotImplementedException ();
146 public virtual bool IsLocationApplicable (string configPath)
148 throw new NotImplementedException ();
151 public virtual bool IsRemote {
153 throw new NotImplementedException ();
157 public virtual bool IsSecondaryRoot (string configPath)
159 throw new NotImplementedException ();
162 public virtual bool IsTrustedConfigPath (string configPath)
164 throw new NotImplementedException ();
167 [MethodImplAttribute(MethodImplOptions.InternalCall)]
168 extern private static string get_bundled_machine_config ();
170 [MethodImplAttribute(MethodImplOptions.InternalCall)]
171 extern private static string get_bundled_app_config ();
174 public virtual Stream OpenStreamForRead (string streamName)
176 if (String.CompareOrdinal (streamName, System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile) == 0) {
178 return (Stream) vmw.common.IOUtils.getStreamForGHConfigs (streamName);
180 string bundle = get_bundled_machine_config ();
182 return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
186 if (String.CompareOrdinal (streamName, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) == 0) {
188 throw new NotImplementedException();
190 string bundle = get_bundled_app_config ();
192 return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
196 if (!File.Exists (streamName))
199 return new FileStream (streamName, FileMode.Open, FileAccess.Read);
202 public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
204 throw new NotImplementedException ();
207 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
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);
215 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
217 throw new NotImplementedException ();
220 public virtual bool PrefetchAll (string configPath, string streamName)
222 throw new NotImplementedException ();
225 public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
227 throw new NotImplementedException ();
230 public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
232 throw new NotImplementedException ();
235 public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
237 throw new NotImplementedException ();
240 public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
242 throw new NotImplementedException ();
245 public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
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);
251 public virtual void WriteCompleted (string streamName, bool success, object writeContext)
255 public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
259 public virtual bool SupportsChangeNotifications {
260 get { return false; }
263 public virtual bool SupportsLocation {
264 get { return false; }
267 public virtual bool SupportsPath {
268 get { return false; }
271 public virtual bool SupportsRefresh {
272 get { return false; }
276 class ExeConfigurationHost: InternalConfigurationHost
278 ExeConfigurationFileMap map;
279 ConfigurationUserLevel level;
281 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
283 map = (ExeConfigurationFileMap) hostInitParams [0];
284 level = (ConfigurationUserLevel) hostInitParams [1];
285 CheckFileMap (level, map);
288 static void CheckFileMap (ConfigurationUserLevel level, ExeConfigurationFileMap map)
291 case ConfigurationUserLevel.None:
292 if (string.IsNullOrEmpty (map.ExeConfigFilename))
293 throw new ArgumentException (
294 "The 'ExeConfigFilename' argument cannot be null.");
296 case ConfigurationUserLevel.PerUserRoamingAndLocal:
297 if (string.IsNullOrEmpty (map.LocalUserConfigFilename))
298 throw new ArgumentException (
299 "The 'LocalUserConfigFilename' argument cannot be null.");
300 goto case ConfigurationUserLevel.PerUserRoaming;
301 case ConfigurationUserLevel.PerUserRoaming:
302 if (string.IsNullOrEmpty (map.RoamingUserConfigFilename))
303 throw new ArgumentException (
304 "The 'RoamingUserConfigFilename' argument cannot be null.");
305 goto case ConfigurationUserLevel.None;
309 public override string GetStreamName (string configPath)
311 switch (configPath) {
312 case "exe": return map.ExeConfigFilename;
313 case "local": return map.LocalUserConfigFilename;
314 case "roaming": return map.RoamingUserConfigFilename;
315 case "machine": return map.MachineConfigFilename;
316 default://return map.ExeConfigFilename;
318 case ConfigurationUserLevel.None:
319 return map.ExeConfigFilename;
320 case ConfigurationUserLevel.PerUserRoaming:
321 return map.RoamingUserConfigFilename;
322 case ConfigurationUserLevel.PerUserRoamingAndLocal:
323 return map.LocalUserConfigFilename;
325 return map.MachineConfigFilename;
330 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
332 map = (ExeConfigurationFileMap) hostInitConfigurationParams [0];
334 if (hostInitConfigurationParams.Length > 1 &&
335 hostInitConfigurationParams [1] is ConfigurationUserLevel)
336 level = (ConfigurationUserLevel) hostInitConfigurationParams [1];
338 CheckFileMap (level, map);
340 if (locationSubPath == null)
342 case ConfigurationUserLevel.PerUserRoaming:
343 if (map.RoamingUserConfigFilename == null)
344 throw new ArgumentException ("RoamingUserConfigFilename must be set correctly");
345 locationSubPath = "roaming";
347 case ConfigurationUserLevel.PerUserRoamingAndLocal:
348 if (map.LocalUserConfigFilename == null)
349 throw new ArgumentException ("LocalUserConfigFilename must be set correctly");
350 locationSubPath = "local";
357 locationConfigPath = null;
359 if (locationSubPath == "exe" || locationSubPath == null && map.ExeConfigFilename != null) {
362 locationConfigPath = map.ExeConfigFilename;
365 if (locationSubPath == "local" && map.LocalUserConfigFilename != null) {
366 configPath = "local";
368 locationConfigPath = map.LocalUserConfigFilename;
371 if (locationSubPath == "roaming" && map.RoamingUserConfigFilename != null) {
372 configPath = "roaming";
374 locationConfigPath = map.RoamingUserConfigFilename;
377 if ((locationSubPath == "machine" || configPath == null) && map.MachineConfigFilename != null) {
378 configPath = "machine";
381 locationSubPath = next;
385 class MachineConfigurationHost: InternalConfigurationHost
387 ConfigurationFileMap map;
389 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
391 map = (ConfigurationFileMap) hostInitParams [0];
394 public override string GetStreamName (string configPath)
396 return map.MachineConfigFilename;
399 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
401 map = (ConfigurationFileMap) hostInitConfigurationParams [0];
402 locationSubPath = null;
404 locationConfigPath = null;
407 public override bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)