KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jmx > JmxInvocationHandler


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jmx;
31
32 import javax.management.Attribute JavaDoc;
33 import javax.management.MBeanServer JavaDoc;
34 import javax.management.NotificationEmitter JavaDoc;
35 import javax.management.NotificationFilter JavaDoc;
36 import javax.management.NotificationListener JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38 import java.lang.reflect.Array JavaDoc;
39 import java.lang.reflect.InvocationHandler JavaDoc;
40 import java.lang.reflect.Method JavaDoc;
41 import java.lang.reflect.Proxy JavaDoc;
42
43 /**
44  * Proxy hander for mbeans.
45  */

46 public class JmxInvocationHandler implements InvocationHandler JavaDoc {
47   private MBeanServer JavaDoc _server;
48   private ClassLoader JavaDoc _loader;
49   private ObjectName JavaDoc _name;
50
51   /**
52    * Creates the invocation handler.
53    */

54   public JmxInvocationHandler(MBeanServer JavaDoc mbeanServer,
55                   ClassLoader JavaDoc loader,
56                   ObjectName JavaDoc objectName)
57   {
58     _server = mbeanServer;
59     _loader = loader;
60     _name = objectName;
61   }
62
63   /**
64    * Creates a new proxy instance.
65    */

66   public static Object JavaDoc newProxyInstance(MBeanServer JavaDoc server,
67                     ClassLoader JavaDoc loader,
68                     ObjectName JavaDoc objectName,
69                     Class JavaDoc interfaceClass,
70                     boolean notificationBroadcaster)
71   {
72     Class JavaDoc []interfaces;
73
74     if (notificationBroadcaster)
75       interfaces = new Class JavaDoc[] { interfaceClass, NotificationEmitter JavaDoc.class };
76     else
77       interfaces = new Class JavaDoc[] { interfaceClass };
78
79     JmxInvocationHandler handler;
80     
81     handler = new JmxInvocationHandler(server, loader, objectName);
82     
83     return Proxy.newProxyInstance(interfaceClass.getClassLoader(),
84                   interfaces,
85                   handler);
86   }
87
88   /**
89    * Handles the object invocation.
90    *
91    * @param proxy the proxy object to invoke
92    * @param method the method to call
93    * @param args the arguments to the proxy object
94    */

95   public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc []args)
96     throws Throwable JavaDoc
97   {
98     String JavaDoc methodName = method.getName();
99     Class JavaDoc []params = method.getParameterTypes();
100
101     // equals and hashCode are special cased
102
if (methodName.equals("equals") &&
103         params.length == 1 && params[0].equals(Object JavaDoc.class)) {
104       Object JavaDoc value = args[0];
105       if (value == null || ! Proxy.isProxyClass(value.getClass()))
106         return Boolean.FALSE;
107
108       JmxInvocationHandler handler;
109
110       handler = (JmxInvocationHandler) Proxy.getInvocationHandler(value);
111
112       return new Boolean JavaDoc(_name.equals(handler._name));
113     }
114     else if (methodName.equals("hashCode") && params.length == 0)
115       return new Integer JavaDoc(_name.hashCode());
116
117     int len = methodName.length();
118     String JavaDoc attrName;
119
120     Class JavaDoc returnType = method.getReturnType();
121     
122     if (params.length == 0 && methodName.startsWith("get") && len > 3) {
123       attrName = methodName.substring(3);
124
125       return marshall(_server.getAttribute(_name, attrName), returnType);
126     }
127     else if (params.length == 1 && returnType.equals(void.class) &&
128          methodName.startsWith("set") && len > 3) {
129       attrName = methodName.substring(3);
130
131       Attribute JavaDoc attr = new Attribute JavaDoc(attrName, args[0]);
132
133       _server.setAttribute(_name, attr);
134
135       return null;
136     }
137     else if (methodName.equals("addNotificationListener")) {
138       if (args.length != 3) {
139       }
140       else if (args[0] instanceof NotificationListener JavaDoc) {
141     _server.addNotificationListener(_name,
142                     (NotificationListener JavaDoc) args[0],
143                     (NotificationFilter JavaDoc) args[1],
144                     args[2]);
145     return null;
146       }
147       else if (args[0] instanceof ObjectName JavaDoc) {
148     _server.addNotificationListener(_name,
149                     (ObjectName JavaDoc) args[0],
150                     (NotificationFilter JavaDoc) args[1],
151                     args[2]);
152     return null;
153       }
154     }
155     else if (methodName.equals("removeNotificationListener")) {
156       if (args.length == 3) {
157     if (args[0] instanceof NotificationListener JavaDoc) {
158       _server.removeNotificationListener(_name,
159                          (NotificationListener JavaDoc) args[0],
160                          (NotificationFilter JavaDoc) args[1],
161                          args[2]);
162       return null;
163     }
164     else if (args[0] instanceof ObjectName JavaDoc) {
165       _server.removeNotificationListener(_name,
166                          (ObjectName JavaDoc) args[0],
167                          (NotificationFilter JavaDoc) args[1],
168                          args[2]);
169       return null;
170     }
171       }
172       else if (args.length == 1) {
173     if (args[0] instanceof NotificationListener JavaDoc) {
174       _server.removeNotificationListener(_name,
175                          (NotificationListener JavaDoc) args[0]);
176       return null;
177     }
178     else if (args[0] instanceof ObjectName JavaDoc) {
179       _server.removeNotificationListener(_name, (ObjectName JavaDoc) args[0]);
180
181       return null;
182     }
183       }
184     }
185
186     String JavaDoc []sig = new String JavaDoc[params.length];
187
188     for (int i = 0; i < sig.length; i++)
189       sig[i] = params[i].getName();
190
191     Object JavaDoc value = _server.invoke(_name, methodName, args, sig);
192
193     return marshall(value, returnType);
194   }
195
196   private Object JavaDoc marshall(Object JavaDoc value, Class JavaDoc retType)
197   {
198     if (retType == null || value == null ||
199     retType.isAssignableFrom(value.getClass()))
200       return value;
201     
202     if (value instanceof ObjectName JavaDoc) {
203       ObjectName JavaDoc name = (ObjectName JavaDoc) value;
204
205       Object JavaDoc proxy = Jmx.find(name, _loader, _server);
206
207       if (retType.isInstance(proxy))
208     return proxy;
209       else
210     return value;
211     }
212     else if (value instanceof ObjectName JavaDoc[] && retType.isArray()) {
213       Class JavaDoc type = retType.getComponentType();
214       ObjectName JavaDoc []names = (ObjectName JavaDoc []) value;
215       
216       Object JavaDoc proxies = Array.newInstance(type, names.length);
217
218       for (int i = 0; i < names.length; i++) {
219     Object JavaDoc proxy = Jmx.find(names[i], _loader, _server);
220
221     if (proxy == null) {
222       Array.set(proxies, i, null);
223       continue;
224     }
225     
226     if (! type.isInstance(proxy))
227       return value;
228
229     Array.set(proxies, i, Jmx.find(names[i], _loader, _server));
230       }
231                        
232       return proxies;
233     }
234
235     return value;
236   }
237 }
238
Popular Tags