1 23 package com.sun.appserv.management.j2ee.statistics; 24 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.io.Serializable ; 31 32 import java.lang.reflect.Method ; 33 34 import javax.management.openmbean.CompositeData ; 35 import javax.management.openmbean.CompositeType ; 36 import javax.management.j2ee.statistics.Stats ; 37 import javax.management.j2ee.statistics.Statistic ; 38 import com.sun.appserv.management.util.j2ee.J2EEUtil; 39 40 import com.sun.appserv.management.util.misc.GSetUtil; 41 42 43 75 public class StatsImpl 76 extends MapGetterInvocationHandler<Statistic > 77 implements Stats , Serializable 78 { 79 static final long serialVersionUID = 6228973710059979557L; 80 81 85 public 86 StatsImpl( final CompositeData compositeData ) 87 { 88 this( createStatisticsMap( compositeData ) ); 89 } 90 91 private static Map <String ,Statistic > 92 requireSerializableMap( final Map <String , Statistic > m ) 93 { 94 for( final Statistic s : m.values() ) 96 { 97 if ( ! (s instanceof Statistic ) ) 98 { 99 throw new IllegalArgumentException (); 100 } 101 } 102 103 Map <String ,Statistic > sMap = null; 104 105 if ( m instanceof Serializable ) 106 { 107 sMap = m; 108 } 109 else 110 { 111 sMap = new HashMap <String , Statistic >( m ); 112 } 113 114 return( sMap ); 115 } 116 117 122 private static Statistic 123 requireStatisticImpl( final Statistic statisticIn ) 124 { 125 Statistic statisticOut = null; 126 127 if ( statisticIn instanceof CountStatisticImpl || 128 statisticIn instanceof RangeStatisticImpl || 129 statisticIn instanceof BoundedRangeStatisticImpl || 130 statisticIn instanceof BoundaryStatisticImpl || 131 statisticIn instanceof TimeStatisticImpl || 132 statisticIn instanceof NumberStatisticImpl || 133 statisticIn instanceof StringStatisticImpl 134 ) 135 { 136 statisticOut = statisticIn; 137 } 138 else if ( statisticIn instanceof MapStatistic ) 139 { 140 final Class <? extends Statistic > theInterface = 141 StatisticFactory.getInterface( statisticIn ); 142 143 if ( theInterface != MapStatistic.class ) 144 { 145 statisticOut = StatisticFactory.create( theInterface, 146 J2EEUtil.statisticToMap( statisticIn ) ); 147 } 148 else if ( ! ( statisticIn instanceof MapStatisticImpl ) ) 149 { 150 statisticOut = new MapStatisticImpl( statisticIn ); 152 } 153 } 154 else 155 { 156 assert( false ) : 158 "requireStatisticImpl: unsupported Statistic type of class " + statisticIn.getClass().getName(); 159 statisticOut = new MapStatisticImpl( statisticIn ); 160 } 161 162 return( statisticOut ); 163 } 164 165 168 private static Statistic [] 169 requireStatisticImpl( final Statistic [] statisticsIn ) 170 { 171 boolean convert = false; 172 173 for( int i = 0; i < statisticsIn.length; ++i ) 175 { 176 if ( ! (statisticsIn[ i ] instanceof MapStatistic ) ) 177 { 178 convert = true; 179 break; 180 } 181 } 182 183 Statistic [] statisticsOut = null; 184 if ( convert ) 185 { 186 statisticsOut = new Statistic [ statisticsIn.length ]; 187 188 for( int i = 0; i < 189 statisticsIn.length; ++i ) 190 { 191 statisticsOut[ i ] = requireStatisticImpl( statisticsIn[ i ] ); 192 } 193 } 194 else 195 { 196 statisticsOut = statisticsIn; 197 } 198 199 return( statisticsOut ); 200 } 201 202 206 public 207 StatsImpl( final Map <String , Statistic > statisticsIn ) 208 { 209 super( requireSerializableMap( statisticsIn ) ); 210 } 211 212 213 215 public 216 StatsImpl( final Statistic [] statistics ) 217 { 218 this( createStatisticsMap( statistics ) ); 219 } 220 221 222 private static Map <String ,Statistic > 223 createStatisticsMap( final CompositeData compositeData ) 224 { 225 final CompositeType compositeType = compositeData.getCompositeType(); 226 final Set keySet = compositeType.keySet(); 227 final Iterator iter = keySet.iterator(); 228 229 final Map <String ,Statistic > map = new HashMap <String ,Statistic >(); 230 231 while ( iter.hasNext() ) 232 { 233 final String name = (String )iter.next(); 234 final CompositeData data = (CompositeData )compositeData.get( name ); 235 236 map.put( name, StatisticFactory.create( data ) ); 237 } 238 239 return( map ); 240 } 241 242 private static Map <String ,Statistic > 243 createStatisticsMap( final Statistic [] statistics ) 244 { 245 final Map <String ,Statistic > m = new HashMap <String ,Statistic >(); 246 247 for( int i = 0; i < statistics.length; ++i ) 248 { 249 final Statistic statistic = requireStatisticImpl( statistics[ i ] ); 250 251 if ( statistic != null ) 252 { 253 m.put( statistic.getName(), statistic ); 254 } 255 } 256 257 return( m ); 258 } 259 260 261 public Statistic 262 getStatistic( String statisticName ) 263 { 264 try 265 { 266 return( (Statistic )getValue( statisticName ) ); 267 } 268 catch( Exception e ) 269 { 270 final String msg = "NOT a Statistic: " + statisticName + 271 " of class " + getValue( statisticName ).getClass(); 272 throw new RuntimeException ( msg, e ); 273 } 274 } 275 276 public String [] 277 getStatisticNames() 278 { 279 final Set <String > names = getMap().keySet(); 280 281 return( GSetUtil.toStringArray( names ) ); 282 } 283 284 public Statistic [] 285 getStatistics() 286 { 287 final Collection <Statistic > values = getMap().values(); 288 final Statistic [] statistics = new Statistic [ values.size() ]; 289 290 return( (Statistic [])values.toArray( statistics ) ); 291 } 292 293 public String 294 toString() 295 { 296 final StringBuffer buf = new StringBuffer (); 297 298 final Statistic [] statistics = getStatistics(); 299 buf.append( statistics.length + " Statistics:\n" ); 300 for( int i = 0; i < statistics.length; ++i ) 301 { 302 buf.append( statistics[ i ].toString() + "\n"); 303 } 304 305 return( buf.toString() ); 306 } 307 308 309 public boolean 310 equals( final Object rhs ) 311 { 312 boolean equals = false; 313 314 if ( rhs != null && rhs instanceof Stats ) 315 { 316 final Stats stats = (Stats )rhs; 317 318 final String [] myNames = getStatisticNames(); 319 final String [] rhsNames = stats.getStatisticNames(); 320 if ( myNames.length == rhsNames.length ) 321 { 322 equals = true; 323 324 for( int i = 0; i < myNames.length; ++i ) 325 { 326 final String statisticName = myNames[ i ]; 327 final Statistic myStatistic = getStatistic( statisticName ); 328 final Statistic rhsStatistic = stats.getStatistic( statisticName ); 329 330 if ( ! myStatistic.equals( rhsStatistic ) ) 331 { 332 equals = false; 333 break; 334 } 335 } 336 } 337 } 338 return( equals ); 339 } 340 341 public Object 342 invoke( 343 Object myProxy, 344 Method method, 345 Object [] args ) 346 throws java.lang.Throwable 347 { 348 Object result = null; 349 final String methodName = method.getName(); 350 final int numArgs = args == null ? 0 : args.length; 351 352 if ( numArgs == 0 && methodName.equals( "getStatisticNames" ) ) 353 { 354 result = getStatisticNames(); 355 } 356 else if ( numArgs == 0 && methodName.equals( "getStatistics" ) ) 357 { 358 result = getStatistics(); 359 } 360 else if ( numArgs == 1 && methodName.equals( "getStatistic" ) && 361 method.getReturnType() == Statistic .class && 362 method.getParameterTypes()[ 0 ] == String .class ) 363 { 364 result = getStatistic( (String )args[ 0 ] ); 365 } 366 else 367 { 368 result = super.invoke( myProxy, method, args ); 369 } 370 371 return( result ); 372 } 373 } 374 375 376 377 378 379 380 | Popular Tags |