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 ();
164 [MethodImplAttribute(MethodImplOptions.InternalCall)]
165 extern private static string get_bundled_machine_config ();
167 [MethodImplAttribute(MethodImplOptions.InternalCall)]
168 extern private static string get_bundled_app_config ();
170 public virtual Stream OpenStreamForRead (string streamName)
172 if (String.CompareOrdinal (streamName, System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile) == 0) {
173 string bundle = get_bundled_machine_config ();
175 return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
178 if (String.CompareOrdinal (streamName, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) == 0) {
179 string bundle = get_bundled_app_config ();
181 return new MemoryStream (System.Text.Encoding.UTF8.GetBytes (bundle));
184 if (!File.Exists (streamName))
187 return new FileStream (streamName, FileMode.Open, FileAccess.Read);
190 public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
192 throw new NotImplementedException ();
195 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
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);
203 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
205 throw new NotImplementedException ();
208 public virtual bool PrefetchAll (string configPath, string streamName)
210 throw new NotImplementedException ();
213 public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
215 throw new NotImplementedException ();
218 public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
220 throw new NotImplementedException ();
223 public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
225 throw new NotImplementedException ();
228 public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
230 throw new NotImplementedException ();
233 public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
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);
239 public virtual void WriteCompleted (string streamName, bool success, object writeContext)
243 public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
247 public virtual bool SupportsChangeNotifications {
248 get { return false; }
251 public virtual bool SupportsLocation {
252 get { return false; }
255 public virtual bool SupportsPath {
256 get { return false; }
259 public virtual bool SupportsRefresh {
260 get { return false; }
264 class ExeConfigurationHost: InternalConfigurationHost
266 ExeConfigurationFileMap map;
267 ConfigurationUserLevel level;
269 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
271 map = (ExeConfigurationFileMap) hostInitParams [0];
272 level = (ConfigurationUserLevel) hostInitParams [1];
273 CheckFileMap (level, map);
276 static void CheckFileMap (ConfigurationUserLevel level, ExeConfigurationFileMap map)
279 case ConfigurationUserLevel.None:
280 if (string.IsNullOrEmpty (map.ExeConfigFilename))
281 throw new ArgumentException (
282 "The 'ExeConfigFilename' argument cannot be null.");
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;
297 public override string GetStreamName (string configPath)
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;
306 case ConfigurationUserLevel.None:
307 return map.ExeConfigFilename;
308 case ConfigurationUserLevel.PerUserRoaming:
309 return map.RoamingUserConfigFilename;
310 case ConfigurationUserLevel.PerUserRoamingAndLocal:
311 return map.LocalUserConfigFilename;
313 return map.MachineConfigFilename;
318 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
320 map = (ExeConfigurationFileMap) hostInitConfigurationParams [0];
322 if (hostInitConfigurationParams.Length > 1 &&
323 hostInitConfigurationParams [1] is ConfigurationUserLevel)
324 level = (ConfigurationUserLevel) hostInitConfigurationParams [1];
326 CheckFileMap (level, map);
328 if (locationSubPath == null)
330 case ConfigurationUserLevel.PerUserRoaming:
331 if (map.RoamingUserConfigFilename == null)
332 throw new ArgumentException ("RoamingUserConfigFilename must be set correctly");
333 locationSubPath = "roaming";
335 case ConfigurationUserLevel.PerUserRoamingAndLocal:
336 if (map.LocalUserConfigFilename == null)
337 throw new ArgumentException ("LocalUserConfigFilename must be set correctly");
338 locationSubPath = "local";
345 locationConfigPath = null;
347 if (locationSubPath == "exe" || locationSubPath == null && map.ExeConfigFilename != null) {
350 locationConfigPath = map.ExeConfigFilename;
353 if (locationSubPath == "local" && map.LocalUserConfigFilename != null) {
354 configPath = "local";
356 locationConfigPath = map.LocalUserConfigFilename;
359 if (locationSubPath == "roaming" && map.RoamingUserConfigFilename != null) {
360 configPath = "roaming";
362 locationConfigPath = map.RoamingUserConfigFilename;
365 if ((locationSubPath == "machine" || configPath == null) && map.MachineConfigFilename != null) {
366 configPath = "machine";
369 locationSubPath = next;
373 class MachineConfigurationHost: InternalConfigurationHost
375 ConfigurationFileMap map;
377 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
379 map = (ConfigurationFileMap) hostInitParams [0];
382 public override string GetStreamName (string configPath)
384 return map.MachineConfigFilename;
387 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
389 map = (ConfigurationFileMap) hostInitConfigurationParams [0];
390 locationSubPath = null;
392 locationConfigPath = null;
395 public override bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)