package org.jboss.mx.util;
import javax.management.JMException;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanNotificationInfo;
import javax.management.Notification;
import javax.management.NotificationEmitter;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import org.jboss.logging.Logger;
import org.jboss.mx.notification.ListenerRegistry;
import org.jboss.mx.notification.ListenerRegistration;
public class JBossNotificationBroadcasterSupport
implements NotificationEmitter
{
private static final Logger log = Logger.getLogger(JBossNotificationBroadcasterSupport.class);
private static final MBeanNotificationInfo[] NO_NOTIFICATIONS = new MBeanNotificationInfo[0];
private ListenerRegistry registry = new ListenerRegistry();
public JBossNotificationBroadcasterSupport()
{
}
public void addNotificationListener(NotificationListener listener,
NotificationFilter filter,
Object handback)
{
try
{
registry.add(listener, filter, handback);
}
catch (JMException e)
{
throw new RuntimeException(e.toString());
}
}
public void removeNotificationListener(NotificationListener listener)
throws ListenerNotFoundException
{
registry.remove(listener);
}
public void removeNotificationListener(NotificationListener listener,
NotificationFilter filter,
Object handback)
throws ListenerNotFoundException
{
registry.remove(listener, filter, handback);
}
public MBeanNotificationInfo[] getNotificationInfo()
{
return NO_NOTIFICATIONS;
}
public void sendNotification(Notification notification)
{
ListenerRegistry.ListenerRegistrationIterator iterator = registry.iterator();
while (iterator.hasNext())
{
ListenerRegistration registration = iterator.nextRegistration();
NotificationFilter filter = registration.getFilter();
if (filter == null)
handleNotification(registration.getListener(), notification, registration.getHandback());
else if (filter.isNotificationEnabled(notification))
handleNotification(registration.getListener(), notification, registration.getHandback());
}
}
public void handleNotification(NotificationListener listener,
Notification notification,
Object handback)
{
try
{
listener.handleNotification(notification, handback);
}
catch (Throwable ignored)
{
log.debug("Ignored unhandled throwable from listener", ignored);
}
}
}