ClassLoader loader = (ClassLoader) domain.getProperty(Deployment.CLASSLOADER_PROPERTY);
setTCCL = Classes.setTCCL(loader);
transactionPropagated = bridgeIncomingTransaction(request);
RemoteMessage msg = _serializer.deserialize(request.getInputStream(), RemoteMessage.class);
if (_log.isDebugEnabled()) {
_log.debug("Remote servlet received request for service " + msg.getService());
}
ServiceReference service = domain.getServiceReference(msg.getService());
SynchronousInOutHandler replyHandler = new SynchronousInOutHandler();
Exchange ex = msg.getOperation() == null
? service.createExchange(replyHandler)
: service.createExchange(msg.getOperation(), replyHandler);
Message m = ex.createMessage();
if (msg.getContext() != null) {
m.getContext().setProperties(msg.getContext().getProperties());
}
m.setContent(msg.getContent());
if (_log.isDebugEnabled()) {
_log.debug("Invoking service " + msg.getService());
}
ex.send(m);
// handle reply or fault
RemoteMessage reply = null;
if (ExchangePattern.IN_OUT.equals(ex.getPattern())) {
replyHandler.waitForOut();
reply = createReplyMessage(ex);
} else if (ExchangeState.FAULT.equals(ex.getState())) {
// Even though this is in-only, we need to report a runtime fault on send
reply = createReplyMessage(ex);
}
if (transactionPropagated) {
bridgeOutgoingTransaction();
transactionPropagated = false;
}
// If there's a reply, send it back
if (reply != null) {
OutputStream out = response.getOutputStream();
if (_log.isDebugEnabled()) {
_log.debug("Writing reply message to HTTP response stream " + msg.getService());
}
_serializer.serialize(reply, RemoteMessage.class, out);
out.flush();
} else {
response.setStatus(HttpServletResponse.SC_NO_CONTENT);
if (_log.isDebugEnabled()) {
_log.debug("No content to return for invocation of " + msg.getService());
}
}
} catch (SwitchYardException syEx) {
if (_log.isDebugEnabled()) {
_log.debug("Failed to process remote invocation", syEx);
}
RemoteMessage reply = new RemoteMessage();
reply.setFault(true);
reply.setContent(syEx);
_serializer.serialize(reply, RemoteMessage.class, response.getOutputStream());
response.getOutputStream().flush();
} finally {
if (transactionPropagated) {
bridgeOutgoingTransaction();