View Javadoc

1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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">/*package*/ Set getTraceWriters() {/package-summary.html">class="comment">package*/ 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 }