private JSONObject emitAttributes ( ObjectName objName ) throws Exception { MBeanServer mBeanServer = ManagementFactory . getPlatformMBeanServer ( ) ; MBeanInfo mBeanInfo = mBeanServer . getMBeanInfo ( objName ) ; JSONObject resp = new JSONObject ( ) ; if ( mBeanInfo != null ) { MBeanAttributeInfo [ ] attrs = mBeanInfo . getAttributes ( ) ; if ( attrs != null ) { List < String > attrNames = new ArrayList < String > ( attrs . length ) ; for ( MBeanAttributeInfo attr : attrs ) { attrNames . add ( attr . getName ( ) ) ; } AttributeList attrList = mBeanServer . getAttributes ( objName , attrNames . toArray ( new String [ 0 ] ) ) ; for ( Attribute attr : attrList . asList ( ) ) { Object value = attr . getValue ( ) ; String attrName = attr . getName ( ) ; if ( attrName != null && value != null ) { String attrValue = null ; if ( value instanceof CompositeDataSupport ) { CompositeDataSupport compositeValue = ( CompositeDataSupport ) value ; if ( compositeValue != null ) { try { if ( compositeValue . containsKey ( CURRENT_VALUE ) ) { Object curValue = compositeValue . get ( CURRENT_VALUE ) ; attrValue = ( curValue == null ? "null" : curValue . toString ( ) ) ; } } catch ( Exception e ) { attrValue = compositeValue . toString ( ) ; } } } if ( attrValue == null ) { attrValue = value . toString ( ) ; } resp . put ( attrName , ( attrValue == null ? "null" : attrValue ) ) ; } } } } return resp ; }
Generate JSON for the MBean attributes