1 7 package test.compliance.core.notification; 8 9 import javax.management.JMException ; 10 import javax.management.ListenerNotFoundException ; 11 import javax.management.MBeanNotificationInfo ; 12 import javax.management.Notification ; 13 import javax.management.NotificationEmitter ; 14 import javax.management.NotificationFilter ; 15 import javax.management.NotificationListener ; 16 import org.jboss.logging.Logger; 17 import org.jboss.mx.notification.ListenerRegistration; 18 import org.jboss.mx.notification.ListenerRegistry; 19 20 27 public class JBossNotificationBroadcasterSupport 28 implements NotificationEmitter 29 { 30 33 private static final Logger log = Logger.getLogger(JBossNotificationBroadcasterSupport.class); 34 35 38 private static final MBeanNotificationInfo [] NO_NOTIFICATIONS = new MBeanNotificationInfo [0]; 39 40 43 private ListenerRegistry registry = new ListenerRegistry(); 44 45 48 public JBossNotificationBroadcasterSupport() 49 { 50 } 51 52 public void addNotificationListener(NotificationListener listener, 53 NotificationFilter filter, 54 Object handback) 55 { 56 try 57 { 58 registry.add(listener, filter, handback); 59 } 60 catch(JMException e) 61 { 62 throw new RuntimeException (e.toString()); 64 } 65 } 66 67 public void removeNotificationListener(NotificationListener listener) 68 throws ListenerNotFoundException 69 { 70 registry.remove(listener); 71 } 72 73 public void removeNotificationListener(NotificationListener listener, 74 NotificationFilter filter, 75 Object handback) 76 throws ListenerNotFoundException 77 { 78 registry.remove(listener, filter, handback); 79 } 80 81 public MBeanNotificationInfo [] getNotificationInfo() 82 { 83 return NO_NOTIFICATIONS; 84 } 85 86 public void sendNotification(Notification notification) 87 { 88 ListenerRegistry.ListenerRegistrationIterator iterator = registry.iterator(); 89 while(iterator.hasNext()) 90 { 91 ListenerRegistration registration = iterator.nextRegistration(); 92 NotificationFilter filter = registration.getFilter(); 93 if(filter == null) 94 { 95 handleNotification(registration.getListener(), notification, registration.getHandback()); 96 } 97 else if(filter.isNotificationEnabled(notification)) 98 { 99 handleNotification(registration.getListener(), notification, registration.getHandback()); 100 } 101 } 102 } 103 104 111 public void handleNotification(NotificationListener listener, 112 Notification notification, 113 Object handback) 114 { 115 try 116 { 117 listener.handleNotification(notification, handback); 118 } 119 catch(Throwable ignored) 120 { 121 log.debug("Ignored unhandled throwable from listener", ignored); 122 } 123 } 124 } 125 | Popular Tags |