File: MTConfigUtil.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (246 lines) | stat: -rw-r--r-- 9,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
using System;
using System.Collections.Concurrent;
using System.Configuration;
using System.Web;
using System.Web.Compilation;
using System.Web.Configuration;
using System.Web.Hosting;
using Microsoft.Build.Utilities;

// Helper class to use 2.0 root config as necessary. Each helper method will
// go through RuntimeConfig (the original code path), except in the
// case of building and targeting 2.0/3.5.
internal class MTConfigUtil {

    private static readonly ConcurrentDictionary<Tuple<Type, VirtualPath>, ConfigurationSection> s_sections =
        new ConcurrentDictionary<Tuple<Type, VirtualPath>, ConfigurationSection>();

    private static readonly ConcurrentDictionary<VirtualPath, Configuration> s_configurations =
        new ConcurrentDictionary<VirtualPath, Configuration>();

    private static string s_machineConfigPath;
    private static VirtualPath s_appVirtualPath;

    // We only need to use the root config of 2.0 if we are building (and
    // not during runtime) and targeting 2.0 or 3.5.
    static private bool? s_useMTConfig;
    static private bool UseMTConfig {
        get {
            if (s_useMTConfig == null) {
                s_useMTConfig = BuildManagerHost.InClientBuildManager &&
                    (MultiTargetingUtil.IsTargetFramework20 || MultiTargetingUtil.IsTargetFramework35);
            }
            return s_useMTConfig.Value;
        }
    }

    // Counterpart for RuntimeConfig.GetAppConfig().Profile;
    static internal ProfileSection GetProfileAppConfig() {
        if (!UseMTConfig) {
            return RuntimeConfig.GetAppConfig().Profile;
        }
        return GetAppConfig<ProfileSection>();
    }

    // Counterpart for RuntimeConfig.GetAppConfig().Pages;
    static internal PagesSection GetPagesAppConfig() {
        if (!UseMTConfig) {
            return RuntimeConfig.GetAppConfig().Pages;
        }
        return GetAppConfig<PagesSection>();
    }

    // Counterpart for RuntimeConfig.GetConfig().Pages;
    static internal PagesSection GetPagesConfig() {
        if (!UseMTConfig) {
            return RuntimeConfig.GetConfig().Pages;
        }
        return GetConfig<PagesSection>();
    }

    // Counterpart for RuntimeConfig.GetConfig(string).Pages
    static internal PagesSection GetPagesConfig(string vpath) {
        if (!UseMTConfig) {
            return RuntimeConfig.GetConfig(vpath).Pages;
        }
        return GetConfig<PagesSection>(vpath);
    }

    // Counterpart for RuntimeConfig.GetConfig(VirtualPath).Pages
    static internal PagesSection GetPagesConfig(VirtualPath vpath) {
        if (!UseMTConfig) {
            return RuntimeConfig.GetConfig(vpath).Pages;
        }
        return GetConfig<PagesSection>(vpath);
    }

    // Counterpart for RuntimeConfig.GetConfig(HttpContext).Pages
    static internal PagesSection GetPagesConfig(HttpContext context) {
        if (!UseMTConfig) {
            return RuntimeConfig.GetConfig(context).Pages;
        }
        return GetConfig<PagesSection>(context);
    }

    // Counterpart for RuntimeConfig.GetConfig().Compilation
    static internal CompilationSection GetCompilationConfig() {
        if (!UseMTConfig) {
            return RuntimeConfig.GetConfig().Compilation;
        }
        return GetConfig<CompilationSection>();
    }

    // Counterpart for RuntimeConfig.GetAppConfig().Compilation
    static internal CompilationSection GetCompilationAppConfig() {
        if (!UseMTConfig) {
            return RuntimeConfig.GetAppConfig().Compilation;
        }
        return GetAppConfig<CompilationSection>();
    }

    // Counterpart for RuntimeConfig.GetConfig(string).Compilation
    static internal CompilationSection GetCompilationConfig(string vpath) {
        if (!UseMTConfig) {
            return RuntimeConfig.GetConfig(vpath).Compilation;
        }
        return GetConfig<CompilationSection>(vpath);
    }

    // Counterpart for RuntimeConfig.GetConfig(VirtualPath).Compilation
    static internal CompilationSection GetCompilationConfig(VirtualPath vpath) {
        if (!UseMTConfig) {
            return RuntimeConfig.GetConfig(vpath).Compilation;
        }
        return GetConfig<CompilationSection>(vpath);
    }

    // Counterpart for RuntimeConfig.GetConfig(HttpContext).Compilation
    static internal CompilationSection GetCompilationConfig(HttpContext context) {
        if (!UseMTConfig) {
            return RuntimeConfig.GetConfig(context).Compilation;
        }
        return GetConfig<CompilationSection>(context);
    }

    // Counterpart to RuntimeConfig.GetConfig()
    static private S GetConfig<S>() where S : ConfigurationSection {
        HttpContext context = HttpContext.Current;
        if (context != null) {
            return GetConfig<S>(context);
        }
        else {
            return GetAppConfig<S>();
        }
    }

    // Counterpart to RuntimeConfig.GetAppConfig()
    static private S GetAppConfig<S>() where S : ConfigurationSection {
        return GetConfig<S>((VirtualPath) null);
    }

    // Counterpart to RuntimeConfig.GetConfig(HttpContext)
    static private S GetConfig<S>(HttpContext context) where S : ConfigurationSection {
        return GetConfig<S>(context.ConfigurationPath);
    }

    // Counterpart to RuntimeConfig.GetConfig(string)
    static private S GetConfig<S>(string vpath) where S : ConfigurationSection{
        return GetConfig<S>(VirtualPath.CreateNonRelativeAllowNull(vpath));
    }

    // Counterpart to RuntimeConfig.GetConfig(VirtualPath)
    static private S GetConfig<S>(VirtualPath vpath) where S : ConfigurationSection {
        Tuple<Type, VirtualPath> key = new Tuple<Type, VirtualPath>(typeof(S), vpath);
        ConfigurationSection result;
        if (!s_sections.TryGetValue(key, out result)) {
            result = GetConfigHelper<S>(vpath);
            s_sections.TryAdd(key, result);
        }
        return result as S;
    }

    // Actual method performing to work to retrieve the required ConfigurationSection.
    static private S GetConfigHelper<S>(VirtualPath vpath) where S : ConfigurationSection{
        string physicalPath = null;
        if (vpath == null || !vpath.IsWithinAppRoot) {
            // If virtual path is null or outside the application root, we use the application level config.
            vpath = HostingEnvironment.ApplicationVirtualPathObject;
            physicalPath = HostingEnvironment.ApplicationPhysicalPath;
        }
        else {
            // If it is not a directory, use the directory as the vpath
            if (!vpath.DirectoryExists()) {
                vpath = vpath.Parent;
            }
            physicalPath = HostingEnvironment.MapPath(vpath);
        }

        Configuration config = GetConfiguration(vpath, physicalPath);

        // Retrieve the specified section
        if (typeof(S) == typeof(CompilationSection)) {
            return config.GetSection("system.web/compilation") as S;
        }
        else if (typeof(S) == typeof(PagesSection)) {
            return config.GetSection("system.web/pages") as S;
        }
        else if (typeof(S) == typeof(ProfileSection)) {
            return config.GetSection("system.web/profile") as S;
        }

        throw new InvalidOperationException(SR.GetString(SR.Config_section_not_supported, typeof(S).FullName));
    }

    static private string MachineConfigPath {
        get {
            if (s_machineConfigPath == null) {
                s_machineConfigPath = ToolLocationHelper.GetPathToDotNetFrameworkFile(@"config\machine.config", TargetDotNetFrameworkVersion.Version20);
                if (string.IsNullOrEmpty(s_machineConfigPath)) {
                    string message = SR.GetString(SR.Downlevel_requires_35);
                    throw new InvalidOperationException(message);
                }
            }
            return s_machineConfigPath;
        }
    }

    static private Configuration GetConfiguration(VirtualPath vpath, string physicalPath) {
        Configuration result;
        if (!s_configurations.TryGetValue(vpath, out result)) {
            result = GetConfigurationHelper(vpath, physicalPath);
            s_configurations.TryAdd(vpath, result);
        }
        return result;
    }

    static private Configuration GetConfigurationHelper(VirtualPath vpath, string physicalPath) {
        // Set up the configuration file map
        string machineConfigPath = MachineConfigPath;
        WebConfigurationFileMap fileMap = new WebConfigurationFileMap(machineConfigPath);

        // Set up file maps for the current directory and parent directories up to application root
        VirtualPath currentVPath = vpath;
        while (currentVPath != null && currentVPath.IsWithinAppRoot) {
            string vpathString = currentVPath.VirtualPathStringNoTrailingSlash;
            if (physicalPath == null) {
                physicalPath = HostingEnvironment.MapPath(currentVPath);
            }
            fileMap.VirtualDirectories.Add(vpathString, new VirtualDirectoryMapping(physicalPath, IsAppRoot(currentVPath)));
            currentVPath = currentVPath.Parent;
            physicalPath = null;
        }

        Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(fileMap, vpath.VirtualPathStringNoTrailingSlash, HostingEnvironment.SiteName);
        return config;
    }

    static private bool IsAppRoot(VirtualPath path) {
        if (s_appVirtualPath == null) {
            s_appVirtualPath = VirtualPath.Create(HttpRuntime.AppDomainAppVirtualPathObject.VirtualPathStringNoTrailingSlash);
        }

        var vpath = VirtualPath.Create(path.VirtualPathStringNoTrailingSlash);
        return s_appVirtualPath.Equals(vpath);
    }

}