1 4 package org.oddjob.jmx.server; 5 6 import javax.management.MBeanAttributeInfo ; 7 import javax.management.MBeanException ; 8 import javax.management.MBeanNotificationInfo ; 9 import javax.management.MBeanOperationInfo ; 10 import javax.management.Notification ; 11 import javax.management.ReflectionException ; 12 13 import org.oddjob.Stateful; 14 import org.oddjob.state.JobStateEvent; 15 import org.oddjob.state.JobStateListener; 16 17 public class StatefulInfo implements InterfaceInfo { 18 19 public static final String STATE_CHANGE_NOTIF_TYPE = "org.oddjob.statechange"; 20 21 public Class interfaceClass() { 22 return Stateful.class; 23 } 24 25 public MBeanAttributeInfo [] getMBeanAttributeInfo() { 26 return new MBeanAttributeInfo [0]; 27 } 28 29 public MBeanOperationInfo [] getMBeanOperationInfo() { 30 return new MBeanOperationInfo [0]; 31 } 32 33 public MBeanNotificationInfo [] getMBeanNotificationInfo() { 34 35 MBeanNotificationInfo [] nInfo = new MBeanNotificationInfo [] { 36 new MBeanNotificationInfo ( 37 new String [] { STATE_CHANGE_NOTIF_TYPE }, 38 Notification .class.getName(), 39 "State change notification.") }; 40 return nInfo; 41 } 42 43 public InterfaceHandler attach(Object target, 44 OddjobMBean ojmb) { 45 Stateful stateful = (Stateful) target; 46 ServerStateHelper stateHelper = new ServerStateHelper(stateful, ojmb); 47 stateful.addJobStateListener(stateHelper); 48 return stateHelper; 49 } 50 51 class ServerStateHelper implements JobStateListener, InterfaceHandler { 52 53 private final Stateful stateful; 54 private final OddjobMBean ojmb; 55 56 57 private Notification lastNotification; 58 59 ServerStateHelper(Stateful stateful, 60 OddjobMBean ojmb) { 61 this.stateful = stateful; 62 this.ojmb = ojmb; 63 } 64 65 70 public void jobStateChange(final JobStateEvent event) { 71 ojmb.runSynchronized(new Runnable () { 72 public void run() { 73 JobStateEvent newEvent = new JobStateEvent(new Object (), event 75 .getJobState(), event.getTime(), event.getException()); 76 Notification notification = new Notification ( 77 STATE_CHANGE_NOTIF_TYPE, ojmb, ojmb.getNextNotificationNumber()); 78 notification.setUserData(newEvent); 79 ojmb.sendNotification(notification); 80 lastNotification = notification; 81 } 82 }); 83 } 84 85 public Object invoke(String actionName, Object [] params, String [] signature) throws MBeanException , ReflectionException { 86 throw new ReflectionException ( 87 new IllegalStateException ("invoked for an unknown method."), 88 actionName); 89 } 90 91 public Notification [] getLastNotifications() { 92 return new Notification [] { lastNotification }; 93 } 94 95 public void destroy() { 96 stateful.removeJobStateListener(this); 97 } 98 } 99 } 100 | Popular Tags |