KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > compliance > core > notification > JBossNotificationBroadcasterSupport


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package test.compliance.core.notification;
8
9 import javax.management.JMException JavaDoc;
10 import javax.management.ListenerNotFoundException JavaDoc;
11 import javax.management.MBeanNotificationInfo JavaDoc;
12 import javax.management.Notification JavaDoc;
13 import javax.management.NotificationEmitter JavaDoc;
14 import javax.management.NotificationFilter JavaDoc;
15 import javax.management.NotificationListener JavaDoc;
16 import org.jboss.logging.Logger;
17 import org.jboss.mx.notification.ListenerRegistration;
18 import org.jboss.mx.notification.ListenerRegistry;
19
20 /**
21  * A helper class for notification broadcasters/emitters
22  *
23  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
24  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
25  * @version $Revision: 33149 $
26  */

27 public class JBossNotificationBroadcasterSupport
28       implements NotificationEmitter JavaDoc
29 {
30    /**
31     * The log
32     */

33    private static final Logger log = Logger.getLogger(JBossNotificationBroadcasterSupport.class);
34
35    /**
36     * No notifications is the default
37     */

38    private static final MBeanNotificationInfo JavaDoc[] NO_NOTIFICATIONS = new MBeanNotificationInfo JavaDoc[0];
39
40    /**
41     * The registered listeners
42     */

43    private ListenerRegistry registry = new ListenerRegistry();
44
45    /**
46     * Construct the new notification broadcaster support object
47     */

48    public JBossNotificationBroadcasterSupport()
49    {
50    }
51
52    public void addNotificationListener(NotificationListener JavaDoc listener,
53                                        NotificationFilter JavaDoc filter,
54                                        Object JavaDoc handback)
55    {
56       try
57       {
58          registry.add(listener, filter, handback);
59       }
60       catch(JMException JavaDoc e)
61       {
62          // This shouldn't happen?
63
throw new RuntimeException JavaDoc(e.toString());
64       }
65    }
66
67    public void removeNotificationListener(NotificationListener JavaDoc listener)
68          throws ListenerNotFoundException JavaDoc
69    {
70       registry.remove(listener);
71    }
72
73    public void removeNotificationListener(NotificationListener JavaDoc listener,
74                                           NotificationFilter JavaDoc filter,
75                                           Object JavaDoc handback)
76          throws ListenerNotFoundException JavaDoc
77    {
78       registry.remove(listener, filter, handback);
79    }
80
81    public MBeanNotificationInfo JavaDoc[] getNotificationInfo()
82    {
83       return NO_NOTIFICATIONS;
84    }
85
86    public void sendNotification(Notification JavaDoc notification)
87    {
88       ListenerRegistry.ListenerRegistrationIterator iterator = registry.iterator();
89       while(iterator.hasNext())
90       {
91          ListenerRegistration registration = iterator.nextRegistration();
92          NotificationFilter JavaDoc 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    /**
105     * Handle the notification, the default implementation is to synchronously invoke the listener.
106     *
107     * @param listener the listener to notify
108     * @param notification the notification
109     * @param handback the handback object
110     */

111    public void handleNotification(NotificationListener JavaDoc listener,
112                                   Notification JavaDoc notification,
113                                   Object JavaDoc handback)
114    {
115       try
116       {
117          listener.handleNotification(notification, handback);
118       }
119       catch(Throwable JavaDoc ignored)
120       {
121          log.debug("Ignored unhandled throwable from listener", ignored);
122       }
123    }
124 }
125
Popular Tags