2008-04-16 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System / System.Diagnostics / Switch.cs
1 //
2 // System.Diagnostics.Switch.cs
3 //
4 // Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation 
5 // can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
6 //
7 // Author:
8 //      John R. Hicks  (angryjohn69@nc.rr.com)
9 //      Jonathan Pryor (jonpryor@vt.edu)
10 //
11 // (C) 2001-2002
12 //
13
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.Collections;
36 using System.Collections.Specialized;
37 #if NET_2_0 && CONFIGURATION_DEP
38 using System.Configuration;
39 #endif
40
41 namespace System.Diagnostics
42 {
43         public abstract class Switch
44         {
45                 private string name;
46                 private string description;
47                 private int switchSetting;
48 #if NET_2_0
49                 private string value;
50                 private string defaultSwitchValue;
51 #endif
52                 // MS Behavior is that (quoting from MSDN for OnSwitchSettingChanged()):
53                 //              "...It is invoked the first time a switch reads its value from the
54                 //              configuration file..."
55                 // The docs + testing implies two things:
56                 //      1. The value of the switch is not read in from the constructor
57                 //      2. The value is instead read in on the first time get_SwitchSetting is
58                 //              invoked
59                 // Assuming that OnSwitchSettingChanged() is invoked on a .config file
60                 // read and on all changes
61                 //
62                 // Thus, we need to keep track of whether or not switchSetting has been
63                 // initialized.  Using `switchSetting=-1' seems logical, but if someone
64                 // actually wants to use -1 as a switch value that would cause problems.
65                 private bool initialized;
66
67                 protected Switch(string displayName, string description)
68                 {
69                         this.name = displayName;
70                         this.description = description;
71                 }
72
73 #if NET_2_0
74                 protected Switch(string displayName, string description, string defaultSwitchValue)
75                         : this (displayName, description)
76                 {
77                         this.defaultSwitchValue = defaultSwitchValue;
78                 }
79 #endif
80
81                 public string Description {
82                         get {return description;}
83                 }
84
85                 public string DisplayName {
86                         get {return name;}
87                 }
88
89                 protected int SwitchSetting {
90                         get {
91                                 if (!initialized) {
92                                         initialized = true;
93                                         GetConfigFileSetting ();
94                                         OnSwitchSettingChanged ();
95                                 }
96                                 return switchSetting;
97                         }
98                         set {
99                                 if(switchSetting != value) {
100                                         switchSetting = value;
101                                         OnSwitchSettingChanged();
102                                 }
103                                 initialized = true;
104                         }
105                 }
106
107 #if NET_2_0
108                 StringDictionary attributes = new StringDictionary ();
109
110 #if XML_DEP
111                 [System.Xml.Serialization.XmlIgnore]
112 #endif
113                 public StringDictionary Attributes {
114                         get { return attributes; }
115                 }
116
117                 protected string Value {
118                         get { return value; }
119                         set {
120                                 this.value = value;
121 #if NET_2_0 && CONFIGURATION_DEP
122                                 try {
123                                         OnValueChanged ();
124                                 } catch (Exception ex) {
125                                         string msg = string.Format ("The config "
126                                                 + "value for Switch '{0}' was "
127                                                 + "invalid.", DisplayName);
128
129                                         throw new ConfigurationErrorsException (
130                                                 msg, ex);
131                                 }
132 #else
133                                 OnValueChanged ();
134 #endif
135                         }
136                 }
137
138                 protected internal virtual string [] GetSupportedAttributes ()
139                 {
140                         return null;
141                 }
142
143                 protected virtual void OnValueChanged ()
144                 {
145                 }
146 #endif
147
148                 private void GetConfigFileSetting ()
149                 {
150                         IDictionary d = (IDictionary) DiagnosticsConfiguration.Settings ["switches"];
151                         
152                         // Load up the specified switch
153                         if (d != null) {
154                                 if (d.Contains (name)) {
155 #if NET_2_0 && CONFIGURATION_DEP
156                                         Value = d [name] as string;
157 #else
158                                         switchSetting = (int) d [name];
159 #endif
160                                         return;
161                                 }
162                         }
163
164 #if NET_2_0
165                         if (defaultSwitchValue != null) {
166                                 value = defaultSwitchValue;
167                                 OnValueChanged ();
168                         }
169 #endif
170                 }
171
172                 protected virtual void OnSwitchSettingChanged()
173                 {
174                         // Do nothing.  This is merely provided for derived classes to know when
175                         // the value of SwitchSetting has changed.
176                 }
177         }
178 }