1 package org.oddjob.jmx.server; 2 3 import javax.management.MBeanAttributeInfo ; 4 import javax.management.MBeanException ; 5 import javax.management.MBeanNotificationInfo ; 6 import javax.management.MBeanOperationInfo ; 7 import javax.management.MBeanParameterInfo ; 8 import javax.management.Notification ; 9 import javax.management.ReflectionException ; 10 11 import org.oddjob.Iconic; 12 import org.oddjob.images.IconEvent; 13 import org.oddjob.images.IconListener; 14 import org.oddjob.images.IconTip; 15 16 20 21 public class IconicInfo implements InterfaceInfo { 22 23 public static final String ICON_CHANGED_NOTIF_TYPE = "org.oddjob.iconchanged"; 24 25 public Class interfaceClass() { 26 return Iconic.class; 27 } 28 29 public MBeanAttributeInfo [] getMBeanAttributeInfo() { 30 return new MBeanAttributeInfo [0]; 31 } 32 33 public MBeanOperationInfo [] getMBeanOperationInfo() { 34 return new MBeanOperationInfo [] { 35 new MBeanOperationInfo ("iconForId", 36 "Retrieve an Icon and ToolTip.", 37 new MBeanParameterInfo [] { new MBeanParameterInfo ("iconId", 38 String .class.getName(), "The icon id.") }, 39 IconTip.class.getName(), MBeanOperationInfo.INFO) }; 40 } 41 42 public MBeanNotificationInfo [] getMBeanNotificationInfo() { 43 return new MBeanNotificationInfo [] { 44 new MBeanNotificationInfo ( 45 new String [] { ICON_CHANGED_NOTIF_TYPE }, 46 Notification .class.getName(), 47 "Icon changed notification.") }; 48 } 49 50 51 public InterfaceHandler attach(Object target, OddjobMBean ojmb) { 52 Iconic iconic = (Iconic) target; 53 ServerIconicHelper iconicHelper = new ServerIconicHelper(iconic, ojmb); 54 iconic.addIconListener(iconicHelper); 55 return iconicHelper; 56 } 57 58 class ServerIconicHelper implements IconListener, InterfaceHandler { 59 60 private final Iconic iconic; 61 private final OddjobMBean ojmb; 62 63 64 private Notification lastNotification; 65 66 ServerIconicHelper(Iconic iconic, OddjobMBean ojmb) { 67 this.iconic = iconic; 68 this.ojmb = ojmb; 69 } 70 71 public void iconEvent(final IconEvent event) { 72 ojmb.runSynchronized(new Runnable () { 73 public void run() { 74 IconEvent newEvent = new IconEvent(new Object (), event.getIconId()); 76 Notification notification = new Notification ( 77 ICON_CHANGED_NOTIF_TYPE, ojmb, ojmb.getNextNotificationNumber()); 78 notification.setUserData(newEvent); 79 ojmb.sendNotification(notification); 80 lastNotification = notification; 81 } 82 }); 83 } 84 85 public Notification [] getLastNotifications() { 86 return new Notification [] { lastNotification }; 87 } 88 89 public Object invoke(String actionName, Object [] params, String [] signature) throws MBeanException , ReflectionException { 90 if ("iconForId".equals(actionName) && signature.length == 1 91 && signature[0].equals(String .class.getName())) { 92 return iconic.iconForId((String ) params[0]); 93 } 94 throw new ReflectionException ( 95 new IllegalStateException ("invoked for an unknown method."), 96 actionName); 97 } 98 99 public void destroy() { 100 iconic.removeIconListener(this); 101 } 102 } 103 104 } | Popular Tags |