1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.jbpm; |
8 | |
|
9 | |
import org.mule.MessageExchangePattern; |
10 | |
import org.mule.api.MuleMessage; |
11 | |
import org.mule.api.config.MuleProperties; |
12 | |
import org.mule.module.bpm.MessageService; |
13 | |
import org.mule.module.bpm.Process; |
14 | |
import org.mule.util.ClassUtils; |
15 | |
|
16 | |
import java.util.HashMap; |
17 | |
import java.util.Map; |
18 | |
import java.util.Map.Entry; |
19 | |
|
20 | |
import org.jbpm.api.JbpmException; |
21 | |
import org.jbpm.api.activity.ActivityExecution; |
22 | |
import org.jbpm.api.listener.EventListener; |
23 | |
import org.jbpm.api.listener.EventListenerExecution; |
24 | |
import org.jbpm.api.model.OpenExecution; |
25 | |
import org.jbpm.internal.log.Log; |
26 | |
import org.jbpm.jpdl.internal.activity.JpdlActivity; |
27 | |
import org.jbpm.pvm.internal.env.EnvironmentImpl; |
28 | |
import org.jbpm.pvm.internal.model.ExecutionImpl; |
29 | |
import org.jbpm.pvm.internal.script.ScriptManager; |
30 | |
|
31 | 0 | public class MuleSendActivity extends JpdlActivity implements EventListener |
32 | |
{ |
33 | |
|
34 | |
|
35 | |
|
36 | |
private String endpoint; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | private MessageExchangePattern mep = MessageExchangePattern.REQUEST_RESPONSE; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
private Class responsePayloadClass; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
private String responseVariableName; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
private String payloadExpression; |
57 | |
|
58 | 0 | private static final Log log = Log.getLog(MuleSendActivity.class.getName()); |
59 | |
|
60 | |
public void execute(ActivityExecution execution) throws Exception |
61 | |
{ |
62 | 0 | perform(execution); |
63 | 0 | ((ExecutionImpl) execution).historyAutomatic(); |
64 | 0 | } |
65 | |
|
66 | |
public void notify(EventListenerExecution execution) throws Exception |
67 | |
{ |
68 | 0 | perform(execution); |
69 | 0 | } |
70 | |
|
71 | |
public void perform(OpenExecution execution) throws Exception |
72 | |
{ |
73 | 0 | MessageService mule = EnvironmentImpl.getCurrent().get(MuleMessageService.class); |
74 | 0 | if (mule == null) |
75 | |
{ |
76 | 0 | throw new JbpmException("The Mule MessageService is not available from the ProcessEngine, you may need to add it to your jbpm.cfg.xml file"); |
77 | |
} |
78 | |
|
79 | 0 | Object payloadObject = null; |
80 | 0 | if (payloadExpression == null) |
81 | |
{ |
82 | 0 | payloadObject = execution.getVariable(Process.PROCESS_VARIABLE_DATA); |
83 | 0 | if (payloadObject == null) |
84 | |
{ |
85 | 0 | payloadObject = execution.getVariable(Process.PROCESS_VARIABLE_INCOMING); |
86 | |
} |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | |
|
91 | |
|
92 | |
|
93 | 0 | payloadObject = ScriptManager.getScriptManager().evaluateExpression(payloadExpression, null); |
94 | |
} |
95 | 0 | if (payloadObject == null) |
96 | |
{ |
97 | 0 | throw new IllegalArgumentException("Payload for message is null. Payload source is \"" |
98 | |
+ payloadExpression + "\""); |
99 | |
} |
100 | |
|
101 | 0 | Map props = new HashMap(); |
102 | |
|
103 | 0 | props.put(Process.PROPERTY_PROCESS_TYPE, ((ExecutionImpl) execution).getProcessDefinition().getName()); |
104 | 0 | props.put(Process.PROPERTY_PROCESS_ID, execution.getId()); |
105 | 0 | String state = Jbpm.getState(execution.getProcessInstance()); |
106 | 0 | props.put("MULE_BPM_PROCESS_STATE", state); |
107 | 0 | log.debug("process state: " + state); |
108 | |
|
109 | |
|
110 | 0 | for (Entry<String, ?> var : execution.getVariables().entrySet()) |
111 | |
{ |
112 | 0 | if (!var.getKey().startsWith(MuleProperties.PROPERTY_PREFIX)) |
113 | |
{ |
114 | 0 | log.debug("process var: " + var.getKey() + " = " + var.getValue()); |
115 | 0 | props.put(var.getKey(), var.getValue()); |
116 | |
} |
117 | |
} |
118 | |
|
119 | |
|
120 | 0 | endpoint = (String) ScriptManager.getScriptManager().evaluateExpression(endpoint, null); |
121 | 0 | MuleMessage response = mule.generateMessage(endpoint, payloadObject, props, mep); |
122 | |
|
123 | 0 | if (mep.hasResponse() && response != null) |
124 | |
{ |
125 | 0 | Object responsePayload = response.getPayload(); |
126 | |
|
127 | |
|
128 | 0 | if (responsePayloadClass != null) |
129 | |
{ |
130 | 0 | log.debug("Validating response type = " + responsePayload.getClass() + ", expected = " + responsePayloadClass); |
131 | 0 | if (!responsePayloadClass.isAssignableFrom(responsePayload.getClass())) |
132 | |
{ |
133 | 0 | throw new JbpmException("Response message is of type " + responsePayload.getClass() + " but expected type is " + responsePayloadClass); |
134 | |
} |
135 | |
} |
136 | |
|
137 | 0 | if (responseVariableName != null) |
138 | |
{ |
139 | 0 | if (responsePayload != null) |
140 | |
{ |
141 | 0 | execution.setVariable(responseVariableName, responsePayload); |
142 | |
} |
143 | |
else |
144 | |
{ |
145 | 0 | log.info("Synchronous message was sent to endpoint " + endpoint + ", but no response was returned."); |
146 | |
} |
147 | |
} |
148 | |
} |
149 | 0 | } |
150 | |
|
151 | |
public void setEndpoint(String endpoint) |
152 | |
{ |
153 | 0 | this.endpoint = endpoint; |
154 | 0 | } |
155 | |
|
156 | |
public void setMessageExchangePattern(String mepString) |
157 | |
{ |
158 | 0 | if (mepString != null) |
159 | |
{ |
160 | 0 | this.mep = MessageExchangePattern.fromString(mepString); |
161 | |
} |
162 | 0 | } |
163 | |
|
164 | |
public String getPayloadExpression() |
165 | |
{ |
166 | 0 | return payloadExpression; |
167 | |
} |
168 | |
|
169 | |
public void setPayloadExpression(String payloadExpression) |
170 | |
{ |
171 | 0 | this.payloadExpression = payloadExpression; |
172 | 0 | } |
173 | |
|
174 | |
public String getResponseVariableName() |
175 | |
{ |
176 | 0 | return responseVariableName; |
177 | |
} |
178 | |
|
179 | |
public void setResponseVariableName(String responseVariableName) |
180 | |
{ |
181 | 0 | this.responseVariableName = responseVariableName; |
182 | 0 | } |
183 | |
|
184 | |
public void setResponsePayloadClass(String className) |
185 | |
{ |
186 | 0 | if (className != null) |
187 | |
{ |
188 | |
try |
189 | |
{ |
190 | 0 | responsePayloadClass = ClassUtils.loadClass(className, this.getClass()); |
191 | |
} |
192 | 0 | catch (ClassNotFoundException e) |
193 | |
{ |
194 | 0 | log.error("Expected message type not valid: " + e.getMessage()); |
195 | 0 | } |
196 | |
} |
197 | 0 | } |
198 | |
|
199 | |
public Class getResponsePayloadClass() |
200 | |
{ |
201 | 0 | return responsePayloadClass; |
202 | |
} |
203 | |
} |