MBeanServerInvocationHandler.java 14.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/* MBeanServerInvocationHandler.java -- Provides a proxy for a bean.
   Copyright (C) 2007 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package javax.management;

import gnu.javax.management.Translator;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * <p>
 * Provides a proxy for a management bean.  The methods
 * of the given interface are fulfilled by redirecting the
 * calls over an {@link MBeanServerConnection} to the bean
 * specified by the supplied {@link ObjectName}.
 * </p>
 * <p>
 * The {@link java.lang.reflect.InvocationHandler} also makes
 * provision for {@link MXBean}s by providing type conversion
 * according to the rules defined for these beans.  The input
 * parameters are converted to their equivalent open type before
 * calling the method, and then the return value is converted
 * back from its open type to the appropriate Java type.  For
 * example, a method that takes an {@link Enum} as input and
 * returns a {@link java.util.List} will have the input value
 * converted from an {@link Enum} to a {@link String}, while
 * the return value will be converted from its return type
 * (an appropriately typed array) to a {@link java.util.List}.
 * </p>
 * <p>
 * The proxy has special cases for the {@link Object#equals(Object)},
 * {@link Object#hashCode()} and {@link Object#toString()} methods.
 * Unless they are specified explictly by the interface, the
 * following behaviour is provided for these methods by the proxy:
 * </p>
 * <ul>
 * <li><code>equals(Object)</code> returns true if the other object
 * is an {@link MBeanServerInvocationHandler} with the same
 * {@link MBeanServerConnection} and {@link ObjectName}.  If an
 * interface class was specified on construction for one of the
 * proxies, then the same class must have also been specified
 * for the other.</li>
 * <li><code>hashCode()</code> returns the same value for
 * equivalent proxies.</li>
 * <li><code>toString()</code> returns a textual representation
 * of the proxy.</li>
 * </ul>
84
 *
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
 * @since 1.5
 */
public class MBeanServerInvocationHandler
  implements InvocationHandler
{

  /**
   * The connection used to make the calls.
   */
  private MBeanServerConnection conn;

  /**
   * The name of the bean to perform operations on.
   */
  private ObjectName name;

  /**
   * True if this proxy is for an {@link MXBean}.
   */
  private boolean mxBean;

  /**
   * The interface class associated with the bean.
   */
  private Class<?> iface;

  /**
   * Constructs a new {@link MBeanServerInvocationHandler}
   * which forwards methods to the supplied bean via the
   * given {@link MBeanServerConnection}.  This constructor
   * is used in preference to
   * {@link JMX#newMBeanProxy(MBeanServerConnection, ObjectName,
   * Class<T>)} if you wish to make your own call to
   * {@link java.lang.reflect.Proxy#newInstance(ClassLoader,
   * Class[], java.lang.reflect.InvocationHandler)} with
   * a different {@link ClassLoader}.  Calling this constructor
   * is equivalent to <code>MBeanServerInvocationHandler(conn,
   * name, false)</code>.  The other constructor should be used
   * instead if the bean being proxied is an {@link MXBean}.
   *
   * @param conn the connection through which methods will
   *             be forwarded to the bean.
   * @param name the name of the bean to use to provide the
   *             actual calls.
   */
  public MBeanServerInvocationHandler(MBeanServerConnection conn,
132
                                      ObjectName name)
133 134 135 136 137 138 139 140 141 142 143 144 145
  {
    this(conn, name, false);
  }

  /**
   * Constructs a new {@link MBeanServerInvocationHandler}
   * which forwards methods to the supplied bean via the
   * given {@link MBeanServerConnection}.  This constructor
   * is used in preference to
   * {@link JMX#newMBeanProxy(MBeanServerConnection, ObjectName,
   * Class<T>)} if you wish to make your own call to
   * {@link java.lang.reflect.Proxy#newInstance(ClassLoader,
   * Class[], java.lang.reflect.InvocationHandler)} with
146
   * a different {@link ClassLoader}.
147 148 149 150 151 152 153 154 155 156
   *
   * @param conn the connection through which methods will
   *             be forwarded to the bean.
   * @param name the name of the bean to use to provide the
   *             actual calls.
   * @param mxBean true if the bean being proxied is an
   *               {@link MXBean}.
   * @since 1.6
   */
  public MBeanServerInvocationHandler(MBeanServerConnection conn,
157
                                      ObjectName name, boolean mxBean)
158 159 160 161 162 163 164 165 166
  {
    this.conn = conn;
    this.name = name;
    this.mxBean = mxBean;
  }

  /**
   * Returns the connection through which the calls to the bean
   * will be made.
167
   *
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
   * @return the connection being used to forward the calls to
   *         the bean.
   * @since 1.6
   */
  public MBeanServerConnection getMBeanServerConnection()
  {
    return conn;
  }

  /**
   * Returns the name of the bean to which method calls are made.
   *
   * @return the bean which provides the actual method calls.
   * @since 1.6
   */
  public ObjectName getObjectName()
  {
    return name;
  }

  /**
   * Called by the proxy class whenever a method is called.  The method
   * is emulated by retrieving an attribute from, setting an attribute on
   * or invoking a method on the server connection as required.  Translation
   * between the Java data types supplied as arguments to the open types used
   * by the bean is provided, as well as translation of the return value back
   * in to the appropriate Java type if the bean is an {@link MXBean}.
   *
   * @param proxy the proxy on which the method was called.
   * @param method the method which was called.
   * @param args the arguments supplied to the method.
   * @return the return value from the method.
   * @throws Throwable if an exception is thrown in performing the
   *                   method emulation.
   */
  public Object invoke(Object proxy, Method method, Object[] args)
    throws Throwable
  {
    String mName = method.getName();
207
    Class<?> proxyClass = proxy.getClass();
208 209
    if (mName.equals("toString"))
      {
210 211 212 213 214
        if (inInterface(mName, proxyClass))
          return conn.invoke(name,mName,null,null);
        else
          return proxyClass.getName() + "[name=" + name
            + ", conn=" + conn + "]";
215 216 217
      }
    if (mName.equals("hashCode"))
      {
218 219 220 221 222
        if (inInterface(mName, proxyClass))
          return conn.invoke(name,mName,null,null);
        else
          return conn.hashCode() + name.hashCode()
            + (iface == null ? 0 : iface.hashCode());
223 224 225
      }
    if (mName.equals("equals"))
      {
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
        if (inInterface(mName, proxyClass, Object.class))
          return conn.invoke(name,mName,new Object[]{args[0]},
                             new String[]{"java.lang.Object"});
        else
          {
            if (args[0].getClass() != proxy.getClass())
              return false;
            InvocationHandler ih = Proxy.getInvocationHandler(args[0]);
            if (ih instanceof MBeanServerInvocationHandler)
              {
                MBeanServerInvocationHandler h =
                  (MBeanServerInvocationHandler) ih;
                return conn.equals(h.getMBeanServerConnection())
                  && name.equals(h.getObjectName())
                  && (iface == null ? h.iface == null
                      : iface.equals(h.iface));
              }
            return false;
          }
245 246 247
      }
    if (NotificationEmitter.class.isAssignableFrom(proxyClass))
      {
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        if (mName.equals("addNotificationListener"))
          {
            conn.addNotificationListener(name,
                                         (NotificationListener) args[0],
                                         (NotificationFilter) args[1],
                                         args[2]);
            return null;
          }
        if (mName.equals("getNotificationInfo"))
          return conn.getMBeanInfo(name).getNotifications();
        if (mName.equals("removeNotificationListener"))
          {
            if (args.length == 1)
              conn.removeNotificationListener(name,
                                              (NotificationListener)
                                              args[0]);
            else
              conn.removeNotificationListener(name,
                                              (NotificationListener)
                                              args[0],
                                              (NotificationFilter)
                                              args[1], args[2]);
            return null;
          }
272 273 274 275 276 277
      }
    String[] sigs;
    if (args == null)
      sigs = null;
    else
      {
278 279 280
        sigs = new String[args.length];
        for (int a = 0; a < args.length; ++a)
          sigs[a] = args[a].getClass().getName();
281 282 283 284 285 286 287 288
      }
    String attrib = null;
    if (mName.startsWith("get"))
      attrib = mName.substring(3);
    else if (mName.startsWith("is"))
      attrib = mName.substring(2);
    if (attrib != null)
      {
289 290 291 292 293
        Object val = conn.getAttribute(name, attrib);
        if (mxBean)
          return Translator.toJava(val, method);
        else
          return val;
294 295 296
      }
    else if (mName.startsWith("set"))
      {
297 298 299 300 301 302 303
        Object arg;
        if (mxBean)
          arg = Translator.fromJava(args, method)[0];
        else
          arg = args[0];
        conn.setAttribute(name, new Attribute(mName.substring(3), arg));
        return null;
304 305
      }
    if (mxBean)
306 307 308
      return Translator.toJava(conn.invoke(name, mName,
                                           Translator.fromJava(args,method),
                                           sigs), method);
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    else
      return conn.invoke(name, mName, args, sigs);
  }

  /**
   * Returns true if this is a proxy for an {@link MXBean}
   * and conversions must be applied to input parameters
   * and return types, according to the rules for such beans.
   *
   * @return true if this is a proxy for an {@link MXBean}.
   * @since 1.6
   */
  public boolean isMXBean()
  {
    return mxBean;
  }

  /**
   * <p>
   * Returns a proxy for the specified bean.  A proxy object is created
   * using <code>Proxy.newProxyInstance(iface.getClassLoader(),
   * new Class[] { iface }, handler)</code>.  The
   * {@link javax.management.NotificationEmitter} class is included as the
   * second element of the array if <code>broadcaster</code> is true.
   * <code>handler</code> refers to the invocation handler which forwards
   * calls to the connection, which is created by a call to
335
   * <code>new MBeanServerInvocationHandler(conn, name)</code>.
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
   * </p>
   * <p>
   * <strong>Note</strong>: use of the proxy may result in
   * {@link java.io.IOException}s from the underlying
   * {@link MBeanServerConnection}.
   * As of 1.6, the use of {@link JMX#newMBeanProxy(MBeanServerConnection,
   * ObjectName,Class)} and {@link JMX#newMBeanProxy(MBeanServerConnection,
   * ObjectName,Class,boolean)} is preferred.
   * </p>
   *
   * @param conn the server connection to use to access the bean.
   * @param name the {@link javax.management.ObjectName} of the
   *             bean to provide a proxy for.
   * @param iface the interface for the bean being proxied.
   * @param broadcaster true if the proxy should implement
   *                    {@link NotificationEmitter}.
   * @return a proxy for the specified bean.
   * @see JMX#newMBeanProxy(MBeanServerConnection,ObjectName,Class)
   */
355 356
  // Suppress warnings as we know an instance of T will be returned.
  @SuppressWarnings("unchecked")
357
  public static <T> T newProxyInstance(MBeanServerConnection conn,
358 359
                                       ObjectName name, Class<T> iface,
                                       boolean broadcaster)
360 361 362
  {
    if (broadcaster)
      return (T) Proxy.newProxyInstance(iface.getClassLoader(),
363 364 365
                                        new Class[] { iface,
                                                      NotificationEmitter.class },
                                        new MBeanServerInvocationHandler(conn,name));
366 367
    else
      return (T) Proxy.newProxyInstance(iface.getClassLoader(),
368 369
                                        new Class[] { iface },
                                        new MBeanServerInvocationHandler(conn,name));
370 371 372 373 374 375 376 377 378 379 380 381 382
  }

  /**
   * Returns true if the specified method is specified
   * by one of the proxy's interfaces.
   *
   * @param name the name of the method to search for.
   * @param proxyClass the class of the proxy.
   * @param args the arguments to the method.
   * @return true if one of the interfaces specifies the
   *         given method.
   */
  private boolean inInterface(String name, Class<?> proxyClass,
383
                              Class<?>... args)
384 385 386
  {
    for (Class<?> iface : proxyClass.getInterfaces())
      {
387 388 389 390 391 392 393 394 395 396
        try
          {
            iface.getMethod(name, args);
            return true;
          }
        catch (NoSuchMethodException e)
          {
            /* Ignored; this interface doesn't specify
               the method. */
          }
397 398 399 400
      }
    return false;
  }

401
}