public Object invoke(Invocation invocation) throws Throwable {
Method invokeMethod = method;
Object target = invocation.getTarget();
String operationName = invocation.getName();
if (dynamic)
{
// See whether we have a fqn
String opName = operationName;
String opClass = null;
int dot = opName.lastIndexOf('.");
if (dot != -1)
{
opClass = operationName.substring(0, dot);
opName = operationName.substring(dot+1);
}
// Does the descriptor have a target?
Descriptor descriptor = invocation.getDescriptor();
if (descriptor != null)
{
Object descriptorTarget = descriptor.getFieldValue(ModelMBeanConstants.TARGET_OBJECT);
if (descriptorTarget != null)
{
String targetType = (String) descriptor.getFieldValue(ModelMBeanConstants.TARGET_TYPE);
if (ModelMBeanConstants.OBJECT_REF.equalsIgnoreCase(targetType) == false)
throw new InvalidTargetObjectTypeException("Target type is " + targetType);
target = descriptorTarget;
// Determine the method
Class clazz = null;
String className = (String) descriptor.getFieldValue(ModelMBeanConstants.CLASS);
if (className == null)
className = opClass;
if (className == null)
clazz = target.getClass();
else
{
try
{
if (clazz == null)
clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
}
catch (Exception e)
{
throw new ReflectionException(e, "Error loading class for operation " + opName);
}
}
Class[] sig;
try
{
sig = invocation.getSignatureClasses();
}
catch (Exception e)
{
throw new ReflectionException(e, "Error loading signature classes for operation " + opName);
}
try
{
invokeMethod = clazz.getDeclaredMethod(opName, sig);
}
catch (Exception e)
{
throw new ReflectionException(e, "Error getting method for operation " + opName);
}
}
}
}
if (target == null)
{
String msg = "Failed to find method for operation: " + invocation
+ " on resource: " + invocation.getInvoker().getResource()
+ " objectName: " + invocation.getInvoker().getObjectName();
throw new ReflectionException(new NullPointerException(msg));
}
try
{
Object[] args = invocation.getArgs();
return invokeMethod.invoke(target, args);
}
catch (NullPointerException e)
{
throw new NullPointerException("Error in operation=" + operationName + " method=" + method + " target=" + target);
}
catch (Throwable t)
{
handleInvocationExceptions(t);
return null;
}
}
|