1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.bpm.jbpm; |
12 | |
|
13 | |
import org.mule.providers.bpm.BPMS; |
14 | |
import org.mule.providers.bpm.MessageService; |
15 | |
import org.mule.util.IOUtils; |
16 | |
import org.mule.util.NumberUtils; |
17 | |
|
18 | |
import java.io.FileNotFoundException; |
19 | |
import java.io.IOException; |
20 | |
import java.io.InputStream; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import org.apache.commons.logging.Log; |
25 | |
import org.apache.commons.logging.LogFactory; |
26 | |
import org.jbpm.JbpmConfiguration; |
27 | |
import org.jbpm.JbpmContext; |
28 | |
import org.jbpm.graph.def.ProcessDefinition; |
29 | |
import org.jbpm.graph.exe.ProcessInstance; |
30 | |
import org.jbpm.taskmgmt.exe.TaskInstance; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public class Jbpm implements BPMS |
51 | |
{ |
52 | 0 | protected static transient Log logger = LogFactory.getLog(Jbpm.class); |
53 | |
|
54 | 0 | protected JbpmConfiguration jbpmConfiguration = null; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
protected boolean containerManaged; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public Jbpm() |
70 | |
{ |
71 | 0 | this(JbpmConfiguration.getInstance()); |
72 | 0 | containerManaged = false; |
73 | 0 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public Jbpm(JbpmConfiguration jbpmConfiguration) |
81 | 0 | { |
82 | 0 | setJbpmConfiguration(jbpmConfiguration); |
83 | 0 | containerManaged = true; |
84 | 0 | } |
85 | |
|
86 | |
public void destroy() |
87 | |
{ |
88 | 0 | if (containerManaged == false && jbpmConfiguration != null) |
89 | |
{ |
90 | 0 | jbpmConfiguration.close(); |
91 | 0 | jbpmConfiguration = null; |
92 | |
} |
93 | 0 | } |
94 | |
|
95 | |
public void setMessageService(MessageService msgService) |
96 | |
{ |
97 | 0 | JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); |
98 | |
try |
99 | |
{ |
100 | 0 | ((MuleMessageService)jbpmContext.getServices().getMessageService()).setMessageService(msgService); |
101 | |
} |
102 | |
finally |
103 | |
{ |
104 | 0 | jbpmContext.close(); |
105 | 0 | } |
106 | 0 | } |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public synchronized Object startProcess(Object processType) throws Exception |
118 | |
{ |
119 | 0 | return startProcess(processType, null, null); |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
public synchronized Object startProcess(Object processType, Object transition, Map processVariables) throws Exception |
128 | |
{ |
129 | 0 | ProcessInstance processInstance = null; |
130 | |
|
131 | 0 | JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); |
132 | |
try |
133 | |
{ |
134 | 0 | ProcessDefinition processDefinition = jbpmContext.getGraphSession().findLatestProcessDefinition( |
135 | |
(String)processType); |
136 | 0 | if (processDefinition == null) |
137 | 0 | throw new IllegalArgumentException("No process definition found for process " + processType); |
138 | |
|
139 | 0 | processInstance = new ProcessInstance(processDefinition); |
140 | |
|
141 | |
|
142 | 0 | if (processVariables != null && !processVariables.isEmpty()) |
143 | |
{ |
144 | 0 | processInstance.getContextInstance().addVariables(processVariables); |
145 | |
} |
146 | 0 | processInstance.getContextInstance().addVariables(processVariables); |
147 | |
|
148 | |
|
149 | 0 | processInstance.signal(); |
150 | |
|
151 | 0 | jbpmContext.save(processInstance); |
152 | |
|
153 | 0 | return processInstance; |
154 | |
} |
155 | 0 | catch (Exception e) |
156 | |
{ |
157 | 0 | jbpmContext.setRollbackOnly(); |
158 | 0 | throw e; |
159 | |
} |
160 | |
finally |
161 | |
{ |
162 | 0 | jbpmContext.close(); |
163 | |
} |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
public synchronized Object advanceProcess(Object processId) throws Exception |
172 | |
{ |
173 | 0 | return advanceProcess(processId, null, null); |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public synchronized Object advanceProcess(Object processId, Object transition, Map processVariables) |
182 | |
throws Exception |
183 | |
{ |
184 | 0 | ProcessInstance processInstance = null; |
185 | |
|
186 | 0 | JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); |
187 | |
try |
188 | |
{ |
189 | |
|
190 | 0 | processInstance = jbpmContext.getGraphSession() |
191 | |
.loadProcessInstance(NumberUtils.toLong(processId)); |
192 | |
|
193 | 0 | if (processInstance.hasEnded()) |
194 | |
{ |
195 | 0 | throw new IllegalStateException( |
196 | |
"Process cannot be advanced because it has already terminated, processId = " + processId); |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | 0 | if (processVariables != null && !processVariables.isEmpty()) |
203 | |
{ |
204 | 0 | processInstance.getContextInstance().addVariables(processVariables); |
205 | |
} |
206 | |
|
207 | |
|
208 | 0 | if (transition != null) |
209 | |
{ |
210 | 0 | processInstance.signal((String)transition); |
211 | |
} |
212 | |
else |
213 | |
{ |
214 | 0 | processInstance.signal(); |
215 | |
} |
216 | |
|
217 | |
|
218 | 0 | jbpmContext.save(processInstance); |
219 | |
|
220 | 0 | return processInstance; |
221 | |
} |
222 | 0 | catch (Exception e) |
223 | |
{ |
224 | 0 | jbpmContext.setRollbackOnly(); |
225 | 0 | throw e; |
226 | |
} |
227 | |
finally |
228 | |
{ |
229 | 0 | jbpmContext.close(); |
230 | |
} |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
public synchronized Object updateProcess(Object processId, Map processVariables) throws Exception |
239 | |
{ |
240 | 0 | ProcessInstance processInstance = null; |
241 | |
|
242 | 0 | JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); |
243 | |
try |
244 | |
{ |
245 | |
|
246 | 0 | processInstance = jbpmContext.getGraphSession() |
247 | |
.loadProcessInstance(NumberUtils.toLong(processId)); |
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | 0 | if (processVariables != null && !processVariables.isEmpty()) |
253 | |
{ |
254 | 0 | processInstance.getContextInstance().addVariables(processVariables); |
255 | |
} |
256 | |
|
257 | |
|
258 | 0 | jbpmContext.save(processInstance); |
259 | |
|
260 | 0 | return processInstance; |
261 | |
} |
262 | 0 | catch (Exception e) |
263 | |
{ |
264 | 0 | jbpmContext.setRollbackOnly(); |
265 | 0 | throw e; |
266 | |
} |
267 | |
finally |
268 | |
{ |
269 | 0 | jbpmContext.close(); |
270 | |
} |
271 | |
} |
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
public synchronized void abortProcess(Object processId) throws Exception |
277 | |
{ |
278 | 0 | JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); |
279 | |
try |
280 | |
{ |
281 | 0 | jbpmContext.getGraphSession().deleteProcessInstance(NumberUtils.toLong(processId)); |
282 | |
} |
283 | 0 | catch (Exception e) |
284 | |
{ |
285 | 0 | jbpmContext.setRollbackOnly(); |
286 | 0 | throw e; |
287 | |
} |
288 | |
finally |
289 | |
{ |
290 | 0 | jbpmContext.close(); |
291 | 0 | } |
292 | 0 | } |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
public boolean isProcess(Object obj) throws Exception |
299 | |
{ |
300 | 0 | return (obj instanceof ProcessInstance); |
301 | |
} |
302 | |
|
303 | |
public Object getId(Object process) throws Exception |
304 | |
{ |
305 | 0 | return new Long(((ProcessInstance)process).getId()); |
306 | |
} |
307 | |
|
308 | |
|
309 | |
|
310 | |
public Object getState(Object process) throws Exception |
311 | |
{ |
312 | 0 | ProcessInstance processInstance = (ProcessInstance) process; |
313 | |
|
314 | 0 | JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); |
315 | |
try |
316 | |
{ |
317 | |
|
318 | 0 | processInstance = jbpmContext.getGraphSession() |
319 | |
.loadProcessInstance(processInstance.getId()); |
320 | 0 | return processInstance.getRootToken().getNode().getName(); |
321 | |
} |
322 | |
finally |
323 | |
{ |
324 | 0 | jbpmContext.close(); |
325 | |
} |
326 | |
} |
327 | |
|
328 | |
public boolean hasEnded(Object process) throws Exception |
329 | |
{ |
330 | 0 | return ((ProcessInstance)process).hasEnded(); |
331 | |
} |
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
public Object lookupProcess(Object processId) throws Exception |
339 | |
{ |
340 | 0 | ProcessInstance processInstance = null; |
341 | |
|
342 | 0 | JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); |
343 | |
try |
344 | |
{ |
345 | |
|
346 | 0 | processInstance = jbpmContext.getGraphSession() |
347 | |
.loadProcessInstance(NumberUtils.toLong(processId)); |
348 | 0 | return processInstance; |
349 | |
} |
350 | |
finally |
351 | |
{ |
352 | 0 | jbpmContext.close(); |
353 | |
} |
354 | |
} |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
public void deployProcess(String processDefinitionFile) throws FileNotFoundException, IOException |
364 | |
{ |
365 | 0 | deployProcessFromStream(IOUtils.getResourceAsStream(processDefinitionFile, getClass())); |
366 | 0 | } |
367 | |
|
368 | |
public void deployProcessFromStream(InputStream processDefinition) |
369 | |
throws FileNotFoundException, IOException |
370 | |
{ |
371 | 0 | deployProcess(ProcessDefinition.parseXmlInputStream(processDefinition)); |
372 | 0 | } |
373 | |
|
374 | |
private void deployProcess(ProcessDefinition processDefinition) |
375 | |
{ |
376 | 0 | JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); |
377 | |
try |
378 | |
{ |
379 | 0 | jbpmContext.deployProcessDefinition(processDefinition); |
380 | |
} |
381 | |
finally |
382 | |
{ |
383 | 0 | jbpmContext.close(); |
384 | 0 | } |
385 | 0 | } |
386 | |
|
387 | |
public ListloadTasks(ProcessInstance process) |
388 | |
{ |
389 | 0 | ListtaskInstances = null; |
390 | |
|
391 | 0 | JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); |
392 | |
try |
393 | |
{ |
394 | 0 | taskInstances = jbpmContext.getTaskMgmtSession().findTaskInstancesByToken( |
395 | |
process.getRootToken().getId()); |
396 | 0 | return taskInstances; |
397 | |
} |
398 | |
finally |
399 | |
{ |
400 | 0 | jbpmContext.close(); |
401 | |
} |
402 | |
} |
403 | |
|
404 | |
public synchronized void completeTask(TaskInstance task) |
405 | |
{ |
406 | 0 | completeTask(task, null); |
407 | 0 | } |
408 | |
|
409 | |
public synchronized void completeTask(TaskInstance task, String transition) |
410 | |
{ |
411 | 0 | JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); |
412 | |
try |
413 | |
{ |
414 | 0 | task = jbpmContext.getTaskMgmtSession().loadTaskInstance(task.getId()); |
415 | 0 | if (transition != null) |
416 | |
{ |
417 | 0 | task.end(transition); |
418 | |
} |
419 | |
else |
420 | |
{ |
421 | 0 | task.end(); |
422 | |
} |
423 | |
} |
424 | |
finally |
425 | |
{ |
426 | 0 | jbpmContext.close(); |
427 | 0 | } |
428 | 0 | } |
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
public JbpmConfiguration getJbpmConfiguration() |
435 | |
{ |
436 | 0 | return jbpmConfiguration; |
437 | |
} |
438 | |
|
439 | |
public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) |
440 | |
{ |
441 | 0 | this.jbpmConfiguration = jbpmConfiguration; |
442 | 0 | } |
443 | |
} |