Merge pull request #1624 from esdrubal/getprocesstimes
[mono.git] / mcs / class / System.Web / Mono.Web.Util / SettingsMappingManager.cs
1 //
2 // Mono.Web.Util.SettingsMappingManager
3 //
4 // Authors:
5 //   Marek Habersack (mhabersack@novell.com)
6 //
7 // (C) 2007 Novell, Inc
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.Collections.Generic;
32 using System.Collections.Specialized;
33 using System.IO;
34 using System.Runtime.InteropServices;
35 using System.Web;
36 using System.Web.Configuration;
37 using System.Xml;
38 using System.Xml.XPath;
39
40 namespace Mono.Web.Util
41 {
42         public class SettingsMappingManager
43         {
44                 const string settingsMapFileName = "settings.map";
45                 const string localSettingsMapFileName = settingsMapFileName + ".config";
46
47                 static object mapperLock = new object ();
48                 
49                 static SettingsMappingManager _instance;
50                 static string _mappingFile;
51                 Dictionary <Type, SettingsMapping> _mappers;
52                 static Dictionary <object, object> _mappedSections;
53                 static SettingsMappingPlatform _myPlatform;
54
55                 static bool _runningOnWindows;
56                 internal static bool IsRunningOnWindows {
57                         get { return _runningOnWindows; }
58                 }
59
60                 public static SettingsMappingPlatform Platform {
61                         get { return _myPlatform; }
62                 }
63                 
64                 public bool HasMappings {
65                         get { return (_mappers != null && _mappers.Count > 0); }
66                 }
67                 
68                 static SettingsMappingManager ()
69                 {
70                         _mappingFile = Path.Combine (Path.GetDirectoryName (RuntimeEnvironment.SystemConfigurationFile), settingsMapFileName);
71                         PlatformID pid = Environment.OSVersion.Platform;
72                         _runningOnWindows = ((int) pid != 128 && (int) pid != 4 && (int) pid != 6);
73
74                 }
75
76                 static public void Init ()
77                 {
78                         if (_instance != null)
79                                 return;
80                         
81                         if (Environment.GetEnvironmentVariable ("MONO_ASPNET_INHIBIT_SETTINGSMAP") != null)
82                                 return;
83                                 
84                         NameValueCollection appSettings = WebConfigurationManager.AppSettings;
85                         if (appSettings != null) {
86                                 string inhibit = appSettings ["MonoAspnetInhibitSettingsMap"];
87                                 if (String.Compare (inhibit, "true", StringComparison.OrdinalIgnoreCase) == 0)
88                                         return;
89                         }
90
91                         if (IsRunningOnWindows)
92                                 _myPlatform = SettingsMappingPlatform.Windows;
93                         else
94                                 _myPlatform = SettingsMappingPlatform.Unix;
95                 
96                         SettingsMappingManager mapper = new SettingsMappingManager ();
97                         mapper.LoadMappings ();
98
99                         if (mapper.HasMappings) {
100                                 _instance = mapper;
101                                 _mappedSections = new Dictionary <object, object> ();
102                         }
103                 }
104                 
105                 void LoadMappings ()
106                 {
107                         if (File.Exists (_mappingFile))
108                                 LoadMappings (_mappingFile);
109
110                         AppDomainSetup domain = AppDomain.CurrentDomain.SetupInformation;
111                         string appMappingFile = Path.Combine (domain.ApplicationBase, localSettingsMapFileName);
112                         if (File.Exists (appMappingFile))
113                                 LoadMappings (appMappingFile);
114                 }
115                 
116                 void LoadMappings (string mappingFilePath)
117                 {
118                         XPathNavigator top;
119                         XPathDocument doc;
120                         try {
121                                 doc = new XPathDocument (mappingFilePath);
122                                 top = doc.CreateNavigator ();
123                         } catch (Exception ex) {
124                                 throw new ApplicationException ("Error loading mapping settings", ex);
125                         }
126                         
127                         XPathNodeIterator iter;
128                         if (_mappers == null)
129                                 _mappers = new Dictionary <Type, SettingsMapping> ();
130                         else {
131                                 iter = top.Select ("//settingsMap/clear");
132                                 if (iter.MoveNext ())
133                                         _mappers.Clear ();
134                         }
135
136                         iter = top.Select ("//settingsMap/map[string-length (@sectionType) > 0 and string-length (@mapperType) > 0 and string-length (@platform) > 0]");
137                         SettingsMapping map;
138                         
139                         while (iter.MoveNext ()) {
140                                 map = new SettingsMapping (iter.Current);
141                                 if (_myPlatform != map.Platform)
142                                         continue;
143                                 
144                                 if (!_mappers.ContainsKey (map.SectionType))
145                                         _mappers.Add (map.SectionType, map);
146                                 else
147                                         _mappers [map.SectionType] = map;
148                         }      
149                 }
150                 
151                 public static object MapSection (object input)
152                 {
153                         if (_instance == null || input == null)
154                                 return input;
155
156                         object mappedSection;
157                         if (_mappedSections.TryGetValue (input, out mappedSection))
158                                 return mappedSection;
159                         
160                         object ret = _instance.MapSection (input, input.GetType ());
161                         lock (mapperLock) {
162                                 if (ret != null && !_mappedSections.ContainsKey (ret))
163                                         _mappedSections.Add (ret, ret);
164                         }
165                         
166                         return ret;
167                 }
168
169                 object MapSection (object input, Type type)
170                 {
171                         if (_mappers == null || _mappers.Count == 0 || !_mappers.ContainsKey (type))
172                                 return input;
173       
174                         SettingsMapping map;
175                         if (!_mappers.TryGetValue (type, out map))
176                                 return input;
177                         
178                         if (map == null)
179                                 return input;
180       
181                         return map.MapSection (input, type);
182                 }
183         }
184 }