public Object invokeJndiMethod(Context context, String path, String methodName, TypeAndValueEntry... params) {
final Object obj;
try {
obj = context.lookup(path);
} catch (NamingException e) {
throw new ServiceException(e);
}
final Class<?> cls = obj.getClass();
final Method method;
final Object[] parameterValues;
try {
if (params == null) {
parameterValues = null;
method = cls.getMethod(methodName);
} else {
final Class<?>[] parameterTypes = new Class<?>[params.length];
parameterValues = new Object[params.length];
for (int i = 0; i < params.length; i++) {
parameterTypes[i] = params[i].type;
parameterValues[i] = params[i].value;
}
method = cls.getMethod(methodName, parameterTypes);
}
} catch (NoSuchMethodException e) {
throw new ServiceException(e);
}
final Object result;
try {
if (parameterValues == null) {
result = method.invoke(obj);
} else {
result = method.invoke(obj, parameterValues);
}
} catch (IllegalAccessException e) {
throw new ServiceException(e);
} catch (InvocationTargetException e) {
throw new ServiceException(e);
}
return result;
}