KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > notification > NotificationListenerProxy


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.notification;
23
24 import java.lang.reflect.InvocationHandler JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.lang.reflect.Proxy JavaDoc;
27
28 import javax.management.Notification JavaDoc;
29 import javax.management.NotificationListener JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31
32 /**
33  * A notification listener used to forward notifications to listeners
34  * added through the mbean server.<p>
35  *
36  * The original source is replaced with the object name.
37  *
38  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
39  * @author <a HREF="mailto:telrod@e2technologies.net">Tom Elrod</a>.
40  * @version $Revision: 37459 $
41  */

42 public class NotificationListenerProxy
43    implements InvocationHandler JavaDoc
44 {
45    // Constants ---------------------------------------------------
46

47    // Attributes --------------------------------------------------
48

49     /**
50     * The original listener
51     */

52    private NotificationListener JavaDoc listener;
53
54    /**
55     * The object name we are proxying
56     */

57    private ObjectName JavaDoc name;
58
59    /**
60     * The implementation method for NotificationListener
61     * that we want to intercept so that we can set the source
62     * of the Notification object.
63     */

64    private static final String JavaDoc METHODNAME = "handleNotification";
65
66    /**
67     * Calculated hascode
68     */

69    private final Integer JavaDoc hashCode;
70
71    // Static ------------------------------------------------------
72

73      public static Object JavaDoc newInstance(ObjectName JavaDoc name,
74                                       NotificationListener JavaDoc listener)
75      {
76          // Using set so don't have interface duplicates (which shouldn't happen anyways)
77
java.util.HashSet JavaDoc set = new java.util.HashSet JavaDoc();
78
79          // Walk the class heirarchy tree and get all interfaces.
80
Class JavaDoc currentClass = listener.getClass();
81          while(currentClass != null)
82          {
83              Class JavaDoc[] classInterfaces = currentClass.getInterfaces();
84              for(int i = 0; i < classInterfaces.length; i++)
85              {
86                 set.add(classInterfaces[i]);
87              }
88              currentClass = currentClass.getSuperclass();
89          }
90          Class JavaDoc[] interfaces = new Class JavaDoc[set.size()];
91          interfaces = (Class JavaDoc[])set.toArray(interfaces);
92
93          return Proxy.newProxyInstance(listener.getClass().getClassLoader(),
94                                        interfaces,
95                                        new NotificationListenerProxy(name, listener));
96      }
97
98    // Constructors ------------------------------------------------
99

100    /**
101     * Create a new Notification Listener Proxy
102     *
103     * @param name the object name
104     * @param listener the original listener
105     */

106    public NotificationListenerProxy(ObjectName JavaDoc name,
107                                     NotificationListener JavaDoc listener)
108    {
109       this.name = name;
110       this.listener = listener;
111       this.hashCode = new Integer JavaDoc(System.identityHashCode(this));
112
113       // Could add code to set the METHODNAME variable based
114
// on the method signature names from the actual listener
115
// interface passed (which would be done dynamically so would
116
// not be hard-coded), in case the NotificationListener
117
// method signature changes.
118
}
119
120    // Public ------------------------------------------------------
121

122     // implementation InvocationHandler
123
public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
124         throws Throwable JavaDoc
125     {
126         String JavaDoc localMethodName = method.getName();
127         // check to see if calling handleNotification() method
128
if(localMethodName.equals(METHODNAME))
129         {
130             for(int x = 0; x < args.length; x++)
131             {
132                 if(args[x] instanceof Notification JavaDoc)
133                 {
134                     // Forward the notification with the object name as source
135
// FIXME: This overwrites the original source, there is no way
136
// to put it back with the current spec
137
((Notification JavaDoc)args[x]).setSource(name);
138                 }
139             }
140         }
141         else if (localMethodName.equals("hashCode"))
142         {
143             return proxyHashCode(proxy);
144         }
145         else if (localMethodName.equals("equals"))
146         {
147             return proxyEquals(proxy, args[0]);
148         }
149         else if (localMethodName.equals("toString"))
150         {
151             return proxyToString(proxy);
152         }
153         return method.invoke(listener, args);
154     }
155
156     protected Integer JavaDoc proxyHashCode(Object JavaDoc proxy)
157     {
158         return this.hashCode;
159     }
160
161     protected Boolean JavaDoc proxyEquals(Object JavaDoc proxy, Object JavaDoc other)
162     {
163         return (proxy == other ? Boolean.TRUE : Boolean.FALSE);
164     }
165
166     protected String JavaDoc proxyToString(Object JavaDoc proxy)
167     {
168         return proxy.getClass().getName() + '@' + Integer.toHexString(proxy.hashCode());
169     }
170
171    // overrides ---------------------------------------------------
172

173    // Protected ---------------------------------------------------
174

175    // Private -----------------------------------------------------
176

177    // Inner classes -----------------------------------------------
178
}
179
Popular Tags