File: Configuration.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (314 lines) | stat: -rw-r--r-- 12,706 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//------------------------------------------------------------------------------
// <copyright file="Configuration.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using ClassConfiguration = System.Configuration.Configuration;
using System.Collections;
using System.Configuration;
using System.Configuration.Internal;
using System.IO;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Threading;
using System.Runtime.Versioning;

namespace System.Configuration {

    //
    // An instance of the Configuration class represents a single level
    // in the configuration hierarchy. Its contents can be edited and
    // saved to disk.
    //
    // It is not thread safe for writing.
    //
    public sealed class Configuration {
        private Type                                _typeConfigHost;                // type of config host
        private object[]                            _hostInitConfigurationParams;   // params to init config host
        private InternalConfigRoot                  _configRoot;        // root of this configuration hierarchy
        private MgmtConfigurationRecord             _configRecord;      // config record for this level in the hierarchy
        private ConfigurationSectionGroup           _rootSectionGroup;  // section group for the root of all sections
        private ConfigurationLocationCollection     _locations;         // support for ConfigurationLocationsCollection
        private ContextInformation                  _evalContext;       // evaluation context
        private Func<string, string>                _TypeStringTransformer           = null;
        private Func<string, string>                _AssemblyStringTransformer       = null;
        private bool                                _TypeStringTransformerIsSet      = false;
        private bool                                _AssemblyStringTransformerIsSet  = false;
        private FrameworkName                       _TargetFramework                 = null;
        private Stack                               _SectionsStack                   = null;

        internal Configuration(string locationSubPath, Type typeConfigHost, params object[] hostInitConfigurationParams) {
            _typeConfigHost = typeConfigHost;
            _hostInitConfigurationParams = hostInitConfigurationParams;

            _configRoot = new InternalConfigRoot(this);

            IInternalConfigHost configHost = (IInternalConfigHost) TypeUtil.CreateInstanceWithReflectionPermission(typeConfigHost);

            // Wrap the host with the UpdateConfigHost to support SaveAs.
            IInternalConfigHost updateConfigHost = new UpdateConfigHost(configHost);

            ((IInternalConfigRoot)_configRoot).Init(updateConfigHost, true);

            //
            // Set the configuration paths for this Configuration.
            // We do this in a separate step so that the WebConfigurationHost
            // can use this object's _configRoot to get the <sites> section,
            // which is used in it's MapPath implementation.
            //
            string configPath, locationConfigPath;
            configHost.InitForConfiguration(ref locationSubPath, out configPath, out locationConfigPath, _configRoot, hostInitConfigurationParams);

            if (!String.IsNullOrEmpty(locationSubPath) && !updateConfigHost.SupportsLocation) {
                throw ExceptionUtil.UnexpectedError("Configuration::ctor");
            }

            if (String.IsNullOrEmpty(locationSubPath) != String.IsNullOrEmpty(locationConfigPath)) {
                throw ExceptionUtil.UnexpectedError("Configuration::ctor");
            }

            // Get the configuration record for this config file.
            _configRecord = (MgmtConfigurationRecord) _configRoot.GetConfigRecord(configPath);

            //
            // Create another MgmtConfigurationRecord for the location that is a child of the above record.
            // Note that this does not match the resolution hiearchy that is used at runtime.
            //
            if (!String.IsNullOrEmpty(locationSubPath)) {
                _configRecord = MgmtConfigurationRecord.Create(
                    _configRoot, _configRecord, locationConfigPath, locationSubPath);
            }

            //
            // Throw if the config record we created contains global errors.
            //
            _configRecord.ThrowIfInitErrors();
        }

        //
        // Create a new instance of Configuration for the locationSubPath,
        // with the initialization parameters that were used to create this configuration.
        //
        internal Configuration OpenLocationConfiguration(string locationSubPath) {
            return new Configuration(locationSubPath, _typeConfigHost, _hostInitConfigurationParams);
        }

        // public properties
        public AppSettingsSection AppSettings {
            get {
                return (AppSettingsSection) GetSection("appSettings");
            }
        }

        public ConnectionStringsSection ConnectionStrings {
            get {
                return (ConnectionStringsSection) GetSection("connectionStrings");
            }
        }

        public string FilePath {
            get {
                return _configRecord.ConfigurationFilePath;
            }
        }

        public bool HasFile {
            get {
                return _configRecord.HasStream;
            }
        }

        public ConfigurationLocationCollection Locations {
            get {
                if (_locations == null) {
                    _locations = _configRecord.GetLocationCollection(this);
                }

                return _locations;
            }
        }

        public ContextInformation EvaluationContext {
            get {
                if (_evalContext == null) {
                    _evalContext = new ContextInformation(_configRecord);
                }

                return _evalContext;
            }
        }

        public ConfigurationSectionGroup RootSectionGroup {
            get {
                if (_rootSectionGroup == null) {
                    _rootSectionGroup = new ConfigurationSectionGroup();
                    _rootSectionGroup.RootAttachToConfigurationRecord(_configRecord);
                }

                return _rootSectionGroup;
            }
        }

        public ConfigurationSectionCollection Sections {
            get {
                return RootSectionGroup.Sections;
            }
        }

        public ConfigurationSectionGroupCollection SectionGroups {
            get {
                return RootSectionGroup.SectionGroups;
            }
        }

        // public methods
        public ConfigurationSection GetSection(string sectionName) {
            ConfigurationSection section = (ConfigurationSection) _configRecord.GetSection(sectionName);

            return section;
        }

        public ConfigurationSectionGroup GetSectionGroup(string sectionGroupName) {
            ConfigurationSectionGroup sectionGroup = _configRecord.GetSectionGroup(sectionGroupName);

            return sectionGroup;
        }

        // NamespaceDeclared
        //
        // Is the namespace declared in the file or not?
        //
        // ie. xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"
        //     (currently this is the only one we allow)
        //
        // get - Return if it was declared in the file.
        // set - Set if we should save the namespace or not
        //
        public bool NamespaceDeclared {
            get {
                return _configRecord.NamespacePresent;
            }
            set {
                _configRecord.NamespacePresent = value;
            }
        }

        public void Save() {
            SaveAsImpl(null, ConfigurationSaveMode.Modified, false);
        }

        public void Save(ConfigurationSaveMode saveMode) {
            SaveAsImpl(null, saveMode, false);
        }

        public void Save(ConfigurationSaveMode saveMode, bool forceSaveAll) {
            SaveAsImpl(null, saveMode, forceSaveAll);
        }

        public void SaveAs(string filename) {
            SaveAs(filename, ConfigurationSaveMode.Modified, false);
        }

        public void SaveAs(string filename, ConfigurationSaveMode saveMode) {
            SaveAs(filename, saveMode, false);
        }

        public void SaveAs(string filename, ConfigurationSaveMode saveMode, bool forceSaveAll) {
            if (String.IsNullOrEmpty(filename)) {
                throw ExceptionUtil.ParameterNullOrEmpty("filename");
            }

            SaveAsImpl(filename, saveMode, forceSaveAll);
        }

        private void SaveAsImpl(string filename, ConfigurationSaveMode saveMode, bool forceSaveAll) {
            if (String.IsNullOrEmpty(filename)) {
                filename = null;
            }
            else {
                filename = System.IO.Path.GetFullPath(filename);
            }

            if (forceSaveAll) {
                ForceGroupsRecursive(RootSectionGroup);
            }
            _configRecord.SaveAs(filename, saveMode, forceSaveAll);
        }

        // Force all sections and section groups to be instantiated.
        private void ForceGroupsRecursive(ConfigurationSectionGroup group) {
            foreach (ConfigurationSection configSection in group.Sections) {
                // Force the section to be read into the cache
                ConfigurationSection section = group.Sections[configSection.SectionInformation.Name];
            }

            foreach (ConfigurationSectionGroup sectionGroup in group.SectionGroups) {
                ForceGroupsRecursive(group.SectionGroups[sectionGroup.Name]);
            }
        }

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        public System.Func<string, string> TypeStringTransformer {
            get {
                return _TypeStringTransformer;
            }
            [ConfigurationPermission(SecurityAction.Demand, Unrestricted=true)]
            set {
                if (_TypeStringTransformer != value) {
                    _TypeStringTransformerIsSet = (value != null);
                    _TypeStringTransformer = value;
                }
            }
        }

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        public System.Func<string, string> AssemblyStringTransformer {
            get {
                return _AssemblyStringTransformer;
            }
            [ConfigurationPermission(SecurityAction.Demand, Unrestricted=true)]
            set {
                if (_AssemblyStringTransformer != value) {
                    _AssemblyStringTransformerIsSet = (value != null);
                    _AssemblyStringTransformer = value;
                }
            }
        }

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        public FrameworkName TargetFramework {
            get {
                return _TargetFramework;
            }
            [ConfigurationPermission(SecurityAction.Demand, Unrestricted=true)]
            set {
                _TargetFramework = value;
            }
        }

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        internal bool TypeStringTransformerIsSet { get { return _TypeStringTransformerIsSet; }}

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        internal bool AssemblyStringTransformerIsSet { get { return _AssemblyStringTransformerIsSet; }}

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        internal Stack SectionsStack {
            get {
                if (_SectionsStack == null)
                    _SectionsStack = new Stack();
                return _SectionsStack;
            }
        }
    }
}