1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.trace;
39
40 import com.gargoylesoftware.base.util.DetailedNullPointerException;
41 import java.beans.PropertyChangeListener;
42 import java.beans.PropertyChangeSupport;
43 import java.util.HashSet;
44 import java.util.Set;
45
46 /***
47 * This class represents a "channel" that diagnostic messages can be written to. The
48 * channel will contain zero or more TraceWriters which will be responsible for handling
49 * the messages.
50 *
51 * @version $Revision: 1.4 $
52 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
53 */
54 public class TraceChannel {
55 private final String name_;
56 private boolean isEnabled_ = true;
57 private PropertyChangeSupport propertyChangeSupport_ = null;
58 private final Set traceWriters_ = new HashSet();
59
60 /***
61 * Create a trace channel with the specified name.
62 *
63 * @param name The name of the channel.
64 */
65 public TraceChannel( final String name ) {
66 assertNotNull("name", name);
67 name_ = name;
68 }
69
70 /***
71 * Return the name of this trace channel.
72 *
73 * @return the name.
74 */
75 public final String getName() {
76 return name_;
77 }
78
79 /***
80 * Return a string representation of this object.
81 * @return a string representation of this object.
82 */
83 public String toString() {
84 final StringBuffer buffer = new StringBuffer();
85 buffer.append( getClass().getName() );
86 buffer.append( "[name=[" );
87 buffer.append( getName() );
88 buffer.append( "] isEnabled=[");
89 buffer.append( isEnabled_ );
90 buffer.append( "]]" );
91
92 return buffer.toString();
93 }
94
95 /***
96 * Set whether or not this channel is enabled. If it is not enabled
97 * then any requests sent to this channel will be discarded.
98 *
99 * @param isEnabled true if this channel should be enabled.
100 */
101 public void setEnabled( final boolean isEnabled ) {
102 final boolean oldValue = isEnabled_;
103 isEnabled_ = isEnabled;
104
105 if( propertyChangeSupport_ != null && oldValue != isEnabled_ ) {
106 propertyChangeSupport_.firePropertyChange("enabled",
107 isEnabled_,
108 oldValue );
109 }
110 }
111
112 /***
113 * Return true if this channel is enabled.
114 * @return true if this channel is enabled.
115 */
116 public boolean isEnabled() {
117 return isEnabled_;
118 }
119
120 /***
121 * Add a property change listener.
122 *
123 * @param listener The new listener
124 */
125 public void addPropertyChangeListener( final PropertyChangeListener listener ) {
126 if( propertyChangeSupport_ == null ) {
127 synchronized(this) {
128 if( propertyChangeSupport_ == null ) {
129 propertyChangeSupport_ = new PropertyChangeSupport(this);
130 }
131 }
132 }
133 propertyChangeSupport_.addPropertyChangeListener(listener);
134 }
135
136 /***
137 * Remove a property change listener.
138 *
139 * @param listener The listener to remove
140 */
141 public void removePropertyChangeListener( final PropertyChangeListener listener ) {
142 if( propertyChangeSupport_ != null ) {
143 propertyChangeSupport_.removePropertyChangeListener(listener);
144 }
145 }
146
147 /***
148 * Return a collection of trace writers
149 * @return The trace writers
150 */
151 /*package*/ Set getTraceWriters() {/package-summary/html">class="comment">package*/ Set getTraceWriters() {/package-summary.html"> Set getTraceWriters() {
152 return traceWriters_;
153 }
154
155 /***
156 * Add a trace writer.
157 * @param writer A trace writer
158 */
159 public void addTraceWriter( final TraceWriter writer ) {
160 assertNotNull("writer", writer);
161 synchronized( traceWriters_ ) {
162 traceWriters_.add(writer);
163 }
164 }
165
166 /***
167 * Remove a trace writer.
168 * @param writer A trace writer
169 */
170 public void removeTraceWriter( final TraceWriter writer ) {
171 assertNotNull("writer", writer);
172 synchronized( traceWriters_ ) {
173 traceWriters_.remove(writer);
174 }
175 }
176
177
178 /***
179 * Verify that the specified value is not null. If it is then throw an exception
180 *
181 * @param fieldName The name of the field to check
182 * @param fieldValue The value of the field to check
183 * @exception DetailedNullPointerException If fieldValue is null
184 */
185 protected final void assertNotNull( final String fieldName, final Object fieldValue )
186 throws DetailedNullPointerException {
187
188 if( fieldValue == null ) {
189 throw new DetailedNullPointerException(fieldName);
190 }
191 }
192 }