1 /* MBeanServerInvocationHandler.java -- Provides a proxy for a bean.
2 Copyright (C) 2007 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.management;
40 import gnu.javax.management.Translator;
42 import java.lang.reflect.InvocationHandler;
43 import java.lang.reflect.Method;
44 import java.lang.reflect.Proxy;
48 * Provides a proxy for a management bean. The methods
49 * of the given interface are fulfilled by redirecting the
50 * calls over an {@link MBeanServerConnection} to the bean
51 * specified by the supplied {@link ObjectName}.
54 * The {@link java.lang.reflect.InvocationHandler} also makes
55 * provision for {@link MXBean}s by providing type conversion
56 * according to the rules defined for these beans. The input
57 * parameters are converted to their equivalent open type before
58 * calling the method, and then the return value is converted
59 * back from its open type to the appropriate Java type. For
60 * example, a method that takes an {@link Enum} as input and
61 * returns a {@link java.util.List} will have the input value
62 * converted from an {@link Enum} to a {@link String}, while
63 * the return value will be converted from its return type
64 * (an appropriately typed array) to a {@link java.util.List}.
67 * The proxy has special cases for the {@link Object#equals(Object)},
68 * {@link Object#hashCode()} and {@link Object#toString()} methods.
69 * Unless they are specified explictly by the interface, the
70 * following behaviour is provided for these methods by the proxy:
73 * <li><code>equals(Object)</code> returns true if the other object
74 * is an {@link MBeanServerInvocationHandler} with the same
75 * {@link MBeanServerConnection} and {@link ObjectName}. If an
76 * interface class was specified on construction for one of the
77 * proxies, then the same class must have also been specified
79 * <li><code>hashCode()</code> returns the same value for
80 * equivalent proxies.</li>
81 * <li><code>toString()</code> returns a textual representation
85 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
88 public class MBeanServerInvocationHandler
89 implements InvocationHandler
93 * The connection used to make the calls.
95 private MBeanServerConnection conn;
98 * The name of the bean to perform operations on.
100 private ObjectName name;
103 * True if this proxy is for an {@link MXBean}.
105 private boolean mxBean;
108 * The interface class associated with the bean.
110 private Class<?> iface;
113 * Constructs a new {@link MBeanServerInvocationHandler}
114 * which forwards methods to the supplied bean via the
115 * given {@link MBeanServerConnection}. This constructor
116 * is used in preference to
117 * {@link JMX#newMBeanProxy(MBeanServerConnection, ObjectName,
118 * Class<T>)} if you wish to make your own call to
119 * {@link java.lang.reflect.Proxy#newInstance(ClassLoader,
120 * Class[], java.lang.reflect.InvocationHandler)} with
121 * a different {@link ClassLoader}. Calling this constructor
122 * is equivalent to <code>MBeanServerInvocationHandler(conn,
123 * name, false)</code>. The other constructor should be used
124 * instead if the bean being proxied is an {@link MXBean}.
126 * @param conn the connection through which methods will
127 * be forwarded to the bean.
128 * @param name the name of the bean to use to provide the
131 public MBeanServerInvocationHandler(MBeanServerConnection conn,
134 this(conn, name, false);
138 * Constructs a new {@link MBeanServerInvocationHandler}
139 * which forwards methods to the supplied bean via the
140 * given {@link MBeanServerConnection}. This constructor
141 * is used in preference to
142 * {@link JMX#newMBeanProxy(MBeanServerConnection, ObjectName,
143 * Class<T>)} if you wish to make your own call to
144 * {@link java.lang.reflect.Proxy#newInstance(ClassLoader,
145 * Class[], java.lang.reflect.InvocationHandler)} with
146 * a different {@link ClassLoader}.
148 * @param conn the connection through which methods will
149 * be forwarded to the bean.
150 * @param name the name of the bean to use to provide the
152 * @param mxBean true if the bean being proxied is an
156 public MBeanServerInvocationHandler(MBeanServerConnection conn,
157 ObjectName name, boolean mxBean)
161 this.mxBean = mxBean;
165 * Returns the connection through which the calls to the bean
168 * @return the connection being used to forward the calls to
172 public MBeanServerConnection getMBeanServerConnection()
178 * Returns the name of the bean to which method calls are made.
180 * @return the bean which provides the actual method calls.
183 public ObjectName getObjectName()
189 * Called by the proxy class whenever a method is called. The method
190 * is emulated by retrieving an attribute from, setting an attribute on
191 * or invoking a method on the server connection as required. Translation
192 * between the Java data types supplied as arguments to the open types used
193 * by the bean is provided, as well as translation of the return value back
194 * in to the appropriate Java type if the bean is an {@link MXBean}.
196 * @param proxy the proxy on which the method was called.
197 * @param method the method which was called.
198 * @param args the arguments supplied to the method.
199 * @return the return value from the method.
200 * @throws Throwable if an exception is thrown in performing the
203 public Object invoke(Object proxy, Method method, Object[] args)
206 String mName = method.getName();
207 Class<?> proxyClass = proxy.getClass();
208 if (mName.equals("toString"))
210 if (inInterface(mName, proxyClass))
211 return conn.invoke(name,mName,null,null);
213 return proxyClass.getName() + "[name=" + name
214 + ", conn=" + conn + "]";
216 if (mName.equals("hashCode"))
218 if (inInterface(mName, proxyClass))
219 return conn.invoke(name,mName,null,null);
221 return conn.hashCode() + name.hashCode()
222 + (iface == null ? 0 : iface.hashCode());
224 if (mName.equals("equals"))
226 if (inInterface(mName, proxyClass, Object.class))
227 return conn.invoke(name,mName,new Object[]{args[0]},
228 new String[]{"java.lang.Object"});
231 if (args[0].getClass() != proxy.getClass())
233 InvocationHandler ih = Proxy.getInvocationHandler(args[0]);
234 if (ih instanceof MBeanServerInvocationHandler)
236 MBeanServerInvocationHandler h =
237 (MBeanServerInvocationHandler) ih;
238 return conn.equals(h.getMBeanServerConnection())
239 && name.equals(h.getObjectName())
240 && (iface == null ? h.iface == null
241 : iface.equals(h.iface));
246 if (NotificationEmitter.class.isAssignableFrom(proxyClass))
248 if (mName.equals("addNotificationListener"))
250 conn.addNotificationListener(name,
251 (NotificationListener) args[0],
252 (NotificationFilter) args[1],
256 if (mName.equals("getNotificationInfo"))
257 return conn.getMBeanInfo(name).getNotifications();
258 if (mName.equals("removeNotificationListener"))
260 if (args.length == 1)
261 conn.removeNotificationListener(name,
262 (NotificationListener)
265 conn.removeNotificationListener(name,
266 (NotificationListener)
278 sigs = new String[args.length];
279 for (int a = 0; a < args.length; ++a)
280 sigs[a] = args[a].getClass().getName();
282 String attrib = null;
283 if (mName.startsWith("get"))
284 attrib = mName.substring(3);
285 else if (mName.startsWith("is"))
286 attrib = mName.substring(2);
289 Object val = conn.getAttribute(name, attrib);
291 return Translator.toJava(val, method);
295 else if (mName.startsWith("set"))
299 arg = Translator.fromJava(args, method)[0];
302 conn.setAttribute(name, new Attribute(mName.substring(3), arg));
306 return Translator.toJava(conn.invoke(name, mName,
307 Translator.fromJava(args,method),
310 return conn.invoke(name, mName, args, sigs);
314 * Returns true if this is a proxy for an {@link MXBean}
315 * and conversions must be applied to input parameters
316 * and return types, according to the rules for such beans.
318 * @return true if this is a proxy for an {@link MXBean}.
321 public boolean isMXBean()
328 * Returns a proxy for the specified bean. A proxy object is created
329 * using <code>Proxy.newProxyInstance(iface.getClassLoader(),
330 * new Class[] { iface }, handler)</code>. The
331 * {@link javax.management.NotificationEmitter} class is included as the
332 * second element of the array if <code>broadcaster</code> is true.
333 * <code>handler</code> refers to the invocation handler which forwards
334 * calls to the connection, which is created by a call to
335 * <code>new MBeanServerInvocationHandler(conn, name)</code>.
338 * <strong>Note</strong>: use of the proxy may result in
339 * {@link java.io.IOException}s from the underlying
340 * {@link MBeanServerConnection}.
341 * As of 1.6, the use of {@link JMX#newMBeanProxy(MBeanServerConnection,
342 * ObjectName,Class)} and {@link JMX#newMBeanProxy(MBeanServerConnection,
343 * ObjectName,Class,boolean)} is preferred.
346 * @param conn the server connection to use to access the bean.
347 * @param name the {@link javax.management.ObjectName} of the
348 * bean to provide a proxy for.
349 * @param iface the interface for the bean being proxied.
350 * @param broadcaster true if the proxy should implement
351 * {@link NotificationEmitter}.
352 * @return a proxy for the specified bean.
353 * @see JMX#newMBeanProxy(MBeanServerConnection,ObjectName,Class)
355 // Suppress warnings as we know an instance of T will be returned.
356 @SuppressWarnings("unchecked")
357 public static <T> T newProxyInstance(MBeanServerConnection conn,
358 ObjectName name, Class<T> iface,
362 return (T) Proxy.newProxyInstance(iface.getClassLoader(),
364 NotificationEmitter.class },
365 new MBeanServerInvocationHandler(conn,name));
367 return (T) Proxy.newProxyInstance(iface.getClassLoader(),
368 new Class[] { iface },
369 new MBeanServerInvocationHandler(conn,name));
373 * Returns true if the specified method is specified
374 * by one of the proxy's interfaces.
376 * @param name the name of the method to search for.
377 * @param proxyClass the class of the proxy.
378 * @param args the arguments to the method.
379 * @return true if one of the interfaces specifies the
382 private boolean inInterface(String name, Class<?> proxyClass,
385 for (Class<?> iface : proxyClass.getInterfaces())
389 iface.getMethod(name, args);
392 catch (NoSuchMethodException e)
394 /* Ignored; this interface doesn't specify