1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.jbpm; |
12 | |
|
13 | |
import org.mule.module.bpm.Process; |
14 | |
import org.mule.util.ClassUtils; |
15 | |
|
16 | |
import java.util.Map; |
17 | |
|
18 | |
import org.jbpm.api.JbpmException; |
19 | |
import org.jbpm.api.activity.ActivityExecution; |
20 | |
import org.jbpm.internal.log.Log; |
21 | |
import org.jbpm.jpdl.internal.activity.StateActivity; |
22 | |
import org.jbpm.pvm.internal.model.ExecutionImpl; |
23 | |
|
24 | |
public class MuleReceiveActivity extends StateActivity |
25 | |
{ |
26 | |
|
27 | |
|
28 | |
|
29 | |
private String endpoint; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
private Class payloadClass; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
private String variableName; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
private final boolean startState; |
45 | |
|
46 | 0 | private static final Log log = Log.getLog(MuleReceiveActivity.class.getName()); |
47 | |
|
48 | |
public MuleReceiveActivity(boolean startState) |
49 | |
{ |
50 | 0 | super(); |
51 | 0 | this.startState = startState; |
52 | 0 | } |
53 | |
|
54 | |
@Override |
55 | |
public void execute(ExecutionImpl execution) |
56 | |
{ |
57 | 0 | execution.historyActivityStart(); |
58 | |
|
59 | 0 | if (startState) |
60 | |
{ |
61 | 0 | execution.signal(); |
62 | |
} |
63 | |
else |
64 | |
{ |
65 | 0 | execution.waitForSignal(); |
66 | |
} |
67 | 0 | } |
68 | |
|
69 | |
@Override |
70 | |
public void signal(ActivityExecution execution, String signalName, Map<String, ?> parameters) |
71 | |
throws Exception |
72 | |
{ |
73 | 0 | Object payload = execution.getVariable(Process.PROCESS_VARIABLE_INCOMING); |
74 | |
|
75 | |
|
76 | 0 | if (endpoint != null) |
77 | |
{ |
78 | 0 | String messageSource = (String) execution.getVariable(Process.PROCESS_VARIABLE_INCOMING_SOURCE); |
79 | 0 | log.debug("Validating message source = " + messageSource + ", expected = " + endpoint); |
80 | 0 | if (!endpoint.equalsIgnoreCase(messageSource)) |
81 | |
{ |
82 | 0 | throw new JbpmException("Incoming message source is " + messageSource + " but expected source is " + endpoint); |
83 | |
} |
84 | |
} |
85 | |
|
86 | |
|
87 | 0 | if (payloadClass != null) |
88 | |
{ |
89 | 0 | log.debug("Validating message type = " + payload.getClass() + ", expected = " + payloadClass); |
90 | 0 | if (!payloadClass.isAssignableFrom(payload.getClass())) |
91 | |
{ |
92 | 0 | throw new JbpmException("Incoming message is of type " + payload.getClass() + " but expected type is " + payloadClass); |
93 | |
} |
94 | |
} |
95 | |
|
96 | |
|
97 | 0 | if (variableName != null) |
98 | |
{ |
99 | 0 | if (payload != null) |
100 | |
{ |
101 | 0 | log.debug("Storing incoming message to variable " + variableName + ", payload = " + payload); |
102 | 0 | execution.setVariable(variableName, payload); |
103 | |
} |
104 | |
else |
105 | |
{ |
106 | 0 | log.info("Synchronous message was sent to endpoint " + endpoint + ", but no response was returned."); |
107 | |
} |
108 | |
} |
109 | |
|
110 | 0 | super.signal(execution, signalName, parameters); |
111 | 0 | } |
112 | |
|
113 | |
public String getEndpoint() |
114 | |
{ |
115 | 0 | return endpoint; |
116 | |
} |
117 | |
|
118 | |
public void setEndpoint(String endpoint) |
119 | |
{ |
120 | 0 | this.endpoint = endpoint; |
121 | 0 | } |
122 | |
|
123 | |
public String getVariableName() |
124 | |
{ |
125 | 0 | return variableName; |
126 | |
} |
127 | |
|
128 | |
public void setVariableName(String variableName) |
129 | |
{ |
130 | 0 | this.variableName = variableName; |
131 | 0 | } |
132 | |
|
133 | |
public void setPayloadClass(String className) |
134 | |
{ |
135 | 0 | if (className != null) |
136 | |
{ |
137 | |
try |
138 | |
{ |
139 | 0 | payloadClass = ClassUtils.loadClass(className, this.getClass()); |
140 | |
} |
141 | 0 | catch (ClassNotFoundException e) |
142 | |
{ |
143 | 0 | log.error("Expected message type not valid: " + e.getMessage()); |
144 | 0 | } |
145 | |
} |
146 | 0 | } |
147 | |
|
148 | |
public Class getPayloadClass() |
149 | |
{ |
150 | 0 | return payloadClass; |
151 | |
} |
152 | |
} |