1 23 package com.sun.enterprise.management.support; 24 25 import java.util.Map ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Collections ; 30 import java.util.Iterator ; 31 32 import java.io.Serializable ; 33 34 import javax.management.ObjectName ; 35 import javax.management.MBeanServer ; 36 import javax.management.MBeanRegistration ; 37 import javax.management.NotificationEmitter ; 38 import javax.management.NotificationFilter ; 39 import javax.management.NotificationListener ; 40 import javax.management.Notification ; 41 import javax.management.ListenerNotFoundException ; 42 import javax.management.NotificationBroadcasterSupport ; 43 import javax.management.MBeanNotificationInfo ; 44 import javax.management.AttributeNotFoundException ; 45 46 import javax.management.Attribute ; 47 import javax.management.AttributeList ; 48 import javax.management.MBeanInfo ; 49 import javax.management.MBeanAttributeInfo ; 50 import javax.management.DynamicMBean ; 51 52 import com.sun.appserv.management.base.Sample; 53 import com.sun.appserv.management.base.AMXDebug; 54 import com.sun.appserv.management.util.jmx.JMXUtil; 55 import com.sun.appserv.management.util.jmx.NotificationBuilder; 56 import com.sun.appserv.management.util.misc.Output; 57 import com.sun.appserv.management.base.Util; 58 59 63 public final class TestDummy 64 implements TestDummyMBean, DynamicMBean , MBeanRegistration 65 { 66 private final NotificationBroadcasterSupport mBroadcaster; 67 68 private final Map <String ,Object > mAttributes; 70 private MBeanInfo mMBeanInfo; 71 private Output mDebug; 72 73 private MBeanServer mServer; 74 private ObjectName mSelfObjectName; 75 76 77 public 78 TestDummy( ) 79 { 80 mAttributes = Collections.synchronizedMap( new HashMap <String ,Object >() ); 81 mMBeanInfo = null; 82 83 mDebug = AMXDebug.getInstance().getOutput( getDebugID() ); 84 86 mBroadcaster = new NotificationBroadcasterSupport (); 87 } 88 89 public String 90 getAttr1() 91 { 92 return (String )mAttributes.get( "Attr1" ); 93 } 94 95 public void 96 setAttr1( final String value ) 97 { 98 setAttribute( "Attr1", value); 99 } 100 101 public String 102 getAttr2() 103 { 104 return (String )mAttributes.get( "Attr2" ); 105 } 106 107 public void 108 setAttr2( final String value ) 109 { 110 setAttribute( "Attr2", value); 111 } 112 113 private void 114 debug( final Object o ) 115 { 116 mDebug.println( o ); 117 } 118 119 private String 120 getDebugID() 121 { 122 return this.getClass().getName(); 123 } 124 125 public void 126 addAttribute( final String name, final Object value ) 127 { 128 if ( name == null || name.length() == 0 ) 129 { 130 debug( "Illegal Attribute name: " + name ); 131 throw new IllegalArgumentException ( ); 132 } 133 134 mAttributes.put( name, value ); 135 mMBeanInfo = null; 136 debug( "added Attribute: " + name ); 137 } 138 139 public void 140 removeAttribute( final String name ) 141 { 142 mAttributes.remove( name ); 143 mMBeanInfo = null; 144 debug( "removed Attribute: " + name ); 145 } 146 147 private synchronized MBeanInfo 148 createMBeanInfo() 149 { 150 debug( "createMBeanInfo"); 151 final MBeanInfo baseMBeanInfo = 152 MBeanInfoConverter.getInstance().convert( TestDummy.class, null ); 153 154 final List <MBeanAttributeInfo > dynamicAttrInfos = new ArrayList <MBeanAttributeInfo >(); 155 156 int i = 0; 157 for( final String name : mAttributes.keySet() ) 158 { 159 final Object value = mAttributes.get( name ); 160 final String type = value == null ? 161 String .class.getName() : value.getClass().getName(); 162 163 final MBeanAttributeInfo info = 164 new MBeanAttributeInfo ( name, type, "dynamically-added Attribute", 165 true, true, false ); 166 dynamicAttrInfos.add( info ); 167 } 168 169 final MBeanAttributeInfo [] dynInfos = new MBeanAttributeInfo [ dynamicAttrInfos.size() ]; 170 dynamicAttrInfos.toArray( dynInfos ); 171 172 final MBeanAttributeInfo [] attrInfos = 173 JMXUtil.mergeMBeanAttributeInfos( dynInfos, baseMBeanInfo.getAttributes() ); 174 175 return( JMXUtil.newMBeanInfo( baseMBeanInfo, attrInfos ) ); 176 } 177 178 public synchronized MBeanInfo 179 getMBeanInfo() 180 { 181 if ( mMBeanInfo == null ) 182 { 183 mMBeanInfo = createMBeanInfo(); 184 } 185 186 return( mMBeanInfo ); 187 } 188 189 public Object 190 getAttribute( final String name ) 191 throws AttributeNotFoundException 192 { 193 if ( ! mAttributes.containsKey( name ) ) 194 { 195 throw new AttributeNotFoundException ( name ); 196 } 197 198 return( mAttributes.get( name ) ); 199 } 200 201 public AttributeList 202 getAttributes( final String [] names ) 203 { 204 final AttributeList attrs = new AttributeList (); 205 206 if ( names != null ) 207 { 208 for( int i = 0; i < names.length; ++i ) 209 { 210 try 211 { 212 final Object result = getAttribute( names[ i ] ); 213 attrs.add( new Attribute ( names[ i ], result ) ); 214 } 215 catch( AttributeNotFoundException e ) 216 { 217 } 218 } 219 } 220 221 return attrs; 222 } 223 224 public AttributeList 225 setAttributes( final AttributeList attrs ) 226 { 227 throw new RuntimeException ( 228 "TestDummy: setAttributes() not yet implemented" ); 229 } 230 231 public Object 232 invoke( 233 final String methodName, 234 final Object [] args, 235 final String [] signature ) 236 { 237 Object result = null; 238 final int numArgs = args == null ? 0 : args.length; 239 240 if ( "addAttribute".equals( methodName ) && numArgs == 2 ) 241 { 242 addAttribute( (String )args[ 0 ], (Object )args[ 1 ] ); 243 } 244 else if ( "removeAttribute".equals( methodName ) && numArgs == 1 ) 245 { 246 removeAttribute( (String )args[ 0 ] ); 247 } 248 else if ( numArgs == 2 && 249 "emitNotifications".equals( methodName ) ) 250 { 251 final String type = (String )args[0]; 252 final int howMany = (Integer )args[1]; 253 result = emitNotifications( type, howMany ); 254 } 255 else 256 { 257 throw new RuntimeException ( "invoke: no such method " + methodName ); 258 } 259 return result; 260 } 261 262 public void 263 setAttribute( final String name, final Object value ) 264 { 265 debug( "setAttribute" + name + "=" + value ); 266 267 addAttribute( name, value ); 268 } 269 270 public void 271 setAttribute( final Attribute attr ) 272 { 273 setAttribute( attr.getName(), attr.getValue() ); 274 } 275 276 protected ObjectName 277 preRegisterModifyName( 278 final MBeanServer server, 279 final ObjectName nameIn ) 280 { 281 final String EXTRA = ""; 283 284 final ObjectName nameOut = Util.newObjectName( nameIn.toString() + EXTRA ); 285 286 return( nameOut ); 287 } 288 289 290 public ObjectName 291 preRegister( 292 final MBeanServer server, 293 final ObjectName nameIn) 294 throws Exception 295 { 296 mServer = server; 297 mSelfObjectName = preRegisterModifyName( server, nameIn ); 298 299 return( mSelfObjectName ); 300 } 301 302 public void 303 postRegister( Boolean registrationSucceeded ) 304 { 305 } 306 307 public void 308 preDeregister() 309 { 310 } 311 312 public void 313 postDeregister() 314 { 315 } 316 317 318 public long 319 emitNotifications( 320 final String notifType, 321 final int howMany ) 322 { 323 final NotificationBuilder builder = 324 new NotificationBuilder( notifType, mSelfObjectName ); 325 326 final long start = System.currentTimeMillis(); 327 328 for( int i = 0; i < howMany; ++i ) 329 { 330 final Notification notif = builder.buildNew( "test Notification" ); 331 332 sendNotification( notif ); 333 } 334 335 final long elapsed = System.currentTimeMillis() - start; 336 return elapsed; 337 } 338 339 public void 340 addNotificationListener(final NotificationListener listener ) 341 { 342 mBroadcaster.addNotificationListener( listener, null, null ); 343 } 344 345 public void 346 addNotificationListener( 347 final NotificationListener listener, 348 final NotificationFilter filter, 349 final Object handback) 350 { 351 mBroadcaster.addNotificationListener( listener, filter, handback ); 352 } 353 354 public void 355 removeNotificationListener( final NotificationListener listener) 356 throws ListenerNotFoundException 357 { 358 mBroadcaster.removeNotificationListener( listener ); 359 } 360 361 public void 362 removeNotificationListener( 363 final NotificationListener listener, 364 final NotificationFilter filter, 365 final Object handback) 366 throws ListenerNotFoundException 367 { 368 mBroadcaster.removeNotificationListener( listener, filter, handback ); 369 } 370 371 public void 372 sendNotification( final Notification notification) 373 { 374 mBroadcaster.sendNotification( notification ); 375 } 376 377 public MBeanNotificationInfo [] 378 getNotificationInfo() 379 { 380 return new MBeanNotificationInfo [0]; 381 } 382 } 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | Popular Tags |