KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > server > interceptor > NotificationListenerMBeanServerInterceptor


1 /*
2  * Copyright (C) MX4J.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.server.interceptor;
10
11 import javax.management.ListenerNotFoundException JavaDoc;
12 import javax.management.Notification JavaDoc;
13 import javax.management.NotificationFilter JavaDoc;
14 import javax.management.NotificationListener JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16
17 import mx4j.server.MBeanMetaData;
18
19 /**
20  * Interceptor that takes care of replacing the source of Notifications to the
21  * ObjectName of the NotificationBroadcaster that emitted it.
22  *
23  * @author <a HREF="mailto:biorn_steedom@users.sourceforge.net">Simone Bordet</a>
24  * @version $Revision: 1.9 $
25  */

26 public class NotificationListenerMBeanServerInterceptor extends DefaultMBeanServerInterceptor
27 {
28    private static class ListenerWrapper implements NotificationListener JavaDoc
29    {
30       private final NotificationListener JavaDoc listener;
31       private final ObjectName JavaDoc objectName;
32
33       private ListenerWrapper(NotificationListener JavaDoc listener, ObjectName JavaDoc name)
34       {
35          this.listener = listener;
36          this.objectName = name;
37       }
38
39       public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
40       {
41          // The JMX spec does not specify how to change the source to be the ObjectName
42
// of the broadcaster. If we serialize the calls to the listeners, then it's
43
// possible to change the source and restore it back to the old value before
44
// calling the next listener; but if we want to support concurrent calls
45
// to the listeners, this is not possible. Here I chose to support concurrent
46
// calls so I change the value once and I never restore it.
47
Object JavaDoc src = notification.getSource();
48          if (!(src instanceof ObjectName JavaDoc))
49          {
50             // Change the source to be the ObjectName of the notification broadcaster
51
// if we are not already an ObjectName (compliant with RI behaviour)
52
notification.setSource(objectName);
53          }
54
55          // Notify the real listener
56
NotificationListener JavaDoc listener = getTargetListener();
57          listener.handleNotification(notification, handback);
58       }
59
60       private NotificationListener JavaDoc getTargetListener()
61       {
62          return listener;
63       }
64
65       public int hashCode()
66       {
67          return getTargetListener().hashCode();
68       }
69
70       public boolean equals(Object JavaDoc obj)
71       {
72          if (obj == null) return false;
73          if (obj == this) return true;
74
75          try
76          {
77             ListenerWrapper other = (ListenerWrapper)obj;
78             return getTargetListener().equals(other.getTargetListener());
79          }
80          catch (ClassCastException JavaDoc ignored)
81          {
82          }
83          return false;
84       }
85
86       public String JavaDoc toString()
87       {
88          return getTargetListener().toString();
89       }
90    }
91
92    public String JavaDoc getType()
93    {
94       return "notificationlistener";
95    }
96
97    public void addNotificationListener(MBeanMetaData metadata, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback)
98    {
99       if (isEnabled())
100       {
101          ListenerWrapper wrapper = new ListenerWrapper(listener, metadata.getObjectName());
102          super.addNotificationListener(metadata, wrapper, filter, handback);
103       }
104       else
105       {
106          super.addNotificationListener(metadata, listener, filter, handback);
107       }
108    }
109
110    public void removeNotificationListener(MBeanMetaData metadata, NotificationListener JavaDoc listener) throws ListenerNotFoundException JavaDoc
111    {
112       if (isEnabled())
113       {
114          ListenerWrapper wrapper = new ListenerWrapper(listener, metadata.getObjectName());
115          super.removeNotificationListener(metadata, wrapper);
116       }
117       else
118       {
119          super.removeNotificationListener(metadata, listener);
120       }
121    }
122
123    public void removeNotificationListener(MBeanMetaData metadata, NotificationListener JavaDoc listener, NotificationFilter JavaDoc filter, Object JavaDoc handback) throws ListenerNotFoundException JavaDoc
124    {
125       if (isEnabled())
126       {
127          ListenerWrapper wrapper = new ListenerWrapper(listener, metadata.getObjectName());
128          super.removeNotificationListener(metadata, wrapper, filter, handback);
129       }
130       else
131       {
132          super.removeNotificationListener(metadata, listener, filter, handback);
133       }
134    }
135 }
136
Popular Tags