1. /*
  2. * @(#)MBeanServerInvocationHandler.java 1.17 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.management;
  8. import java.lang.reflect.InvocationHandler;
  9. import java.lang.reflect.Method;
  10. import java.lang.reflect.Proxy;
  11. /**
  12. * <p>{@link InvocationHandler} that forwards methods in an MBean's
  13. * management interface through the MBean server to the MBean.</p>
  14. *
  15. * <p>Given an {@link MBeanServerConnection}, the {@link ObjectName}
  16. * of an MBean within that MBean server, and a Java interface
  17. * <code>Intf</code> that describes the management interface of the
  18. * MBean using the patterns for a Standard MBean, this class can be
  19. * used to construct a proxy for the MBean. The proxy implements
  20. * the interface <code>Intf</code> such that all of its methods are
  21. * forwarded through the MBean server to the MBean.</p>
  22. *
  23. * <p>If you have an MBean server <code>mbs</code> containing an MBean
  24. * with {@link ObjectName} <code>name</code>, and if the MBean's
  25. * management interface is described by the Java interface
  26. * <code>Intf</code>, you can construct a proxy for the MBean like
  27. * this:</p>
  28. *
  29. * <pre>
  30. * Intf proxy = (Intf)
  31. * MBeanServerInvocationHandler.{@link #newProxyInstance newProxyInstance}(mbs,
  32. * name,
  33. * Intf.class,
  34. * false);
  35. * </pre>
  36. *
  37. * <p>Suppose, for example, <code>Intf</code> looks like this:</p>
  38. *
  39. * <pre>
  40. * public interface Intf {
  41. * public String getSomeAttribute();
  42. * public void setSomeAttribute(String value);
  43. * public void someOperation(String param1, int param2);
  44. * }
  45. * </pre>
  46. *
  47. * <p>Then you can execute:</p>
  48. *
  49. * <ul>
  50. *
  51. * <li><code>proxy.getSomeAttribute()</code> which will result in a
  52. * call to <code>mbs.</code>{@link MBeanServerConnection#getAttribute
  53. * getAttribute}<code>(name, "SomeAttribute")</code>.
  54. *
  55. * <li><code>proxy.setSomeAttribute("whatever")</code> which will result
  56. * in a call to <code>mbs.</code>{@link MBeanServerConnection#setAttribute
  57. * setAttribute}<code>(name, new Attribute("SomeAttribute", "whatever"))</code>.
  58. *
  59. * <li><code>proxy.someOperation("param1", 2)</code> which will be
  60. * translated into a call to <code>mbs.</code>{@link
  61. * MBeanServerConnection#invoke invoke}<code>(name, "someOperation", <etc>)</code>.
  62. *
  63. * </ul>
  64. *
  65. * <p>If the last parameter to {@link #newProxyInstance
  66. * newProxyInstance} is <code>true</code>, then the MBean is assumed
  67. * to be a {@link NotificationBroadcaster} or {@link
  68. * NotificationEmitter} and the returned proxy will implement {@link
  69. * NotificationEmitter}. A call to {@link
  70. * NotificationBroadcaster#addNotificationListener} on the proxy will
  71. * result in a call to {@link
  72. * MBeanServerConnection#addNotificationListener(ObjectName,
  73. * NotificationListener, NotificationFilter, Object)}, and likewise
  74. * for the other methods of {@link NotificationBroadcaster} and {@link
  75. * NotificationEmitter}.</p>
  76. *
  77. * <p>The methods {@link Object#toString()}, {@link Object#hashCode()},
  78. * and {@link Object#equals(Object)}, when invoked on a proxy using
  79. * this invocation handler, are forwarded to the MBean server as
  80. * methods on the proxied MBean. This will only work if the MBean
  81. * declares those methods in its management interface.</p>
  82. *
  83. * @since 1.5
  84. * @since.unbundled JMX 1.2
  85. */
  86. public class MBeanServerInvocationHandler implements InvocationHandler {
  87. /**
  88. * <p>Invocation handler that forwards methods through an MBean
  89. * server. This constructor may be called instead of relying on
  90. * {@link #newProxyInstance}, for instance if you need to supply a
  91. * different {@link ClassLoader} to {@link
  92. * Proxy#newProxyInstance Proxy.newProxyInstance}.</p>
  93. *
  94. * @param connection the MBean server connection through which all
  95. * methods of a proxy using this handler will be forwarded.
  96. *
  97. * @param objectName the name of the MBean within the MBean server
  98. * to which methods will be forwarded.
  99. */
  100. public MBeanServerInvocationHandler(MBeanServerConnection connection,
  101. ObjectName objectName) {
  102. this.connection = connection;
  103. this.objectName = objectName;
  104. /* Could check here whether the MBean exists. */
  105. }
  106. /**
  107. * <p>Return a proxy that implements the given interface by
  108. * forwarding its methods through the given MBean server to the
  109. * named MBean.</p>
  110. *
  111. * <p>This method is equivalent to {@link Proxy#newProxyInstance
  112. * Proxy.newProxyInstance}<code>(interfaceClass.getClassLoader(),
  113. * interfaces, handler)</code>. Here <code>handler</code> is the
  114. * result of {@link #MBeanServerInvocationHandler new
  115. * MBeanServerInvocationHandler(connection, objectName)}, and
  116. * <code>interfaces</code> is an array that has one element if
  117. * <code>notificationBroadcaster</code> is false and two if it is
  118. * true. The first element of <code>interfaces</code> is
  119. * <code>interfaceClass</code> and the second, if present, is
  120. * <code>NotificationEmitter.class</code>.
  121. *
  122. * @param connection the MBean server to forward to.
  123. * @param objectName the name of the MBean within
  124. * <code>connection</code> to forward to.
  125. * @param interfaceClass the management interface that the MBean
  126. * exports, which will also be implemented by the returned proxy.
  127. * @param notificationBroadcaster make the returned proxy
  128. * implement {@link NotificationEmitter} by forwarding its methods
  129. * via <code>connection</code>.
  130. *
  131. * @return the new proxy instance.
  132. */
  133. public static Object newProxyInstance(MBeanServerConnection connection,
  134. ObjectName objectName,
  135. Class interfaceClass,
  136. boolean notificationBroadcaster) {
  137. final InvocationHandler handler =
  138. new MBeanServerInvocationHandler(connection, objectName);
  139. final Class[] interfaces;
  140. if (notificationBroadcaster) {
  141. interfaces =
  142. new Class[] {interfaceClass, NotificationEmitter.class};
  143. } else
  144. interfaces = new Class[] {interfaceClass};
  145. return Proxy.newProxyInstance(interfaceClass.getClassLoader(),
  146. interfaces,
  147. handler);
  148. }
  149. public Object invoke(Object proxy, Method method, Object[] args)
  150. throws Throwable {
  151. final Class methodClass = method.getDeclaringClass();
  152. if (methodClass.equals(NotificationBroadcaster.class)
  153. || methodClass.equals(NotificationEmitter.class))
  154. return invokeBroadcasterMethod(proxy, method, args);
  155. final String methodName = method.getName();
  156. final Class[] paramTypes = method.getParameterTypes();
  157. final Class returnType = method.getReturnType();
  158. /* Inexplicably, InvocationHandler specifies that args is null
  159. when the method takes no arguments rather than a
  160. zero-length array. */
  161. final int nargs = (args == null) ? 0 : args.length;
  162. if (methodName.startsWith("get")
  163. && methodName.length() > 3
  164. && nargs == 0
  165. && !returnType.equals(Void.TYPE)) {
  166. return connection.getAttribute(objectName,
  167. methodName.substring(3));
  168. }
  169. if (methodName.startsWith("is")
  170. && methodName.length() > 2
  171. && nargs == 0
  172. && (returnType.equals(Boolean.TYPE)
  173. || returnType.equals(Boolean.class))) {
  174. return connection.getAttribute(objectName,
  175. methodName.substring(2));
  176. }
  177. if (methodName.startsWith("set")
  178. && methodName.length() > 3
  179. && nargs == 1
  180. && returnType.equals(Void.TYPE)) {
  181. Attribute attr = new Attribute(methodName.substring(3), args[0]);
  182. connection.setAttribute(objectName, attr);
  183. return null;
  184. }
  185. final String[] signature = new String[paramTypes.length];
  186. for (int i = 0; i < paramTypes.length; i++)
  187. signature[i] = paramTypes[i].getName();
  188. try {
  189. return connection.invoke(objectName, methodName, args, signature);
  190. } catch (MBeanException e) {
  191. throw e.getTargetException();
  192. }
  193. /* The invoke may fail because it can't get to the MBean, with
  194. one of the these exceptions declared by
  195. MBeanServerConnection.invoke:
  196. - RemoteException: can't talk to MBeanServer;
  197. - InstanceNotFoundException: objectName is not registered;
  198. - ReflectionException: objectName is registered but does not
  199. have the method being invoked.
  200. In all of these cases, the exception will be wrapped by the
  201. proxy mechanism in an UndeclaredThrowableException unless
  202. it happens to be declared in the "throws" clause of the
  203. method being invoked on the proxy.
  204. */
  205. }
  206. private Object invokeBroadcasterMethod(Object proxy, Method method,
  207. Object[] args) throws Exception {
  208. final String methodName = method.getName();
  209. final int nargs = (args == null) ? 0 : args.length;
  210. if (methodName.equals("addNotificationListener")) {
  211. /* The various throws of IllegalArgumentException here
  212. should not happen, since we know what the methods in
  213. NotificationBroadcaster and NotificationEmitter
  214. are. */
  215. if (nargs != 3) {
  216. final String msg =
  217. "Bad arg count to addNotificationListener: " + nargs;
  218. throw new IllegalArgumentException(msg);
  219. }
  220. /* Other inconsistencies will produce ClassCastException
  221. below. */
  222. NotificationListener listener = (NotificationListener) args[0];
  223. NotificationFilter filter = (NotificationFilter) args[1];
  224. Object handback = args[2];
  225. connection.addNotificationListener(objectName,
  226. listener,
  227. filter,
  228. handback);
  229. return null;
  230. } else if (methodName.equals("removeNotificationListener")) {
  231. /* NullPointerException if method with no args, but that
  232. shouldn't happen because removeNL does have args. */
  233. NotificationListener listener = (NotificationListener) args[0];
  234. switch (nargs) {
  235. case 1:
  236. connection.removeNotificationListener(objectName, listener);
  237. return null;
  238. case 3:
  239. NotificationFilter filter = (NotificationFilter) args[1];
  240. Object handback = args[2];
  241. connection.removeNotificationListener(objectName,
  242. listener,
  243. filter,
  244. handback);
  245. return null;
  246. default:
  247. final String msg =
  248. "Bad arg count to removeNotificationListener: " + nargs;
  249. throw new IllegalArgumentException(msg);
  250. }
  251. } else if (methodName.equals("getNotificationInfo")) {
  252. if (args != null) {
  253. throw new IllegalArgumentException("getNotificationInfo has " +
  254. "args");
  255. }
  256. MBeanInfo info = connection.getMBeanInfo(objectName);
  257. return info.getNotifications();
  258. } else {
  259. throw new IllegalArgumentException("Bad method name: " +
  260. methodName);
  261. }
  262. }
  263. private final MBeanServerConnection connection;
  264. private final ObjectName objectName;
  265. }