1 22 package org.jboss.mx.util; 23 24 import javax.management.JMException ; 25 import javax.management.ListenerNotFoundException ; 26 import javax.management.MBeanNotificationInfo ; 27 import javax.management.Notification ; 28 import javax.management.NotificationEmitter ; 29 import javax.management.NotificationFilter ; 30 import javax.management.NotificationListener ; 31 32 import org.jboss.logging.Logger; 33 import org.jboss.mx.notification.ListenerRegistration; 34 import org.jboss.mx.notification.ListenerRegistry; 35 36 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong; 37 38 45 public class JBossNotificationBroadcasterSupport implements NotificationEmitter 46 { 47 48 private static final Logger log = Logger.getLogger(JBossNotificationBroadcasterSupport.class); 49 50 51 private static final MBeanNotificationInfo [] NO_NOTIFICATIONS = new MBeanNotificationInfo [0]; 52 53 54 private ListenerRegistry registry = new ListenerRegistry(); 55 56 57 private SynchronizedLong sequenceNumber = new SynchronizedLong(0); 58 59 62 public JBossNotificationBroadcasterSupport() 63 { 64 } 65 66 public void addNotificationListener(NotificationListener listener, 67 NotificationFilter filter, 68 Object handback) 69 { 70 try 71 { 72 registry.add(listener, filter, handback); 73 } 74 catch (JMException e) 75 { 76 throw new RuntimeException (e.toString()); 78 } 79 } 80 81 public void removeNotificationListener(NotificationListener listener) 82 throws ListenerNotFoundException 83 { 84 registry.remove(listener); 85 } 86 87 public void removeNotificationListener(NotificationListener listener, 88 NotificationFilter filter, 89 Object handback) 90 throws ListenerNotFoundException 91 { 92 registry.remove(listener, filter, handback); 93 } 94 95 public MBeanNotificationInfo [] getNotificationInfo() 96 { 97 return NO_NOTIFICATIONS; 98 } 99 100 public void sendNotification(Notification notification) 101 { 102 ListenerRegistry.ListenerRegistrationIterator iterator = registry.iterator(); 103 while (iterator.hasNext()) 104 { 105 ListenerRegistration registration = iterator.nextRegistration(); 106 NotificationFilter filter = registration.getFilter(); 107 if (filter == null) 108 handleNotification(registration.getListener(), notification, registration.getHandback()); 109 else if (filter.isNotificationEnabled(notification)) 110 handleNotification(registration.getListener(), notification, registration.getHandback()); 111 } 112 } 113 114 121 public void handleNotification(NotificationListener listener, 122 Notification notification, 123 Object handback) 124 { 125 try 126 { 127 listener.handleNotification(notification, handback); 128 } 129 catch (Throwable ignored) 130 { 131 log.debug("Ignored unhandled throwable from listener", ignored); 132 } 133 } 134 135 141 public long nextNotificationSequenceNumber() 142 { 143 return sequenceNumber.increment(); 144 } 145 } 146 | Popular Tags |