SettingsMappingManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // System.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. #if NET_2_0
  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. namespace System.Web.Util
  40. {
  41. public class SettingsMappingManager
  42. {
  43. const string settingsMapFileName = "settings.map";
  44. const string localSettingsMapFileName = settingsMapFileName + ".config";
  45. static SettingsMappingManager _instance;
  46. static string _mappingFile;
  47. Dictionary <Type, SettingsMapping> _mappers;
  48. static Dictionary <object, object> _mappedSections;
  49. static SettingsMappingPlatform _myPlatform;
  50. public static SettingsMappingPlatform Platform {
  51. get { return _myPlatform; }
  52. }
  53. public bool HasMappings {
  54. get { return (_mappers != null && _mappers.Count > 0); }
  55. }
  56. static SettingsMappingManager ()
  57. {
  58. _mappingFile = Path.Combine (Path.GetDirectoryName (RuntimeEnvironment.SystemConfigurationFile), settingsMapFileName);
  59. }
  60. static public void Init ()
  61. {
  62. if (_instance != null)
  63. return;
  64. if (Environment.GetEnvironmentVariable ("MONO_ASPNET_INHIBIT_SETTINGSMAP") != null)
  65. return;
  66. NameValueCollection appSettings = WebConfigurationManager.AppSettings;
  67. if (appSettings != null) {
  68. string inhibit = appSettings ["MonoAspnetInhibitSettingsMap"];
  69. if (String.Compare (inhibit, "true", StringComparison.OrdinalIgnoreCase) == 0)
  70. return;
  71. }
  72. if (HttpApplication.IsRunningOnWindows)
  73. _myPlatform = SettingsMappingPlatform.Windows;
  74. else
  75. _myPlatform = SettingsMappingPlatform.Unix;
  76. SettingsMappingManager mapper = new SettingsMappingManager ();
  77. mapper.LoadMappings ();
  78. if (mapper.HasMappings) {
  79. _instance = mapper;
  80. _mappedSections = new Dictionary <object, object> ();
  81. }
  82. }
  83. void LoadMappings ()
  84. {
  85. if (File.Exists (_mappingFile))
  86. LoadMappings (_mappingFile);
  87. AppDomainSetup domain = AppDomain.CurrentDomain.SetupInformation;
  88. string appMappingFile = Path.Combine (domain.ApplicationBase, localSettingsMapFileName);
  89. if (File.Exists (appMappingFile))
  90. LoadMappings (appMappingFile);
  91. }
  92. void LoadMappings (string mappingFilePath)
  93. {
  94. XPathNavigator top;
  95. XPathDocument doc;
  96. try {
  97. doc = new XPathDocument (mappingFilePath);
  98. top = doc.CreateNavigator ();
  99. } catch (Exception ex) {
  100. throw new ApplicationException ("Error loading mapping settings", ex);
  101. }
  102. XPathNodeIterator iter;
  103. if (_mappers == null)
  104. _mappers = new Dictionary <Type, SettingsMapping> ();
  105. else {
  106. iter = top.Select ("//settingsMap/clear");
  107. if (iter.MoveNext ())
  108. _mappers.Clear ();
  109. }
  110. iter = top.Select ("//settingsMap/map[string-length (@sectionType) > 0 and string-length (@mapperType) > 0 and string-length (@platform) > 0]");
  111. SettingsMapping map;
  112. while (iter.MoveNext ()) {
  113. map = new SettingsMapping (iter.Current);
  114. if (_myPlatform != map.Platform)
  115. continue;
  116. if (!_mappers.ContainsKey (map.SectionType))
  117. _mappers.Add (map.SectionType, map);
  118. else
  119. _mappers [map.SectionType] = map;
  120. }
  121. }
  122. public static object MapSection (object input)
  123. {
  124. if (_instance == null || input == null)
  125. return input;
  126. object mappedSection;
  127. if (_mappedSections.TryGetValue (input, out mappedSection))
  128. return mappedSection;
  129. object ret = _instance.MapSection (input, input.GetType ());
  130. if (ret != null && !_mappedSections.ContainsKey (ret))
  131. _mappedSections.Add (ret, ret);
  132. return ret;
  133. }
  134. object MapSection (object input, Type type)
  135. {
  136. if (_mappers == null || _mappers.Count == 0 || !_mappers.ContainsKey (type))
  137. return input;
  138. SettingsMapping map;
  139. if (!_mappers.TryGetValue (type, out map))
  140. return input;
  141. if (map == null)
  142. return input;
  143. return map.MapSection (input, type);
  144. }
  145. }
  146. }
  147. #endif