2
|
1 |
/*
|
715
|
2 |
* Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.management;
|
|
27 |
|
|
28 |
import com.sun.jmx.mbeanserver.MXBeanProxy;
|
|
29 |
|
|
30 |
import java.lang.ref.WeakReference;
|
|
31 |
import java.lang.reflect.InvocationHandler;
|
|
32 |
import java.lang.reflect.Method;
|
|
33 |
import java.lang.reflect.Proxy;
|
|
34 |
import java.util.Arrays;
|
|
35 |
import java.util.WeakHashMap;
|
687
|
36 |
import javax.management.openmbean.MXBeanMappingFactory;
|
|
37 |
|
|
38 |
import static javax.management.JMX.MBeanOptions;
|
2
|
39 |
|
|
40 |
/**
|
|
41 |
* <p>{@link InvocationHandler} that forwards methods in an MBean's
|
|
42 |
* management interface through the MBean server to the MBean.</p>
|
|
43 |
*
|
|
44 |
* <p>Given an {@link MBeanServerConnection}, the {@link ObjectName}
|
|
45 |
* of an MBean within that MBean server, and a Java interface
|
|
46 |
* <code>Intf</code> that describes the management interface of the
|
|
47 |
* MBean using the patterns for a Standard MBean or an MXBean, this
|
|
48 |
* class can be used to construct a proxy for the MBean. The proxy
|
|
49 |
* implements the interface <code>Intf</code> such that all of its
|
|
50 |
* methods are forwarded through the MBean server to the MBean.</p>
|
|
51 |
*
|
|
52 |
* <p>If the {@code InvocationHandler} is for an MXBean, then the parameters of
|
|
53 |
* a method are converted from the type declared in the MXBean
|
|
54 |
* interface into the corresponding mapped type, and the return value
|
|
55 |
* is converted from the mapped type into the declared type. For
|
|
56 |
* example, with the method<br>
|
|
57 |
|
|
58 |
* {@code public List<String> reverse(List<String> list);}<br>
|
|
59 |
|
|
60 |
* and given that the mapped type for {@code List<String>} is {@code
|
|
61 |
* String[]}, a call to {@code proxy.reverse(someList)} will convert
|
|
62 |
* {@code someList} from a {@code List<String>} to a {@code String[]},
|
|
63 |
* call the MBean operation {@code reverse}, then convert the returned
|
|
64 |
* {@code String[]} into a {@code List<String>}.</p>
|
|
65 |
*
|
|
66 |
* <p>The method Object.toString(), Object.hashCode(), or
|
|
67 |
* Object.equals(Object), when invoked on a proxy using this
|
|
68 |
* invocation handler, is forwarded to the MBean server as a method on
|
|
69 |
* the proxied MBean only if it appears in one of the proxy's
|
|
70 |
* interfaces. For a proxy created with {@link
|
|
71 |
* JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class)
|
|
72 |
* JMX.newMBeanProxy} or {@link
|
|
73 |
* JMX#newMXBeanProxy(MBeanServerConnection, ObjectName, Class)
|
|
74 |
* JMX.newMXBeanProxy}, this means that the method must appear in the
|
|
75 |
* Standard MBean or MXBean interface. Otherwise these methods have
|
|
76 |
* the following behavior:
|
|
77 |
* <ul>
|
|
78 |
* <li>toString() returns a string representation of the proxy
|
|
79 |
* <li>hashCode() returns a hash code for the proxy such
|
|
80 |
* that two equal proxies have the same hash code
|
|
81 |
* <li>equals(Object)
|
|
82 |
* returns true if and only if the Object argument is of the same
|
|
83 |
* proxy class as this proxy, with an MBeanServerInvocationHandler
|
|
84 |
* that has the same MBeanServerConnection and ObjectName; if one
|
|
85 |
* of the {@code MBeanServerInvocationHandler}s was constructed with
|
|
86 |
* a {@code Class} argument then the other must have been constructed
|
|
87 |
* with the same {@code Class} for {@code equals} to return true.
|
|
88 |
* </ul>
|
|
89 |
*
|
|
90 |
* @since 1.5
|
|
91 |
*/
|
|
92 |
public class MBeanServerInvocationHandler implements InvocationHandler {
|
|
93 |
/**
|
|
94 |
* <p>Invocation handler that forwards methods through an MBean
|
|
95 |
* server to a Standard MBean. This constructor may be called
|
|
96 |
* instead of relying on {@link
|
|
97 |
* JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class)
|
|
98 |
* JMX.newMBeanProxy}, for instance if you need to supply a
|
|
99 |
* different {@link ClassLoader} to {@link Proxy#newProxyInstance
|
|
100 |
* Proxy.newProxyInstance}.</p>
|
|
101 |
*
|
|
102 |
* <p>This constructor is not appropriate for an MXBean. Use
|
|
103 |
* {@link #MBeanServerInvocationHandler(MBeanServerConnection,
|
|
104 |
* ObjectName, boolean)} for that. This constructor is equivalent
|
|
105 |
* to {@code new MBeanServerInvocationHandler(connection,
|
|
106 |
* objectName, false)}.</p>
|
|
107 |
*
|
|
108 |
* @param connection the MBean server connection through which all
|
|
109 |
* methods of a proxy using this handler will be forwarded.
|
|
110 |
*
|
|
111 |
* @param objectName the name of the MBean within the MBean server
|
|
112 |
* to which methods will be forwarded.
|
|
113 |
*/
|
|
114 |
public MBeanServerInvocationHandler(MBeanServerConnection connection,
|
|
115 |
ObjectName objectName) {
|
|
116 |
|
687
|
117 |
this(connection, objectName, null);
|
2
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* <p>Invocation handler that can forward methods through an MBean
|
|
122 |
* server to a Standard MBean or MXBean. This constructor may be called
|
|
123 |
* instead of relying on {@link
|
|
124 |
* JMX#newMXBeanProxy(MBeanServerConnection, ObjectName, Class)
|
|
125 |
* JMX.newMXBeanProxy}, for instance if you need to supply a
|
|
126 |
* different {@link ClassLoader} to {@link Proxy#newProxyInstance
|
|
127 |
* Proxy.newProxyInstance}.</p>
|
|
128 |
*
|
|
129 |
* @param connection the MBean server connection through which all
|
|
130 |
* methods of a proxy using this handler will be forwarded.
|
|
131 |
*
|
|
132 |
* @param objectName the name of the MBean within the MBean server
|
|
133 |
* to which methods will be forwarded.
|
|
134 |
*
|
|
135 |
* @param isMXBean if true, the proxy is for an {@link MXBean}, and
|
|
136 |
* appropriate mappings will be applied to method parameters and return
|
|
137 |
* values.
|
|
138 |
*
|
|
139 |
* @since 1.6
|
|
140 |
*/
|
|
141 |
public MBeanServerInvocationHandler(MBeanServerConnection connection,
|
|
142 |
ObjectName objectName,
|
|
143 |
boolean isMXBean) {
|
687
|
144 |
this(connection, objectName, isMXBean ? MBeanOptions.MXBEAN : null);
|
|
145 |
}
|
|
146 |
|
|
147 |
public MBeanServerInvocationHandler(MBeanServerConnection connection,
|
|
148 |
ObjectName objectName,
|
|
149 |
MBeanOptions options) {
|
|
150 |
if (options == null)
|
|
151 |
options = new MBeanOptions();
|
2
|
152 |
if (connection == null) {
|
|
153 |
throw new IllegalArgumentException("Null connection");
|
|
154 |
}
|
|
155 |
if (objectName == null) {
|
|
156 |
throw new IllegalArgumentException("Null object name");
|
|
157 |
}
|
|
158 |
this.connection = connection;
|
|
159 |
this.objectName = objectName;
|
687
|
160 |
this.options = options.canonical();
|
2
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* <p>The MBean server connection through which the methods of
|
|
165 |
* a proxy using this handler are forwarded.</p>
|
|
166 |
*
|
|
167 |
* @return the MBean server connection.
|
|
168 |
*
|
|
169 |
* @since 1.6
|
|
170 |
*/
|
|
171 |
public MBeanServerConnection getMBeanServerConnection() {
|
|
172 |
return connection;
|
|
173 |
}
|
|
174 |
|
|
175 |
/**
|
|
176 |
* <p>The name of the MBean within the MBean server to which methods
|
|
177 |
* are forwarded.
|
|
178 |
*
|
|
179 |
* @return the object name.
|
|
180 |
*
|
|
181 |
* @since 1.6
|
|
182 |
*/
|
|
183 |
public ObjectName getObjectName() {
|
|
184 |
return objectName;
|
|
185 |
}
|
|
186 |
|
|
187 |
/**
|
|
188 |
* <p>If true, the proxy is for an MXBean, and appropriate mappings
|
|
189 |
* are applied to method parameters and return values.
|
|
190 |
*
|
|
191 |
* @return whether the proxy is for an MXBean.
|
|
192 |
*
|
|
193 |
* @since 1.6
|
|
194 |
*/
|
|
195 |
public boolean isMXBean() {
|
687
|
196 |
return options.isMXBean();
|
|
197 |
}
|
|
198 |
|
|
199 |
/**
|
|
200 |
* <p>Return the {@link MBeanOptions} used for this proxy.</p>
|
|
201 |
*
|
|
202 |
* @return the MBeanOptions.
|
|
203 |
*/
|
|
204 |
public MBeanOptions getMBeanOptions() {
|
|
205 |
return options.uncanonical();
|
2
|
206 |
}
|
|
207 |
|
|
208 |
/**
|
|
209 |
* <p>Return a proxy that implements the given interface by
|
|
210 |
* forwarding its methods through the given MBean server to the
|
|
211 |
* named MBean. As of 1.6, the methods {@link
|
|
212 |
* JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class)} and
|
|
213 |
* {@link JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class,
|
|
214 |
* boolean)} are preferred to this method.</p>
|
|
215 |
*
|
|
216 |
* <p>This method is equivalent to {@link Proxy#newProxyInstance
|
|
217 |
* Proxy.newProxyInstance}<code>(interfaceClass.getClassLoader(),
|
|
218 |
* interfaces, handler)</code>. Here <code>handler</code> is the
|
|
219 |
* result of {@link #MBeanServerInvocationHandler new
|
|
220 |
* MBeanServerInvocationHandler(connection, objectName)}, and
|
|
221 |
* <code>interfaces</code> is an array that has one element if
|
|
222 |
* <code>notificationBroadcaster</code> is false and two if it is
|
|
223 |
* true. The first element of <code>interfaces</code> is
|
|
224 |
* <code>interfaceClass</code> and the second, if present, is
|
|
225 |
* <code>NotificationEmitter.class</code>.
|
|
226 |
*
|
|
227 |
* @param connection the MBean server to forward to.
|
|
228 |
* @param objectName the name of the MBean within
|
|
229 |
* <code>connection</code> to forward to.
|
|
230 |
* @param interfaceClass the management interface that the MBean
|
|
231 |
* exports, which will also be implemented by the returned proxy.
|
|
232 |
* @param notificationBroadcaster make the returned proxy
|
|
233 |
* implement {@link NotificationEmitter} by forwarding its methods
|
|
234 |
* via <code>connection</code>. A call to {@link
|
|
235 |
* NotificationBroadcaster#addNotificationListener} on the proxy will
|
|
236 |
* result in a call to {@link
|
|
237 |
* MBeanServerConnection#addNotificationListener(ObjectName,
|
|
238 |
* NotificationListener, NotificationFilter, Object)}, and likewise
|
|
239 |
* for the other methods of {@link NotificationBroadcaster} and {@link
|
|
240 |
* NotificationEmitter}.
|
|
241 |
*
|
|
242 |
* @param <T> allows the compiler to know that if the {@code
|
|
243 |
* interfaceClass} parameter is {@code MyMBean.class}, for example,
|
|
244 |
* then the return type is {@code MyMBean}.
|
|
245 |
*
|
|
246 |
* @return the new proxy instance.
|
|
247 |
*
|
|
248 |
* @see JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class)
|
|
249 |
*/
|
|
250 |
public static <T> T newProxyInstance(MBeanServerConnection connection,
|
|
251 |
ObjectName objectName,
|
|
252 |
Class<T> interfaceClass,
|
|
253 |
boolean notificationBroadcaster) {
|
|
254 |
final InvocationHandler handler =
|
|
255 |
new MBeanServerInvocationHandler(connection, objectName);
|
1510
|
256 |
final Class<?>[] interfaces;
|
2
|
257 |
if (notificationBroadcaster) {
|
|
258 |
interfaces =
|
1510
|
259 |
new Class<?>[] {interfaceClass, NotificationEmitter.class};
|
2
|
260 |
} else
|
1510
|
261 |
interfaces = new Class<?>[] {interfaceClass};
|
2
|
262 |
|
|
263 |
Object proxy =
|
|
264 |
Proxy.newProxyInstance(interfaceClass.getClassLoader(),
|
|
265 |
interfaces,
|
|
266 |
handler);
|
|
267 |
return interfaceClass.cast(proxy);
|
|
268 |
}
|
|
269 |
|
|
270 |
public Object invoke(Object proxy, Method method, Object[] args)
|
|
271 |
throws Throwable {
|
1510
|
272 |
final Class<?> methodClass = method.getDeclaringClass();
|
2
|
273 |
|
|
274 |
if (methodClass.equals(NotificationBroadcaster.class)
|
|
275 |
|| methodClass.equals(NotificationEmitter.class))
|
|
276 |
return invokeBroadcasterMethod(proxy, method, args);
|
|
277 |
|
|
278 |
// local or not: equals, toString, hashCode
|
|
279 |
if (shouldDoLocally(proxy, method))
|
|
280 |
return doLocally(proxy, method, args);
|
|
281 |
|
|
282 |
try {
|
687
|
283 |
if (isMXBean()) {
|
2
|
284 |
MXBeanProxy p = findMXBeanProxy(methodClass);
|
|
285 |
return p.invoke(connection, objectName, method, args);
|
|
286 |
} else {
|
|
287 |
final String methodName = method.getName();
|
1510
|
288 |
final Class<?>[] paramTypes = method.getParameterTypes();
|
|
289 |
final Class<?> returnType = method.getReturnType();
|
2
|
290 |
|
|
291 |
/* Inexplicably, InvocationHandler specifies that args is null
|
|
292 |
when the method takes no arguments rather than a
|
|
293 |
zero-length array. */
|
|
294 |
final int nargs = (args == null) ? 0 : args.length;
|
|
295 |
|
|
296 |
if (methodName.startsWith("get")
|
|
297 |
&& methodName.length() > 3
|
|
298 |
&& nargs == 0
|
|
299 |
&& !returnType.equals(Void.TYPE)) {
|
|
300 |
return connection.getAttribute(objectName,
|
|
301 |
methodName.substring(3));
|
|
302 |
}
|
|
303 |
|
|
304 |
if (methodName.startsWith("is")
|
|
305 |
&& methodName.length() > 2
|
|
306 |
&& nargs == 0
|
|
307 |
&& (returnType.equals(Boolean.TYPE)
|
|
308 |
|| returnType.equals(Boolean.class))) {
|
|
309 |
return connection.getAttribute(objectName,
|
|
310 |
methodName.substring(2));
|
|
311 |
}
|
|
312 |
|
|
313 |
if (methodName.startsWith("set")
|
|
314 |
&& methodName.length() > 3
|
|
315 |
&& nargs == 1
|
|
316 |
&& returnType.equals(Void.TYPE)) {
|
|
317 |
Attribute attr = new Attribute(methodName.substring(3), args[0]);
|
|
318 |
connection.setAttribute(objectName, attr);
|
|
319 |
return null;
|
|
320 |
}
|
|
321 |
|
|
322 |
final String[] signature = new String[paramTypes.length];
|
|
323 |
for (int i = 0; i < paramTypes.length; i++)
|
|
324 |
signature[i] = paramTypes[i].getName();
|
|
325 |
return connection.invoke(objectName, methodName,
|
|
326 |
args, signature);
|
|
327 |
}
|
|
328 |
} catch (MBeanException e) {
|
|
329 |
throw e.getTargetException();
|
|
330 |
} catch (RuntimeMBeanException re) {
|
|
331 |
throw re.getTargetException();
|
|
332 |
} catch (RuntimeErrorException rre) {
|
|
333 |
throw rre.getTargetError();
|
|
334 |
}
|
|
335 |
/* The invoke may fail because it can't get to the MBean, with
|
|
336 |
one of the these exceptions declared by
|
|
337 |
MBeanServerConnection.invoke:
|
|
338 |
- RemoteException: can't talk to MBeanServer;
|
|
339 |
- InstanceNotFoundException: objectName is not registered;
|
|
340 |
- ReflectionException: objectName is registered but does not
|
|
341 |
have the method being invoked.
|
|
342 |
In all of these cases, the exception will be wrapped by the
|
|
343 |
proxy mechanism in an UndeclaredThrowableException unless
|
|
344 |
it happens to be declared in the "throws" clause of the
|
|
345 |
method being invoked on the proxy.
|
|
346 |
*/
|
|
347 |
}
|
|
348 |
|
687
|
349 |
private MXBeanProxy findMXBeanProxy(Class<?> mxbeanInterface) {
|
|
350 |
MXBeanMappingFactory mappingFactory = options.getMXBeanMappingFactory();
|
2
|
351 |
synchronized (mxbeanProxies) {
|
687
|
352 |
ClassToProxy classToProxy = mxbeanProxies.get(mappingFactory);
|
|
353 |
if (classToProxy == null) {
|
|
354 |
classToProxy = new ClassToProxy();
|
|
355 |
mxbeanProxies.put(mappingFactory, classToProxy);
|
2
|
356 |
}
|
687
|
357 |
WeakReference<MXBeanProxy> wr = classToProxy.get(mxbeanInterface);
|
|
358 |
MXBeanProxy p;
|
|
359 |
if (wr != null) {
|
|
360 |
p = wr.get();
|
|
361 |
if (p != null)
|
|
362 |
return p;
|
|
363 |
}
|
|
364 |
p = new MXBeanProxy(mxbeanInterface, mappingFactory);
|
|
365 |
classToProxy.put(mxbeanInterface, new WeakReference<MXBeanProxy>(p));
|
2
|
366 |
return p;
|
|
367 |
}
|
|
368 |
}
|
687
|
369 |
private static final WeakHashMap<MXBeanMappingFactory, ClassToProxy>
|
|
370 |
mxbeanProxies = newWeakHashMap();
|
|
371 |
private static class ClassToProxy
|
|
372 |
extends WeakHashMap<Class<?>, WeakReference<MXBeanProxy>> {}
|
|
373 |
|
|
374 |
private static <K, V> WeakHashMap<K, V> newWeakHashMap() {
|
|
375 |
return new WeakHashMap<K, V>();
|
|
376 |
}
|
2
|
377 |
|
|
378 |
private Object invokeBroadcasterMethod(Object proxy, Method method,
|
|
379 |
Object[] args) throws Exception {
|
|
380 |
final String methodName = method.getName();
|
|
381 |
final int nargs = (args == null) ? 0 : args.length;
|
|
382 |
|
|
383 |
if (methodName.equals("addNotificationListener")) {
|
|
384 |
/* The various throws of IllegalArgumentException here
|
|
385 |
should not happen, since we know what the methods in
|
|
386 |
NotificationBroadcaster and NotificationEmitter
|
|
387 |
are. */
|
|
388 |
if (nargs != 3) {
|
|
389 |
final String msg =
|
|
390 |
"Bad arg count to addNotificationListener: " + nargs;
|
|
391 |
throw new IllegalArgumentException(msg);
|
|
392 |
}
|
|
393 |
/* Other inconsistencies will produce ClassCastException
|
|
394 |
below. */
|
|
395 |
|
|
396 |
NotificationListener listener = (NotificationListener) args[0];
|
|
397 |
NotificationFilter filter = (NotificationFilter) args[1];
|
|
398 |
Object handback = args[2];
|
|
399 |
connection.addNotificationListener(objectName,
|
|
400 |
listener,
|
|
401 |
filter,
|
|
402 |
handback);
|
|
403 |
return null;
|
|
404 |
|
|
405 |
} else if (methodName.equals("removeNotificationListener")) {
|
|
406 |
|
|
407 |
/* NullPointerException if method with no args, but that
|
|
408 |
shouldn't happen because removeNL does have args. */
|
|
409 |
NotificationListener listener = (NotificationListener) args[0];
|
|
410 |
|
|
411 |
switch (nargs) {
|
|
412 |
case 1:
|
|
413 |
connection.removeNotificationListener(objectName, listener);
|
|
414 |
return null;
|
|
415 |
|
|
416 |
case 3:
|
|
417 |
NotificationFilter filter = (NotificationFilter) args[1];
|
|
418 |
Object handback = args[2];
|
|
419 |
connection.removeNotificationListener(objectName,
|
|
420 |
listener,
|
|
421 |
filter,
|
|
422 |
handback);
|
|
423 |
return null;
|
|
424 |
|
|
425 |
default:
|
|
426 |
final String msg =
|
|
427 |
"Bad arg count to removeNotificationListener: " + nargs;
|
|
428 |
throw new IllegalArgumentException(msg);
|
|
429 |
}
|
|
430 |
|
|
431 |
} else if (methodName.equals("getNotificationInfo")) {
|
|
432 |
|
|
433 |
if (args != null) {
|
|
434 |
throw new IllegalArgumentException("getNotificationInfo has " +
|
|
435 |
"args");
|
|
436 |
}
|
|
437 |
|
|
438 |
MBeanInfo info = connection.getMBeanInfo(objectName);
|
|
439 |
return info.getNotifications();
|
|
440 |
|
|
441 |
} else {
|
|
442 |
throw new IllegalArgumentException("Bad method name: " +
|
|
443 |
methodName);
|
|
444 |
}
|
|
445 |
}
|
|
446 |
|
|
447 |
private boolean shouldDoLocally(Object proxy, Method method) {
|
|
448 |
final String methodName = method.getName();
|
|
449 |
if ((methodName.equals("hashCode") || methodName.equals("toString"))
|
|
450 |
&& method.getParameterTypes().length == 0
|
|
451 |
&& isLocal(proxy, method))
|
|
452 |
return true;
|
|
453 |
if (methodName.equals("equals")
|
|
454 |
&& Arrays.equals(method.getParameterTypes(),
|
1510
|
455 |
new Class<?>[] {Object.class})
|
2
|
456 |
&& isLocal(proxy, method))
|
|
457 |
return true;
|
|
458 |
return false;
|
|
459 |
}
|
|
460 |
|
|
461 |
private Object doLocally(Object proxy, Method method, Object[] args) {
|
|
462 |
final String methodName = method.getName();
|
|
463 |
|
|
464 |
if (methodName.equals("equals")) {
|
|
465 |
|
|
466 |
if (this == args[0]) {
|
|
467 |
return true;
|
|
468 |
}
|
|
469 |
|
|
470 |
if (!(args[0] instanceof Proxy)) {
|
|
471 |
return false;
|
|
472 |
}
|
|
473 |
|
|
474 |
final InvocationHandler ihandler =
|
|
475 |
Proxy.getInvocationHandler(args[0]);
|
|
476 |
|
|
477 |
if (ihandler == null ||
|
|
478 |
!(ihandler instanceof MBeanServerInvocationHandler)) {
|
|
479 |
return false;
|
|
480 |
}
|
|
481 |
|
|
482 |
final MBeanServerInvocationHandler handler =
|
|
483 |
(MBeanServerInvocationHandler)ihandler;
|
|
484 |
|
|
485 |
return connection.equals(handler.connection) &&
|
|
486 |
objectName.equals(handler.objectName) &&
|
|
487 |
proxy.getClass().equals(args[0].getClass());
|
|
488 |
} else if (methodName.equals("toString")) {
|
687
|
489 |
return (isMXBean() ? "MX" : "M") + "BeanProxy(" +
|
2
|
490 |
connection + "[" + objectName + "])";
|
|
491 |
} else if (methodName.equals("hashCode")) {
|
|
492 |
return objectName.hashCode()+connection.hashCode();
|
|
493 |
}
|
|
494 |
|
|
495 |
throw new RuntimeException("Unexpected method name: " + methodName);
|
|
496 |
}
|
|
497 |
|
|
498 |
private static boolean isLocal(Object proxy, Method method) {
|
|
499 |
final Class<?>[] interfaces = proxy.getClass().getInterfaces();
|
|
500 |
if(interfaces == null) {
|
|
501 |
return true;
|
|
502 |
}
|
|
503 |
|
|
504 |
final String methodName = method.getName();
|
|
505 |
final Class<?>[] params = method.getParameterTypes();
|
|
506 |
for (Class<?> intf : interfaces) {
|
|
507 |
try {
|
|
508 |
intf.getMethod(methodName, params);
|
|
509 |
return false; // found method in one of our interfaces
|
|
510 |
} catch (NoSuchMethodException nsme) {
|
|
511 |
// OK.
|
|
512 |
}
|
|
513 |
}
|
|
514 |
|
|
515 |
return true; // did not find in any interface
|
|
516 |
}
|
|
517 |
|
|
518 |
private final MBeanServerConnection connection;
|
|
519 |
private final ObjectName objectName;
|
687
|
520 |
private final MBeanOptions options;
|
2
|
521 |
}
|