SettingsBase.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // System.Web.UI.WebControls.SettingsBase.cs
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.ComponentModel;
  32. namespace System.Configuration
  33. {
  34. public abstract class SettingsBase
  35. {
  36. protected SettingsBase ()
  37. {
  38. }
  39. public void Initialize (SettingsContext context,
  40. SettingsPropertyCollection properties,
  41. SettingsProviderCollection providers)
  42. {
  43. this.context = context;
  44. this.properties = properties;
  45. this.providers = providers;
  46. // values do not seem to be reset here!! (otherwise one of the SettingsBaseTest will fail)
  47. }
  48. public virtual void Save ()
  49. {
  50. if (sync)
  51. lock (this)
  52. SaveCore ();
  53. else
  54. SaveCore ();
  55. }
  56. void SaveCore ()
  57. {
  58. //
  59. // Copied from ApplicationSettingsBase
  60. //
  61. #if (CONFIGURATION_DEP)
  62. /* ew.. this needs to be more efficient */
  63. foreach (SettingsProvider provider in Providers) {
  64. SettingsPropertyValueCollection cache = new SettingsPropertyValueCollection ();
  65. foreach (SettingsPropertyValue val in PropertyValues) {
  66. if (val.Property.Provider == provider)
  67. cache.Add (val);
  68. }
  69. if (cache.Count > 0)
  70. provider.SetPropertyValues (Context, cache);
  71. }
  72. #endif
  73. }
  74. public static SettingsBase Synchronized (SettingsBase settingsBase)
  75. {
  76. settingsBase.sync = true;
  77. return settingsBase;
  78. }
  79. public virtual SettingsContext Context {
  80. get { return context; }
  81. }
  82. [Browsable (false)]
  83. public bool IsSynchronized {
  84. get { return sync; }
  85. }
  86. public virtual object this [ string propertyName ] {
  87. get
  88. {
  89. if (sync)
  90. lock (this) {
  91. return GetPropertyValue (propertyName);
  92. }
  93. else
  94. return GetPropertyValue (propertyName);
  95. }
  96. set
  97. {
  98. if (sync)
  99. lock (this) {
  100. SetPropertyValue (propertyName, value);
  101. }
  102. else
  103. SetPropertyValue (propertyName, value);
  104. }
  105. }
  106. public virtual SettingsPropertyCollection Properties {
  107. get {
  108. // It seems that Properties.IsSynchronized is
  109. // nothing to do with this.IsSynchronized.
  110. return properties;
  111. }
  112. }
  113. public virtual SettingsPropertyValueCollection PropertyValues {
  114. get {
  115. if (sync) {
  116. lock (this) {
  117. return values;
  118. }
  119. } else {
  120. return values;
  121. }
  122. }
  123. }
  124. public virtual SettingsProviderCollection Providers {
  125. get {
  126. return providers;
  127. }
  128. }
  129. object GetPropertyValue (string propertyName)
  130. {
  131. SettingsProperty prop = null;
  132. if (Properties == null || (prop = Properties [propertyName]) == null)
  133. throw new SettingsPropertyNotFoundException (
  134. string.Format ("The settings property '{0}' was not found", propertyName));
  135. if (values [propertyName] == null)
  136. foreach (SettingsPropertyValue v in prop.Provider.GetPropertyValues (Context, Properties))
  137. values.Add (v);
  138. return PropertyValues [propertyName].PropertyValue;
  139. }
  140. void SetPropertyValue (string propertyName, object value)
  141. {
  142. SettingsProperty prop = null;
  143. if (Properties == null || (prop = Properties [propertyName]) == null)
  144. throw new SettingsPropertyNotFoundException (
  145. string.Format ("The settings property '{0}' was not found", propertyName));
  146. if (prop.IsReadOnly)
  147. throw new SettingsPropertyIsReadOnlyException (
  148. string.Format ("The settings property '{0}' is read only", propertyName));
  149. if (prop.PropertyType != value.GetType ())
  150. throw new SettingsPropertyWrongTypeException (
  151. string.Format ("The value supplied is of a type incompatible with the settings property '{0}'", propertyName));
  152. PropertyValues [propertyName].PropertyValue = value;
  153. }
  154. bool sync;
  155. SettingsContext context;
  156. SettingsPropertyCollection properties;
  157. SettingsProviderCollection providers;
  158. SettingsPropertyValueCollection values = new SettingsPropertyValueCollection ();
  159. }
  160. }
  161. #endif