1 23 package com.sun.enterprise.management.monitor; 24 25 import javax.management.ObjectName ; 26 27 import java.util.Set ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.Arrays ; 33 34 import java.lang.reflect.Method ; 35 import java.lang.reflect.InvocationTargetException ; 36 37 import javax.management.j2ee.statistics.Statistic ; 38 import javax.management.j2ee.statistics.Stats ; 39 import javax.management.j2ee.statistics.CountStatistic ; 40 import javax.management.j2ee.statistics.BoundaryStatistic ; 41 import javax.management.j2ee.statistics.RangeStatistic ; 42 import javax.management.j2ee.statistics.BoundedRangeStatistic ; 43 import javax.management.j2ee.statistics.TimeStatistic ; 44 45 import javax.management.openmbean.CompositeData ; 46 import javax.management.openmbean.CompositeDataSupport ; 47 48 import com.sun.appserv.management.base.AMX; 49 import com.sun.appserv.management.base.Util; 50 51 import com.sun.appserv.management.monitor.MonitoringStats; 52 import com.sun.appserv.management.monitor.MonitoringRoot; 53 import com.sun.appserv.management.monitor.ServerRootMonitor; 54 import com.sun.appserv.management.monitor.TransactionServiceMonitor; 55 56 import com.sun.appserv.management.util.misc.GSetUtil; 57 import com.sun.appserv.management.util.misc.ExceptionUtil; 58 import com.sun.appserv.management.util.misc.CollectionUtil; 59 import com.sun.appserv.management.util.misc.StringUtil; 60 import com.sun.appserv.management.util.jmx.JMXUtil; 61 import com.sun.appserv.management.util.j2ee.J2EEUtil; 62 63 import com.sun.appserv.management.config.ModuleMonitoringLevelsConfig; 64 65 import com.sun.appserv.management.j2ee.statistics.*; 66 67 68 public final class MonitorTest 69 extends AMXMonitorTestBase 70 { 71 public 72 MonitorTest() 73 { 74 } 75 76 77 78 public void 79 checkStatisticNames( final MonitoringStats mon ) 80 { 81 final Stats stats = mon.getStats(); 82 83 final Set <String > namesFromMon = GSetUtil.newStringSet( mon.getStatisticNames() ); 84 final Set <String > namesFromStats = GSetUtil.newStringSet( stats.getStatisticNames() ); 85 86 assert( namesFromStats.equals( namesFromMon ) ): 87 "statistic names from stats.getStatisticNames() != mon.getStatisticNames(): " + 88 namesFromStats + " != " + namesFromMon; 89 } 90 91 public void 92 checkNumStatistics( final MonitoringStats mon ) 93 { 94 final Stats stats = mon.getStats(); 95 assert( stats != null ) : "null Stats from: " + Util.getObjectName( mon ); 96 final String [] allNames = mon.getStatisticNames(); 97 98 final Statistic [] statistics = mon.getStatistics( allNames ); 99 assert( statistics.length == allNames.length ) : 100 "wrong number of statistics from: " + Util.getObjectName( mon ) + 101 ", got " + statistics.length + ", should be " + allNames.length; 102 } 103 104 public void 105 checkOpenStats( final MonitoringStats mon ) 106 { 107 final CompositeDataSupport openStats = mon.getOpenStats(); 108 assert( openStats != null ) : "null OpenStats from: " + Util.getObjectName( mon ); 109 110 final StatsImpl stats = new StatsImpl( openStats ); 111 112 final Set <String > fromOpenStats = GSetUtil.newStringSet( stats.getStatisticNames() ); 113 final Set <String > fromStats = GSetUtil.newStringSet( mon.getStats().getStatisticNames() ); 114 assert( fromOpenStats.equals( fromStats ) ) : 115 "openStats Statistic names don't match Stats Statistic names: " + 116 fromOpenStats + " != " + fromStats; 117 } 118 119 private final boolean 120 isLegalStatistic( final Statistic s) 121 { 122 return( (s instanceof CountStatistic ) || 124 (s instanceof BoundaryStatistic ) || 125 (s instanceof RangeStatistic ) || 126 (s instanceof BoundedRangeStatistic ) || 127 (s instanceof TimeStatistic ) || 128 (s instanceof StringStatistic ) ); 129 } 130 131 private final boolean 132 isLegalStatisticImpl( final Statistic s) 133 { 134 boolean isLegal = isLegalStatistic( s ); 135 if ( isLegal ) 136 { 137 final Class theClass = s.getClass(); 138 139 if( (theClass == CountStatisticImpl.class) || 140 (theClass == BoundaryStatisticImpl.class) || 141 (theClass == RangeStatisticImpl.class) || 142 (theClass == BoundedRangeStatisticImpl.class) || 143 (theClass == TimeStatisticImpl.class ) || 144 (theClass == StringStatisticImpl.class ) ); 145 } 146 return( isLegal ); 147 } 148 149 150 private void 151 checkLegalStatistic( 152 final ObjectName objectName, 153 final Statistic s ) 154 { 155 assert( isLegalStatistic( s ) ) : "Statistic " + s.getName() + 156 " in \"" + objectName + 157 "\" is not a known type of Statistic"; 158 159 assert( isLegalStatisticImpl( s ) ) : "Statistic " + s.getName() + 160 " in \"" + objectName + 161 "\" uses an implementation not intended by the API: " + 162 s.getClass(); 163 } 164 165 public void 166 checkGetStatistic( final MonitoringStats mon ) 167 { 168 final Stats stats = mon.getStats(); 169 170 final ObjectName objectName = Util.getObjectName( mon ); 171 172 final String [] names = mon.getStatisticNames(); 173 for( int i = 0; i < names.length; ++i ) 174 { 175 final String name = names[ i ]; 176 final Statistic s = mon.getStatistic( name ); 177 assert( s != null ); 178 assert( s.getName().equals( name ) ); 179 180 checkLegalStatistic( objectName, s ); 181 } 182 } 183 184 public void 185 checkGetStats( final MonitoringStats mon ) 186 { 187 final Stats stats = mon.getStats(); 188 189 final ObjectName objectName = Util.getObjectName( mon ); 190 191 final String [] names = stats.getStatisticNames(); 192 for( int i = 0; i < names.length; ++i ) 193 { 194 final Statistic s = stats.getStatistic( names[ i ] ); 195 assert( s != null ); 196 assert( s.getName().equals( names[ i ] ) ); 197 198 checkLegalStatistic( objectName, s ); 199 } 200 } 201 202 public void 203 checkGetOpenStatistic( final MonitoringStats mon ) 204 { 205 final Stats stats = mon.getStats(); 206 207 final String [] names = mon.getStatisticNames(); 208 for( int i = 0; i < names.length; ++i ) 209 { 210 final String name = names[ i ]; 211 212 final CompositeData d = mon.getOpenStatistic( name ); 213 final Statistic s = StatisticFactory.create( d ); 214 final Statistic s2 = mon.getStatistic( name ); 215 216 assert( s.getName().equals( name ) ); 217 } 219 220 final CompositeDataSupport [] all = mon.getOpenStatistics( names ); 221 assert( all != null ); 222 assert( all.length == names.length ); 223 } 224 225 public void 226 checkMonitoringStats( final ObjectName objectName ) 227 throws Exception 228 { 229 final MonitoringStats mon = getProxyFactory().getProxy( objectName, MonitoringStats.class); 230 231 mon.refresh(); 232 mon.getStatsInterfaceName(); 233 234 checkNumStatistics( mon ); 235 236 checkStatisticNames( mon ); 237 238 checkGetStatistic( mon ); 239 240 checkGetStats( mon ); 241 242 checkGetOpenStatistic( mon ); 243 244 checkOpenStats( mon ); 245 } 246 247 private Set <MonitoringStats> 248 getAllMonitoringStats() 249 throws ClassNotFoundException 250 { 251 final long start = now(); 252 253 final Set <MonitoringStats> all = 254 getQueryMgr().queryInterfaceSet( MonitoringStats.class.getName(), null ); 255 256 for( final MonitoringStats stats : all ) 257 { 258 } 259 260 printElapsed( "getAllMonitoringStats", all.size(), start ); 261 262 return( all ); 263 } 264 265 266 267 private String 268 getterToName( final String getterName ) 269 { 270 return StringUtil.stripPrefix( getterName, JMXUtil.GET ); 271 } 272 273 private String [] 274 methodsToNames( final Method [] methods ) 275 { 276 final String [] result = new String [ methods.length ]; 277 278 for( int i = 0; i < methods.length; ++i ) 279 { 280 result[ i ] = getterToName( methods[ i ].getName() ); 281 } 282 283 Arrays.sort( result ); 284 return( result ); 285 } 286 287 public void 288 checkStats( 289 final MonitoringStats mon, 290 final Stats stats ) 291 throws InvocationTargetException , IllegalAccessException , NoSuchMethodException 292 { 293 final ObjectName objectName = Util.getObjectName( mon ); 294 295 trace( "checkStats: " + objectName ); 296 297 final Method [] methodsViaNames = J2EEUtil.getStatisticGetterMethodsUsingNames( stats ); 298 299 final Method [] methods = stats.getClass().getMethods(); 300 301 final Set <String > statisticNames = GSetUtil.newSet( stats.getStatisticNames() ); 302 303 for( int methodIdx = 0; methodIdx < methodsViaNames.length; ++methodIdx ) 304 { 305 final Method method = methodsViaNames[ methodIdx ]; 306 final String methodName = method.getName(); 307 308 final Class <?> returnType = method.getReturnType(); 309 310 final String statisticName = getterToName( methodName ); 311 if ( ! statisticNames.contains( statisticName ) ) 312 { 313 warning( "Statistic " + quote( statisticName ) + " as derived from " + method + 314 " missing from " + quote( objectName ) + 315 " available names = " + toString( statisticNames ) ); 316 } 317 318 try 319 { 320 final Object o = method.invoke( stats, (Object [])null ); 321 assert( o != null ); 322 assert( Statistic .class.isAssignableFrom( o.getClass() ) ); 323 324 assert( returnType.isAssignableFrom( o.getClass() ) ) : 325 "Method " + methodName + " of MBean " + objectName + 326 " returned object not assignable to " + returnType.getName(); 327 328 final Statistic stat = (Statistic )method.invoke( stats, (Object [])null ); 329 assert( method.getReturnType().isAssignableFrom( stat.getClass() ) ); 330 331 final Statistic s = mon.getStatistic( stat.getName() ); 332 assert( returnType.isAssignableFrom( s.getClass() ) ) : 333 "getStatistic() of MBean " + objectName + 334 " returned Statistic not assignable to " + returnType.getName(); 335 336 } 338 catch( Exception e ) 339 { 340 final Throwable rootCause = ExceptionUtil.getRootCause( e ); 341 342 warning( 343 "Failure calling " + method + " on Stats for " + objectName + " = " + 344 rootCause.getClass().getName() + "\n" + 345 "Statistic names = " + toString( stats.getStatisticNames() ) ); 346 } 347 } 348 } 349 350 public void 351 checkAllStats( final ObjectName objectName ) 352 throws InvocationTargetException , IllegalAccessException , NoSuchMethodException 353 { 354 trace( "checkAllStats: " + objectName ); 355 356 final MonitoringStats mon = 357 getProxyFactory().getProxy( objectName, MonitoringStats.class); 358 359 final Method [] methods = mon.getClass().getMethods(); 360 361 final Method specificStatsMethod = getSpecificStatsGetterMethod( mon ); 362 363 364 final Stats plainStats = mon.getStats(); 367 assert( specificStatsMethod.getReturnType().isAssignableFrom( plainStats.getClass() ) ) : 368 "Stats returned from " + objectName + " getStats() should be assignable to " + 369 specificStatsMethod.getReturnType().getName(); 370 checkStats( mon, plainStats ); 371 372 Stats stats = null; 373 try 374 { 375 stats = (Stats )specificStatsMethod.invoke( mon, (Object [])null ); 377 } 378 catch( Exception e ) 379 { 380 final Throwable rootCause = ExceptionUtil.getRootCause( e ); 381 382 failure( 383 "Failure calling " + specificStatsMethod.getName() + "() on " + objectName + " = " + 384 rootCause.getClass().getName() ); 385 } 386 387 assert( plainStats.getClass() == stats.getClass() ); 388 checkStats( mon, stats ); 389 } 390 391 394 public void 395 testMonitoringStats() 396 throws Exception 397 { 398 final long start = now(); 399 400 final Set <MonitoringStats> all = getAllMonitoringStats(); 401 402 testAll( Util.toObjectNames( all ), "checkMonitoringStats" ); 403 404 printElapsed( "testMonitoringStats", all.size(), start ); 405 } 406 407 public void 408 xtestStats() 409 throws Exception 410 { 411 trace( "testStats: "); 412 final long start = now(); 413 414 final Set <MonitoringStats> all = getAllMonitoringStats(); 415 assert( all.size() >= 10 ) : "Monitoring is not turned on"; 416 417 419 testAll( Util.toObjectNames( all ), "checkAllStats" ); 420 421 printElapsed( "testStats", all.size(), start ); 422 } 423 424 425 429 public Method 430 getSpecificStatsGetterMethod( final MonitoringStats mon ) 431 throws InvocationTargetException , IllegalAccessException , NoSuchMethodException 432 { 433 final Method [] methods = mon.getClass().getMethods(); 434 435 Method result = null; 436 437 for( int methodIdx = 0; methodIdx < methods.length; ++methodIdx ) 438 { 439 final Method method = methods[ methodIdx ]; 440 final String methodName = method.getName(); 441 442 if ( JMXUtil.isGetter( method ) && ! methodName.equals( "getStats" ) && 443 Stats .class.isAssignableFrom( method.getReturnType() ) && 444 method.getParameterTypes().length == 0 ) 445 { 446 result = method; 447 break; 448 } 449 } 450 451 if ( result == null ) 452 { 453 throw new NoSuchMethodException ( "Can't find specific Stats getter in " + 454 quote( Util.getObjectName( mon ) ) ); 455 } 456 457 458 return( result ); 459 } 460 461 462 463 public void 464 checkStatsClassSuppliesAllStatistics( final ObjectName objectName ) 465 throws InvocationTargetException , IllegalAccessException , NoSuchMethodException 466 { 467 469 try 470 { 471 final MonitoringStats mon = getProxyFactory().getProxy( objectName, MonitoringStats.class); 472 473 final Method m = getSpecificStatsGetterMethod( mon ); 474 final Stats stats = (Stats )m.invoke( mon, (Object [])null ); 475 final Method [] methodsViaIntrospection = J2EEUtil.getStatisticGetterMethodsUsingIntrospection( stats ); 476 final Method [] methodsViaNames = J2EEUtil.getStatisticGetterMethodsUsingNames( stats ); 477 478 assert GSetUtil.newSet( methodsViaNames ).equals( GSetUtil.newSet( methodsViaIntrospection ) ) : 479 "Statistic names for " + quote( objectName ) + 480 " obtained via Statistic names do not match those obtained via introspection: \n" + 481 "via names:" + toString( methodsToNames(methodsViaNames) ) + 482 "\nvia introspection: " + toString( methodsToNames(methodsViaIntrospection) ); 483 484 final String [] namesFromMethods = methodsToNames( methodsViaNames ); 485 486 assert GSetUtil.newSet( namesFromMethods ).equals( GSetUtil.newSet( stats.getStatisticNames() ) ) : 487 "MBean " + quote( objectName ) + " Stats object of class " + stats.getClass().getName() + 488 " has Statistic methods that don't match getStatisticNames() =>\n" + 489 toString( namesFromMethods ) + " != " + 490 toString( stats.getStatisticNames() ); 491 } 492 catch( Exception e ) 493 { 494 trace( "Caught exception for " + StringUtil.quote(JMXUtil.toString(objectName)) + 495 " = " + e.getClass().getName() + ": " + StringUtil.quote(e.getMessage()) + "\n" + 496 ExceptionUtil.getStackTrace( ExceptionUtil.getRootCause(e) ) ); 497 } 498 } 499 500 504 public void 505 testStatsClassSuppliesAllStatistics() 506 throws Exception 507 { 508 final long start = now(); 510 511 final Set <MonitoringStats> all = getAllMonitoringStats(); 512 513 testAll( Util.toObjectNames( all ), "checkStatsClassSuppliesAllStatistics" ); 514 515 printElapsed( "testStatsClassSuppliesAllStatistics", all.size(), start ); 516 } 517 } 518 519 520 521 522 523 524 525 526 527 | Popular Tags |