this.methodInfoCache = methodInfoCache;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
BeanInvocation invocation = new BeanInvocation(method, args);
ExchangePattern pattern = ExchangePattern.InOut;
MethodInfo methodInfo = methodInfoCache.getMethodInfo(method);
if (methodInfo != null) {
pattern = methodInfo.getPattern();
}
Exchange exchange = new DefaultExchange(endpoint, pattern);
exchange.getIn().setBody(invocation);
// process the exchange
if (LOG.isTraceEnabled()) {
LOG.trace("Proxied method call " + method.getName() + " invoking producer: " + producer);
}
producer.process(exchange);
// check if we had an exception
Throwable cause = exchange.getException();
if (cause != null) {
Throwable found = findSuitableException(cause, method);
if (found != null) {
throw found;
}
// special for runtime camel exceptions as they can be nested
if (cause instanceof RuntimeCamelException) {
// if the inner cause is a runtime exception we can throw it directly
if (cause.getCause() instanceof RuntimeException) {
throw (RuntimeException) ((RuntimeCamelException) cause).getCause();
}
throw (RuntimeCamelException) cause;
}
// okay just throw the exception as is
throw cause;
}
// do not return a reply if the method is VOID or the MEP is not OUT capable
Class<?> to = method.getReturnType();
if (to == Void.TYPE || !pattern.isOutCapable()) {
return null;
}
// only convert if there is a body
if (!exchange.hasOut() || exchange.getOut().getBody() == null) {