1 23 package com.sun.appserv.management.util.jmx; 24 25 import java.util.Map ; 26 import java.util.Set ; 27 import java.util.HashMap ; 28 import java.util.Date ; 29 import java.util.Iterator ; 30 import java.util.Collection ; 31 import java.io.Serializable ; 32 33 import java.math.BigInteger ; 34 import java.math.BigDecimal ; 35 36 import java.lang.reflect.Array ; 37 38 import javax.management.ObjectName ; 39 import javax.management.openmbean.OpenType ; 40 import javax.management.openmbean.ArrayType ; 41 import javax.management.openmbean.CompositeData ; 42 import javax.management.openmbean.CompositeDataSupport ; 43 import javax.management.openmbean.CompositeType ; 44 import javax.management.openmbean.TabularType ; 45 import javax.management.openmbean.TabularData ; 46 import javax.management.openmbean.SimpleType ; 47 import javax.management.openmbean.OpenDataException ; 48 import javax.management.openmbean.InvalidOpenTypeException ; 49 50 import com.sun.appserv.management.util.misc.ArrayConversion; 51 import com.sun.appserv.management.util.misc.IteratorUtil; 52 import com.sun.appserv.management.util.misc.TypeCast; 53 54 57 public final class OpenMBeanUtil 58 { 59 private OpenMBeanUtil() {} 60 61 private static Map <Class ,SimpleType > SIMPLETYPES_MAP = null; 62 private static Map <Class ,SimpleType > 63 getSimpleTypesMap() 64 { 65 if ( SIMPLETYPES_MAP == null ) 66 { 67 final Map <Class ,SimpleType > m = new HashMap <Class ,SimpleType >(); 68 69 m.put( Byte .class, SimpleType.BYTE ); 70 m.put( Short .class, SimpleType.SHORT ); 71 m.put( Integer .class, SimpleType.INTEGER ); 72 m.put( Long .class, SimpleType.LONG ); 73 m.put( BigInteger .class, SimpleType.BIGINTEGER ); 74 m.put( BigDecimal .class, SimpleType.BIGDECIMAL ); 75 m.put( Float .class, SimpleType.FLOAT ); 76 m.put( Double .class, SimpleType.DOUBLE ); 77 78 m.put( Character .class, SimpleType.CHARACTER ); 79 m.put( Boolean .class, SimpleType.BOOLEAN ); 80 m.put( String .class, SimpleType.STRING ); 81 m.put( Date .class, SimpleType.DATE ); 82 m.put( Void .class, SimpleType.VOID ); 83 84 m.put( ObjectName .class, SimpleType.OBJECTNAME ); 85 86 SIMPLETYPES_MAP = m; 87 } 88 89 return( SIMPLETYPES_MAP ); 90 } 91 92 95 static public SimpleType 96 getSimpleType( final Class c ) 97 { 98 final SimpleType type = (SimpleType )(getSimpleTypesMap().get( c )); 99 100 return( type ); 101 102 } 103 104 private static final String [] EMPTY_STRING_ARRAY = new String [ 0 ]; 105 private static final OpenType [] EMPTY_OPENTYPES = new OpenType [ 0 ]; 106 107 108 109 112 private static Object 113 getAnyArrayElement( Object o ) 114 { 115 Object result = null; 116 117 final int length = Array.getLength( o ); 118 if ( length != 0 ) 119 { 120 for( int i = 0; i < length; ++i ) 121 { 122 final Object element = Array.get( o, 0 ); 123 124 if ( element != null ) 125 { 126 if ( element.getClass().isArray() ) 127 { 128 result = getAnyArrayElement( element ); 129 if ( result != null ) 130 break; 131 } 132 else 133 { 134 result = element; 135 break; 136 } 137 } 138 } 139 } 140 141 return( result ); 142 } 143 144 145 private static int 146 getArrayDimensions( final Class theClass ) 147 { 148 final String classname = theClass.getName(); 149 150 int dim = 0; 151 while ( classname.charAt( dim ) == '[' ) 152 { 153 ++dim; 154 } 155 156 return( dim ); 157 } 158 159 160 163 static public OpenType 164 getOpenType( final Object o ) 165 throws InvalidOpenTypeException , OpenDataException 166 { 167 if ( o == null ) 168 { 169 throw new IllegalArgumentException (); 171 } 172 173 OpenType type = getSimpleType( o.getClass() ); 174 175 if ( type == null ) 176 { 177 final Class theClass = o.getClass(); 178 179 if ( theClass.isArray() ) 180 { 181 final int length = Array.getLength( o ); 182 final int dimensions = getArrayDimensions( theClass ); 183 final Class elementClass = theClass.getComponentType(); 184 185 final SimpleType simpleType = getSimpleType( elementClass ); 186 if ( simpleType != null ) 187 { 188 type = new ArrayType ( dimensions, simpleType ); 189 } 190 else 191 { 192 final Object element = getAnyArrayElement( o ); 193 194 if ( CompositeData .class.isAssignableFrom( elementClass ) ) 195 { 196 if ( element == null ) 197 { 198 type = SimpleType.VOID; 199 } 200 else 201 { 202 type = new ArrayType ( dimensions, ((CompositeData )element).getCompositeType() ); 203 } 204 } 205 else if ( TabularData .class.isAssignableFrom( elementClass ) ) 206 { 207 if ( element == null ) 208 { 209 type = SimpleType.VOID; 210 } 211 else 212 { 213 type = new ArrayType ( dimensions, ((TabularData )element).getTabularType() ); 214 } 215 } 216 } 217 218 } 219 else if ( o instanceof CompositeData ) 220 { 221 type = ((CompositeData )o).getCompositeType(); 222 } 223 else if ( o instanceof TabularData ) 224 { 225 type = ((TabularData )o).getTabularType(); 226 } 227 } 228 229 if ( type == null ) 230 { 231 throw new IllegalArgumentException ( o.getClass().getName() ); 232 } 233 234 return( type ); 235 } 236 237 246 public static Map <String ,Serializable > 247 convertTypes( final Map <String ,Serializable > orig ) 248 { 249 final Map <String ,Serializable > 250 result = new HashMap <String ,Serializable >(); 251 252 for( final String key : orig.keySet() ) 253 { 254 final Serializable value = (Serializable )orig.get( key ); 255 256 if ( value instanceof Collection ) 257 { 258 Object [] newValue = 259 IteratorUtil.toArray( ((Collection <Serializable >)value).iterator() ); 260 newValue = ArrayConversion.specializeArray( newValue ); 261 262 result.put( key, newValue ); 263 } 264 else 265 { 266 result.put( key, value ); 267 } 268 } 269 270 271 return( result ); 272 } 273 274 282 public static CompositeType 283 mapToCompositeType( 284 final String typeName, 285 final String description, 286 final Map <String ,?> map, 287 CompositeTypeFromNameCallback callback) 288 throws OpenDataException 289 { 290 final String [] itemNames = new String [ map.keySet().size()]; 291 map.keySet().toArray( itemNames ); 292 293 final String [] itemDescriptions = new String [ itemNames.length ]; 294 final OpenType [] itemTypes = new OpenType [ itemNames.length ]; 295 296 for( int i = 0; i < itemNames.length; ++i ) 297 { 298 final String name = itemNames[ i ]; 299 final Object value = map.get( name ); 300 301 itemDescriptions[ i ] = "value " + name; 302 if ( value == null ) 303 { 304 itemTypes[ i ] = callback.getOpenTypeFromName( name ); 306 } 307 else 308 { 309 itemTypes[ i ] = getOpenType( value ); 310 } 311 } 312 313 final CompositeType type = new CompositeType ( 314 typeName, 315 description, 316 itemNames, 317 itemDescriptions, 318 itemTypes ); 319 320 return( type ); 321 } 322 323 331 public static CompositeData 332 mapToCompositeData( 333 final String typeName, 334 final String description, 335 final Map <String ,Object > map ) 336 throws OpenDataException 337 { 338 final CompositeType type = mapToCompositeType( typeName, description, map, null); 339 340 return( new CompositeDataSupport ( type, map ) ); 341 } 342 343 344 347 public static Map <String ,Serializable > 348 compositeDataToMap( final CompositeData data ) 349 { 350 final Map <String ,Serializable > map = new HashMap <String ,Serializable >(); 351 final CompositeType type = data.getCompositeType(); 352 final Set <String > keySet = TypeCast.asSet( type.keySet() ); 353 354 for( String name : keySet ) 355 { 356 map.put( name, (Serializable )data.get( name ) ); 357 } 358 359 return( map ); 360 } 361 362 363 364 367 public static OpenType 368 getStackTraceElementOpenType() 369 throws OpenDataException 370 { 371 final String [] itemNames = new String [] 372 { 373 "ClassName", 374 "FileName", 375 "LineNumber", 376 "IsNativeMethod", 377 }; 378 379 final String [] descriptions = new String [ ] 380 { 381 "ClassName", 382 "FileName", 383 "LineNumber", 384 "IsNativeMethod", 385 }; 386 387 final OpenType [] openTypes = new OpenType [ itemNames.length ]; 388 openTypes[ 0 ] = SimpleType.STRING; 389 openTypes[ 1 ] = SimpleType.STRING; 390 openTypes[ 2 ] = SimpleType.INTEGER; 391 openTypes[ 3 ] = SimpleType.BOOLEAN; 392 393 return( new CompositeType ( 394 StackTraceElement .class.getName(), 395 "StackTraceElement composite type", 396 itemNames, 397 descriptions, 398 openTypes 399 ) ); 400 } 401 402 405 public static OpenType 406 getThrowableOpenType( final Throwable t) 407 throws OpenDataException 408 { 409 final String [] itemNames = new String [] 410 { 411 "Message", 412 "Cause", 413 "StackTrace", 414 }; 415 416 final String [] descriptions = new String [ ] 417 { 418 "The message from the Throwable", 419 "The cause (if any) from the Throwable", 420 "The stack trace from the Throwable", 421 }; 422 423 final OpenType [] openTypes = new OpenType [ itemNames.length ]; 424 425 openTypes[ 0 ] = SimpleType.STRING; 426 openTypes[ 1 ] = t.getCause() == null ? SimpleType.VOID : getThrowableOpenType( t.getCause() ); 427 openTypes[ 2 ] = new ArrayType ( t.getStackTrace().length, 428 getStackTraceElementOpenType() ); 429 430 431 return( new CompositeType ( 432 t.getClass().getName(), 433 "Throwable composite type", 434 itemNames, 435 descriptions, 436 openTypes 437 ) ); 438 } 439 440 } 441 442 443 444 445 446 447 | Popular Tags |