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