SettingsMappingManager.cs 4.8 KB

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