// Make sure message has a messageId
checkMessageId(message);
Object serviceResult = null;
boolean serviced = false;
Service service = null;
String destId = message.getDestination();
try
{
String serviceId = (String)destinationToService.get(destId);
if (serviceId != null)
{
service = (Service)services.get(serviceId);
serviced = true;
Destination destination = service.getDestination(destId);
inspectOperation(message, destination);
// Remove the validate endopint header if it was set.
if (message.headerExists(Message.VALIDATE_ENDPOINT_HEADER))
message.getHeaders().remove(Message.VALIDATE_ENDPOINT_HEADER);
if (Log.isDebug())
Log.getLogger(getLogCategory(message)).debug(
"Before invoke service: " + service.getId() + StringUtils.NEWLINE +
" incomingMessage: " + message + StringUtils.NEWLINE);
extractRemoteCredentials(service, message);
serviceResult = service.serviceMessage(message);
}
if (!serviced)
{
MessageException lme = new MessageException();
// No destination with id ''{0}'' is registered with any service.
lme.setMessage(NO_SERVICE_FOR_DEST, new Object[] {destId});
throw lme;
}
if (Log.isDebug())
{
String debugServiceResult = Log.getPrettyPrinter().prettify(serviceResult);
Log.getLogger(getLogCategory(message)).debug(
"After invoke service: " + service.getId() + StringUtils.NEWLINE +
" reply: " + debugServiceResult + StringUtils.NEWLINE);
}
AcknowledgeMessage ack = null;
if (serviceResult instanceof AcknowledgeMessage)
{
// service will return an ack if they need to transform it in some
// service-specific way (paging is an example)
ack = (AcknowledgeMessage)serviceResult;
}
else
{
// most services will return a result of some sort, possibly null,
// and expect the broker to compose a message to deliver it
ack = new AcknowledgeMessage();
ack.setBody(serviceResult);
}
ack.setCorrelationId(message.getMessageId());
ack.setClientId(message.getClientId());
return ack;
}
catch (MessageException exc)
{
Log.getLogger(LogCategories.MESSAGE_GENERAL).error(
"Exception when invoking service: " +
(service == null ? "(none)" : service.getId()) +
StringUtils.NEWLINE +
" with message: " + message + StringUtils.NEWLINE +
" exception: " + exc + StringUtils.NEWLINE);
if (exc.getRootCause() != null)
Log.getLogger(LogCategories.MESSAGE_GENERAL).error(
"Root cause: " +
ExceptionUtil.toString(exc.getRootCause()));
throw exc;
}
catch (RuntimeException exc)
{
Log.getLogger(LogCategories.MESSAGE_GENERAL).error(
"Exception when invoking service: " +
(service == null ? "(none)" : service.getId()) +
StringUtils.NEWLINE +
" with message: " + message + StringUtils.NEWLINE +
" exception: " + ExceptionUtil.toString(exc) + StringUtils.NEWLINE);
throw exc;
}
catch (Error exc)
{
Log.getLogger(LogCategories.MESSAGE_GENERAL).error(
"Error when invoking service: " +
(service == null ? "(none)" : service.getId()) +
StringUtils.NEWLINE +
" with message: " + message + StringUtils.NEWLINE +
" error: " + ExceptionUtil.toString(exc) + StringUtils.NEWLINE);
throw exc;