1 //------------------------------------------------------------------------------
2 // <copyright file="SqlCacheDependencyDatabase.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 namespace System.Web.Configuration {
8     using System;
9     using System.Xml;
10     using System.Configuration;
11     using System.Collections.Specialized;
12     using System.Collections;
13     using System.Globalization;
14     using System.IO;
15     using System.Text;
16     using System.Diagnostics;
17     using System.Security.Permissions;
18 
19     // class SqlCacheDependencySection
20 
21     public sealed class SqlCacheDependencyDatabase : ConfigurationElement {
22         private static readonly ConfigurationElementProperty s_elemProperty = new ConfigurationElementProperty(new CallbackValidator(typeof(SqlCacheDependencyDatabase), Validate));
23 
24         private static ConfigurationPropertyCollection _properties;
25         private static readonly ConfigurationProperty _propName;
26         private static readonly ConfigurationProperty _propConnectionStringName;
27         private static readonly ConfigurationProperty _propPollTime;
28 
SqlCacheDependencyDatabase()29         static SqlCacheDependencyDatabase() {
30             // Property initialization
31             _properties = new ConfigurationPropertyCollection();
32 
33             _propName =
34                 new ConfigurationProperty("name",
35                                             typeof(string),
36                                             null,
37                                             null,
38                                             StdValidatorsAndConverters.NonEmptyStringValidator,
39                                             ConfigurationPropertyOptions.IsRequired |
40                                             ConfigurationPropertyOptions.IsKey);
41 
42             _propConnectionStringName =
43                 new ConfigurationProperty("connectionStringName",
44                                             typeof(string),
45                                             null,
46                                             null,
47                                             StdValidatorsAndConverters.NonEmptyStringValidator,
48                                             ConfigurationPropertyOptions.IsRequired);
49 
50             _propPollTime = new ConfigurationProperty("pollTime",
51                                             typeof(int),
52                                             60000,
53                                             ConfigurationPropertyOptions.None);
54 
55             _properties.Add(_propName);
56             _properties.Add(_propConnectionStringName);
57             _properties.Add(_propPollTime);
58         }
59 
60         private int defaultPollTime;    // This may be set by the outer node to specify the default poll time (i.e. not specified on this node)
61 
SqlCacheDependencyDatabase(string name, string connectionStringName, int pollTime)62         public SqlCacheDependencyDatabase(string name, string connectionStringName, int pollTime) {
63             Name = name;
64             ConnectionStringName = connectionStringName;
65             PollTime = pollTime;
66         }
67 
SqlCacheDependencyDatabase(string name, string connectionStringName)68         public SqlCacheDependencyDatabase(string name, string connectionStringName) {
69             Name = name;
70             ConnectionStringName = connectionStringName;
71         }
72 
SqlCacheDependencyDatabase()73         internal SqlCacheDependencyDatabase() {
74         }
75 
76         protected override ConfigurationPropertyCollection Properties {
77             get {
78                 return _properties;
79             }
80         }
81         protected override ConfigurationElementProperty ElementProperty {
82             get {
83                 return s_elemProperty;
84             }
85         }
Validate(object value)86         private static void Validate(object value) {
87             if (value == null) {
88                 throw new ArgumentNullException("sqlCacheDependencyDatabase");
89             }
90             Debug.Assert(value is SqlCacheDependencyDatabase);
91 
92             SqlCacheDependencyDatabase elem = (SqlCacheDependencyDatabase)value;
93 
94             if (elem.PollTime != 0 && elem.PollTime < 500) {
95                 throw new ConfigurationErrorsException(
96                     SR.GetString(SR.Invalid_sql_cache_dep_polltime),
97                     elem.ElementInformation.Properties["pollTime"].Source,
98                     elem.ElementInformation.Properties["pollTime"].LineNumber);
99             }
100         }
101 
102 
CheckDefaultPollTime(int value)103         internal void CheckDefaultPollTime(int value) {
104             // This method will be called by the outer node.
105             // If the poolTime property is not specified in the node, then grab the one
106             // from above.
107             if (ElementInformation.Properties["pollTime"].ValueOrigin == PropertyValueOrigin.Default) {
108                 defaultPollTime = value;
109             }
110         }
111 
112         [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
113         [StringValidator(MinLength = 1)]
114         public string Name {
115             get {
116                 return (string)base[_propName];
117             }
118             set {
119                 base[_propName] = value;
120             }
121         }
122 
123         [ConfigurationProperty("connectionStringName", IsRequired = true)]
124         [StringValidator(MinLength = 1)]
125         public string ConnectionStringName {
126             get {
127                 return (string)base[_propConnectionStringName];
128             }
129             set {
130                 base[_propConnectionStringName] = value;
131             }
132         }
133 
134         [ConfigurationProperty("pollTime", DefaultValue = 60000)]
135         public int PollTime {
136             get {
137                 if (ElementInformation.Properties["pollTime"].ValueOrigin == PropertyValueOrigin.Default) {
138                     return defaultPollTime;   // Return the default value from outer node
139                 }
140                 else {
141                     return (int)base[_propPollTime];
142                 }
143             }
144             set {
145                 base[_propPollTime] = value;
146             }
147         }
148     }
149 }
150