1 8 9 package mx4j.server.interceptor; 10 11 import javax.management.ListenerNotFoundException ; 12 import javax.management.Notification ; 13 import javax.management.NotificationFilter ; 14 import javax.management.NotificationListener ; 15 import javax.management.ObjectName ; 16 17 import mx4j.server.MBeanMetaData; 18 19 26 public class NotificationListenerMBeanServerInterceptor extends DefaultMBeanServerInterceptor 27 { 28 private static class ListenerWrapper implements NotificationListener 29 { 30 private final NotificationListener listener; 31 private final ObjectName objectName; 32 33 private ListenerWrapper(NotificationListener listener, ObjectName name) 34 { 35 this.listener = listener; 36 this.objectName = name; 37 } 38 39 public void handleNotification(Notification notification, Object handback) 40 { 41 Object src = notification.getSource(); 48 if (!(src instanceof ObjectName )) 49 { 50 notification.setSource(objectName); 53 } 54 55 NotificationListener listener = getTargetListener(); 57 listener.handleNotification(notification, handback); 58 } 59 60 private NotificationListener getTargetListener() 61 { 62 return listener; 63 } 64 65 public int hashCode() 66 { 67 return getTargetListener().hashCode(); 68 } 69 70 public boolean equals(Object obj) 71 { 72 if (obj == null) return false; 73 if (obj == this) return true; 74 75 try 76 { 77 ListenerWrapper other = (ListenerWrapper)obj; 78 return getTargetListener().equals(other.getTargetListener()); 79 } 80 catch (ClassCastException ignored) 81 { 82 } 83 return false; 84 } 85 86 public String toString() 87 { 88 return getTargetListener().toString(); 89 } 90 } 91 92 public String getType() 93 { 94 return "notificationlistener"; 95 } 96 97 public void addNotificationListener(MBeanMetaData metadata, NotificationListener listener, NotificationFilter filter, Object handback) 98 { 99 if (isEnabled()) 100 { 101 ListenerWrapper wrapper = new ListenerWrapper(listener, metadata.getObjectName()); 102 super.addNotificationListener(metadata, wrapper, filter, handback); 103 } 104 else 105 { 106 super.addNotificationListener(metadata, listener, filter, handback); 107 } 108 } 109 110 public void removeNotificationListener(MBeanMetaData metadata, NotificationListener listener) throws ListenerNotFoundException 111 { 112 if (isEnabled()) 113 { 114 ListenerWrapper wrapper = new ListenerWrapper(listener, metadata.getObjectName()); 115 super.removeNotificationListener(metadata, wrapper); 116 } 117 else 118 { 119 super.removeNotificationListener(metadata, listener); 120 } 121 } 122 123 public void removeNotificationListener(MBeanMetaData metadata, NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException 124 { 125 if (isEnabled()) 126 { 127 ListenerWrapper wrapper = new ListenerWrapper(listener, metadata.getObjectName()); 128 super.removeNotificationListener(metadata, wrapper, filter, handback); 129 } 130 else 131 { 132 super.removeNotificationListener(metadata, listener, filter, handback); 133 } 134 } 135 } 136 | Popular Tags |