KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > mx > Listener


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.mx;
10
11 import javax.management.InstanceNotFoundException JavaDoc;
12 import javax.management.ListenerNotFoundException JavaDoc;
13 import javax.management.MBeanServer JavaDoc;
14 import javax.management.Notification JavaDoc;
15 import javax.management.NotificationFilter JavaDoc;
16 import javax.management.NotificationListener JavaDoc;
17 import javax.management.ObjectName JavaDoc;
18
19 import org.apache.log4j.Logger;
20
21 /**
22  * Helper class that filter and listen notifications and help to registration.
23  *
24  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
25  * @version $Revision: 1.2 $
26  */

27 public class Listener implements NotificationFilter JavaDoc, NotificationListener JavaDoc
28 {
29
30    protected final MBeanServer JavaDoc server;
31    protected final Logger log;
32
33    public Listener(MBeanServer JavaDoc server)
34    {
35       if (server == null)
36       {
37          throw new IllegalArgumentException JavaDoc("server must not be null");
38       }
39       this.server = server;
40       this.log = Logger.getLogger(getClass());
41    }
42
43    public void register(ObjectName JavaDoc broadcaster)
44    {
45       register(broadcaster, null);
46    }
47
48    public void register(ObjectName JavaDoc broadcaster, Object JavaDoc handback)
49    {
50       try
51       {
52          if (broadcaster == null)
53          {
54             throw new IllegalArgumentException JavaDoc("Broadcaster is null");
55          }
56
57          log.debug("Register notifications on MBean " + broadcaster.getCanonicalName());
58          server.addNotificationListener(broadcaster, this, this, handback);
59       }
60       catch (InstanceNotFoundException JavaDoc e)
61       {
62          throw new ListenerException(e);
63       }
64    }
65
66    public void unregister(ObjectName JavaDoc broadcaster)
67    {
68       try
69       {
70          if (broadcaster == null)
71          {
72             throw new IllegalArgumentException JavaDoc("Broadcaster is null");
73          }
74
75          log.debug("Unregister notifications on MBean " + broadcaster.getCanonicalName());
76          server.removeNotificationListener(broadcaster, this);
77       }
78       catch (InstanceNotFoundException JavaDoc e)
79       {
80          throw new ListenerException(e);
81       }
82       catch (ListenerNotFoundException JavaDoc e)
83       {
84          throw new ListenerException(e);
85       }
86    }
87
88    /**
89     * Returns true by default.
90     */

91    public boolean isNotificationEnabled(Notification JavaDoc notification)
92    {
93       return true;
94    }
95
96    /**
97     * Does not perform anything by default.
98     */

99    public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
100    {
101    }
102 }
103
Popular Tags