2006-01-26 Chris Toshok <toshok@ximian.com>
[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
36 namespace System.Configuration
37 {
38         abstract class InternalConfigurationHost: IInternalConfigHost
39         {
40                 public virtual object CreateConfigurationContext (string configPath, string locationSubPath)
41                 {
42                         return null;
43                 }
44                 
45                 public virtual object CreateDeprecatedConfigContext (string configPath)
46                 {
47                         throw new NotImplementedException ();
48                 }
49                 
50                 public virtual void DeleteStream (string streamName)
51                 {
52                         File.Delete (streamName);
53                 }
54                 
55                 string IInternalConfigHost.DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
56                 {
57                         return protectedSection.DecryptSection (encryptedXml, protectionProvider);
58                 }
59                 
60                 string IInternalConfigHost.EncryptSection (string clearXml, ProtectedConfigurationProvider protectionProvider, ProtectedConfigurationSection protectedSection)
61                 {
62                         return protectedSection.EncryptSection (clearXml, protectionProvider);
63                 }
64                 
65                 public virtual string GetConfigPathFromLocationSubPath (string configPath, string locationSubPath)
66                 {
67                         return configPath;
68                 }
69                 
70                 public virtual Type GetConfigType (string typeName, bool throwOnError)
71                 {
72                         Type type = Type.GetType (typeName);
73                         if (type == null && throwOnError)
74                                 throw new ConfigurationErrorsException ("Type '" + typeName + "' not found.");
75                         return type;
76                 }
77                 
78                 public virtual string GetConfigTypeName (Type t)
79                 {
80                         return t.AssemblyQualifiedName;
81                 }
82                 
83                 public virtual void GetRestrictedPermissions (IInternalConfigRecord configRecord, out PermissionSet permissionSet, out bool isHostReady)
84                 {
85                         throw new NotImplementedException ();
86                 }
87                 
88                 public abstract string GetStreamName (string configPath);
89                 public abstract void Init (IInternalConfigRoot root, params object[] hostInitParams);
90                 public abstract void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams);
91                 
92                 [MonoTODO ("remote config")]
93                 public virtual string GetStreamNameForConfigSource (string streamName, string configSource)
94                 {
95                         throw new NotSupportedException ("mono does not support remote configuration");
96                 }
97                 
98                 public virtual object GetStreamVersion (string streamName)
99                 {
100                         throw new NotImplementedException ();
101                 }
102                 
103                 public virtual IDisposable Impersonate ()
104                 {
105                         throw new NotImplementedException ();
106                 }
107                 
108                 public virtual bool IsAboveApplication (string configPath)
109                 {
110                         throw new NotImplementedException ();
111                 }
112                 
113                 public virtual bool IsConfigRecordRequired (string configPath)
114                 {
115                         throw new NotImplementedException ();
116                 }
117                 
118                 public virtual bool IsDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition)
119                 {
120                         switch (allowDefinition) {
121                                 case ConfigurationAllowDefinition.MachineOnly:
122                                         return configPath == "machine";
123                                 case ConfigurationAllowDefinition.MachineToApplication:
124                                         return configPath == "machine" || configPath == "exe";
125                                 default:
126                                         return true;
127                         }
128                 }
129                 
130                 public virtual bool IsFile (string streamName)
131                 {
132                         throw new NotImplementedException ();
133                 }
134
135                 public virtual bool IsFullTrustSectionWithoutAptcaAllowed (IInternalConfigRecord configRecord)
136                 {
137                         throw new NotImplementedException ();
138                 }
139
140                 public virtual bool IsInitDelayed (IInternalConfigRecord configRecord)
141                 {
142                         throw new NotImplementedException ();
143                 }
144
145                 public virtual bool IsLocationApplicable (string configPath)
146                 {
147                         throw new NotImplementedException ();
148                 }
149
150                 public virtual bool IsRemote {
151                         get {
152                                 throw new NotImplementedException ();
153                         }
154                 }
155
156                 public virtual bool IsSecondaryRoot (string configPath)
157                 {
158                         throw new NotImplementedException ();
159                 }
160
161                 public virtual bool IsTrustedConfigPath (string configPath)
162                 {
163                         throw new NotImplementedException ();
164                 }
165                 
166                 public virtual Stream OpenStreamForRead (string streamName)
167                 {
168                         if (!File.Exists (streamName))
169                                 throw new ConfigurationException ("File '" + streamName + "' not found");
170                                 
171                         return new FileStream (streamName, FileMode.Open, FileAccess.Read);
172                 }
173
174                 public virtual Stream OpenStreamForRead (string streamName, bool assertPermissions)
175                 {
176                         throw new NotImplementedException ();
177                 }
178                 
179                 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext)
180                 {
181                         return new FileStream (streamName, FileMode.Create, FileAccess.Write);
182                 }
183
184                 public virtual Stream OpenStreamForWrite (string streamName, string templateStreamName, ref object writeContext, bool assertPermissions)
185                 {
186                         throw new NotImplementedException ();
187                 }
188                 
189                 public virtual bool PrefetchAll (string configPath, string streamName)
190                 {
191                         throw new NotImplementedException ();
192                 }
193                 
194                 public virtual bool PrefetchSection (string sectionGroupName, string sectionName)
195                 {
196                         throw new NotImplementedException ();
197                 }
198
199                 public virtual void RequireCompleteInit (IInternalConfigRecord configRecord)
200                 {
201                         throw new NotImplementedException ();
202                 }
203
204                 public virtual object StartMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
205                 {
206                         throw new NotImplementedException ();
207                 }
208                 
209                 public virtual void StopMonitoringStreamForChanges (string streamName, StreamChangeCallback callback)
210                 {
211                         throw new NotImplementedException ();
212                 }
213                 
214                 public virtual void VerifyDefinitionAllowed (string configPath, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, IConfigErrorInfo errorInfo)
215                 {
216                         if (!IsDefinitionAllowed (configPath, allowDefinition, allowExeDefinition))
217                                 throw new ConfigurationErrorsException ("The section can't be defined in this file (the allowed definition context is '" + allowDefinition + "').", errorInfo.Filename, errorInfo.LineNumber);
218                 }
219                 
220                 public virtual void WriteCompleted (string streamName, bool success, object writeContext)
221                 {
222                 }
223
224                 public virtual void WriteCompleted (string streamName, bool success, object writeContext, bool assertPermissions)
225                 {
226                 }
227                 
228                 public virtual bool SupportsChangeNotifications {
229                         get { return false; }
230                 }
231                 
232                 public virtual bool SupportsLocation {
233                         get { return false; }
234                 }
235                 
236                 public virtual bool SupportsPath {
237                         get { return false; }
238                 }
239                 
240                 public virtual bool SupportsRefresh {
241                         get { return false; }
242                 }
243         }
244         
245         class ExeConfigurationHost: InternalConfigurationHost
246         {
247                 ExeConfigurationFileMap map;
248                 
249                 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
250                 {
251                         map = (ExeConfigurationFileMap) hostInitParams [0];
252                 }
253                 
254                 public override string GetStreamName (string configPath)
255                 {
256                         switch (configPath) {
257                                 case "exe": return map.ExeConfigFilename; 
258                                 case "local": return map.LocalUserConfigFilename;
259                                 case "roaming": return map.LocalUserConfigFilename;
260                                 case "machine": return map.MachineConfigFilename;
261                                 default: return map.ExeConfigFilename;
262                         }
263                 }
264                 
265                 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
266                 {
267                         map = (ExeConfigurationFileMap) hostInitConfigurationParams [0];
268                         configPath = null;
269                         string next = null;
270
271                         locationConfigPath = null;
272
273                         if ((locationSubPath == "exe" || locationSubPath == null) && map.ExeConfigFilename != null) {
274                                 configPath = "exe";
275                                 next = "local";
276                                 locationConfigPath = map.ExeConfigFilename;
277                         }
278                         
279                         if ((locationSubPath == "local" || configPath == null) && map.LocalUserConfigFilename != null) {
280                                 configPath = "local";
281                                 next = "roaming";
282                                 locationConfigPath = map.LocalUserConfigFilename;
283                         }
284                         
285                         if ((locationSubPath == "roaming" || configPath == null) && map.RoamingUserConfigFilename != null) {
286                                 configPath = "roaming";
287                                 next = "machine";
288                                 locationConfigPath = map.RoamingUserConfigFilename;
289                         }
290                         
291                         if ((locationSubPath == "machine" || configPath == null) && map.MachineConfigFilename != null) {
292                                 configPath = "machine";
293                                 next = null;
294                         }
295
296                         locationSubPath = next;
297                 }
298         }
299         
300         class MachineConfigurationHost: InternalConfigurationHost
301         {
302                 ConfigurationFileMap map;
303                 
304                 public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
305                 {
306                         map = (ConfigurationFileMap) hostInitParams [0];
307                 }
308                 
309                 public override string GetStreamName (string configPath)
310                 {
311                         return map.MachineConfigFilename;
312                 }
313                 
314                 public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
315                 {
316                         map = (ConfigurationFileMap) hostInitConfigurationParams [0];
317                         locationSubPath = null;
318                         configPath = null;
319                         locationConfigPath = null;
320                 }
321         }       
322 }
323
324 #endif