Merge pull request #1109 from adbre/iss358
[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 #if NET_2_0
31 using System;
32 using System.Collections.Generic;
33 using System.Collections.Specialized;
34 using System.IO;
35 using System.Runtime.InteropServices;
36 using System.Web;
37 using System.Web.Configuration;
38 using System.Xml;
39 using System.Xml.XPath;
40
41 namespace Mono.Web.Util
42 {
43         public class SettingsMappingManager
44         {
45                 const string settingsMapFileName = "settings.map";
46                 const string localSettingsMapFileName = settingsMapFileName + ".config";
47
48                 static object mapperLock = new object ();
49                 
50                 static SettingsMappingManager _instance;
51                 static string _mappingFile;
52                 Dictionary <Type, SettingsMapping> _mappers;
53                 static Dictionary <object, object> _mappedSections;
54                 static SettingsMappingPlatform _myPlatform;
55
56                 static bool _runningOnWindows;
57                 internal static bool IsRunningOnWindows {
58                         get { return _runningOnWindows; }
59                 }
60
61                 public static SettingsMappingPlatform Platform {
62                         get { return _myPlatform; }
63                 }
64                 
65                 public bool HasMappings {
66                         get { return (_mappers != null && _mappers.Count > 0); }
67                 }
68                 
69                 static SettingsMappingManager ()
70                 {
71                         _mappingFile = Path.Combine (Path.GetDirectoryName (RuntimeEnvironment.SystemConfigurationFile), settingsMapFileName);
72                         PlatformID pid = Environment.OSVersion.Platform;
73                         _runningOnWindows = ((int) pid != 128 && (int) pid != 4 && (int) pid != 6);
74
75                 }
76
77                 static public void Init ()
78                 {
79                         if (_instance != null)
80                                 return;
81                         
82                         if (Environment.GetEnvironmentVariable ("MONO_ASPNET_INHIBIT_SETTINGSMAP") != null)
83                                 return;
84                                 
85                         NameValueCollection appSettings = WebConfigurationManager.AppSettings;
86                         if (appSettings != null) {
87                                 string inhibit = appSettings ["MonoAspnetInhibitSettingsMap"];
88                                 if (String.Compare (inhibit, "true", StringComparison.OrdinalIgnoreCase) == 0)
89                                         return;
90                         }
91
92                         if (IsRunningOnWindows)
93                                 _myPlatform = SettingsMappingPlatform.Windows;
94                         else
95                                 _myPlatform = SettingsMappingPlatform.Unix;
96                 
97                         SettingsMappingManager mapper = new SettingsMappingManager ();
98                         mapper.LoadMappings ();
99
100                         if (mapper.HasMappings) {
101                                 _instance = mapper;
102                                 _mappedSections = new Dictionary <object, object> ();
103                         }
104                 }
105                 
106                 void LoadMappings ()
107                 {
108                         if (File.Exists (_mappingFile))
109                                 LoadMappings (_mappingFile);
110
111                         AppDomainSetup domain = AppDomain.CurrentDomain.SetupInformation;
112                         string appMappingFile = Path.Combine (domain.ApplicationBase, localSettingsMapFileName);
113                         if (File.Exists (appMappingFile))
114                                 LoadMappings (appMappingFile);
115                 }
116                 
117                 void LoadMappings (string mappingFilePath)
118                 {
119                         XPathNavigator top;
120                         XPathDocument doc;
121                         try {
122                                 doc = new XPathDocument (mappingFilePath);
123                                 top = doc.CreateNavigator ();
124                         } catch (Exception ex) {
125                                 throw new ApplicationException ("Error loading mapping settings", ex);
126                         }
127                         
128                         XPathNodeIterator iter;
129                         if (_mappers == null)
130                                 _mappers = new Dictionary <Type, SettingsMapping> ();
131                         else {
132                                 iter = top.Select ("//settingsMap/clear");
133                                 if (iter.MoveNext ())
134                                         _mappers.Clear ();
135                         }
136
137                         iter = top.Select ("//settingsMap/map[string-length (@sectionType) > 0 and string-length (@mapperType) > 0 and string-length (@platform) > 0]");
138                         SettingsMapping map;
139                         
140                         while (iter.MoveNext ()) {
141                                 map = new SettingsMapping (iter.Current);
142                                 if (_myPlatform != map.Platform)
143                                         continue;
144                                 
145                                 if (!_mappers.ContainsKey (map.SectionType))
146                                         _mappers.Add (map.SectionType, map);
147                                 else
148                                         _mappers [map.SectionType] = map;
149                         }      
150                 }
151                 
152                 public static object MapSection (object input)
153                 {
154                         if (_instance == null || input == null)
155                                 return input;
156
157                         object mappedSection;
158                         if (_mappedSections.TryGetValue (input, out mappedSection))
159                                 return mappedSection;
160                         
161                         object ret = _instance.MapSection (input, input.GetType ());
162                         lock (mapperLock) {
163                                 if (ret != null && !_mappedSections.ContainsKey (ret))
164                                         _mappedSections.Add (ret, ret);
165                         }
166                         
167                         return ret;
168                 }
169
170                 object MapSection (object input, Type type)
171                 {
172                         if (_mappers == null || _mappers.Count == 0 || !_mappers.ContainsKey (type))
173                                 return input;
174       
175                         SettingsMapping map;
176                         if (!_mappers.TryGetValue (type, out map))
177                                 return input;
178                         
179                         if (map == null)
180                                 return input;
181       
182                         return map.MapSection (input, type);
183                 }
184         }
185 }
186 #endif