SettingsMappingManager.cs 5.3 KB

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