}
@Override
public String invoke(BpmStep step, Map params) throws Exception {
try {
ProcessInstance processInstance = step.getProcessInstance();
payload = params.get("payload");
LocalMuleClient client = mulePluginManager.getMuleContext().getClient();
// XStream xs = new XStream();
// xs.registerConverter(new MyPersistentSetConverter(xs.getMapper()), XStream.PRIORITY_VERY_HIGH);
// xs.omitField(ProcessInstance.class, "definition");
// xs.omitField(ProcessInstance.class, "processLogs");
// String input = xs.toXML(processInstance);
if (asynchronous) {
client.dispatch(destinationEndpointUrl, processInstance, null);
} else {
MuleMessage muleMessage = client.send(destinationEndpointUrl,
payload != null ? payload : processInstance,
null, timeout);
if (muleMessage != null) {
ExceptionPayload exceptionPayload = muleMessage.getExceptionPayload();
if (exceptionPayload != null) {
logger.log(Level.SEVERE, "Mule step has failed: " + exceptionPayload.getMessage(), exceptionPayload.getException());
return "FAIL";
}
Object payload = muleMessage.getPayload();
if (payload instanceof String) {
return (String)payload;
} else if (payload instanceof ProcessInstanceAttribute) {
ProcessInstanceAttribute pia = (ProcessInstanceAttribute) payload;
ProcessInstanceAttribute attributeByKey = processInstance.findAttributeByKey(pia.getKey());
if (attributeByKey != null) {
processInstance.removeAttribute(attributeByKey);
}
processInstance.addAttribute(pia);
return pia.toString();
} else if (payload instanceof ProcessInstanceAttribute[]) {
ProcessInstanceAttribute[] pias = (ProcessInstanceAttribute[]) payload;
for (ProcessInstanceAttribute pia : pias) {
ProcessInstanceAttribute attributeByKey = processInstance.findAttributeByKey(pia.getKey());
if (attributeByKey != null) {
processInstance.removeAttribute(attributeByKey);
}
processInstance.addAttribute(pia);
}
} else if (payload instanceof Iterable) {
Iterable pias = (Iterable) payload;
for (Object o : pias) {
if (o instanceof ProcessInstanceAttribute) {
ProcessInstanceAttribute pia = (ProcessInstanceAttribute) o;
ProcessInstanceAttribute attributeByKey = processInstance.findAttributeByKey(pia.getKey());
if (attributeByKey != null) {
processInstance.removeAttribute(attributeByKey);
}
processInstance.addAttribute(pia);
}
}
}
}