1 23 package com.sun.appserv.management.util.j2ee; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Set ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 import java.util.Collections ; 32 33 import java.lang.reflect.Method ; 34 35 import javax.management.openmbean.CompositeType ; 36 import javax.management.openmbean.CompositeData ; 37 import javax.management.openmbean.CompositeDataSupport ; 38 import javax.management.openmbean.OpenType ; 39 import javax.management.openmbean.OpenDataException ; 40 41 import javax.management.j2ee.statistics.*; 42 43 import com.sun.appserv.management.util.jmx.OpenMBeanUtil; 44 import com.sun.appserv.management.util.misc.GSetUtil; 45 import com.sun.appserv.management.util.misc.CollectionUtil; 46 import com.sun.appserv.management.util.jmx.JMXUtil; 47 48 import com.sun.appserv.management.j2ee.statistics.MapStatistic; 49 import com.sun.appserv.management.j2ee.statistics.StringStatistic; 50 import com.sun.appserv.management.j2ee.statistics.NumberStatistic; 51 52 55 public class J2EEUtil 56 { 57 private final static String GET = "get"; 58 59 private J2EEUtil() {} 60 61 62 private static final Set <String > IGNORE = 63 GSetUtil.newUnmodifiableStringSet( "getClass" ); 64 65 73 public static Map <String ,Object > 74 statisticToMap( Statistic statistic ) 75 { 76 final Map <String ,Object > result = new HashMap <String ,Object >(); 77 78 if ( statistic instanceof MapStatistic ) 79 { 80 result.putAll( ((MapStatistic)statistic).asMap() ); 81 } 82 else 83 { 84 final Method [] methods = statistic.getClass().getMethods(); 86 87 for( int i = 0; i < methods.length; ++i ) 88 { 89 final Method method = methods[ i ]; 90 final String methodName = method.getName(); 91 92 if ( methodName.startsWith( GET ) && 93 ! IGNORE.contains( methodName ) && 94 method.getParameterTypes().length == 0 && 95 method.getExceptionTypes().length == 0 && 96 method.getReturnType() != Void .class ) 97 { 98 try 99 { 100 final Object value = method.invoke( statistic, (Object [])null ); 101 102 final String name = methodName.substring( GET.length(), methodName.length() ); 103 result.put( name, value ); 104 } 105 catch( Exception e ) 106 { 107 } 109 } 110 } 111 } 112 113 return( result ); 114 } 115 116 117 120 public static String 121 getStatisticType( Statistic statistic ) 122 { 123 String type = getType( STATISTIC_CLASSES, statistic ); 124 125 if ( type == null ) 126 { 127 type = statistic.getClass().getName(); 128 } 129 130 return( type ); 131 } 132 133 134 137 private static String 138 getType( Class [] classes, Object o ) 139 { 140 String type = null; 141 142 for( int i = 0; i < classes.length; ++i ) 143 { 144 final Class theClass = classes[ i ]; 145 146 if ( theClass.isInstance( o ) ) 147 { 148 type = theClass.getName(); 149 break; 150 } 151 } 152 return( type ); 153 } 154 155 private static final Class [] STATISTIC_CLASSES = 156 { 157 BoundaryStatistic.class, 158 BoundedRangeStatistic.class, 159 TimeStatistic.class, 160 RangeStatistic.class, 161 CountStatistic.class, 162 StringStatistic.class, 163 NumberStatistic.class, 164 MapStatistic.class, 165 }; 166 167 168 169 private static CompositeType 170 statisticMapToCompositeType( final String statisticType, final Map <String ,?> map ) 171 throws OpenDataException 172 { 173 final String description = "J2EE management statistic " + statisticType; 174 175 return( OpenMBeanUtil.mapToCompositeType( statisticType, description, map, null) ); 176 } 177 178 179 182 public static CompositeType 183 statisticToCompositeType( Statistic statistic ) 184 throws OpenDataException 185 { 186 final String statisticType = getStatisticType( statistic ); 187 final Map <String ,Object > map = statisticToMap( statistic ); 188 189 return( statisticMapToCompositeType( statisticType, map ) ); 190 } 191 192 197 public static CompositeDataSupport 198 statisticToCompositeData( Statistic statistic ) 199 throws OpenDataException 200 { 201 final String statisticType = getStatisticType( statistic ); 202 203 final Map <String ,Object > map = statisticToMap( statistic ); 204 205 final CompositeType type = statisticMapToCompositeType( statisticType, map ); 206 207 return( new CompositeDataSupport ( type, map ) ); 208 } 209 210 211 private static final Class [] STATS_CLASSES = 212 { 213 EJBStats.class, 214 MessageDrivenBeanStats.class, 215 SessionBeanStats.class, 216 StatefulSessionBeanStats.class, 217 StatelessSessionBeanStats.class, 218 EntityBeanStats.class, 219 JavaMailStats.class, 220 JCAConnectionStats.class, 221 JCAConnectionPoolStats.class, 222 JDBCConnectionPoolStats.class, 223 JCAStats.class, 224 JDBCConnectionPoolStats.class, 225 JDBCStats.class, 226 JMSConnectionStats.class, 227 JMSConsumerStats.class, 228 JMSEndpointStats.class, 229 JMSProducerStats.class, 230 JMSSessionStats.class, 231 JMSStats.class, 232 JVMStats.class, 233 ServletStats.class, 234 URLStats.class 235 }; 236 237 240 public static String 241 getStatsType( Stats stats ) 242 { 243 String type = getType( STATS_CLASSES, stats ); 244 245 if ( type == null ) 246 { 247 type = stats.getClass().getName(); 248 } 249 250 return( type ); 251 } 252 253 254 291 public static CompositeDataSupport 292 statsToCompositeData( final Stats stats ) 293 throws OpenDataException 294 { 295 final String statsType = getStatsType( stats ); 296 297 final Statistic[] statistics = stats.getStatistics(); 298 final String [] names = new String [ statistics.length ]; 299 final CompositeData [] datas = new CompositeData [ names.length ]; 300 final String [] itemDescriptions = new String [ names.length ]; 301 final CompositeType [] itemTypes = new CompositeType [ names.length ]; 302 303 for( int i = 0; i < names.length; ++i ) 304 { 305 names[ i ] = statistics[ i ].getName(); 306 datas[ i ] = statisticToCompositeData( statistics[ i ] ); 307 itemTypes[ i ] = datas[ i ].getCompositeType(); 308 itemDescriptions[ i ] = statistics[ i ].getName(); 309 } 310 311 final CompositeType type = new CompositeType ( 312 statsType, 313 "CompositeData for " + statsType, 314 names, 315 itemDescriptions, 316 itemTypes ); 317 318 return( new CompositeDataSupport ( type, names, datas ) ); 319 } 320 321 322 326 public static Map <String ,Statistic> 327 statsToMap( final Stats stats ) 328 { 329 final Statistic[] statistics = stats.getStatistics(); 330 331 return( statisticsToMap( statistics ) ); 332 } 333 334 335 341 public static Map <String ,Statistic> 342 statisticsToMap( final Statistic[] statistics ) 343 { 344 final Map <String ,Statistic> m = new HashMap <String ,Statistic>(); 345 346 for( int i = 0; i < statistics.length; ++i ) 347 { 348 final String name = statistics[ i ].getName(); 349 m.put( name, statistics[ i ] ); 350 } 351 352 return( m ); 353 } 354 355 356 public static Method [] 357 getStatisticGetterMethodsUsingNames( final Stats stats) 358 throws NoSuchMethodException 359 { 360 final String [] statisticNames = stats.getStatisticNames(); 361 362 final Class statsClass = stats.getClass(); 363 final Method [] methods = new Method [ statisticNames.length ]; 364 365 final Set <String > missing = new HashSet <String >(); 366 367 for( int i = 0; i < statisticNames.length; ++i ) 368 { 369 final String methodName = JMXUtil.GET + statisticNames[ i ]; 371 try 372 { 373 methods[ i ] = statsClass.getMethod( methodName, (Class [])null ); 374 } 375 catch( final NoSuchMethodException e ) 376 { 377 missing.add( methodName ); 378 } 379 } 380 381 if ( missing.size() != 0 ) 382 { 383 throw new NoSuchMethodException ( 384 "Missing methods: in object of class " + stats.getClass().getName() + 385 ": " + CollectionUtil.toString( missing, ", ") ); 386 } 387 388 return( methods ); 389 } 390 391 public static Method [] 392 getStatisticGetterMethodsUsingIntrospection( final Stats stats) 393 { 394 final Method [] candidates = stats.getClass().getMethods(); 395 396 final List <Method > results = new ArrayList <Method >(); 397 for( int methodIdx = 0; methodIdx < candidates.length; ++methodIdx ) 398 { 399 final Method method = candidates[ methodIdx ]; 400 final String methodName = method.getName(); 401 402 final Class returnType = method.getReturnType(); 403 404 if ( JMXUtil.isGetter( method ) && 405 Statistic.class.isAssignableFrom( returnType ) && 406 method.getParameterTypes().length == 0 ) 407 { 408 results.add( method ); 409 } 410 } 411 412 return (Method [])results.toArray( new Method [ results.size() ] ); 413 } 414 415 416 } 417 418 419 420 421 422 | Popular Tags |