1 23 package com.sun.enterprise.management; 24 25 import java.io.IOException ; 26 27 import java.util.Set ; 28 import java.util.Map ; 29 import java.util.Collection ; 30 import java.util.HashSet ; 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.Field ; 33 import java.util.Iterator ; 34 import java.util.Arrays ; 35 36 37 import javax.management.MBeanServerConnection ; 38 import javax.management.ObjectName ; 39 import javax.management.Attribute ; 40 import javax.management.JMException ; 41 import javax.management.Notification ; 42 import javax.management.NotificationListener ; 43 44 import com.sun.appserv.management.base.AMX; 45 import com.sun.appserv.management.base.XTypes; 46 import com.sun.appserv.management.base.QueryMgr; 47 import com.sun.appserv.management.base.SystemInfo; 48 import com.sun.appserv.management.base.BulkAccess; 49 import com.sun.appserv.management.base.NotificationServiceMgr; 50 import com.sun.appserv.management.base.Util; 51 import com.sun.appserv.management.base.XTypesMapper; 52 import com.sun.appserv.management.j2ee.J2EETypesMapper; 53 54 55 import com.sun.appserv.management.DomainRoot; 56 57 import com.sun.appserv.management.config.AMXConfig; 58 import com.sun.appserv.management.config.DomainConfig; 59 import com.sun.appserv.management.config.ConfigConfig; 60 import com.sun.appserv.management.config.ModuleMonitoringLevelsConfig; 61 import com.sun.appserv.management.config.ModuleMonitoringLevelValues; 62 import com.sun.appserv.management.config.NodeAgentConfig; 63 64 import com.sun.appserv.management.client.ProxyFactory; 65 import com.sun.appserv.management.client.ConnectionSource; 66 import com.sun.appserv.management.client.AppserverConnectionSource; 67 68 import com.sun.appserv.management.j2ee.J2EEDomain; 69 70 import com.sun.appserv.management.util.jmx.MBeanServerConnectionConnectionSource; 71 import com.sun.appserv.management.util.jmx.JMXUtil; 72 73 import com.sun.appserv.management.util.stringifier.SmartStringifier; 74 import com.sun.appserv.management.util.jmx.stringifier.AttributeStringifier; 75 76 import com.sun.appserv.management.util.misc.StringUtil; 77 import com.sun.appserv.management.util.misc.ClassUtil; 78 import com.sun.appserv.management.util.misc.GSetUtil; 79 import com.sun.appserv.management.util.misc.CollectionUtil; 80 import com.sun.appserv.management.util.misc.ExceptionUtil; 81 import com.sun.appserv.management.util.misc.TypeCast; 82 83 import com.sun.enterprise.management.PropertyKeys; 84 import com.sun.enterprise.management.util.jmx.JMXTestBase; 85 import com.sun.enterprise.management.support.AMXDebugStuff; 86 import com.sun.enterprise.management.support.AMXDebugSupportMBean; 87 88 91 public class AMXTestBase extends JMXTestBase 92 { 93 private ConnectionSource mConnectionSource; 94 private final DomainRoot mDomainRoot; 95 private final ProxyFactory mProxyFactory; 96 private final Set <ObjectName > mNotTested; 97 private final TestUtil mTestUtil; 98 99 100 protected static final String PE_CONFIG_NAME = "server-config"; 101 102 private final static boolean WEB_MODULE_MONITOR_BROKEN = true; 103 104 private static boolean MONITORING_ENABLED = false; 105 106 protected static final String NEWLINE = System.getProperty( "line.separator" ); 107 108 109 public 110 AMXTestBase( ) 111 { 112 checkConnection( getConnection() ); 113 114 mNotTested = new HashSet <ObjectName >(); 115 try 116 { 117 mConnectionSource = 118 new MBeanServerConnectionConnectionSource( getConnection() ); 119 120 mProxyFactory = ProxyFactory.getInstance( mConnectionSource, true ); 121 mDomainRoot = mProxyFactory.getDomainRoot(); 122 } 123 catch( Exception e ) 124 { 125 throw new RuntimeException ( e.toString(), e ); 126 } 127 128 mTestUtil = new TestUtil( getDomainRoot() ); 129 } 130 131 protected Set <AMX> 132 getAllAMX() 133 { 134 return getTestUtil().getAllAMX(); 135 } 136 137 protected ModuleMonitoringLevelsConfig 138 getModuleMonitoringLevelsConfig( final String configName ) 139 { 140 final ConfigConfig cc = configName == null ? getConfigConfig() : getConfigConfig( configName ); 141 assert( cc != null ); 142 143 final ModuleMonitoringLevelsConfig mon = 144 cc.getMonitoringServiceConfig().getModuleMonitoringLevelsConfig(); 145 146 return( mon ); 147 } 148 149 protected ModuleMonitoringLevelsConfig 150 getModuleMonitoringLevelsConfig( ) 151 { 152 return getModuleMonitoringLevelsConfig( null ); 153 } 154 155 158 protected synchronized void 159 turnOnMonitoring() 160 { 161 if ( ! MONITORING_ENABLED ) 162 { 163 synchronized( AMXTestBase.class ) 164 { 165 final String [] configNames = getConfigNames(); 166 for( int i = 0; i < configNames.length; ++i ) 167 { 168 getModuleMonitoringLevelsConfig( configNames[ i ] ).changeAll( ModuleMonitoringLevelValues.HIGH ); 169 } 170 171 MONITORING_ENABLED = true; 172 } 173 } 174 } 175 176 protected synchronized void 177 turnOffMonitoring() 178 { 179 synchronized( AMXTestBase.class ) 180 { 181 getModuleMonitoringLevelsConfig().changeAll( ModuleMonitoringLevelValues.OFF ); 182 MONITORING_ENABLED = false; 183 } 184 } 185 186 private static final Set <String > EXPECTED_REMOTE_INCOMPLETE_TYPES = 187 GSetUtil.newUnmodifiableStringSet( XTypes.CALL_FLOW_MONITOR, XTypes.LOGGING ); 188 189 protected boolean 190 isRemoteIncomplete( final ObjectName objectName ) 191 { 192 final AMX amx = getProxyFactory().getProxy( objectName, AMX.class ); 193 final String j2eeType = amx.getJ2EEType(); 194 195 final boolean isRemote = ! amx.isDAS(); 196 197 if ( isRemote && 198 ! EXPECTED_REMOTE_INCOMPLETE_TYPES.contains( j2eeType ) ) 199 { 200 warning( "isRemoteIncomplete: not expecting j2eeType=" + j2eeType + 201 ", has the implementation changed?" ); 202 } 203 204 return isRemote; 205 } 206 207 208 protected boolean 209 shouldTest( final ObjectName objectName ) 210 { 211 boolean shouldTest = ! isRemoteIncomplete( objectName ); 212 213 return( shouldTest ); 214 } 215 216 protected void 217 waitUnregistered( final ObjectName objectName ) 218 throws IOException 219 { 220 final MBeanServerConnection conn = getConnection(); 221 if ( conn.isRegistered( objectName ) ) 222 { 223 mySleep( 100 ); 224 } 225 226 while ( conn.isRegistered( objectName ) ) 227 { 228 trace( "waitUnregistered: " + objectName); 229 mySleep( 100 ); 230 } 231 } 232 233 234 239 protected void 240 waitProxyGone( 241 final ProxyFactory factory, 242 final ObjectName objectName ) 243 { 244 long millis = 1; 245 246 while ( factory.getProxy( objectName, AMX.class, false ) != null ) 247 { 248 mySleep( millis ); 249 millis *= 2; 250 trace( "waitProxyGone: waiting for proxy to disappear: " + objectName ); 251 } 252 } 253 254 protected final void 255 notTested( final ObjectName objectName ) 256 { 257 if ( isRemoteIncomplete( objectName ) ) 258 { 259 trace( "remoteIncomplete (this is OK): " + objectName ); 260 } 261 else 262 { 263 mNotTested.add( objectName ); 264 } 265 } 266 267 protected final Set <ObjectName > 268 getNotTestedSet( ) 269 { 270 return( mNotTested ); 271 } 272 273 public static void 274 mySleep( final long millis ) 275 { 276 try 277 { 278 Thread.sleep( millis ); 279 } 280 catch( InterruptedException e ) 281 { 282 } 283 } 284 285 protected final void 286 warnNotTested( ) 287 { 288 final Set <ObjectName > notTested = getNotTestedSet(); 289 290 if ( notTested.size() != 0 ) 291 { 292 final Set j2eeTypes = 293 JMXUtil.getKeyPropertySet( AMX.J2EE_TYPE_KEY, notTested ); 294 295 trace( "WARNING: DID NOT TEST: " + notTested.size() + " MBeans of types {" + 296 toString( j2eeTypes ) + "}" ); 297 } 298 } 299 300 301 302 protected void 303 checkConnection( final MBeanServerConnection conn ) 304 { 305 assert( getConnection() != null ); 306 307 try 308 { 309 conn.isRegistered( JMXUtil.getMBeanServerDelegateObjectName() ); 310 } 311 catch( Exception e ) 312 { 313 fail( "Connection failed:\n" + 314 ExceptionUtil.getStackTrace( getRootCause( e ) ) ); 315 } 316 } 317 318 protected void 319 checkConnection( ) 320 { 321 checkConnection( getConnection( ) ); 322 } 323 324 protected TestUtil 325 getTestUtil() 326 { 327 return mTestUtil; 328 } 329 330 331 protected final AMX 332 getProxy( final ObjectName objectName ) 333 { 334 final ProxyFactory factory = ProxyFactory.getInstance( getConnectionSource(), true); 335 336 final AMX proxy = factory.getProxy( objectName, AMX.class ); 337 338 return( proxy ); 339 } 340 341 344 protected final <T> T 345 getProxy( 346 final ObjectName objectName, 347 final Class <T> theClass) 348 { 349 return( theClass.cast( getProxy(objectName) ) ); 350 } 351 352 353 protected final DomainRoot 354 getDomainRoot() 355 { 356 assert( mDomainRoot != null ) : "mDomainRoot is null"; 357 return( mDomainRoot ); 358 } 359 360 protected final DomainConfig 361 getDomainConfig() 362 { 363 return( getDomainRoot().getDomainConfig() ); 364 } 365 366 protected final J2EEDomain 367 getJ2EEDomain() 368 { 369 return( getDomainRoot().getJ2EEDomain() ); 370 } 371 372 protected String [] 373 getConfigNames() 374 { 375 final Map <String ,ConfigConfig> configMap = 376 getDomainConfig().getConfigConfigMap(); 377 378 return( GSetUtil.toStringArray( configMap.keySet() ) ); 379 } 380 381 protected ConfigConfig 382 getConfigConfig( final String name ) 383 { 384 final Map <String ,ConfigConfig> configs = getDomainConfig().getConfigConfigMap(); 385 386 return configs.get( name == null ? PE_CONFIG_NAME : name ); 387 } 388 389 protected static ConfigConfig 390 getConfigConfig( final AMXConfig any ) 391 { 392 final ObjectName objectName = Util.getObjectName( any ); 393 final String configName = objectName.getKeyProperty( XTypes.CONFIG_CONFIG ); 394 395 return any.getDomainRoot().getDomainConfig().getConfigConfigMap().get( configName ); 396 } 397 398 protected ConfigConfig 399 getConfigConfig( ) 400 { 401 return( getConfigConfig( PE_CONFIG_NAME ) ); 402 } 403 404 405 protected QueryMgr 406 getQueryMgr() 407 { 408 assert( mDomainRoot != null ); 409 final QueryMgr proxy = getDomainRoot().getQueryMgr(); 410 assert( proxy != null ); 411 return( proxy ); 412 } 413 414 415 protected NotificationServiceMgr 416 getNotificationServiceMgr() 417 { 418 return( getDomainRoot().getNotificationServiceMgr() ); 419 } 420 421 protected BulkAccess 422 getBulkAccess() 423 { 424 return( getDomainRoot().getBulkAccess() ); 425 } 426 427 protected ConnectionSource 428 getConnectionSource() 429 { 430 assert( mConnectionSource != null ); 431 return( mConnectionSource ); 432 } 433 434 protected MBeanServerConnection 435 getConnection() 436 { 437 return( getGlobalConnection() ); 438 } 439 440 protected ProxyFactory 441 getProxyFactory() 442 { 443 return( mProxyFactory ); 444 } 445 446 447 protected Class 448 getInterfaceClass( AMX proxy ) 449 throws ClassNotFoundException 450 { 451 final String name = Util.getExtra( proxy ).getInterfaceName(); 452 return( ClassUtil.getClassFromName( name ) ); 453 } 454 455 protected <T extends AMX> boolean 456 testOnProxies( 457 final Collection <T> proxies, 458 final Method method ) 459 throws Exception 460 { 461 final long start = now(); 462 463 boolean failed = false; 464 465 int testCount = 0; 466 467 final Object [] args = new Object [ 1 ]; 468 for( final T proxy : proxies ) 469 { 470 final ObjectName objectName = Util.getExtra( proxy ).getObjectName(); 471 472 if ( ! shouldTest( objectName ) ) 473 { 474 notTested( objectName ); 475 continue; 476 } 477 478 ++testCount; 479 try 480 { 481 args[ 0 ] = proxy; 482 method.invoke( this, args ); 483 } 484 catch( Exception e ) 485 { 486 trace( method.getName() + " failed for proxy: " + 487 quote( JMXUtil.toString( objectName ) ) ); 488 failed = true; 489 trace( ExceptionUtil.toString( e ) ); 490 } 491 } 492 493 494 final long elapsed = now() - start; 495 printVerbose( "Ran test method " + method.getName() + " on " + testCount + 496 " candidates in " + elapsed + "ms"); 497 warnNotTested(); 498 499 warnNotTested(); 500 501 return( ! failed ); 502 } 503 504 505 protected boolean 506 testOnObjectNames( 507 final Collection <ObjectName > objectNames, 508 final Method method ) 509 throws Exception 510 { 511 boolean failed = false; 512 513 final Object [] args = new Object [ 1 ]; 514 515 int testCount = 0; 516 final long start = now(); 517 518 for( final ObjectName objectName : objectNames ) 519 { 520 if ( ! shouldTest( objectName ) ) 521 { 522 notTested( objectName ); 523 continue; 524 } 525 526 ++testCount; 527 try 528 { 529 args[ 0 ] = objectName; 530 method.invoke( this, args ); 531 } 532 catch( Exception e ) 533 { 534 final Throwable rootCause = getRootCause( e ); 535 trace( method.getName() + " failed for: " + 536 quote( JMXUtil.toString( objectName ) ) + " with Exception of type " + 537 rootCause.getClass().getName() + ", msg = " + rootCause.getMessage() ); 538 failed = true; 539 } 540 } 541 542 final long elapsed = now() - start; 543 printVerbose( "Ran test method " + method.getName() + " on " + testCount + 544 " candidates in " + elapsed + "ms"); 545 warnNotTested(); 546 547 return( ! failed ); 548 } 549 550 551 552 protected final static Class [] OBJECTNAME_SIG = new Class [] { ObjectName .class }; 553 protected final static Class [] PROXY_SIG = new Class [] { AMX.class }; 554 555 protected void 556 testAll( final Collection <ObjectName > objectNames, final String methodName ) 557 throws Exception 558 { 559 final boolean success = testOnObjectNames( objectNames, 560 this.getClass().getMethod( methodName, OBJECTNAME_SIG ) ); 561 562 assert( success ); 563 } 564 565 protected <T extends AMX> void 566 testAllProxies( final Collection <T> proxies, final String methodName ) 567 throws Exception 568 { 569 final boolean success = testOnProxies( proxies, 570 this.getClass().getMethod( methodName, PROXY_SIG ) ); 571 572 assert( success ); 573 } 574 575 protected void 576 testAll( String methodName ) 577 throws Exception 578 { 579 final Set <ObjectName > names = getTestUtil().getAllObjectNames(); 580 581 testAll( names, methodName ); 582 } 583 584 public void 585 setUp() throws Exception 586 { 587 super.setUp(); 588 589 turnOnMonitoring(); 590 } 591 592 public void 593 testAssertsOn() 594 { 595 checkAssertsOn(); 596 } 597 598 private static final String DEFAULT_INSTANCE_NAME = "test"; 599 600 protected static String 601 getDefaultInstanceName( final String qualifier ) 602 { 603 String name = null; 604 605 if ( qualifier == null ) 606 { 607 name = DEFAULT_INSTANCE_NAME; 608 } 609 else 610 { 611 name = qualifier + "." + DEFAULT_INSTANCE_NAME; 612 } 613 return name; 614 } 615 616 protected Throwable 617 getRootCause( Throwable t ) 618 { 619 return ExceptionUtil.getRootCause( t ); 620 } 621 622 protected String 623 getStackTrace( Throwable t ) 624 { 625 return ExceptionUtil.getStackTrace( t ); 626 } 627 628 protected String 629 getRootCauseStackTrace( Throwable t ) 630 { 631 return getStackTrace( getRootCause( t ) ); 632 } 633 634 protected Map <String ,AppserverConnectionSource> 635 getNodeAgents() 636 { 637 final Map <?,?> m = Map .class.cast( getEnvValue( PropertyKeys.NODE_AGENTS_KEY ) ); 638 return TypeCast.checkedMap( m, 639 String .class, AppserverConnectionSource.class); 640 } 641 642 643 protected String 644 getDASNodeAgentName() 645 { 646 return getEnvString( PropertyKeys.DAS_NODE_AGENT_NAME, null); 647 } 648 649 protected NodeAgentConfig 650 getDASNodeAgentConfig() 651 { 652 final String name = getDASNodeAgentName(); 653 654 NodeAgentConfig config = null; 655 if ( name != null ) 656 { 657 config = getDomainConfig().getNodeAgentConfigMap().get( name ); 658 } 659 660 return config; 661 } 662 663 protected boolean 664 getTestOffline() 665 { 666 return getEnvBoolean( PropertyKeys.TEST_OFFLINE_KEY, false ); 667 } 668 669 675 protected boolean 676 checkNotOffline( final String testName ) 677 { 678 boolean offline = getTestOffline(); 679 680 if ( offline ) 681 { 682 } 684 685 return ! offline; 686 } 687 688 689 public static Capabilities 690 getDefaultCapabilities() 691 { 692 return getOfflineCapableCapabilities( true ); 693 } 694 695 protected static Capabilities 696 getOfflineCapableCapabilities( boolean offlineCapable ) 697 { 698 final Capabilities c = new Capabilities( ); 699 c.setOfflineCapable( offlineCapable ); 700 701 return c; 702 } 703 704 AMXDebugSupportMBean 705 getAMXDebugSupportMBean() 706 { 707 final ObjectName objectName = Util.newObjectName( AMXDebugSupportMBean.OBJECT_NAME ); 708 709 try 710 { 711 return (AMXDebugSupportMBean)newProxy( objectName, AMXDebugSupportMBean.class ); 712 } 713 catch( Exception e ) 714 { 715 assert false : "Can't get proxy to " + objectName; 716 } 717 return null; 718 } 719 720 protected boolean 721 supportsMultipleServers() 722 { 723 return supportsMultipleServers( getDomainRoot() ); 724 } 725 726 public static boolean 727 supportsMultipleServers( final DomainRoot domainRoot ) 728 { 729 return domainRoot.getSystemInfo().supportsFeature( SystemInfo.MULTIPLE_SERVERS_FEATURE ); 730 } 731 732 733 protected boolean 734 supportsClusters() 735 { 736 return getDomainRoot().getSystemInfo().supportsFeature( SystemInfo.CLUSTERS_FEATURE ); 737 } 738 } 739 740 741 742 743 744 745 746 747 748 749 | Popular Tags |