BindingOperationInfo oi,
Object[] params,
Map<String, Object> context,
Exchange exchange) throws Exception {
Bus origBus = BusFactory.getAndSetThreadDefaultBus(bus);
ClassLoaderHolder origLoader = null;
try {
ClassLoader loader = bus.getExtension(ClassLoader.class);
if (loader != null) {
origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
}
if (exchange == null) {
exchange = new ExchangeImpl();
}
exchange.setSynchronous(callback == null);
Endpoint endpoint = getEndpoint();
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Invoke, operation info: " + oi + ", params: " + Arrays.toString(params));
}
Message message = endpoint.getBinding().createMessage();
// Make sure INVOCATION CONTEXT, REQUEST_CONTEXT and RESPONSE_CONTEXT are present
// on message
Map<String, Object> reqContext = null;
Map<String, Object> resContext = null;
if (context == null) {
context = new HashMap<String, Object>();
}
reqContext = CastUtils.cast((Map<?, ?>)context.get(REQUEST_CONTEXT));
resContext = CastUtils.cast((Map<?, ?>)context.get(RESPONSE_CONTEXT));
if (reqContext == null) {
reqContext = new HashMap<String, Object>(getRequestContext());
context.put(REQUEST_CONTEXT, reqContext);
}
if (resContext == null) {
resContext = new HashMap<String, Object>();
context.put(RESPONSE_CONTEXT, resContext);
}
message.put(Message.INVOCATION_CONTEXT, context);
setContext(reqContext, message);
exchange.putAll(reqContext);
setParameters(params, message);
if (null != oi) {
exchange.setOneWay(oi.getOutput() == null);
}
exchange.setOutMessage(message);
exchange.put(ClientCallback.class, callback);
setOutMessageProperties(message, oi);
setExchangeProperties(exchange, endpoint, oi);
PhaseInterceptorChain chain = setupInterceptorChain(endpoint);
message.setInterceptorChain(chain);
if (callback == null) {
chain.setFaultObserver(outFaultObserver);
} else {
// We need to wrap the outFaultObserver if the callback is not null
// calling the conduitSelector.complete to make sure the fail over feature works
chain.setFaultObserver(new MessageObserver() {
public void onMessage(Message message) {
Exception ex = message.getContent(Exception.class);
if (ex != null) {
getConduitSelector().complete(message.getExchange());
if (message.getContent(Exception.class) == null) {
// handle the right response
List<Object> resList = null;
Message inMsg = message.getExchange().getInMessage();
Map<String, Object> ctx = responseContext.get(Thread.currentThread());
resList = CastUtils.cast(inMsg.getContent(List.class));
Object[] result = resList == null ? null : resList.toArray();
callback.handleResponse(ctx, result);
return;
}
}
outFaultObserver.onMessage(message);
}
});
}
prepareConduitSelector(message);
// add additional interceptors and such
modifyChain(chain, message, false);
try {
chain.doIntercept(message);
} catch (Fault fault) {
enrichFault(fault);
throw fault;
}
if (callback != null) {
return null;
} else {
return processResult(message, exchange, oi, resContext);
}
} finally {
if (origLoader != null) {
origLoader.reset();
}
if (origBus != bus) {
BusFactory.setThreadDefaultBus(origBus);
}
}