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.
31 using System.Collections.Generic;
32 using System.Collections.Specialized;
34 using System.Runtime.InteropServices;
36 using System.Web.Configuration;
38 using System.Xml.XPath;
40 namespace Mono.Web.Util
42 public class SettingsMappingManager
44 const string settingsMapFileName = "settings.map";
45 const string localSettingsMapFileName = settingsMapFileName + ".config";
47 static object mapperLock = new object ();
49 static SettingsMappingManager _instance;
50 static string _mappingFile;
51 Dictionary <Type, SettingsMapping> _mappers;
52 static Dictionary <object, object> _mappedSections;
53 static SettingsMappingPlatform _myPlatform;
55 static bool _runningOnWindows;
56 internal static bool IsRunningOnWindows {
57 get { return _runningOnWindows; }
60 public static SettingsMappingPlatform Platform {
61 get { return _myPlatform; }
64 public bool HasMappings {
65 get { return (_mappers != null && _mappers.Count > 0); }
68 static SettingsMappingManager ()
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);
76 static public void Init ()
78 if (_instance != null)
81 if (Environment.GetEnvironmentVariable ("MONO_ASPNET_INHIBIT_SETTINGSMAP") != null)
84 NameValueCollection appSettings = WebConfigurationManager.AppSettings;
85 if (appSettings != null) {
86 string inhibit = appSettings ["MonoAspnetInhibitSettingsMap"];
87 if (String.Compare (inhibit, "true", StringComparison.OrdinalIgnoreCase) == 0)
91 if (IsRunningOnWindows)
92 _myPlatform = SettingsMappingPlatform.Windows;
94 _myPlatform = SettingsMappingPlatform.Unix;
96 SettingsMappingManager mapper = new SettingsMappingManager ();
97 mapper.LoadMappings ();
99 if (mapper.HasMappings) {
101 _mappedSections = new Dictionary <object, object> ();
107 if (File.Exists (_mappingFile))
108 LoadMappings (_mappingFile);
110 AppDomainSetup domain = AppDomain.CurrentDomain.SetupInformation;
111 string appMappingFile = Path.Combine (domain.ApplicationBase, localSettingsMapFileName);
112 if (File.Exists (appMappingFile))
113 LoadMappings (appMappingFile);
116 void LoadMappings (string mappingFilePath)
121 doc = new XPathDocument (mappingFilePath);
122 top = doc.CreateNavigator ();
123 } catch (Exception ex) {
124 throw new ApplicationException ("Error loading mapping settings", ex);
127 XPathNodeIterator iter;
128 if (_mappers == null)
129 _mappers = new Dictionary <Type, SettingsMapping> ();
131 iter = top.Select ("//settingsMap/clear");
132 if (iter.MoveNext ())
136 iter = top.Select ("//settingsMap/map[string-length (@sectionType) > 0 and string-length (@mapperType) > 0 and string-length (@platform) > 0]");
139 while (iter.MoveNext ()) {
140 map = new SettingsMapping (iter.Current);
141 if (_myPlatform != map.Platform)
144 if (!_mappers.ContainsKey (map.SectionType))
145 _mappers.Add (map.SectionType, map);
147 _mappers [map.SectionType] = map;
151 public static object MapSection (object input)
153 if (_instance == null || input == null)
156 object mappedSection;
157 if (_mappedSections.TryGetValue (input, out mappedSection))
158 return mappedSection;
160 object ret = _instance.MapSection (input, input.GetType ());
162 if (ret != null && !_mappedSections.ContainsKey (ret))
163 _mappedSections.Add (ret, ret);
169 object MapSection (object input, Type type)
171 if (_mappers == null || _mappers.Count == 0 || !_mappers.ContainsKey (type))
175 if (!_mappers.TryGetValue (type, out map))
181 return map.MapSection (input, type);