public void perform(OpenExecution execution) throws Exception
{
MessageService mule = EnvironmentImpl.getCurrent().get(MuleMessageService.class);
if (mule == null)
{
throw new JbpmException("The Mule MessageService is not available from the ProcessEngine, you may need to add it to your jbpm.cfg.xml file");
}
if (payloadExpression == null)
{
payloadObject = execution.getVariable(Process.PROCESS_VARIABLE_DATA);
if (payloadObject == null)
{
payloadObject = execution.getVariable(Process.PROCESS_VARIABLE_INCOMING);
}
}
else
{
// The payloadSource may be specified using an expression (e.g.,
// #{myObject.myStuff.myField} would first retrieve the process
// variable "myObject" and then call .getMyStuff().getMyField()
payloadObject = ScriptManager.getScriptManager().evaluateExpression(payloadExpression, null);
}
if (payloadObject == null)
{
throw new IllegalArgumentException("Payload for message is null. Payload source is \""
+ payloadExpression + "\"");
}
Map props = new HashMap();
props.put(Process.PROPERTY_PROCESS_TYPE, ((ExecutionImpl) execution).getProcessDefinition().getName());
props.put(Process.PROPERTY_PROCESS_ID, execution.getId());
String state = Jbpm.getState(execution.getProcessInstance());
props.put("MULE_BPM_PROCESS_STATE", state);
log.debug("process state: " + state);
// Set process vars as properties on outgoing Mule messages.
for (Entry<String, ?> var : execution.getVariables().entrySet())
{
if (!var.getKey().startsWith(MuleProperties.PROPERTY_PREFIX))
{
log.debug("process var: " + var.getKey() + " = " + var.getValue());
props.put(var.getKey(), var.getValue());
}
}
// Just in case the endpoint itself is an expression
endpoint = (String) ScriptManager.getScriptManager().evaluateExpression(endpoint, null);
MuleMessage response = mule.generateMessage(endpoint, payloadObject, props, mep);
if (mep.hasResponse() && response != null)
{
Object responsePayload = response.getPayload();
// Validate expected response type
if (responsePayloadClass != null)
{
log.debug("Validating response type = " + responsePayload.getClass() + ", expected = " + responsePayloadClass);
if (!responsePayloadClass.isAssignableFrom(responsePayload.getClass()))
{
throw new JbpmException("Response message is of type " + responsePayload.getClass() + " but expected type is " + responsePayloadClass);
}
}
if (responseVariableName != null)
{