KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > util > Debug


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.admin.util;
24
25 //test
26

27 // JDK includes
28
import java.io.PrintStream JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31
32 // our includes
33
import com.sun.enterprise.admin.util.Assert;
34 import com.sun.enterprise.admin.util.ExceptionUtil;
35 import com.sun.enterprise.admin.util.ThrowableToString;
36
37
38 /**
39     Base functionality for output of debugging information.
40  */

41 interface IOutput
42 {
43     public void print( String JavaDoc msg );
44     public void println( String JavaDoc msg );
45
46     public void close();
47
48     public void flush();
49 }
50
51 class NullOutput implements IOutput
52 {
53     NullOutput( )
54     {
55     }
56
57         public void
58     print( String JavaDoc msg )
59     {
60         // do nothing
61
}
62
63         public void
64     println( String JavaDoc msg )
65     {
66         // do nothing
67
}
68
69         public void
70     close()
71     {
72         // do nothing
73
}
74
75         public void
76     flush()
77     {
78         // do nothing
79
}
80 }
81
82 /**
83     non-public helper class
84  */

85  class Output implements IOutput
86  {
87     OutputStream JavaDoc mOutputStream = null;
88     PrintStream JavaDoc mPrint = null;
89     boolean mCloseWhenDone = false;
90
91         public
92     Output(
93         OutputStream JavaDoc outputStream,
94         boolean closeWhenDone )
95         throws Exception JavaDoc
96     {
97         Assert.assertit( outputStream != null, "null outputStream" );
98
99         mPrint = new PrintStream JavaDoc( outputStream );
100
101         // don't assign these unless we were successful creating mPrintWriter
102
mOutputStream = outputStream;
103         mCloseWhenDone = closeWhenDone;
104     }
105
106     /**
107         Call to release/close the output stream when done.
108
109         This prevents files from remaining open for a long time.
110      */

111         public void
112     close()
113     {
114         if ( mCloseWhenDone )
115         {
116             try
117             {
118                 mOutputStream.close();
119             }
120             catch( Exception JavaDoc e )
121             {
122                 ExceptionUtil.ignoreException( e );
123             }
124         }
125         mOutputStream = null;
126         mPrint = null;
127         mCloseWhenDone = false;
128     }
129
130         public void
131     print( String JavaDoc msg )
132     {
133         mPrint.print( msg );
134     }
135
136         public void
137     println( String JavaDoc msg )
138     {
139         mPrint.println( msg );
140     }
141
142         public void
143     flush()
144     {
145         try
146         {
147             mOutputStream.flush();
148         }
149         catch( Exception JavaDoc e )
150         {
151             ExceptionUtil.ignoreException( e );
152         }
153     }
154  }
155
156
157
158     
159 /*
160     Implementation notes:
161
162       I eliminated all tests for null by defining a NullOutput class.
163     This results in a much cleaner design than testing everywhere
164     for null. There is a class invariant that "output always exists".
165     Referring to (2), this also allows things to not fail during early
166     system initialization.
167
168
169
170  */

171 /**
172     Debug provides debugging output facilities.
173     <p>
174     Basic use:
175     1. Initialize it by calling setDefaultOutput() or setFile()
176     in your main() routine or whenever you want to start it.
177     2. Invoke Debug.print() or Debug.println() as desired.
178
179
180     You can disable Debug output by calling setEnabled( false ).
181     This will leave the output alone, but temporarily disable output.
182     This is one reason everything should be routed through internalPrint().
183
184  */

185 public class Debug
186 {
187     public final static int LOW = 1,
188                             MEDIUM = 2,
189                             HIGH = 3,
190                             kDefaultSeverity = LOW;
191
192     private static int sDebugLevel = kDefaultSeverity;
193     private static IOutput sOutput = new NullOutput();
194     static
195     {
196         sOutput = createDefaultOutput();
197     }
198     private static boolean sEnabled = false;
199
200
201         private static Output
202     createOutput(
203         OutputStream JavaDoc outputStream,
204         boolean closeWhenDone ) throws Exception JavaDoc
205     {
206         // caution: don't close old before successfully creating new
207
final Output newOutput = new Output( outputStream, closeWhenDone);
208
209         // success--close possible existing output
210
cleanup();
211         sOutput = newOutput;
212
213         return( newOutput );
214     }
215
216         private static IOutput
217     createDefaultOutput()
218     {
219         IOutput output = null;
220
221         try
222         {
223             output = createOutput
224             ( System.err, false );
225         }
226         catch( Exception JavaDoc e )
227         {
228             ExceptionUtil.ignoreException( e );
229             output = new NullOutput();
230         }
231         return( output );
232     }
233
234
235
236
237         private static String JavaDoc
238     getSeverityString( int severity )
239     {
240         Assert.assertit( isValidSeverity( severity ), "illegal severity" );
241         
242         String JavaDoc s = null;
243
244         switch( severity )
245         {
246             case LOW: s = "LOW"; break;
247             case MEDIUM: s = "MEDIUM"; break;
248             case HIGH: s = "HIGH"; break;
249         }
250         return( s );
251     }
252
253         private static String JavaDoc
254     getPrefixString( int severity )
255     {
256         return( "DEBUG: severity " + getSeverityString( severity ) + ": " );
257     }
258
259         private static boolean
260     testSeverity( int severity )
261     {
262         return( severity >= sDebugLevel );
263     }
264
265         private static void
266     flush() throws Exception JavaDoc
267     {
268         sOutput.flush();
269     }
270     
271
272
273         private static void
274     internalPrintWithException(
275         Object JavaDoc msg,
276         int severity,
277         boolean newline ) throws Exception JavaDoc
278     {
279         Assert.assertit( msg != null, "null msg" );
280
281         if( testSeverity( severity ) )
282         {
283             final String JavaDoc maybeNewline = newline ? "\n" : "";
284
285             final String JavaDoc wholeMsg =
286                 getPrefixString( severity ) + msg.toString();
287             if ( newline )
288             {
289                 sOutput.println( wholeMsg );
290             }
291             else
292             {
293                 sOutput.print( wholeMsg );
294             }
295         }
296     }
297
298         private static void
299     internalPrint( Object JavaDoc msg, int severity, boolean newline )
300     {
301         Assert.assertit( isValidSeverity( severity ), "illegal severity" );
302         
303         if ( sEnabled )
304         {
305             try
306             {
307                 internalPrintWithException( msg, severity, newline );
308             }
309             catch( Exception JavaDoc e )
310             {
311                 ExceptionUtil.ignoreException( e );
312             }
313         }
314     }
315
316
317
318     /**
319          Set the output file name and force new output to this file
320          immediately.
321          <p>
322          Any existing output will be flushed and closed.
323     */

324         public static void
325     setFile( String JavaDoc name ) throws Exception JavaDoc
326     {
327         final FileOutputStream JavaDoc outputStream = new FileOutputStream JavaDoc( name );
328
329         sOutput = createOutput( outputStream, true );
330     }
331
332     /**
333          Set the output to the default (standard error) and force
334          new output to this immediately.
335          <p>
336          Any existing output will be flushed and closed.
337     */

338         public static void
339     setDefaultOutput( )
340     {
341         createDefaultOutput();
342     }
343
344
345     /**
346         Return the current enabled status.
347     */

348         static public boolean
349     getEnabled( )
350     {
351         return( sEnabled );
352     }
353
354
355     /**
356         Set the enabled status. It can be reenabled later without affecting
357         the current output settings.
358
359         @param enabled whether Debug should be enabled or not.
360     */

361         static public void
362     setEnabled( boolean enabled )
363     {
364         sEnabled = enabled;
365     }
366
367         static private boolean
368     isValidSeverity(int severity)
369     {
370         return( severity == LOW || severity == MEDIUM || severity == HIGH );
371     }
372     
373     /**
374          Set the desired debug level cutoff value. When a call is made,
375          output is produced only if the severity level meets or exceeds
376          the value set here.
377
378          @param severity the new severity cutoff
379     */

380         public static void
381     setDebugLevel(int severity )
382     {
383         Assert.assertit( isValidSeverity( severity ), "illegal severity" );
384             
385         sDebugLevel = severity;
386     }
387
388
389     /**
390          Get the current debug level cutoff value.
391     */

392         public static int
393     getDebugLevel()
394     {
395         return sDebugLevel;
396     }
397
398
399     /**
400         Print the message if the severity level meets or exceeds the current
401         severity cutoff.
402
403         @param msg object on which toString() will be called.
404         @param severity severity level (LOW, MEDIUM, HIGH)
405     */

406         public static void
407     print( Object JavaDoc msg, int severity )
408     {
409         internalPrint( msg, severity, false );
410     }
411
412
413     /**
414         Print the message if the severity level meets or exceeds the current
415         severity cutoff as a new line.
416
417         @param msg object on which toString() will be called.
418         @param severity severity level (LOW, MEDIUM, HIGH)
419     */

420         public static void
421     println( Object JavaDoc msg, int severity )
422     {
423         internalPrint( msg, severity, true );
424     }
425
426     /**
427         Print the message with severity LOW.
428
429         @param msg object on which toString() will be called.
430     */

431         public static void
432     print( Object JavaDoc msg )
433     {
434         internalPrint( msg, kDefaultSeverity, false);
435     }
436
437
438     /**
439         Print the message with severity LOW as a new line.
440
441         @param msg object on which toString() will be called.
442     */

443         public static void
444     println( Object JavaDoc msg )
445     {
446         internalPrint( msg, kDefaultSeverity, true );
447     }
448
449     /**
450         Print this Throwable and its backtrace.
451
452
453         @param e Exception on which printStackTrace() will be called.
454     */

455         public static void
456     printStackTrace( Throwable JavaDoc e )
457     {
458         Assert.assertit( e != null, "null exception" );
459         
460         internalPrint( new ThrowableToString( e ), HIGH, false );
461     }
462
463     /**
464         Close any open files or data. Resets to state in which all print()
465         calls will be ignored.
466     */

467         public static void
468     cleanup()
469     {
470         sOutput.close();
471         sOutput = new NullOutput();
472     }
473
474 }
475
476
Popular Tags