public class MBeanClientInterceptor extends java.lang.Object implements MethodInterceptor, BeanClassLoaderAware, InitializingBean, DisposableBean
MethodInterceptor
that routes calls to an
MBean running on the supplied MBeanServerConnection
.
Works for both local and remote MBeanServerConnection
s.
By default, the MBeanClientInterceptor
will connect to the
MBeanServer
and cache MBean metadata at startup. This can
be undesirable when running against a remote MBeanServer
that may not be running when the application starts. Through setting the
connectOnStartup
property to "false",
you can defer this process until the first invocation against the proxy.
This functionality is usually used through MBeanProxyFactoryBean
.
See the javadoc of that class for more information.
MBeanProxyFactoryBean
,
setConnectOnStartup(boolean)
Modifier and Type | Class and Description |
---|---|
private static class |
MBeanClientInterceptor.MethodCacheKey
Simple wrapper class around a method name and its signature.
|
Modifier and Type | Field and Description |
---|---|
private java.lang.String |
agentId |
private java.util.Map<java.lang.String,javax.management.MBeanAttributeInfo> |
allowedAttributes |
private java.util.Map<MBeanClientInterceptor.MethodCacheKey,javax.management.MBeanOperationInfo> |
allowedOperations |
private java.lang.ClassLoader |
beanClassLoader |
private boolean |
connectOnStartup |
private ConnectorDelegate |
connector |
private java.util.Map<java.lang.String,?> |
environment |
private javax.management.MBeanServerInvocationHandler |
invocationHandler |
protected Log |
logger
Logger available to subclasses
|
private java.lang.Class<?> |
managementInterface |
private javax.management.ObjectName |
objectName |
private java.lang.Object |
preparationMonitor |
private boolean |
refreshOnConnectFailure |
private javax.management.MBeanServerConnection |
server |
private javax.management.MBeanServerConnection |
serverToUse |
private javax.management.remote.JMXServiceURL |
serviceUrl |
private java.util.Map<java.lang.reflect.Method,java.lang.String[]> |
signatureCache |
private boolean |
useStrictCasing |
Constructor and Description |
---|
MBeanClientInterceptor() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Prepares the
MBeanServerConnection if the "connectOnStartup"
is turned on (which it is by default). |
private java.lang.Object |
convertDataArrayToTargetArray(java.lang.Object[] array,
java.lang.Class<?> targetClass) |
private java.util.Collection<?> |
convertDataArrayToTargetCollection(java.lang.Object[] array,
java.lang.Class<?> collectionType,
java.lang.Class<?> elementType) |
protected java.lang.Object |
convertResultValueIfNecessary(java.lang.Object result,
MethodParameter parameter)
Convert the given result object (from attribute access or operation invocation)
to the specified target class for returning from the proxy method.
|
void |
destroy()
Invoked by a BeanFactory on destruction of a singleton.
|
protected java.lang.Object |
doInvoke(MethodInvocation invocation)
Route the invocation to the configured managed resource.
|
java.util.Map<java.lang.String,?> |
getEnvironment()
Allow Map access to the environment to be set for the connector,
with the option to add or override specific entries.
|
protected java.lang.Class<?> |
getManagementInterface()
Return the management interface of the target MBean,
or
null if none specified. |
protected java.lang.Object |
handleConnectFailure(MethodInvocation invocation,
java.lang.Exception ex)
Refresh the connection and retry the MBean invocation if possible.
|
java.lang.Object |
invoke(MethodInvocation invocation)
Route the invocation to the configured managed resource..
|
private java.lang.Object |
invokeAttribute(java.beans.PropertyDescriptor pd,
MethodInvocation invocation) |
private java.lang.Object |
invokeOperation(java.lang.reflect.Method method,
java.lang.Object[] args)
Routes a method invocation (not a property get/set) to the corresponding
operation on the managed resource.
|
protected boolean |
isPrepared()
Return whether this client interceptor has already been prepared,
i.e.
|
void |
prepare()
Ensures that an
MBeanServerConnection is configured and attempts
to detect a local connection if one is not supplied. |
private void |
retrieveMBeanInfo()
Loads the management interface info for the configured MBean into the caches.
|
void |
setAgentId(java.lang.String agentId)
Set the agent id of the
MBeanServer to locate. |
void |
setBeanClassLoader(java.lang.ClassLoader beanClassLoader)
Callback that supplies the bean
class loader to
a bean instance. |
void |
setConnectOnStartup(boolean connectOnStartup)
Set whether or not the proxy should connect to the
MBeanServer
at creation time ("true") or the first time it is invoked ("false"). |
void |
setEnvironment(java.util.Map<java.lang.String,?> environment)
Specify the environment for the JMX connector.
|
void |
setManagementInterface(java.lang.Class<?> managementInterface)
Set the management interface of the target MBean, exposing bean property
setters and getters for MBean attributes and conventional Java methods
for MBean operations.
|
void |
setObjectName(java.lang.Object objectName)
Set the
ObjectName of the MBean which calls are routed to,
as ObjectName instance or as String . |
void |
setRefreshOnConnectFailure(boolean refreshOnConnectFailure)
Set whether to refresh the MBeanServer connection on connect failure.
|
void |
setServer(javax.management.MBeanServerConnection server)
Set the
MBeanServerConnection used to connect to the
MBean which all invocations are routed to. |
void |
setServiceUrl(java.lang.String url)
Set the service URL of the remote
MBeanServer . |
void |
setUseStrictCasing(boolean useStrictCasing)
Set whether to use strict casing for attributes.
|
protected final Log logger
private javax.management.MBeanServerConnection server
private javax.management.remote.JMXServiceURL serviceUrl
private java.util.Map<java.lang.String,?> environment
private java.lang.String agentId
private boolean connectOnStartup
private boolean refreshOnConnectFailure
private javax.management.ObjectName objectName
private boolean useStrictCasing
private java.lang.Class<?> managementInterface
private java.lang.ClassLoader beanClassLoader
private final ConnectorDelegate connector
private javax.management.MBeanServerConnection serverToUse
private javax.management.MBeanServerInvocationHandler invocationHandler
private java.util.Map<java.lang.String,javax.management.MBeanAttributeInfo> allowedAttributes
private java.util.Map<MBeanClientInterceptor.MethodCacheKey,javax.management.MBeanOperationInfo> allowedOperations
private final java.util.Map<java.lang.reflect.Method,java.lang.String[]> signatureCache
private final java.lang.Object preparationMonitor
public void setServer(javax.management.MBeanServerConnection server)
MBeanServerConnection
used to connect to the
MBean which all invocations are routed to.public void setServiceUrl(java.lang.String url) throws java.net.MalformedURLException
MBeanServer
.java.net.MalformedURLException
public void setEnvironment(java.util.Map<java.lang.String,?> environment)
JMXConnectorFactory.connect(javax.management.remote.JMXServiceURL, java.util.Map)
public java.util.Map<java.lang.String,?> getEnvironment()
Useful for specifying entries directly, for example via "environment[myKey]". This is particularly useful for adding or overriding entries in child bean definitions.
public void setAgentId(java.lang.String agentId)
MBeanServer
to locate.
Default is none. If specified, this will result in an
attempt being made to locate the attendant MBeanServer, unless
the "serviceUrl"
property has been set.
Specifying the empty String indicates the platform MBeanServer.
public void setConnectOnStartup(boolean connectOnStartup)
MBeanServer
at creation time ("true") or the first time it is invoked ("false").
Default is "true".public void setRefreshOnConnectFailure(boolean refreshOnConnectFailure)
Can be turned on to allow for hot restart of the JMX server, automatically reconnecting and retrying in case of an IOException.
public void setObjectName(java.lang.Object objectName) throws javax.management.MalformedObjectNameException
ObjectName
of the MBean which calls are routed to,
as ObjectName
instance or as String
.javax.management.MalformedObjectNameException
public void setUseStrictCasing(boolean useStrictCasing)
When using strict casing, a JavaBean property with a getter such as
getFoo()
translates to an attribute called Foo
.
With strict casing disabled, getFoo()
would translate to just
foo
.
public void setManagementInterface(java.lang.Class<?> managementInterface)
protected final java.lang.Class<?> getManagementInterface()
null
if none specified.public void setBeanClassLoader(java.lang.ClassLoader beanClassLoader)
BeanClassLoaderAware
class loader
to
a bean instance.
Invoked after the population of normal bean properties but
before an initialization callback such as
InitializingBean's
InitializingBean.afterPropertiesSet()
method or a custom init-method.
setBeanClassLoader
in interface BeanClassLoaderAware
beanClassLoader
- the owning class loader; may be null
in
which case a default ClassLoader
must be used, for example
the ClassLoader
obtained via
ClassUtils.getDefaultClassLoader()
public void afterPropertiesSet()
MBeanServerConnection
if the "connectOnStartup"
is turned on (which it is by default).afterPropertiesSet
in interface InitializingBean
public void prepare()
MBeanServerConnection
is configured and attempts
to detect a local connection if one is not supplied.private void retrieveMBeanInfo() throws MBeanInfoRetrievalException
MBeanInfoRetrievalException
protected boolean isPrepared()
public java.lang.Object invoke(MethodInvocation invocation) throws java.lang.Throwable
invoke
in interface MethodInterceptor
invocation
- the MethodInvocation
to re-routejava.lang.Throwable
- an invocation error propagated to the userdoInvoke(org.aopalliance.intercept.MethodInvocation)
,
handleConnectFailure(org.aopalliance.intercept.MethodInvocation, java.lang.Exception)
protected java.lang.Object handleConnectFailure(MethodInvocation invocation, java.lang.Exception ex) throws java.lang.Throwable
If not configured to refresh on connect failure, this method simply rethrows the original exception.
invocation
- the invocation that failedex
- the exception raised on remote invocationjava.lang.Throwable
- an exception raised by the new invocation,
if it failed as wellsetRefreshOnConnectFailure(boolean)
,
doInvoke(org.aopalliance.intercept.MethodInvocation)
protected java.lang.Object doInvoke(MethodInvocation invocation) throws java.lang.Throwable
MBeanServerConnection.get/setAttribute
and method invocation to
MBeanServerConnection.invoke
.invocation
- the MethodInvocation
to re-routejava.lang.Throwable
- an invocation error propagated to the userprivate java.lang.Object invokeAttribute(java.beans.PropertyDescriptor pd, MethodInvocation invocation) throws javax.management.JMException, java.io.IOException
javax.management.JMException
java.io.IOException
private java.lang.Object invokeOperation(java.lang.reflect.Method method, java.lang.Object[] args) throws javax.management.JMException, java.io.IOException
method
- the method corresponding to operation on the managed resource.args
- the invocation argumentsjavax.management.JMException
java.io.IOException
protected java.lang.Object convertResultValueIfNecessary(java.lang.Object result, MethodParameter parameter)
result
- the result object as returned by the MBeanServer
parameter
- the method parameter of the proxy method that's been invokedprivate java.lang.Object convertDataArrayToTargetArray(java.lang.Object[] array, java.lang.Class<?> targetClass) throws java.lang.NoSuchMethodException
java.lang.NoSuchMethodException
private java.util.Collection<?> convertDataArrayToTargetCollection(java.lang.Object[] array, java.lang.Class<?> collectionType, java.lang.Class<?> elementType) throws java.lang.NoSuchMethodException
java.lang.NoSuchMethodException
public void destroy()
DisposableBean
destroy
in interface DisposableBean