1 23 24 28 29 34 35 package com.sun.enterprise.management.support; 36 37 import java.util.Set ; 38 39 import javax.management.ObjectName ; 40 import javax.management.MBeanServer ; 41 import javax.management.MBeanInfo ; 42 import javax.management.MBeanAttributeInfo ; 43 import javax.management.Attribute ; 44 import javax.management.AttributeList ; 45 import javax.management.InstanceNotFoundException ; 46 import javax.management.AttributeNotFoundException ; 47 import javax.management.InvalidAttributeValueException ; 48 import javax.management.ReflectionException ; 49 import javax.management.MBeanException ; 50 import javax.management.ReflectionException ; 51 52 import com.sun.enterprise.management.support.AMXAttributeNameMapper; 53 import com.sun.appserv.management.util.stringifier.SmartStringifier; 54 55 import com.sun.appserv.management.util.jmx.JMXUtil; 56 import com.sun.appserv.management.util.misc.ArrayConversion; 57 import com.sun.appserv.management.util.misc.CollectionUtil; 58 import com.sun.appserv.management.util.misc.StringUtil; 59 60 61 65 public class MappedDelegate extends DelegateBase 66 { 67 70 private final Delegate mDelegate; 71 72 75 private final MBeanInfo mUnmappedMBeanInfo; 76 77 80 private final MBeanInfo mMappedMBeanInfo; 81 82 85 private final Set <String > mUnmappedAttributeNames; 86 87 90 private final AMXAttributeNameMapper mAttributeNameMapper; 91 92 93 96 public 97 MappedDelegate( 98 final Delegate delegate, 99 final AMXAttributeNameMapper mapper ) 100 { 101 super( "MappedDelegate." + (delegate == null ? "null" : delegate.getID()), null); 102 103 mDelegate = delegate; 104 105 mUnmappedMBeanInfo = getUnmappedMBeanInfoFresh(); 106 107 final String [] unmappedAttributeNames = 108 JMXUtil.getAttributeNames( mUnmappedMBeanInfo.getAttributes() ); 109 mUnmappedAttributeNames = ArrayConversion.arrayToSet( unmappedAttributeNames ); 110 111 mAttributeNameMapper = mapper; 112 114 mMappedMBeanInfo = mapMBeanInfo( mDelegate.getMBeanInfo() ); 115 } 116 117 121 protected String 122 derivedToOriginal( final String mappedName ) 123 { 124 return( mAttributeNameMapper.derivedToOriginal( mappedName ) ); 125 } 126 127 130 private Attribute 131 derivedToOriginal( final Attribute mappedAttr ) 132 { 133 final String unmappedName = derivedToOriginal( mappedAttr.getName() ); 134 135 return( new Attribute ( unmappedName, mappedAttr.getValue() ) ); 136 } 137 138 141 protected String [] 142 derivedToOriginal( final String [] mappedNames ) 143 { 144 final String [] unmappedNames = new String [ mappedNames.length ]; 145 146 for( int i = 0; i < unmappedNames.length; ++i ) 147 { 148 unmappedNames[ i ] = derivedToOriginal( mappedNames[ i ] ); 149 } 150 151 return( unmappedNames ); 152 } 153 154 155 159 private String 160 originalToDerived( final String unmappedName ) 161 { 162 return( mAttributeNameMapper.originalToDerived( unmappedName ) ); 163 } 164 165 166 168 private Attribute 169 originalToDerived( final Attribute unmappedAttr ) 170 { 171 final String mappedName = originalToDerived( unmappedAttr.getName() ); 172 return( new Attribute ( mappedName, unmappedAttr.getValue() ) ); 173 } 174 175 176 protected AttributeList 177 derivedToOriginal( final AttributeList mappedAttrs ) 178 { 179 final int numAttrs = mappedAttrs.size(); 181 final AttributeList unmappedAttrs = new AttributeList (); 182 for( int i = 0; i < numAttrs; ++i ) 183 { 184 unmappedAttrs.add( derivedToOriginal( (Attribute )mappedAttrs.get( i ) ) ); 185 } 186 187 return( unmappedAttrs ); 188 } 189 190 protected AttributeList 191 originalToDerived( final AttributeList unmappedAttrs ) 192 { 193 final int numAttrs = unmappedAttrs.size(); 195 final AttributeList mappedAttrs = new AttributeList (); 196 for( int i = 0; i < numAttrs; ++i ) 197 { 198 mappedAttrs.add( originalToDerived( (Attribute )unmappedAttrs.get( i ) ) ); 199 } 200 201 202 return( unmappedAttrs ); 203 } 204 205 public boolean 206 supportsAttribute( final String mappedName ) 207 { 208 return( mAttributeNameMapper.getDerivedNames().contains( mappedName ) ); 209 } 210 211 private void 212 checkSupported( final String mappedName ) 213 throws AttributeNotFoundException 214 { 215 if ( ! supportsAttribute( mappedName ) ) 216 { 217 debug( "Attribute " + mappedName + " is not supported, have: " + 218 CollectionUtil.toString( mAttributeNameMapper.getDerivedNames(), ", ") ); 219 throw new AttributeNotFoundException ( mappedName ); 220 } 221 } 222 223 public Object 224 getAttribute( final String mappedName ) 225 throws AttributeNotFoundException 226 { 227 checkSupported( mappedName ); 228 229 final String unmappedName = mAttributeNameMapper.derivedToOriginal( mappedName ); 230 231 return( mDelegate.getAttribute( unmappedName ) ); 232 } 233 234 public AttributeList 235 getAttributes( final String [] mappedNames ) 236 { 237 final AttributeList unmappedResults = 238 mDelegate.getAttributes( derivedToOriginal( mappedNames ) ); 239 240 return( originalToDerived( unmappedResults ) ); 241 } 242 243 public void 244 setAttribute( final Attribute mappedAttr ) 245 throws AttributeNotFoundException , InvalidAttributeValueException 246 { 247 checkSupported( mappedAttr.getName() ); 248 249 final Attribute unmappedAttr = derivedToOriginal( mappedAttr ); 250 251 mDelegate.setAttribute( unmappedAttr ); 252 } 253 254 public AttributeList 255 setAttributes( final AttributeList mappedAttrs ) 256 { 257 final AttributeList unmappedAttrs = derivedToOriginal( mappedAttrs ); 259 260 final AttributeList unmappedResults = 262 mDelegate.setAttributes( unmappedAttrs ); 263 264 return( originalToDerived( unmappedResults ) ); 265 } 266 267 protected MBeanInfo 268 getUnmappedMBeanInfoFresh() 269 { 270 return( mDelegate.getMBeanInfo( ) ); 271 } 272 273 protected MBeanInfo 274 getUnmappedMBeanInfo() 275 { 276 return( mUnmappedMBeanInfo ); 277 } 278 279 private MBeanAttributeInfo 280 mapAttributeInfo( final MBeanAttributeInfo unmappedInfo ) 281 { 282 String mappedName = originalToDerived( unmappedInfo.getName() ); 283 284 if ( mappedName == null ) 285 { 286 mappedName = unmappedInfo.getName(); 287 } 288 289 final MBeanAttributeInfo mappedInfo = 290 new MBeanAttributeInfo ( 291 mappedName, 292 unmappedInfo.getType(), 293 unmappedInfo.getDescription(), 294 unmappedInfo.isReadable(), 295 unmappedInfo.isWritable(), 296 unmappedInfo.isIs() 297 ); 298 299 return( mappedInfo ); 300 } 301 302 private MBeanAttributeInfo [] 303 mapAttributeInfos( final MBeanAttributeInfo [] unmappedInfos ) 304 { 305 final MBeanAttributeInfo [] mappedInfos = 306 new MBeanAttributeInfo [ unmappedInfos.length ]; 307 308 for ( int i = 0; i < unmappedInfos.length; ++i ) 309 { 310 final String attrName = unmappedInfos[ i ].getName(); 311 assert( attrName != null ); 312 313 mappedInfos[ i ] = mapAttributeInfo( unmappedInfos[ i ] ); 314 assert( mappedInfos[ i ].getName() != null ); 315 debug( "Mapped " + unmappedInfos[ i ].getName() + " to " + mappedInfos[i].getName() ); 316 } 317 return( mappedInfos ); 318 } 319 320 323 private MBeanInfo 324 mapMBeanInfo( final MBeanInfo unmappedMBeanInfo ) 325 { 326 final MBeanAttributeInfo [] mappedAttributeInfos = 327 mapAttributeInfos( unmappedMBeanInfo.getAttributes() ); 328 329 final MBeanInfo mappedInfo = new MBeanInfo ( 330 unmappedMBeanInfo.getClassName(), 331 unmappedMBeanInfo.getDescription(), 332 mappedAttributeInfos, 333 unmappedMBeanInfo.getConstructors(), 334 unmappedMBeanInfo.getOperations(), 335 unmappedMBeanInfo.getNotifications() 336 ); 337 338 return( mappedInfo ); 339 } 340 341 342 protected MBeanInfo 343 getMappedMBeanInfo() 344 { 345 return( mMappedMBeanInfo ); 346 } 347 348 public MBeanInfo 349 getMBeanInfo() 350 { 351 return( getMappedMBeanInfo() ); 352 } 353 354 356 public String [] 357 getAttributeNames( ) 358 { 359 final String [] names = 360 JMXUtil.getAttributeNames( getMBeanInfo().getAttributes() ); 361 362 return( names ); 363 } 364 365 366 367 370 public final Object 371 invoke( 372 String operationName, 373 Object [] args, 374 String [] types ) 375 { 376 final Object result = mDelegate.invoke( operationName, args, types ); 377 378 return( result ); 379 } 380 } 381 382 383 384 385 386 387 388 389 | Popular Tags |