SettingsBase.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. using System;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. namespace System.Configuration
  32. {
  33. public abstract class SettingsBase
  34. {
  35. protected SettingsBase ()
  36. {
  37. }
  38. public void Initialize (SettingsContext context,
  39. SettingsPropertyCollection properties,
  40. SettingsProviderCollection providers)
  41. {
  42. this.context = context;
  43. this.properties = properties;
  44. this.providers = providers;
  45. // values do not seem to be reset here!! (otherwise one of the SettingsBaseTest will fail)
  46. }
  47. public virtual void Save ()
  48. {
  49. if (sync)
  50. lock (this)
  51. SaveCore ();
  52. else
  53. SaveCore ();
  54. }
  55. void SaveCore ()
  56. {
  57. //
  58. // Copied from ApplicationSettingsBase
  59. //
  60. #if (CONFIGURATION_DEP)
  61. /* ew.. this needs to be more efficient */
  62. foreach (SettingsProvider provider in Providers) {
  63. SettingsPropertyValueCollection cache = new SettingsPropertyValueCollection ();
  64. foreach (SettingsPropertyValue val in PropertyValues) {
  65. if (val.Property.Provider == provider)
  66. cache.Add (val);
  67. }
  68. if (cache.Count > 0)
  69. provider.SetPropertyValues (Context, cache);
  70. }
  71. #endif
  72. }
  73. public static SettingsBase Synchronized (SettingsBase settingsBase)
  74. {
  75. settingsBase.sync = true;
  76. return settingsBase;
  77. }
  78. public virtual SettingsContext Context {
  79. get { return context; }
  80. }
  81. [Browsable (false)]
  82. public bool IsSynchronized {
  83. get { return sync; }
  84. }
  85. public virtual object this [ string propertyName ] {
  86. get
  87. {
  88. if (sync)
  89. lock (this) {
  90. return GetPropertyValue (propertyName);
  91. }
  92. else
  93. return GetPropertyValue (propertyName);
  94. }
  95. set
  96. {
  97. if (sync)
  98. lock (this) {
  99. SetPropertyValue (propertyName, value);
  100. }
  101. else
  102. SetPropertyValue (propertyName, value);
  103. }
  104. }
  105. public virtual SettingsPropertyCollection Properties {
  106. get {
  107. // It seems that Properties.IsSynchronized is
  108. // nothing to do with this.IsSynchronized.
  109. return properties;
  110. }
  111. }
  112. public virtual SettingsPropertyValueCollection PropertyValues {
  113. get {
  114. if (sync) {
  115. lock (this) {
  116. return values;
  117. }
  118. } else {
  119. return values;
  120. }
  121. }
  122. }
  123. public virtual SettingsProviderCollection Providers {
  124. get {
  125. return providers;
  126. }
  127. }
  128. object GetPropertyValue (string propertyName)
  129. {
  130. SettingsProperty prop = null;
  131. if (Properties == null || (prop = Properties [propertyName]) == null)
  132. throw new SettingsPropertyNotFoundException (
  133. string.Format ("The settings property '{0}' was not found", propertyName));
  134. if (values [propertyName] == null)
  135. foreach (SettingsPropertyValue v in prop.Provider.GetPropertyValues (Context, Properties))
  136. values.Add (v);
  137. return PropertyValues [propertyName].PropertyValue;
  138. }
  139. void SetPropertyValue (string propertyName, object value)
  140. {
  141. SettingsProperty prop = null;
  142. if (Properties == null || (prop = Properties [propertyName]) == null)
  143. throw new SettingsPropertyNotFoundException (
  144. string.Format ("The settings property '{0}' was not found", propertyName));
  145. if (prop.IsReadOnly)
  146. throw new SettingsPropertyIsReadOnlyException (
  147. string.Format ("The settings property '{0}' is read only", propertyName));
  148. if (prop.PropertyType != value.GetType ())
  149. throw new SettingsPropertyWrongTypeException (
  150. string.Format ("The value supplied is of a type incompatible with the settings property '{0}'", propertyName));
  151. PropertyValues [propertyName].PropertyValue = value;
  152. }
  153. bool sync;
  154. SettingsContext context;
  155. SettingsPropertyCollection properties;
  156. SettingsProviderCollection providers;
  157. SettingsPropertyValueCollection values = new SettingsPropertyValueCollection ();
  158. }
  159. }