001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.util;
023:
024: import javax.management.JMException;
025: import javax.management.ListenerNotFoundException;
026: import javax.management.MBeanNotificationInfo;
027: import javax.management.Notification;
028: import javax.management.NotificationEmitter;
029: import javax.management.NotificationFilter;
030: import javax.management.NotificationListener;
031:
032: import org.jboss.logging.Logger;
033: import org.jboss.mx.notification.ListenerRegistration;
034: import org.jboss.mx.notification.ListenerRegistry;
035:
036: import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
037:
038: /**
039: * A helper class for notification broadcasters/emitters
040: *
041: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
042: * @author <a href="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
043: * @version $Revision: 57200 $
044: */
045: public class JBossNotificationBroadcasterSupport implements
046: NotificationEmitter {
047: /** The log */
048: private static final Logger log = Logger
049: .getLogger(JBossNotificationBroadcasterSupport.class);
050:
051: /** No notifications is the default */
052: private static final MBeanNotificationInfo[] NO_NOTIFICATIONS = new MBeanNotificationInfo[0];
053:
054: /** The registered listeners */
055: private ListenerRegistry registry = new ListenerRegistry();
056:
057: /** Sequence number for jmx notifications we send out */
058: private SynchronizedLong sequenceNumber = new SynchronizedLong(0);
059:
060: /**
061: * Construct the new notification broadcaster support object
062: */
063: public JBossNotificationBroadcasterSupport() {
064: }
065:
066: public void addNotificationListener(NotificationListener listener,
067: NotificationFilter filter, Object handback) {
068: try {
069: registry.add(listener, filter, handback);
070: } catch (JMException e) {
071: // This shouldn't happen?
072: throw new RuntimeException(e.toString());
073: }
074: }
075:
076: public void removeNotificationListener(NotificationListener listener)
077: throws ListenerNotFoundException {
078: registry.remove(listener);
079: }
080:
081: public void removeNotificationListener(
082: NotificationListener listener, NotificationFilter filter,
083: Object handback) throws ListenerNotFoundException {
084: registry.remove(listener, filter, handback);
085: }
086:
087: public MBeanNotificationInfo[] getNotificationInfo() {
088: return NO_NOTIFICATIONS;
089: }
090:
091: public void sendNotification(Notification notification) {
092: ListenerRegistry.ListenerRegistrationIterator iterator = registry
093: .iterator();
094: while (iterator.hasNext()) {
095: ListenerRegistration registration = iterator
096: .nextRegistration();
097: NotificationFilter filter = registration.getFilter();
098: if (filter == null)
099: handleNotification(registration.getListener(),
100: notification, registration.getHandback());
101: else if (filter.isNotificationEnabled(notification))
102: handleNotification(registration.getListener(),
103: notification, registration.getHandback());
104: }
105: }
106:
107: /**
108: * Handle the notification, the default implementation is to synchronously invoke the listener.
109: *
110: * @param listener the listener to notify
111: * @param notification the notification
112: * @param handback the handback object
113: */
114: public void handleNotification(NotificationListener listener,
115: Notification notification, Object handback) {
116: try {
117: listener.handleNotification(notification, handback);
118: } catch (Throwable ignored) {
119: log.debug("Ignored unhandled throwable from listener",
120: ignored);
121: }
122: }
123:
124: /**
125: * The <code>nextNotificationSequenceNumber</code> method returns
126: * the next sequence number for use in notifications.
127: *
128: * @return a <code>long</code> value
129: */
130: public long nextNotificationSequenceNumber() {
131: return sequenceNumber.increment();
132: }
133: }
|