@SuppressWarnings({"unchecked"})
public void callback(Message message) {
final List<Object> parms = message.get(List.class, "MethodParms");
if ((parms == null && targetTypes.length != 0) || (parms.size() != targetTypes.length)) {
throw new MessageDeliveryFailure(
"wrong number of arguments sent to endpoint. (received: "
+ (parms == null ? 0 : parms.size())
+ "; required: " + targetTypes.length + ")");
}
for (int i = 0; i < parms.size(); i++) {
Object p = parms.get(i);
if (p != null && !targetTypes[i].isAssignableFrom(p.getClass())) {
if (canConvert(targetTypes[i], p.getClass())) {
p = convert(p, targetTypes[i]);
}
else if (targetTypes[i].isArray()) {
if (p instanceof Collection) {
final Collection c = (Collection) p;
p = c.toArray((Object[]) Array.newInstance(targetTypes[i].getComponentType(), c.size()));
}
else if (p.getClass().isArray()) {
final int length = Array.getLength(p);
final Class toComponentType = p.getClass().getComponentType();
final Object parmValue = p;
final Object newArray = Array.newInstance(targetTypes[i].getComponentType(), length);
for (int x = 0; x < length; x++) {
Array.set(newArray, x, convert(Array.get(parmValue, x), toComponentType));
}
p = newArray;
}
}
else {
throw new MessageDeliveryFailure("type mismatch in method parameters " +
" (got types: "
+ Arrays.toString(getTypesFrom(parms))
+ "; but expected: "
+ Arrays.toString(targetTypes)
+ ")");
}
}
}
try {
RpcContext.set(message);
final Object methReply = method.invoke(serviceProvider.get(message), parms.toArray(new Object[parms.size()]));
if (method.getReturnType().equals(void.class)) {
createConversation(message)
.subjectProvided()
.noErrorHandling().sendNowWith(bus);
}
else {
createConversation(message)
.subjectProvided()
.with("MethodReply", methReply)
.noErrorHandling().sendNowWith(bus);
}
}
catch (MessageDeliveryFailure e) {
throw e;
}
catch (InvocationTargetException e) {
throw new MessageDeliveryFailure("error invoking endpoint", e.getCause());
}
catch (Exception e) {
throw new MessageDeliveryFailure("error invoking endpoint", e);
}
finally {
RpcContext.remove();
}
}