javax.management

Class MBeanServerInvocationHandler

public class MBeanServerInvocationHandler extends Object implements InvocationHandler

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}.

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}.

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:

Since: 1.5

Constructor Summary
MBeanServerInvocationHandler(MBeanServerConnection conn, ObjectName name)
Constructs a new {@link MBeanServerInvocationHandler} which forwards methods to the supplied bean via the given {@link MBeanServerConnection}.
MBeanServerInvocationHandler(MBeanServerConnection conn, ObjectName name, boolean mxBean)
Constructs a new {@link MBeanServerInvocationHandler} which forwards methods to the supplied bean via the given {@link MBeanServerConnection}.
Method Summary
MBeanServerConnectiongetMBeanServerConnection()
Returns the connection through which the calls to the bean will be made.
ObjectNamegetObjectName()
Returns the name of the bean to which method calls are made.
Objectinvoke(Object proxy, Method method, Object[] args)
Called by the proxy class whenever a method is called.
booleanisMXBean()
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.
static <T> TnewProxyInstance(MBeanServerConnection conn, ObjectName name, Class<T> iface, boolean broadcaster)

Returns a proxy for the specified bean.

Constructor Detail

MBeanServerInvocationHandler

public MBeanServerInvocationHandler(MBeanServerConnection conn, ObjectName name)
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)} 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 MBeanServerInvocationHandler(conn, name, false). The other constructor should be used instead if the bean being proxied is an {@link MXBean}.

Parameters: conn the connection through which methods will be forwarded to the bean. name the name of the bean to use to provide the actual calls.

MBeanServerInvocationHandler

public MBeanServerInvocationHandler(MBeanServerConnection conn, ObjectName name, boolean mxBean)
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)} 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}.

Parameters: conn the connection through which methods will be forwarded to the bean. name the name of the bean to use to provide the actual calls. mxBean true if the bean being proxied is an {@link MXBean}.

Since: 1.6

Method Detail

getMBeanServerConnection

public MBeanServerConnection getMBeanServerConnection()
Returns the connection through which the calls to the bean will be made.

Returns: the connection being used to forward the calls to the bean.

Since: 1.6

getObjectName

public ObjectName getObjectName()
Returns the name of the bean to which method calls are made.

Returns: the bean which provides the actual method calls.

Since: 1.6

invoke

public Object invoke(Object proxy, Method method, Object[] args)
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}.

Parameters: proxy the proxy on which the method was called. method the method which was called. args the arguments supplied to the method.

Returns: the return value from the method.

Throws: Throwable if an exception is thrown in performing the method emulation.

isMXBean

public boolean isMXBean()
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.

Returns: true if this is a proxy for an {@link MXBean}.

Since: 1.6

newProxyInstance

public static <T> T newProxyInstance(MBeanServerConnection conn, ObjectName name, Class<T> iface, boolean broadcaster)

Returns a proxy for the specified bean. A proxy object is created using Proxy.newProxyInstance(iface.getClassLoader(), new Class[] { iface }, handler). The {@link javax.management.NotificationEmitter} class is included as the second element of the array if broadcaster is true. handler refers to the invocation handler which forwards calls to the connection, which is created by a call to new MBeanServerInvocationHandler(conn, name).

Note: 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.

Parameters: conn the server connection to use to access the bean. name the {@link javax.management.ObjectName} of the bean to provide a proxy for. iface the interface for the bean being proxied. broadcaster true if the proxy should implement {@link NotificationEmitter}.

Returns: a proxy for the specified bean.

See Also: newMBeanProxy