2 // Mono.Web.Util.SettingsMappingManager
5 // Marek Habersack (mhabersack@novell.com)
7 // (C) 2007 Novell, Inc
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections.Generic;
33 using System.Collections.Specialized;
35 using System.Runtime.InteropServices;
37 using System.Web.Configuration;
39 using System.Xml.XPath;
41 namespace Mono.Web.Util
43 public class SettingsMappingManager
45 const string settingsMapFileName = "settings.map";
46 const string localSettingsMapFileName = settingsMapFileName + ".config";
48 static object mapperLock = new object ();
50 static SettingsMappingManager _instance;
51 static string _mappingFile;
52 Dictionary <Type, SettingsMapping> _mappers;
53 static Dictionary <object, object> _mappedSections;
54 static SettingsMappingPlatform _myPlatform;
56 static bool _runningOnWindows;
57 internal static bool IsRunningOnWindows {
58 get { return _runningOnWindows; }
61 public static SettingsMappingPlatform Platform {
62 get { return _myPlatform; }
65 public bool HasMappings {
66 get { return (_mappers != null && _mappers.Count > 0); }
69 static SettingsMappingManager ()
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);
77 static public void Init ()
79 if (_instance != null)
82 if (Environment.GetEnvironmentVariable ("MONO_ASPNET_INHIBIT_SETTINGSMAP") != null)
85 NameValueCollection appSettings = WebConfigurationManager.AppSettings;
86 if (appSettings != null) {
87 string inhibit = appSettings ["MonoAspnetInhibitSettingsMap"];
88 if (String.Compare (inhibit, "true", StringComparison.OrdinalIgnoreCase) == 0)
92 if (IsRunningOnWindows)
93 _myPlatform = SettingsMappingPlatform.Windows;
95 _myPlatform = SettingsMappingPlatform.Unix;
97 SettingsMappingManager mapper = new SettingsMappingManager ();
98 mapper.LoadMappings ();
100 if (mapper.HasMappings) {
102 _mappedSections = new Dictionary <object, object> ();
108 if (File.Exists (_mappingFile))
109 LoadMappings (_mappingFile);
111 AppDomainSetup domain = AppDomain.CurrentDomain.SetupInformation;
112 string appMappingFile = Path.Combine (domain.ApplicationBase, localSettingsMapFileName);
113 if (File.Exists (appMappingFile))
114 LoadMappings (appMappingFile);
117 void LoadMappings (string mappingFilePath)
122 doc = new XPathDocument (mappingFilePath);
123 top = doc.CreateNavigator ();
124 } catch (Exception ex) {
125 throw new ApplicationException ("Error loading mapping settings", ex);
128 XPathNodeIterator iter;
129 if (_mappers == null)
130 _mappers = new Dictionary <Type, SettingsMapping> ();
132 iter = top.Select ("//settingsMap/clear");
133 if (iter.MoveNext ())
137 iter = top.Select ("//settingsMap/map[string-length (@sectionType) > 0 and string-length (@mapperType) > 0 and string-length (@platform) > 0]");
140 while (iter.MoveNext ()) {
141 map = new SettingsMapping (iter.Current);
142 if (_myPlatform != map.Platform)
145 if (!_mappers.ContainsKey (map.SectionType))
146 _mappers.Add (map.SectionType, map);
148 _mappers [map.SectionType] = map;
152 public static object MapSection (object input)
154 if (_instance == null || input == null)
157 object mappedSection;
158 if (_mappedSections.TryGetValue (input, out mappedSection))
159 return mappedSection;
161 object ret = _instance.MapSection (input, input.GetType ());
163 if (ret != null && !_mappedSections.ContainsKey (ret))
164 _mappedSections.Add (ret, ret);
170 object MapSection (object input, Type type)
172 if (_mappers == null || _mappers.Count == 0 || !_mappers.ContainsKey (type))
176 if (!_mappers.TryGetValue (type, out map))
182 return map.MapSection (input, type);