MBeanServerInvocationHandler.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* MBeanServerInvocationHandler.java -- Provides a proxy for a bean.
  2. Copyright (C) 2007 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.management;
  32. import gnu.javax.management.Translator;
  33. import java.lang.reflect.InvocationHandler;
  34. import java.lang.reflect.Method;
  35. import java.lang.reflect.Proxy;
  36. /**
  37. * <p>
  38. * Provides a proxy for a management bean. The methods
  39. * of the given interface are fulfilled by redirecting the
  40. * calls over an {@link MBeanServerConnection} to the bean
  41. * specified by the supplied {@link ObjectName}.
  42. * </p>
  43. * <p>
  44. * The {@link java.lang.reflect.InvocationHandler} also makes
  45. * provision for {@link MXBean}s by providing type conversion
  46. * according to the rules defined for these beans. The input
  47. * parameters are converted to their equivalent open type before
  48. * calling the method, and then the return value is converted
  49. * back from its open type to the appropriate Java type. For
  50. * example, a method that takes an {@link Enum} as input and
  51. * returns a {@link java.util.List} will have the input value
  52. * converted from an {@link Enum} to a {@link String}, while
  53. * the return value will be converted from its return type
  54. * (an appropriately typed array) to a {@link java.util.List}.
  55. * </p>
  56. * <p>
  57. * The proxy has special cases for the {@link Object#equals(Object)},
  58. * {@link Object#hashCode()} and {@link Object#toString()} methods.
  59. * Unless they are specified explictly by the interface, the
  60. * following behaviour is provided for these methods by the proxy:
  61. * </p>
  62. * <ul>
  63. * <li><code>equals(Object)</code> returns true if the other object
  64. * is an {@link MBeanServerInvocationHandler} with the same
  65. * {@link MBeanServerConnection} and {@link ObjectName}. If an
  66. * interface class was specified on construction for one of the
  67. * proxies, then the same class must have also been specified
  68. * for the other.</li>
  69. * <li><code>hashCode()</code> returns the same value for
  70. * equivalent proxies.</li>
  71. * <li><code>toString()</code> returns a textual representation
  72. * of the proxy.</li>
  73. * </ul>
  74. *
  75. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  76. * @since 1.5
  77. */
  78. public class MBeanServerInvocationHandler
  79. implements InvocationHandler
  80. {
  81. /**
  82. * The connection used to make the calls.
  83. */
  84. private MBeanServerConnection conn;
  85. /**
  86. * The name of the bean to perform operations on.
  87. */
  88. private ObjectName name;
  89. /**
  90. * True if this proxy is for an {@link MXBean}.
  91. */
  92. private boolean mxBean;
  93. /**
  94. * The interface class associated with the bean.
  95. */
  96. private Class<?> iface;
  97. /**
  98. * Constructs a new {@link MBeanServerInvocationHandler}
  99. * which forwards methods to the supplied bean via the
  100. * given {@link MBeanServerConnection}. This constructor
  101. * is used in preference to
  102. * {@link JMX#newMBeanProxy(MBeanServerConnection, ObjectName,
  103. * Class<T>)} if you wish to make your own call to
  104. * {@link java.lang.reflect.Proxy#newInstance(ClassLoader,
  105. * Class[], java.lang.reflect.InvocationHandler)} with
  106. * a different {@link ClassLoader}. Calling this constructor
  107. * is equivalent to <code>MBeanServerInvocationHandler(conn,
  108. * name, false)</code>. The other constructor should be used
  109. * instead if the bean being proxied is an {@link MXBean}.
  110. *
  111. * @param conn the connection through which methods will
  112. * be forwarded to the bean.
  113. * @param name the name of the bean to use to provide the
  114. * actual calls.
  115. */
  116. public MBeanServerInvocationHandler(MBeanServerConnection conn,
  117. ObjectName name)
  118. {
  119. this(conn, name, false);
  120. }
  121. /**
  122. * Constructs a new {@link MBeanServerInvocationHandler}
  123. * which forwards methods to the supplied bean via the
  124. * given {@link MBeanServerConnection}. This constructor
  125. * is used in preference to
  126. * {@link JMX#newMBeanProxy(MBeanServerConnection, ObjectName,
  127. * Class<T>)} if you wish to make your own call to
  128. * {@link java.lang.reflect.Proxy#newInstance(ClassLoader,
  129. * Class[], java.lang.reflect.InvocationHandler)} with
  130. * a different {@link ClassLoader}.
  131. *
  132. * @param conn the connection through which methods will
  133. * be forwarded to the bean.
  134. * @param name the name of the bean to use to provide the
  135. * actual calls.
  136. * @param mxBean true if the bean being proxied is an
  137. * {@link MXBean}.
  138. * @since 1.6
  139. */
  140. public MBeanServerInvocationHandler(MBeanServerConnection conn,
  141. ObjectName name, boolean mxBean)
  142. {
  143. this.conn = conn;
  144. this.name = name;
  145. this.mxBean = mxBean;
  146. }
  147. /**
  148. * Returns the connection through which the calls to the bean
  149. * will be made.
  150. *
  151. * @return the connection being used to forward the calls to
  152. * the bean.
  153. * @since 1.6
  154. */
  155. public MBeanServerConnection getMBeanServerConnection()
  156. {
  157. return conn;
  158. }
  159. /**
  160. * Returns the name of the bean to which method calls are made.
  161. *
  162. * @return the bean which provides the actual method calls.
  163. * @since 1.6
  164. */
  165. public ObjectName getObjectName()
  166. {
  167. return name;
  168. }
  169. /**
  170. * Called by the proxy class whenever a method is called. The method
  171. * is emulated by retrieving an attribute from, setting an attribute on
  172. * or invoking a method on the server connection as required. Translation
  173. * between the Java data types supplied as arguments to the open types used
  174. * by the bean is provided, as well as translation of the return value back
  175. * in to the appropriate Java type if the bean is an {@link MXBean}.
  176. *
  177. * @param proxy the proxy on which the method was called.
  178. * @param method the method which was called.
  179. * @param args the arguments supplied to the method.
  180. * @return the return value from the method.
  181. * @throws Throwable if an exception is thrown in performing the
  182. * method emulation.
  183. */
  184. public Object invoke(Object proxy, Method method, Object[] args)
  185. throws Throwable
  186. {
  187. String mName = method.getName();
  188. Class<?> proxyClass = proxy.getClass();
  189. if (mName.equals("toString"))
  190. {
  191. if (inInterface(mName, proxyClass))
  192. return conn.invoke(name,mName,null,null);
  193. else
  194. return proxyClass.getName() + "[name=" + name
  195. + ", conn=" + conn + "]";
  196. }
  197. if (mName.equals("hashCode"))
  198. {
  199. if (inInterface(mName, proxyClass))
  200. return conn.invoke(name,mName,null,null);
  201. else
  202. return conn.hashCode() + name.hashCode()
  203. + (iface == null ? 0 : iface.hashCode());
  204. }
  205. if (mName.equals("equals"))
  206. {
  207. if (inInterface(mName, proxyClass, Object.class))
  208. return conn.invoke(name,mName,new Object[]{args[0]},
  209. new String[]{"java.lang.Object"});
  210. else
  211. {
  212. if (args[0].getClass() != proxy.getClass())
  213. return false;
  214. InvocationHandler ih = Proxy.getInvocationHandler(args[0]);
  215. if (ih instanceof MBeanServerInvocationHandler)
  216. {
  217. MBeanServerInvocationHandler h =
  218. (MBeanServerInvocationHandler) ih;
  219. return conn.equals(h.getMBeanServerConnection())
  220. && name.equals(h.getObjectName())
  221. && (iface == null ? h.iface == null
  222. : iface.equals(h.iface));
  223. }
  224. return false;
  225. }
  226. }
  227. if (NotificationEmitter.class.isAssignableFrom(proxyClass))
  228. {
  229. if (mName.equals("addNotificationListener"))
  230. {
  231. conn.addNotificationListener(name,
  232. (NotificationListener) args[0],
  233. (NotificationFilter) args[1],
  234. args[2]);
  235. return null;
  236. }
  237. if (mName.equals("getNotificationInfo"))
  238. return conn.getMBeanInfo(name).getNotifications();
  239. if (mName.equals("removeNotificationListener"))
  240. {
  241. if (args.length == 1)
  242. conn.removeNotificationListener(name,
  243. (NotificationListener)
  244. args[0]);
  245. else
  246. conn.removeNotificationListener(name,
  247. (NotificationListener)
  248. args[0],
  249. (NotificationFilter)
  250. args[1], args[2]);
  251. return null;
  252. }
  253. }
  254. String[] sigs;
  255. if (args == null)
  256. sigs = null;
  257. else
  258. {
  259. sigs = new String[args.length];
  260. for (int a = 0; a < args.length; ++a)
  261. sigs[a] = args[a].getClass().getName();
  262. }
  263. String attrib = null;
  264. if (mName.startsWith("get"))
  265. attrib = mName.substring(3);
  266. else if (mName.startsWith("is"))
  267. attrib = mName.substring(2);
  268. if (attrib != null)
  269. {
  270. Object val = conn.getAttribute(name, attrib);
  271. if (mxBean)
  272. return Translator.toJava(val, method);
  273. else
  274. return val;
  275. }
  276. else if (mName.startsWith("set"))
  277. {
  278. Object arg;
  279. if (mxBean)
  280. arg = Translator.fromJava(args, method)[0];
  281. else
  282. arg = args[0];
  283. conn.setAttribute(name, new Attribute(mName.substring(3), arg));
  284. return null;
  285. }
  286. if (mxBean)
  287. return Translator.toJava(conn.invoke(name, mName,
  288. Translator.fromJava(args,method),
  289. sigs), method);
  290. else
  291. return conn.invoke(name, mName, args, sigs);
  292. }
  293. /**
  294. * Returns true if this is a proxy for an {@link MXBean}
  295. * and conversions must be applied to input parameters
  296. * and return types, according to the rules for such beans.
  297. *
  298. * @return true if this is a proxy for an {@link MXBean}.
  299. * @since 1.6
  300. */
  301. public boolean isMXBean()
  302. {
  303. return mxBean;
  304. }
  305. /**
  306. * <p>
  307. * Returns a proxy for the specified bean. A proxy object is created
  308. * using <code>Proxy.newProxyInstance(iface.getClassLoader(),
  309. * new Class[] { iface }, handler)</code>. The
  310. * {@link javax.management.NotificationEmitter} class is included as the
  311. * second element of the array if <code>broadcaster</code> is true.
  312. * <code>handler</code> refers to the invocation handler which forwards
  313. * calls to the connection, which is created by a call to
  314. * <code>new MBeanServerInvocationHandler(conn, name)</code>.
  315. * </p>
  316. * <p>
  317. * <strong>Note</strong>: use of the proxy may result in
  318. * {@link java.io.IOException}s from the underlying
  319. * {@link MBeanServerConnection}.
  320. * As of 1.6, the use of {@link JMX#newMBeanProxy(MBeanServerConnection,
  321. * ObjectName,Class)} and {@link JMX#newMBeanProxy(MBeanServerConnection,
  322. * ObjectName,Class,boolean)} is preferred.
  323. * </p>
  324. *
  325. * @param conn the server connection to use to access the bean.
  326. * @param name the {@link javax.management.ObjectName} of the
  327. * bean to provide a proxy for.
  328. * @param iface the interface for the bean being proxied.
  329. * @param broadcaster true if the proxy should implement
  330. * {@link NotificationEmitter}.
  331. * @return a proxy for the specified bean.
  332. * @see JMX#newMBeanProxy(MBeanServerConnection,ObjectName,Class)
  333. */
  334. // Suppress warnings as we know an instance of T will be returned.
  335. @SuppressWarnings("unchecked")
  336. public static <T> T newProxyInstance(MBeanServerConnection conn,
  337. ObjectName name, Class<T> iface,
  338. boolean broadcaster)
  339. {
  340. if (broadcaster)
  341. return (T) Proxy.newProxyInstance(iface.getClassLoader(),
  342. new Class[] { iface,
  343. NotificationEmitter.class },
  344. new MBeanServerInvocationHandler(conn,name));
  345. else
  346. return (T) Proxy.newProxyInstance(iface.getClassLoader(),
  347. new Class[] { iface },
  348. new MBeanServerInvocationHandler(conn,name));
  349. }
  350. /**
  351. * Returns true if the specified method is specified
  352. * by one of the proxy's interfaces.
  353. *
  354. * @param name the name of the method to search for.
  355. * @param proxyClass the class of the proxy.
  356. * @param args the arguments to the method.
  357. * @return true if one of the interfaces specifies the
  358. * given method.
  359. */
  360. private boolean inInterface(String name, Class<?> proxyClass,
  361. Class<?>... args)
  362. {
  363. for (Class<?> iface : proxyClass.getInterfaces())
  364. {
  365. try
  366. {
  367. iface.getMethod(name, args);
  368. return true;
  369. }
  370. catch (NoSuchMethodException e)
  371. {
  372. /* Ignored; this interface doesn't specify
  373. the method. */
  374. }
  375. }
  376. return false;
  377. }
  378. }