public Object invoke(Object proxy,
Method method,
Object[] args) throws Throwable {
// if the method belongs to ProxyInstance, then invoke locally
Class type = method.getDeclaringClass();
if (type == MBeanProxyInstance.class) {
return method.invoke(this, args);
}
String methodName = method.getName();
// Get attribute
if (methodName.startsWith("get") && args == null)
{
String attrName = methodName.substring(3);
MBeanAttributeInfo info = (MBeanAttributeInfo) attributeMap.get(attrName);
if (info != null)
{
String retType = method.getReturnType().getName();
if (retType.equals(info.getType()))
{
try
{
return server.getAttribute(name, attrName);
}
catch (Exception e)
{
throw JMXExceptionDecoder.decode(e);
}
}
}
}
// Is attribute
else if (methodName.startsWith("is") && args == null)
{
String attrName = methodName.substring(2);
MBeanAttributeInfo info = (MBeanAttributeInfo) attributeMap.get(attrName);
if (info != null && info.isIs())
{
Class retType = method.getReturnType();
if (retType.equals(Boolean.class) || retType.equals(Boolean.TYPE))
{
try
{
return server.getAttribute(name, attrName);
}
catch (Exception e)
{
throw JMXExceptionDecoder.decode(e);
}
}
}
}
// Set attribute
else if (methodName.startsWith("set") && args != null && args.length == 1)
{
String attrName = methodName.substring(3);
MBeanAttributeInfo info = (MBeanAttributeInfo) attributeMap.get(attrName);
if (info != null && method.getReturnType() == Void.TYPE)
{
try
{
server.setAttribute(name, new Attribute(attrName, args[0]));
return null;
}
catch (Exception e)
{
throw JMXExceptionDecoder.decode(e);
}
}
}
// Operation
// convert the parameter types to strings for JMX
Class[] types = method.getParameterTypes();
String[] sig = new String[types.length];
for (int i = 0; i < types.length; i++) {
sig[i] = types[i].getName();
}
// invoke the server and decode JMX exceptions
try {
return server.invoke(name, methodName, args == null ? EMPTY_ARGS : args, sig);
}
catch (Exception e) {
throw JMXExceptionDecoder.decode(e);
}
}
Invoke the configured MBean via the target MBeanServer and decode
any resulting JMX exceptions that are thrown. |