1 23 package com.sun.appserv.management.base; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Set ; 28 import java.util.HashSet ; 29 30 import java.io.File ; 31 32 import com.sun.appserv.management.util.misc.Output; 33 import com.sun.appserv.management.util.misc.FileOutput; 34 import com.sun.appserv.management.util.misc.OutputIgnore; 35 import com.sun.appserv.management.util.misc.StringUtil; 36 import com.sun.appserv.management.util.misc.GSetUtil; 37 import com.sun.appserv.management.util.misc.DebugOutImpl; 38 39 40 122 public final class AMXDebug 123 { 124 private final Map <String ,WrapOutput> mOutputs; 125 126 private static final AMXDebug INSTANCE = new AMXDebug(); 127 128 private final File mDir; 129 private boolean mMadeDebugDir; 130 private boolean mDefaultDebug; 131 private final boolean mAppend; 132 133 134 public static final String AMX_DEBUG_ENABLED_SPROP = "AMX-DEBUG.enabled"; 135 136 139 public static final String AMX_DEBUG_APPEND_SPROP = "AMX-DEBUG.append"; 140 141 153 public static final String AMX_DEBUG_DIR_SPROP = "AMX-DEBUG.dir"; 154 155 160 public static final String AMX_DEBUG_SUBDIR = "AMX-DEBUG"; 161 162 165 public static final String AMX_DEBUG_SUFFIX = ".debug"; 166 167 private final WrapOutput mDebug; 169 170 private final String NEWLINE; 171 private final Set <Character > ILLEGAL_CHARS; 172 173 private final char[] ILLEGAL_CHARS_ARRAY = 174 { 175 '\u0000', 176 '?', '*', '|', '\'', '|', '\\', '/', ':', 177 }; 178 179 private 180 AMXDebug() 181 { 182 ILLEGAL_CHARS = new HashSet <Character >(); 183 for( final char c : ILLEGAL_CHARS_ARRAY ) 184 { 185 ILLEGAL_CHARS.add( c ); 186 } 187 188 NEWLINE = System.getProperty( "line.separator" ); 189 assert( NEWLINE != null ); 190 191 String value = System.getProperty( AMX_DEBUG_ENABLED_SPROP ); 192 if ( value == null ) 193 { 194 value = System.getProperty( "AMX-DEBUG" ); 196 if ( value != null && value.equals( "" ) ) 197 { 198 value = "true"; 199 } 200 } 201 mDefaultDebug = (value != null) && Boolean.parseBoolean( value ); 202 203 value = System.getProperty( AMX_DEBUG_APPEND_SPROP ); 204 mAppend = (value != null) && Boolean.parseBoolean( value ); 205 206 mOutputs = new HashMap <String ,WrapOutput>(); 207 208 mDir = getDir(); 209 mMadeDebugDir = false; 210 211 if ( mDefaultDebug ) 212 { 213 makeDebugDir(); 214 } 215 216 mDebug = _getOutput( this.getClass().getName() ); 217 mark( mDebug, getStdMarker( "AMXDebug started " ) ); 218 mDebug.println( "*** System Properties ***" ); 219 dumpSystemProps( mDebug ); 220 221 mark( mDebug, getStdMarker( "AMXDebug initialization done" ) ); 222 } 223 224 private void 225 dumpSystemProps( final Output output ) 226 { 227 final java.util.Properties props = System.getProperties(); 228 229 final String [] keys = (String [])props.keySet().toArray( new String [0] ); 230 java.util.Arrays.sort( keys ); 231 for( final String key : keys ) 232 { 233 debug( key + "=" + props.getProperty( key ) ); 234 } 235 236 } 237 238 private void 239 makeDebugDir() 240 { 241 if ( ! mMadeDebugDir ) 242 { 243 mDir.mkdirs(); 244 mMadeDebugDir = true; 245 } 246 } 247 248 private void 249 debug( final String s ) 250 { 251 253 if ( mDefaultDebug && mDebug != null ) 254 { 255 mDebug.println( "" + s ); 256 } 257 } 258 259 private static String 260 parens( final String s ) 261 { 262 return "(" + s + ")"; 263 } 264 265 private File 266 getDir() 267 { 268 final String value = System.getProperty( AMX_DEBUG_DIR_SPROP ); 269 270 File debugDir = null; 271 272 273 if ( value == null ) 274 { 275 final String instanceRoot = System.getProperty( "com.sun.aas.instanceRoot" ); 276 277 File parentDir = null; 278 if ( instanceRoot != null ) 279 { 280 parentDir = new File ( instanceRoot ); 281 } 282 else 283 { 284 parentDir = new File ( System.getProperty( "user.home" ) ); 285 } 286 debugDir = new File ( parentDir, AMX_DEBUG_SUBDIR ); 287 } 288 else 289 { 290 debugDir = new File ( value ); 291 } 292 293 return debugDir; 294 } 295 296 public String [] 297 getOutputIDs() 298 { 299 return GSetUtil.toStringArray( mOutputs.keySet() ); 300 } 301 302 306 public boolean 307 getDefaultDebug() 308 { 309 return mDefaultDebug; 310 } 311 312 317 public void 318 setDefaultDebug( final boolean debug ) 319 { 320 mDefaultDebug = debug; 321 if ( mDefaultDebug ) 322 { 323 makeDebugDir(); 324 } 325 mDebug.setDebug( debug ); 326 debug( "setDefaultDebug" + parens( "" + debug ) ); 327 } 328 329 333 public boolean 334 getDebug( final String id ) 335 { 336 return _getOutput( id ).getDebug(); 337 } 338 339 344 public void 345 setDebug( final String id, final boolean debug) 346 { 347 if ( debug ) 348 { 349 makeDebugDir(); 350 } 351 _getOutput( id ).setDebug( debug ); 352 debug( "setDebug" + parens( id + ", " + debug ) ); 353 } 354 355 359 public void 360 setAll( final boolean debug ) 361 { 362 debug( "setAll" + parens( "" + debug ) ); 363 364 setDefaultDebug( debug ); 365 for( final WrapOutput w : mOutputs.values() ) 366 { 367 w.setDebug( debug ); 368 } 369 } 370 371 374 public void 375 cleanup() 376 { 377 debug( "cleanup()" ); 378 379 setDefaultDebug( false ); 380 setAll( false ); 381 } 382 383 387 public void 388 reset( final String id ) 389 { 390 debug( "reset" + parens(id) ); 391 _getOutput(id).reset(); 392 } 393 394 395 private static final String DASHES = "----------"; 396 397 400 public String 401 getStdMarker() 402 { 403 return getStdMarker( "" ); 404 } 405 406 410 public String 411 getStdMarker( final String msg) 412 { 413 return( NEWLINE + NEWLINE + 414 DASHES + " " + new java.util.Date () + " " + msg + DASHES + NEWLINE ); 415 } 416 417 421 public void 422 mark( final Output output, final String marker ) 423 { 424 output.println( marker == null ? getStdMarker() : marker ); 425 } 426 427 430 public void 431 mark( final String id, final String marker ) 432 { 433 mark( getOutput(id), marker ); 434 } 435 436 439 public void 440 mark( final String id ) 441 { 442 mark( id, null ); 443 } 444 445 448 public void 449 markAll( final String marker ) 450 { 451 for( final WrapOutput w : mOutputs.values() ) 452 { 453 if ( w.getDebug() ) { 455 mark( w, marker ); 456 } 457 } 458 } 459 460 463 public void 464 markAll() 465 { 466 markAll( null ); 467 } 468 469 472 public static AMXDebug 473 getInstance() 474 { 475 return INSTANCE; 476 } 477 478 484 public File 485 getOutputFile( final String id ) 486 { 487 final String filename = makeSafeForFile( id ) + AMX_DEBUG_SUFFIX; 488 489 return new java.io.File ( mDir, filename ); 490 } 491 492 private WrapOutput 493 _getOutput( final String id ) 494 { 495 WrapOutput output = mOutputs.get( id ); 496 if ( output == null ) 497 { 498 synchronized( this ) 499 { 500 if ( mOutputs.get( id ) == null ) 501 { 502 debug( "Creating output for " + StringUtil.quote( id ) ); 503 try 504 { 505 output = new WrapOutput( getOutputFile( id ), mDefaultDebug ); 506 mOutputs.put( id, output ); 507 } 508 catch( Throwable t ) 509 { 510 debug( "Couldn't create output for " + StringUtil.quote( id ) ); 511 } 512 } 513 } 514 } 515 516 return output; 517 } 518 519 public Output 520 getShared() 521 { 522 return getOutput( "AMXDebug-Shared" ); 523 } 524 525 531 public Output 532 getOutput( final String id ) 533 { 534 return _getOutput( id ); 535 } 536 537 538 541 private String 542 makeSafeForFile( final String id ) 543 { 544 if ( id == null ) 545 { 546 throw new IllegalArgumentException ( id ); 547 } 548 549 final StringBuilder s = new StringBuilder (); 550 551 final char[] chars = id.toCharArray(); 552 for( final char c : chars ) 553 { 554 if ( ILLEGAL_CHARS.contains( c ) ) 555 { 556 s.append( "_" ); 557 } 558 else 559 { 560 s.append( c ); 561 } 562 } 563 564 return s.toString(); 565 } 566 567 572 public final class WrapOutput implements Output 573 { 574 private Output mWrapped; 575 private File mFile; 576 private Output mFileOutput; 577 private boolean mDebug; 578 579 private 580 WrapOutput( final File file, final boolean debug ) 581 { 582 mDebug = debug; 583 mWrapped = OutputIgnore.INSTANCE; 584 mFile = file; 585 mFileOutput = new FileOutput( file, mAppend); 586 checkStatus(); 587 } 588 public boolean 589 getDebug() 590 { 591 return mDebug; 592 } 593 594 601 public void 602 setDebug( final boolean debug ) 603 { 604 mDebug = debug; 605 606 checkStatus(); 607 } 608 609 public void 610 print( final Object o ) 611 { 612 mWrapped.print( o ); 613 } 614 615 public void 616 println( final Object o ) 617 { 618 mWrapped.println( o ); 619 } 620 621 public void 622 printError( final Object o ) 623 { 624 mWrapped.printError( o ); 625 } 626 627 public void 628 printDebug( final Object o ) 629 { 630 mWrapped.printDebug( o ); 631 } 632 633 public synchronized void 634 reset() 635 { 636 mWrapped = OutputIgnore.INSTANCE; 637 mFileOutput.close(); 638 639 mFileOutput = new FileOutput( mFile ); 641 642 checkStatus(); 643 } 644 645 public void 646 close() 647 { 648 reset(); 649 } 650 651 655 private synchronized void 656 checkStatus() 657 { 658 if ( getDebug() ) 659 { 660 mWrapped = mFileOutput; 661 } 662 else 663 { 664 mWrapped.println( "turning DEBUG OFF" ); 665 mWrapped = OutputIgnore.INSTANCE; 666 } 667 } 668 } 669 670 671 public static String 672 methodString( final String name, final Object ... args ) 673 { 674 return DebugOutImpl.methodString( name, (Object [])args ); 675 } 676 } 677 678 679 680 681 682 683 684 685 686 687 688 689 | Popular Tags |