1 23 package com.sun.enterprise.management.monitor; 24 25 import javax.management.ObjectName ; 26 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.Set ; 30 import java.util.HashSet ; 31 import java.util.Map ; 32 import java.util.HashMap ; 33 import java.util.Arrays ; 34 35 import java.io.IOException ; 36 37 import javax.management.MBeanInfo ; 38 import javax.management.MBeanOperationInfo ; 39 import javax.management.j2ee.statistics.Statistic ; 40 import javax.management.j2ee.statistics.Stats ; 41 import javax.management.j2ee.statistics.CountStatistic ; 42 import javax.management.j2ee.statistics.BoundaryStatistic ; 43 import javax.management.j2ee.statistics.RangeStatistic ; 44 import javax.management.j2ee.statistics.BoundedRangeStatistic ; 45 import javax.management.j2ee.statistics.TimeStatistic ; 46 47 import com.sun.appserv.management.util.j2ee.J2EEUtil; 48 import com.sun.appserv.management.util.misc.CollectionUtil; 49 import com.sun.appserv.management.util.misc.ExceptionUtil; 50 import com.sun.appserv.management.util.misc.GSetUtil; 51 import com.sun.appserv.management.util.misc.StringUtil; 52 import com.sun.appserv.management.util.stringifier.ArrayStringifier; 53 import com.sun.appserv.management.util.jmx.JMXUtil; 54 55 import com.sun.appserv.management.j2ee.statistics.*; 56 import com.sun.appserv.management.base.Util; 57 58 import com.sun.enterprise.management.support.ComSunAppservTest; 59 import com.sun.enterprise.management.support.OldMonitorTypes; 60 61 65 public final class ComSunAppservMonitorTest extends ComSunAppservTest 66 { 67 public 68 ComSunAppservMonitorTest() 69 { 70 } 71 72 73 private interface MonitorIntf 74 { 75 public String [] getStatisticNames() throws Exception ; 76 public Statistic [] getStatistics() throws Exception ; 77 }; 78 79 private final class MonitorImpl implements MonitorIntf 80 { 81 final ObjectName mObjectName; 82 83 public 84 MonitorImpl( 85 final ObjectName objectName ) 86 { 87 mObjectName = objectName; 88 } 89 90 public String [] 91 getStatisticNames() 92 throws Exception 93 { 94 final String [] statisticNames = 95 (String [])getMBeanServerConnection().invoke( mObjectName, "getStatisticNames", null, null ); 96 97 return statisticNames; 98 } 99 100 public Statistic [] 101 getStatistics() 102 throws Exception 103 { 104 final Statistic [] statistics = 105 (Statistic [])getMBeanServerConnection().invoke( mObjectName, "getStatistics", null, null ); 106 107 return statistics; 108 } 109 }; 110 111 private MonitorIntf 112 getMonitorIntf( final ObjectName objectName ) 113 { 114 final MonitorIntf intf = new MonitorImpl( objectName ); 115 boolean basicsOK = true; 116 117 if ( ! hasStatisticSupport( objectName ) ) 118 { 119 warning( "MBean " + StringUtil.quote( objectName ) + 120 " doesn't have getStatisticNames() and getStatistics() methods." ); 121 return null; 122 } 123 124 try 125 { 126 final String [] statisticNames = intf.getStatisticNames(); 127 if ( statisticNames == null ) 128 { 129 warning( "MBean " + StringUtil.quote( objectName ) + 130 " returned null from getStatisticNames()" ); 131 basicsOK = false; 132 } 133 if ( statisticNames.length == 0 ) 134 { 135 warning( "MBean " + StringUtil.quote( objectName ) + 136 " returned an empty String[] from getStatisticNames()" ); 137 basicsOK = false; 138 } 139 } 140 catch( Exception e ) 141 { 142 final Throwable rootCause = ExceptionUtil.getRootCause( e ); 143 144 warning( "MBean " + StringUtil.quote( objectName ) + 145 " threw an exception from getStatisticNames(): " + rootCause ); 146 basicsOK = false; 147 } 148 149 if ( basicsOK ) 150 { 151 try 152 { 153 final Statistic [] statistics = intf.getStatistics(); 154 if ( statistics == null ) 155 { 156 warning( "MBean " + StringUtil.quote( objectName ) + 157 " returned null from getStatistics()" ); 158 basicsOK = false; 159 } 160 if ( statistics != null && statistics.length == 0 ) 161 { 162 warning( "MBean " + StringUtil.quote( objectName ) + 163 " returned an empty Statistic[] from getStatistics()" ); 164 basicsOK = false; 165 } 166 } 167 catch( Exception e ) 168 { 169 final Throwable rootCause = ExceptionUtil.getRootCause( e ); 170 171 warning( "MBean " + StringUtil.quote( objectName ) + 172 " threw an exception from getStatistics(): " + rootCause ); 173 basicsOK = false; 174 } 175 } 176 177 return basicsOK ? intf : null; 178 } 179 180 181 private boolean 182 checkMonitor(final ObjectName objectName ) 183 throws Exception 184 { 185 boolean worksOK = true; 186 187 final MonitorIntf intf = getMonitorIntf( objectName ); 188 if ( intf != null ) 189 { 190 final String [] statisticNames = intf.getStatisticNames(); 191 final Set <String > statisticNamesSet = GSetUtil.newStringSet( statisticNames ); 192 193 final Statistic [] statistics = intf.getStatistics(); 195 final Set <String > statisticNamesFromStatisticsSet = new HashSet <String >(); 196 for( final Statistic s : statistics ) 197 { 198 statisticNamesFromStatisticsSet.add( s.getName() ); 199 } 200 201 if ( ! statisticNamesSet.equals( statisticNamesFromStatisticsSet ) ) 202 { 203 final String [] statisticNamesFromStatistics = 204 GSetUtil.toStringArray( statisticNamesFromStatisticsSet ); 205 206 Arrays.sort( statisticNames ); 207 Arrays.sort( statisticNamesFromStatistics ); 208 209 printVerbose( "WARNING: MBean " + StringUtil.quote( objectName ) + 210 " returns Statistic names from getStatisticNames() " + 211 "that disagree with the names actually " + 212 "found in Statistics from getStatistics(): " + 213 "getStatisticNames() = {" + ArrayStringifier.stringify( statisticNames, "," ) + 214 "}, getStatistics() = {" + 215 ArrayStringifier.stringify( statisticNamesFromStatistics, "," ) + "}" ); 216 worksOK = false; 217 } 218 } 219 220 return( worksOK ); 221 } 222 223 224 242 243 250 private static final Set <String > COM_SUN_APPSERV_MONITOR_TYPES = 251 GSetUtil.newUnmodifiableStringSet( 252 "jvm", 253 "ejb", 254 "standalone-ejb-module", 255 "bean-pool", 256 "bean-cache", 257 "bean-method", 258 "servlet", 259 "virtual-server", 260 "webmodule-virtual-server", 261 "http-listener", 262 "transaction-service", 263 "thread-pool", 264 "connection-manager", 265 "jdbc-connection-pool", 266 "connector-connection-pool", 267 "file-cache", 268 "keep-alive", 269 "dns", 270 "connection-queue", 271 "webservice-endpoint" ); 272 273 274 private boolean 275 shouldBeTested( final ObjectName objectName ) 276 throws Exception 277 { 278 final String type = objectName.getKeyProperty( "type" ); 279 280 return COM_SUN_APPSERV_MONITOR_TYPES.contains( type ); 281 } 282 283 private boolean 284 hasStatisticSupport( final ObjectName objectName ) 285 { 286 boolean hasSupport = false; 287 288 try 289 { 290 final MBeanInfo mbeanInfo = getMBeanServerConnection().getMBeanInfo( objectName ); 291 boolean shouldTest = false; 292 293 final MBeanOperationInfo [] candidates = mbeanInfo.getOperations(); 294 if ( JMXUtil.findOperations( candidates, "getStatisticNames" ).length != 0 && 295 JMXUtil.findOperations( candidates, "getStatistics" ).length != 0 ) 296 { 297 hasSupport = true; 298 } 299 } 300 catch( Exception e ) 301 { 302 final Throwable rootCause = ExceptionUtil.getRootCause( e ); 303 304 warning( "hasStatisticSupport: got exception: " + rootCause ); 305 } 306 307 return hasSupport; 308 } 309 310 public void 311 testAllMonitor() 312 throws Exception 313 { 314 try 315 { 316 Class.forName( "com.sun.enterprise.admin.monitor.stats.CountStatisticImpl" ); 317 } 318 catch( ClassNotFoundException e ) 319 { 320 failure( "ComSunAppservMonitorTest.testAllMonitor: " + 321 "CLASSPATH is missing Statistic classes, skipping tests. " + 322 "Use 'maven run-tests' instead of 'ant run-tests'" ); 323 return; 324 } 325 326 final Map <String ,ObjectName > m = getAllComSunAppservMonitor(); 327 328 final Collection <ObjectName > objectNames = m.values(); 329 final Set <ObjectName > defective = new HashSet <ObjectName >(); 330 int testedCount = 0; 331 for( final ObjectName objectName : objectNames ) 332 { 333 if ( ! shouldBeTested( objectName ) ) 334 { 335 continue; 336 } 337 338 ++testedCount; 339 340 if ( ! checkMonitor( objectName ) ) 341 { 342 defective.add( objectName ); 343 } 344 } 345 346 printVerbose( "ComSunAppservMonitorTest.testAllMonitor: checked " + 347 testedCount + " com.sun.appserv:category=monitor MBeans for basic functionality, " + 348 defective.size() + " failures." ); 349 350 if ( defective.size() != 0 ) 351 { 352 final String [] names = new String [ defective.size() ]; 354 int i = 0; 355 for( final ObjectName objectName : defective ) 356 { 357 names[ i ] = objectName.getCanonicalKeyPropertyListString(); 358 ++i; 359 } 360 361 Arrays.sort( names ); 362 363 final boolean verbose = getVerbose(); 364 365 warning( "The following " + defective.size() + 366 " com.sun.appserv MBeans don't work correctly, so " + 367 "subsequent tests (eg J2EETest) may fail:\n" + 368 ArrayStringifier.stringify( names, "\n") + 369 (verbose ? "" : "\n(set amxtest.verbose=true for details)") ); 370 } 371 } 372 373 } 374 375 376 377 378 379 380 | Popular Tags |