1 23 24 29 package com.sun.enterprise.jmx.kstat; 30 31 import javax.management.*; 32 33 import java.lang.reflect.Method ; 34 35 import java.util.ArrayList ; 36 import java.util.Set ; 37 import java.util.Iterator ; 38 39 40 43 public class kstatMBean implements DynamicMBean 44 { 45 private final kstat mSource; 46 private final kstatMgrMBean mMgr; 47 private MBeanInfo mMBeanInfo; 48 49 public 50 kstatMBean( kstatMgrMBean mgr, final kstat source ) 51 { 52 mSource = source; 53 mMgr = mgr; 54 } 55 56 public AttributeList 57 getAttributes( String [] names ) 58 { 59 final AttributeList list = new AttributeList(); 60 61 for( int i = 0; i < names.length; ++i ) 62 { 63 final Object value = _getAttribute( names[ i ] ); 64 if ( value != null ) 65 { 66 list.add( new Attribute( names[ i ], value ) ); 67 } 68 } 69 70 return( list ); 71 } 72 73 private Object 74 _getAttribute( String name ) 75 { 76 final Object value = mSource.getValue( name ); 77 78 return( value ); 79 } 80 81 public Object 82 getAttribute( String name ) 83 { 84 return( _getAttribute( name ) ); 85 } 86 87 public AttributeList 88 setAttributes( AttributeList names ) 89 { 90 throw new java.lang.UnsupportedOperationException ( "can't set kstat value" ); 91 } 92 93 public void 94 setAttribute( Attribute attr ) 95 { 96 throw new java.lang.UnsupportedOperationException ( "can't set kstat value" ); 97 } 98 99 private Object 100 internalInvoke( String actionName, Object [] params, String [] signature ) 101 throws Exception 102 { 103 Object result = null; 104 105 if ( actionName.equals( "refresh" ) ) 106 { 107 refresh(); 108 } 109 else 110 { 111 throw new OperationsException( "No such method: " + actionName ); 112 } 113 114 return( result ); 115 } 116 117 118 public Object 119 invoke( String actionName, Object [] params, String [] signature ) 120 throws MBeanException, ReflectionException 121 { 122 Object result = null; 123 124 try 125 { 126 result = internalInvoke( actionName, params, signature ); 127 } 128 catch( MBeanException e ) 129 { 130 throw e; 131 } 132 catch( ReflectionException e ) 133 { 134 throw e; 135 } 136 catch( Exception e ) 137 { 138 throw new MBeanException( e ); 139 } 140 141 return( result ); 142 } 143 144 private MBeanAttributeInfo 145 createAttrInfo( String attrName ) 146 { 147 final MBeanAttributeInfo info = new MBeanAttributeInfo( 148 attrName, 149 mSource.getAttributeType( attrName ).getName(), 150 "", 151 true, false, false ); 152 153 return( info ); 154 } 155 156 private MBeanAttributeInfo [] 157 createAttrInfos() 158 { 159 final ArrayList list = new ArrayList (); 160 161 final Set attrNames = mSource.getAttributeNames(); 162 final Iterator iter = attrNames.iterator(); 163 while ( iter.hasNext() ) 164 { 165 final String attrName = (String )iter.next(); 166 try 167 { 168 final MBeanAttributeInfo info = createAttrInfo( attrName ); 169 170 list.add( info ); 171 } 172 catch( IllegalArgumentException e ) 173 { 174 } 177 } 178 179 final MBeanAttributeInfo [] infos = new MBeanAttributeInfo [ list.size() ]; 180 list.toArray( infos ); 181 182 return( infos ); 183 } 184 185 public void 186 refresh() 187 throws Exception 188 { 189 mMgr.refresh( mSource.getScopedName() ); 190 } 191 192 193 private MBeanOperationInfo [] 194 createOperationInfos() 195 { 196 final MBeanOperationInfo [] infos = new MBeanOperationInfo[ 1 ]; 197 198 try 199 { 200 final Method m = this.getClass().getDeclaredMethod( "refresh", null); 201 infos[ 0 ] = new MBeanOperationInfo( 202 "refresh all attributes for " + mSource.getScopedName(), 203 m ); 204 } 205 catch( NoSuchMethodException e ) 206 { 207 assert( false ); 208 } 209 210 return( infos ); 211 } 212 213 public synchronized MBeanInfo 214 getMBeanInfo( ) 215 { 216 if ( mMBeanInfo == null ) 217 { 218 final MBeanAttributeInfo [] attrInfo = createAttrInfos(); 219 final MBeanOperationInfo [] operationInfo = createOperationInfos(); 220 final MBeanConstructorInfo [] constructorInfo = new MBeanConstructorInfo[0]; 221 final MBeanNotificationInfo [] notificationInfo = new MBeanNotificationInfo[0]; 222 223 final MBeanInfo info = new MBeanInfo( 224 this.getClass().getName(), 225 mSource.getScopedName(), 226 attrInfo, 227 constructorInfo, 228 operationInfo, 229 notificationInfo ); 230 231 mMBeanInfo = info; 232 } 233 234 return( mMBeanInfo ); 235 } 236 }; 237 238 239 | Popular Tags |