KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > util > JBossNotificationBroadcasterSupport


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mx.util;
23
24 import javax.management.JMException JavaDoc;
25 import javax.management.ListenerNotFoundException JavaDoc;
26 import javax.management.MBeanNotificationInfo JavaDoc;
27 import javax.management.Notification JavaDoc;
28 import javax.management.NotificationEmitter JavaDoc;
29 import javax.management.NotificationFilter JavaDoc;
30 import javax.management.NotificationListener JavaDoc;
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 /**
39  * A helper class for notification broadcasters/emitters
40  *
41  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
42  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
43  * @version $Revision: 37459 $
44  */

45 public class JBossNotificationBroadcasterSupport implements NotificationEmitter JavaDoc
46 {
47    /** The log */
48    private static final Logger log = Logger.getLogger(JBossNotificationBroadcasterSupport.class);
49    
50    /** No notifications is the default */
51    private static final MBeanNotificationInfo JavaDoc[] NO_NOTIFICATIONS = new MBeanNotificationInfo JavaDoc[0];
52
53    /** The registered listeners */
54    private ListenerRegistry registry = new ListenerRegistry();
55    
56    /** Sequence number for jmx notifications we send out */
57    private SynchronizedLong sequenceNumber = new SynchronizedLong(0);
58
59    /**
60     * Construct the new notification broadcaster support object
61     */

62    public JBossNotificationBroadcasterSupport()
63    {
64    }
65
66    public void addNotificationListener(NotificationListener JavaDoc listener,
67                                        NotificationFilter JavaDoc filter,
68                                        Object JavaDoc handback)
69    {
70       try
71       {
72          registry.add(listener, filter, handback);
73       }
74       catch (JMException JavaDoc e)
75       {
76          // This shouldn't happen?
77
throw new RuntimeException JavaDoc(e.toString());
78       }
79    }
80
81    public void removeNotificationListener(NotificationListener JavaDoc listener)
82        throws ListenerNotFoundException JavaDoc
83    {
84       registry.remove(listener);
85    }
86
87    public void removeNotificationListener(NotificationListener JavaDoc listener,
88                                           NotificationFilter JavaDoc filter,
89                                           Object JavaDoc handback)
90       throws ListenerNotFoundException JavaDoc
91    {
92       registry.remove(listener, filter, handback);
93    }
94
95    public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
96    {
97       return NO_NOTIFICATIONS;
98    }
99
100    public void sendNotification(Notification JavaDoc notification)
101    {
102       ListenerRegistry.ListenerRegistrationIterator iterator = registry.iterator();
103       while (iterator.hasNext())
104       {
105          ListenerRegistration registration = iterator.nextRegistration();
106          NotificationFilter JavaDoc 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    /**
115     * Handle the notification, the default implementation is to synchronously invoke the listener.
116     *
117     * @param listener the listener to notify
118     * @param notification the notification
119     * @param handback the handback object
120     */

121    public void handleNotification(NotificationListener JavaDoc listener,
122                                      Notification JavaDoc notification,
123                                      Object JavaDoc handback)
124    {
125       try
126       {
127          listener.handleNotification(notification, handback);
128       }
129       catch (Throwable JavaDoc ignored)
130       {
131          log.debug("Ignored unhandled throwable from listener", ignored);
132       }
133    }
134    
135    /**
136     * The <code>nextNotificationSequenceNumber</code> method returns
137     * the next sequence number for use in notifications.
138     *
139     * @return a <code>long</code> value
140     */

141    public long nextNotificationSequenceNumber()
142    {
143       return sequenceNumber.increment();
144    }
145 }
146
Popular Tags