1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.bpm.jbpm.actions; |
12 | |
|
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.config.MuleProperties; |
15 | |
import org.mule.transport.bpm.ProcessConnector; |
16 | |
import org.mule.transport.bpm.jbpm.MuleMessageService; |
17 | |
import org.mule.util.StringUtils; |
18 | |
|
19 | |
import java.util.HashMap; |
20 | |
import java.util.Map; |
21 | |
|
22 | |
import org.apache.commons.jxpath.JXPathContext; |
23 | |
import org.jbpm.graph.exe.ExecutionContext; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 2 | public class SendMuleEvent extends LoggingActionHandler |
38 | |
{ |
39 | |
|
40 | |
private static final long serialVersionUID = 1L; |
41 | |
|
42 | 2 | boolean synchronous = true; |
43 | 2 | String endpoint = null; |
44 | 2 | String transformers = null; |
45 | 2 | Map properties = null; |
46 | |
|
47 | |
|
48 | |
|
49 | 2 | String payload = null; |
50 | 2 | String payloadSource = null; |
51 | |
|
52 | |
|
53 | |
private Object payloadObject; |
54 | |
|
55 | |
public void execute(ExecutionContext executionContext) throws Exception |
56 | |
{ |
57 | 2 | super.execute(executionContext); |
58 | |
|
59 | 2 | MuleMessageService mule = (MuleMessageService) executionContext.getJbpmContext().getServices() |
60 | |
.getMessageService(); |
61 | |
|
62 | 2 | if (transformers != null) |
63 | |
{ |
64 | 0 | endpoint += "?transformers=" + transformers; |
65 | |
} |
66 | |
|
67 | 2 | if (payload == null) |
68 | |
{ |
69 | 0 | if (payloadSource == null) |
70 | |
{ |
71 | 0 | payloadObject = executionContext.getVariable(ProcessConnector.PROCESS_VARIABLE_DATA); |
72 | 0 | if (payloadObject == null) |
73 | |
{ |
74 | 0 | payloadObject = executionContext.getVariable(ProcessConnector.PROCESS_VARIABLE_INCOMING); |
75 | |
} |
76 | |
} |
77 | |
else |
78 | |
{ |
79 | |
|
80 | |
|
81 | |
|
82 | 0 | String[] tokens = StringUtils.split(payloadSource, ".", 2); |
83 | 0 | payloadObject = executionContext.getVariable(tokens[0]); |
84 | 0 | if (tokens.length > 1) |
85 | |
{ |
86 | 0 | JXPathContext context = JXPathContext.newContext(payloadObject); |
87 | 0 | payloadObject = context.getValue(tokens[1].replaceAll("\\.", "/")); |
88 | |
} |
89 | 0 | } |
90 | |
} |
91 | |
else |
92 | |
{ |
93 | 2 | payloadObject = payload; |
94 | |
} |
95 | 2 | if (payloadObject == null) |
96 | |
{ |
97 | 0 | throw new IllegalArgumentException("Payload for message is null. Payload source is \"" |
98 | |
+ payloadSource + "\""); |
99 | |
} |
100 | |
|
101 | 2 | Map props = new HashMap(); |
102 | 2 | props.put(ProcessConnector.PROPERTY_PROCESS_TYPE, executionContext.getProcessDefinition().getName()); |
103 | 2 | props.put(ProcessConnector.PROPERTY_PROCESS_ID, new Long(executionContext.getProcessInstance() |
104 | |
.getId())); |
105 | 2 | props.put(MuleProperties.MULE_CORRELATION_ID_PROPERTY, new Long(executionContext.getProcessInstance() |
106 | |
.getId()).toString()); |
107 | 2 | props |
108 | |
.put(ProcessConnector.PROPERTY_PROCESS_STARTED, executionContext.getProcessInstance().getStart()); |
109 | 2 | if (properties != null) |
110 | |
{ |
111 | 0 | props.putAll(properties); |
112 | |
} |
113 | |
|
114 | 2 | MuleMessage response = mule.generateMessage(endpoint, payloadObject, props, synchronous); |
115 | 2 | if (synchronous) |
116 | |
{ |
117 | 0 | if (response != null) |
118 | |
{ |
119 | 0 | executionContext.setVariable(ProcessConnector.PROCESS_VARIABLE_INCOMING, response.getPayload()); |
120 | |
} |
121 | |
else |
122 | |
{ |
123 | 0 | logger.info("Synchronous message was sent to endpoint " + endpoint |
124 | |
+ ", but no response was returned."); |
125 | |
} |
126 | |
} |
127 | 2 | } |
128 | |
|
129 | |
} |