1 23 24 28 29 34 35 package com.sun.enterprise.management.support; 36 37 import java.util.logging.Logger ; 38 import java.util.logging.Level ; 39 import java.util.Map ; 40 import java.util.HashMap ; 41 42 import java.io.Serializable ; 43 44 import javax.management.ObjectName ; 45 import javax.management.DynamicMBean ; 46 import javax.management.MBeanServer ; 47 import javax.management.MBeanRegistration ; 48 import javax.management.InstanceNotFoundException ; 49 import javax.management.IntrospectionException ; 50 import javax.management.ReflectionException ; 51 52 import javax.management.AttributeChangeNotification ; 53 import javax.management.Notification ; 54 import javax.management.NotificationFilter ; 55 import javax.management.NotificationListener ; 56 import javax.management.ListenerNotFoundException ; 57 58 import com.sun.appserv.management.util.misc.StringUtil; 59 import com.sun.appserv.management.base.AMXLoggerBase; 60 import com.sun.appserv.management.base.LoggerSupport; 61 import com.sun.appserv.management.base.AMXDebug; 62 import com.sun.appserv.management.base.AMXMBeanLogging; 63 64 import com.sun.appserv.management.util.stringifier.SmartStringifier; 65 import com.sun.appserv.management.util.jmx.NotificationBuilder; 66 import com.sun.appserv.management.util.jmx.NotificationSender; 67 import com.sun.appserv.management.util.jmx.NotificationEmitterSupport; 68 import com.sun.appserv.management.util.jmx.AttributeChangeNotificationBuilder; 69 import com.sun.appserv.management.util.jmx.stringifier.NotificationStringifier; 70 71 import com.sun.appserv.management.util.misc.Output; 72 import com.sun.appserv.management.util.misc.FileOutput; 73 import com.sun.appserv.management.util.misc.OutputIgnore; 74 75 import com.sun.logging.LogDomains; 76 77 81 public abstract class MBeanImplBase 82 implements MBeanRegistration , NotificationSender, AMXMBeanLogging 83 { 84 protected static final String [] EMPTY_STRING_ARRAY = new String [0]; 85 86 89 protected MBeanServer mServer; 90 91 96 protected ObjectName mSelfObjectName; 97 98 99 private NotificationEmitterSupport mNotificationEmitter; 100 101 protected final Logger mLogger; 102 103 private Map <String ,NotificationBuilder> mNotificationBuilders; 104 105 private final Output mDebug; 106 107 public 108 MBeanImplBase() 109 { 110 mLogger = Logger.getLogger( LogDomains.ADMIN_LOGGER ); 111 mNotificationEmitter = null; 112 113 118 mDebug = AMXDebug.getInstance().getOutput( getDebugID() ); 119 } 120 121 122 public final int 123 getListenerCount() 124 { 125 return( getNotificationEmitter().getListenerCount() ); 126 } 127 128 public final int 129 getNotificationTypeListenerCount( final String type ) 130 { 131 return( getNotificationEmitter().getNotificationTypeListenerCount( type ) ); 132 } 133 134 138 synchronized protected final NotificationEmitterSupport 139 getNotificationEmitter() 140 { 141 if ( mNotificationEmitter == null ) 142 { 143 mNotificationEmitter = new NotificationEmitterSupport( true ); 144 } 145 146 return( mNotificationEmitter ); 147 } 148 149 public synchronized void 150 addNotificationListener( 151 final NotificationListener listener ) 152 { 153 getNotificationEmitter().addNotificationListener( listener, null, null ); 154 } 155 156 public synchronized void 157 addNotificationListener( 158 final NotificationListener listener, 159 final NotificationFilter filter, 160 final Object handback) 161 { 162 getNotificationEmitter().addNotificationListener( listener, filter, handback ); 163 } 164 165 public synchronized void 166 removeNotificationListener( final NotificationListener listener) 167 throws ListenerNotFoundException 168 { 169 getNotificationEmitter().removeNotificationListener( listener ); 170 } 171 172 public synchronized void 173 removeNotificationListener( 174 final NotificationListener listener, 175 final NotificationFilter filter, 176 final Object handback) 177 throws ListenerNotFoundException 178 { 179 getNotificationEmitter().removeNotificationListener( listener, filter, handback ); 180 } 181 182 public void 183 sendNotification( final Notification notification) 184 { 185 getNotificationEmitter().sendNotification( notification ); 186 } 187 188 protected NotificationBuilder 189 createNotificationBuilder( final String notificationType ) 190 { 191 NotificationBuilder builder = null; 192 193 if ( notificationType.equals( AttributeChangeNotification.ATTRIBUTE_CHANGE ) ) 194 { 195 builder = new AttributeChangeNotificationBuilder( getObjectName() ); 196 } 197 else 198 { 199 builder = new NotificationBuilder( notificationType, getObjectName() ); 200 } 201 202 return( builder ); 203 } 204 205 209 protected synchronized NotificationBuilder 210 getNotificationBuilder( final String notificationType ) 211 { 212 if ( mNotificationBuilders == null ) 213 { 214 mNotificationBuilders = new HashMap <String ,NotificationBuilder>(); 215 } 216 217 NotificationBuilder builder = 218 (NotificationBuilder)mNotificationBuilders.get( notificationType ); 219 220 if ( builder == null ) 221 { 222 builder = createNotificationBuilder( notificationType ); 223 mNotificationBuilders.put( notificationType, builder ); 224 } 225 226 return( builder ); 227 } 228 229 232 protected void 233 sendNotification( final String notificationType ) 234 { 235 sendNotification( notificationType, notificationType, null, null ); 236 } 237 238 239 243 protected void 244 sendNotification( 245 final String notificationType, 246 final String key, 247 final Serializable value) 248 { 249 final String message = "no message specified"; 250 251 sendNotification( notificationType, message, key, value ); 252 } 253 254 258 protected void 259 sendNotification( 260 final String notificationType, 261 final String message, 262 final String key, 263 final Serializable value) 264 { 265 final NotificationBuilder builder = 266 getNotificationBuilder( notificationType ); 267 268 final Notification notif = builder.buildNew( message ); 269 NotificationBuilder.putMapData( notif, key, value ); 270 271 sendNotification( notif ); 272 } 273 274 275 public final ObjectName 276 getObjectName() 277 { 278 return( mSelfObjectName ); 279 } 280 281 public String 282 getJMXDomain() 283 { 284 return( getObjectName().getDomain() ); 285 } 286 287 public final MBeanServer 288 getMBeanServer() 289 { 290 return( mServer ); 291 } 292 293 protected static String 294 toString( Object o ) 295 { 296 if ( o == null ) 297 { 298 return( "" + o ); 299 } 300 301 return( SmartStringifier.toString( o ) ); 302 } 303 304 protected final void 305 trace( Object o ) 306 { 307 debug( o ); 308 } 309 310 protected final void 311 logSevere( Object o ) 312 { 313 final String msg = toString( o ); 314 debug( msg ); 315 316 if ( getMBeanLogLevelInt() <= Level.SEVERE.intValue() ) 317 { 318 getMBeanLogger().severe( msg ); 319 } 320 } 321 322 protected final void 323 logWarning( Object o ) 324 { 325 final String msg = toString( o ); 326 debug( msg ); 327 328 if ( getMBeanLogLevelInt() <= Level.WARNING.intValue() ) 329 { 330 getMBeanLogger().warning( msg ); 331 } 332 } 333 334 protected final void 335 logInfo( Object o ) 336 { 337 final String msg = toString( o ); 338 debug( msg ); 339 340 if ( getMBeanLogLevelInt() <= Level.INFO.intValue() ) 341 { 342 getMBeanLogger().info( msg ); 343 } 344 } 345 346 protected final void 347 logFine( Object o ) 348 { 349 final String msg = toString( o ); 350 debug( msg ); 351 352 if ( getMBeanLogLevelInt() <= Level.FINE.intValue() ) 353 { 354 getMBeanLogger().fine( msg ); 355 } 356 } 357 358 protected final void 359 logFiner( Object o ) 360 { 361 final String msg = toString( o ); 362 debug( msg ); 363 364 if ( getMBeanLogLevelInt() <= Level.FINER.intValue() ) 365 { 366 getMBeanLogger().finer( msg ); 367 } 368 } 369 370 protected final void 371 logFinest( Object o ) 372 { 373 final String msg = toString( o ); 374 debug( msg ); 375 376 if ( getMBeanLogLevelInt() <= Level.FINEST.intValue() ) 377 { 378 getMBeanLogger().finest( msg ); 379 } 380 } 381 382 protected final Logger 383 getMBeanLogger() 384 { 385 return mLogger; 386 } 387 388 389 public final Level 390 getMBeanLogLevel() 391 { 392 Logger logger = getMBeanLogger(); 393 assert( logger != null ); 394 395 Level level = logger.getLevel(); 396 while ( level == null ) 397 { 398 logger = logger.getParent(); 399 level = logger.getLevel(); 400 } 401 402 return( level ); 403 } 404 405 406 public final void 407 setMBeanLogLevel( final Level level ) 408 { 409 getMBeanLogger().setLevel( level ); 410 } 411 412 protected final int 413 getMBeanLogLevelInt() 414 { 415 return( getMBeanLogLevel().intValue() ); 416 } 417 418 public final String 419 getMBeanLoggerName() 420 { 421 return( getMBeanLogger().getName() ); 422 } 423 424 private static final Level [] LOG_LEVELS = 425 { 426 Level.ALL, 427 Level.SEVERE, 428 Level.WARNING, 429 Level.INFO, 430 Level.FINE, 431 Level.FINER, 432 Level.FINEST, 433 Level.OFF, 434 }; 435 436 public final void 437 setMBeanLogLevelString( final String levelString ) 438 { 439 Level level = null; 440 for( int i = 0; i < LOG_LEVELS.length; ++i ) 441 { 442 if ( levelString.equals( LOG_LEVELS[ i ].getName() ) ) 443 { 444 level = LOG_LEVELS[ i ]; 445 break; 446 } 447 } 448 449 if ( level == null ) 450 { 451 throw new IllegalArgumentException ( levelString ); 452 } 453 454 setMBeanLogLevel( level ); 455 } 456 457 protected static String 458 quote( final Object o ) 459 { 460 return( StringUtil.quote( "" + o ) ); 461 } 462 463 public ObjectName 464 preRegister( 465 final MBeanServer server, 466 final ObjectName nameIn) 467 throws Exception 468 { 469 assert( nameIn != null ); 470 mServer = server; 471 mSelfObjectName = nameIn; 472 473 return( mSelfObjectName ); 475 } 476 477 public void 478 postRegister( Boolean registrationDone ) 479 { 480 if ( registrationDone.booleanValue() ) 481 { 482 getMBeanLogger().finest( "postRegister: " + getObjectName()); 483 } 484 else 485 { 486 getMBeanLogger().finest( "postRegister: FAILURE: " + getObjectName()); 487 } 488 } 489 490 public void 491 preDeregister() 492 throws Exception 493 { 494 getMBeanLogger().finest( "preDeregister: " + getObjectName() ); 495 } 496 497 public void 498 postDeregister() 499 { 500 getMBeanLogger().finest( "postDeregister: " + getObjectName() ); 501 502 if ( mNotificationEmitter != null ) 503 { 504 mNotificationEmitter.cleanup(); 505 mNotificationEmitter = null; 506 } 507 508 if ( mNotificationBuilders != null ) 509 { 510 mNotificationBuilders.clear(); 511 mNotificationBuilders = null; 512 } 513 514 515 mServer = null; 516 mSelfObjectName = null; 517 518 } 519 520 protected String 521 getDebugID() 522 { 523 return this.getClass().getName(); 524 } 525 526 527 534 public final boolean 535 getAMXDebug() 536 { 537 return AMXDebug.getInstance().getDebug( getDebugID() ); 538 } 539 540 public boolean 541 enableAMXDebug( final boolean enabled ) 542 { 543 final boolean formerValue = getAMXDebug(); 544 if ( formerValue != enabled ) 545 { 546 setAMXDebug( enabled ); 547 } 548 return formerValue; 549 } 550 551 protected Output 552 getDebugOutput() 553 { 554 return AMXDebug.getInstance().getOutput( getDebugID() ); 555 } 556 557 public final void 558 setAMXDebug( final boolean debug ) 559 { 560 AMXDebug.getInstance().setDebug( getDebugID(), debug); 561 } 562 563 protected boolean 564 shouldOmitObjectNameForDebug() 565 { 566 return mSelfObjectName == null; 567 } 568 569 protected final void 570 debug(final Object o) 571 { 572 if ( getAMXDebug() ) 573 { 574 final String newline = System.getProperty( "line.separator" ); 575 576 if ( shouldOmitObjectNameForDebug() ) 577 { 578 mDebug.println( o ); 579 } 580 else 581 { 582 mDebug.println( mSelfObjectName.toString() ); 583 mDebug.println( "===> " + o ); 584 } 585 mDebug.println( newline ); 586 } 587 } 588 589 protected void 590 debugMethod( final String methodName, final Object ... args ) 591 { 592 if ( getAMXDebug() ) 593 { 594 debug( AMXDebug.methodString( methodName, args ) ); 595 } 596 } 597 598 protected void 599 debugMethod( 600 final String msg, 601 final String methodName, 602 final Object ... args ) 603 { 604 if ( getAMXDebug() ) 605 { 606 debug( AMXDebug.methodString( methodName, args ) + ": " + msg ); 607 } 608 } 609 610 protected void 611 debug( final Object ... args ) 612 { 613 if ( getAMXDebug() ) 614 { 615 debug( StringUtil.toString( "", args) ); 616 } 617 } 618 619 620 protected boolean 621 sleepMillis( final long millis ) 622 { 623 boolean interrupted = false; 624 625 try 626 { 627 Thread.sleep( millis ); 628 } 629 catch( InterruptedException e ) 630 { 631 Thread.interrupted(); 632 interrupted = true; 633 } 634 635 return interrupted; 636 } 637 } 638 639 640 641 642 643 644 645 646 | Popular Tags |