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.util.Date;
42  
43  /***
44   * An item of data to be written.
45   *
46   * @version $Revision: 1.5 $
47   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
48   */
49  public class TraceItem implements Cloneable {
50  
51      private TraceItem prevItem_;
52      private TraceItem nextItem_;
53  
54      private TraceChannel channel_;
55  
56      private String message_;
57      private Throwable throwable_;
58      private Date time_;
59      private Thread thread_;
60      private Object lock_;
61  
62      /***
63       * Create a new item.
64       */
65      public TraceItem() {
66      }
67  
68      /***
69       * Reset all the values to null
70       */
71      /*package*/void clear() {/package-summary/html">class="comment">package*/void clear() {/package-summary.html">/*package*/void clear() {/package-summary.html">class="comment">package*/void clear() {
72          message_   = null;
73          throwable_ = null;
74          time_      = null;
75          thread_    = null;
76          lock_      = null;
77          channel_   = null;
78      }
79  
80      /*** @return true if the message has no content */
81      boolean isClear() {
82          return message_ == null
83              && throwable_ == null
84              && time_ == null
85              && thread_ == null
86              && lock_ == null
87              && channel_ == null;
88      }
89  
90      /***
91       * Return a string representation of this object for testing purposes.
92       * @return The string representation
93       */
94      public String toString() {
95          final StringBuffer buffer = new StringBuffer(200);
96          buffer.append("TraceItem[message=[");
97          buffer.append(message_);
98          buffer.append("] throwable=[");
99          buffer.append(throwable_);
100         buffer.append("] time=[");
101         buffer.append(time_);
102         buffer.append("] thread=[");
103         buffer.append(thread_);
104         buffer.append("] lock=[");
105         buffer.append(lock_);
106         buffer.append("] channel=[");
107         buffer.append(channel_);
108         buffer.append("]]");
109         return buffer.toString();
110     }
111     /***
112      * Return true if this item contains any text.
113      * @return true if this item contains any text.
114      */
115     public boolean containsText() {
116         return message_ != null | throwable_ != null;
117     }
118 
119     /***
120      * Return the message.
121     * @return the message.
122      */
123     public String getMessage() {
124         return message_;
125     }
126 
127     /*** @param string The message */
128     void setMessage( final String string ) {
129         message_ = string;
130     }
131 
132     /***
133      * Return the throwable.
134      * @return the throwable.
135      */
136     public Throwable getThrowable() {
137         return throwable_;
138     }
139 
140     /*** @param t The throwable */
141     void setThrowable( final Throwable t ) {
142         throwable_ = t;
143     }
144 
145     /***
146      * Return the time that the item was written to Trace
147      * @return the time.
148      */
149     public Date getTime() {
150         return time_;
151     }
152 
153     /*** @param time The time */
154     void setTime( final Date time ) {
155         assertNotNull("time", time);
156         time_ = time;
157     }
158 
159     /*** @return The thread */
160     Thread getThread() {
161         return thread_;
162     }
163 
164     /*** @param thread The thread */
165     void setThread( final Thread thread ) {
166         assertNotNull("thread", thread);
167         thread_ = thread;
168     }
169     /***
170      * Return the name of the thread that called Trace.
171      * @return the name of the thread.
172      */
173     public String getThreadName() {
174 
175         if( thread_ == null ) {
176             return null;
177         }
178 
179         try {
180             return thread_.getName();
181         }
182         catch( final NullPointerException e ) {
183             // Workaround for bug in JDK1.2.2 where getName() will throw
184             // a NullPointerException in some cases.
185             return "Unknown thread[hash=" + thread_.hashCode() + "]";
186         }
187     }
188 
189     /***
190      * Return the lock.
191      * @return the lock.
192      */
193     public Object getLock() {
194         return lock_;
195     }
196     
197     /*** @param lock The lock */
198     void setLock( final Object lock ) {
199         lock_ = lock;
200     }
201 
202     /***
203      * Return the previous item.
204      * @return the previous item.
205      */
206     public TraceItem getPrevItem() {
207         return prevItem_;
208     }
209     
210     /*** @param item The previous item */
211     void setPrevItem( final TraceItem item ) {
212         prevItem_ = item;
213     }
214 
215     /***
216      * Return the next item.
217      * @return the next item.
218      */
219     public TraceItem getNextItem() {
220         return nextItem_;
221     }
222     
223     /*** @param item The next item */
224     void setNextItem( final TraceItem item ) {
225         nextItem_ = item;
226     }
227 
228     /***
229      * Return the channel.
230      * @return the channel.
231      */
232     public TraceChannel getChannel() {
233         return channel_;
234     }
235     
236     /*** @param channel The channel */
237     void setChannel( final TraceChannel channel ) {
238         assertNotNull("channel", channel);
239         channel_ = channel;
240     }
241 
242     /***
243      * Return a copy of this object.
244      * @return A copy.
245      * @throws CloneNotSupportedException If this object is not cloneable.
246      */
247     public Object clone() throws CloneNotSupportedException {
248         return super.clone();
249     }
250 
251 
252     /***
253      * Verify that the specified value is not null.  If it is then throw an exception
254      *
255      * @param fieldName The name of the field to check
256      * @param fieldValue The value of the field to check
257      * @exception DetailedNullPointerException If fieldValue is null
258      */
259     protected final void assertNotNull( final String fieldName, final Object fieldValue )
260         throws DetailedNullPointerException {
261 
262         if( fieldValue == null ) {
263             throw new DetailedNullPointerException(fieldName);
264         }
265     }
266 }
267