1 23 package com.sun.enterprise.admin.util; 24 25 27 import java.io.PrintStream ; 29 import java.io.FileOutputStream ; 30 import java.io.OutputStream ; 31 32 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 41 interface IOutput 42 { 43 public void print( String msg ); 44 public void println( String 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 msg ) 59 { 60 } 62 63 public void 64 println( String msg ) 65 { 66 } 68 69 public void 70 close() 71 { 72 } 74 75 public void 76 flush() 77 { 78 } 80 } 81 82 85 class Output implements IOutput 86 { 87 OutputStream mOutputStream = null; 88 PrintStream mPrint = null; 89 boolean mCloseWhenDone = false; 90 91 public 92 Output( 93 OutputStream outputStream, 94 boolean closeWhenDone ) 95 throws Exception 96 { 97 Assert.assertit( outputStream != null, "null outputStream" ); 98 99 mPrint = new PrintStream ( outputStream ); 100 101 mOutputStream = outputStream; 103 mCloseWhenDone = closeWhenDone; 104 } 105 106 111 public void 112 close() 113 { 114 if ( mCloseWhenDone ) 115 { 116 try 117 { 118 mOutputStream.close(); 119 } 120 catch( Exception 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 msg ) 132 { 133 mPrint.print( msg ); 134 } 135 136 public void 137 println( String msg ) 138 { 139 mPrint.println( msg ); 140 } 141 142 public void 143 flush() 144 { 145 try 146 { 147 mOutputStream.flush(); 148 } 149 catch( Exception e ) 150 { 151 ExceptionUtil.ignoreException( e ); 152 } 153 } 154 } 155 156 157 158 159 171 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 outputStream, 204 boolean closeWhenDone ) throws Exception 205 { 206 final Output newOutput = new Output( outputStream, closeWhenDone); 208 209 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 e ) 227 { 228 ExceptionUtil.ignoreException( e ); 229 output = new NullOutput(); 230 } 231 return( output ); 232 } 233 234 235 236 237 private static String 238 getSeverityString( int severity ) 239 { 240 Assert.assertit( isValidSeverity( severity ), "illegal severity" ); 241 242 String 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 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 267 { 268 sOutput.flush(); 269 } 270 271 272 273 private static void 274 internalPrintWithException( 275 Object msg, 276 int severity, 277 boolean newline ) throws Exception 278 { 279 Assert.assertit( msg != null, "null msg" ); 280 281 if( testSeverity( severity ) ) 282 { 283 final String maybeNewline = newline ? "\n" : ""; 284 285 final String 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 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 e ) 310 { 311 ExceptionUtil.ignoreException( e ); 312 } 313 } 314 } 315 316 317 318 324 public static void 325 setFile( String name ) throws Exception 326 { 327 final FileOutputStream outputStream = new FileOutputStream ( name ); 328 329 sOutput = createOutput( outputStream, true ); 330 } 331 332 338 public static void 339 setDefaultOutput( ) 340 { 341 createDefaultOutput(); 342 } 343 344 345 348 static public boolean 349 getEnabled( ) 350 { 351 return( sEnabled ); 352 } 353 354 355 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 380 public static void 381 setDebugLevel(int severity ) 382 { 383 Assert.assertit( isValidSeverity( severity ), "illegal severity" ); 384 385 sDebugLevel = severity; 386 } 387 388 389 392 public static int 393 getDebugLevel() 394 { 395 return sDebugLevel; 396 } 397 398 399 406 public static void 407 print( Object msg, int severity ) 408 { 409 internalPrint( msg, severity, false ); 410 } 411 412 413 420 public static void 421 println( Object msg, int severity ) 422 { 423 internalPrint( msg, severity, true ); 424 } 425 426 431 public static void 432 print( Object msg ) 433 { 434 internalPrint( msg, kDefaultSeverity, false); 435 } 436 437 438 443 public static void 444 println( Object msg ) 445 { 446 internalPrint( msg, kDefaultSeverity, true ); 447 } 448 449 455 public static void 456 printStackTrace( Throwable e ) 457 { 458 Assert.assertit( e != null, "null exception" ); 459 460 internalPrint( new ThrowableToString( e ), HIGH, false ); 461 } 462 463 467 public static void 468 cleanup() 469 { 470 sOutput.close(); 471 sOutput = new NullOutput(); 472 } 473 474 } 475 476 | Popular Tags |