@SuppressWarnings({"unchecked"})
public void callback(Message message) {
Object[] parms = message.get(Object[].class, "MethodParms");
if ((parms == null && targetTypes.length != 0) || (parms.length != targetTypes.length)) {
throw new MessageDeliveryFailure("wrong number of arguments sent to endpoint. (received: "
+ (parms == null ? 0 : parms.length) + "; required: " + targetTypes.length + ")");
}
for (int i = 0; i < parms.length; i++) {
if (parms[i] != null && !targetTypes[i].isAssignableFrom(parms[i].getClass())) {
if (canConvert(targetTypes[i], parms[i].getClass())) {
parms[i] = convert(parms[i], targetTypes[i]);
}
else if (targetTypes[i].isArray()) {
if (parms[i] instanceof Collection) {
Collection c = (Collection) parms[i];
parms[i] = c.toArray((Object[]) Array.newInstance(targetTypes[i].getComponentType(), c.size()));
}
else if (parms[i].getClass().isArray()) {
int length = Array.getLength(parms[i]);
Class toComponentType = parms[i].getClass().getComponentType();
Object parmValue = parms[i];
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));
}
parms[i] = newArray;
}
}
else {
throw new MessageDeliveryFailure("type mismatch in method parameters");
}
}
}
try {
createConversation(message)
.subjectProvided()
.with("MethodReply", method.invoke(serviceProvider.get(), parms))
.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);
}
}