Object payload = msg.getBody();
Object contextId = null;
EndpointReference from = msg.getFrom();
ReferenceParameters parameters = null;
if (from != null) {
parameters = from.getReferenceParameters();
}
// check what sort of context is required
if (scopeContainer != null) {
Scope scope = scopeContainer.getScope();
if (scope == Scope.REQUEST) {
contextId = Thread.currentThread();
} else if (scope == Scope.CONVERSATION && parameters != null) {
contextId = parameters.getConversationID();
}
}
try {
// The following call might create a new conversation, as a result, the msg.getConversationID() might
// return a new value
InstanceWrapper wrapper = scopeContainer.getWrapper(contextId);
// detects whether the scope container has created a conversation Id. This will
// happen in the case that the component has conversational scope but only the
// callback interface is conversational. Or in the callback case if the service interface
// is conversational and the callback interface isn't. If we are in this situation we need
// to get the contextId of this component and remove it after we have invoked the method on
// it. It is possible that the component instance will not go away when it is removed below
// because a callback conversation will still be holding a reference to it
boolean removeTemporaryConversationalComponentAfterCall = false;
if (parameters != null && (contextId == null) && (parameters.getConversationID() != null)) {
contextId = parameters.getConversationID();
removeTemporaryConversationalComponentAfterCall = true;
}
Object instance = wrapper.getInstance();
// If the method couldn't be computed statically, or the instance being
// invoked is a user-specified callback object that doesn't implement
// the service interface from which the reflective method was obtained,
// compute the method object dynamically for this invocation.
Method imethod = method;
if (imethod == null || !imethod.getDeclaringClass().isInstance(instance)) {
try {
imethod = JavaInterfaceUtil.findMethod(instance.getClass(), operation);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("Callback object does not provide method " + e.getMessage());
}
}
Object ret;
if (payload != null && !payload.getClass().isArray()) {
ret = imethod.invoke(instance, payload);
} else {
ret = imethod.invoke(instance, (Object[])payload);
}
scopeContainer.returnWrapper(wrapper, contextId);
if ((sequence == ConversationSequence.CONVERSATION_END) || (removeTemporaryConversationalComponentAfterCall)) {
// if end conversation, or we have the special case where a conversational
// object was created to service the stateless half of a stateful component
scopeContainer.remove(contextId);
parameters.setConversationID(null);
}
msg.setBody(ret);
} catch (InvocationTargetException e) {
Throwable cause = e.getTargetException();
boolean isChecked = false;
for (DataType<?> d : operation.getFaultTypes()) {
if (d.getPhysical().isInstance(cause)) {
isChecked = true;
msg.setFaultBody(cause);
break;
}
}
if (sequence != ConversationSequence.CONVERSATION_NONE ){
try {
// // If the exception is not a business exception then end the conversation
// boolean businessException = false;
//
// for (DataType dataType : operation.getFaultTypes()){
// if ((dataType.getPhysical() == e.getCause().getClass()) &&
// (contextId != null) ){
// businessException = true;
// break;
// }
// }
if (!isChecked && contextId != null) {
scopeContainer.remove(contextId);
parameters.setConversationID(null);
}
} catch (Exception ex){
// TODO - sure what the best course of action is here. We have
// a system exception in the middle of a business exception
}