1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.providers.bpm; |
12 | |
|
13 | |
import org.mule.config.ConfigurationException; |
14 | |
import org.mule.config.i18n.MessageFactory; |
15 | |
import org.mule.extras.client.MuleClient; |
16 | |
import org.mule.providers.AbstractConnector; |
17 | |
import org.mule.umo.UMOException; |
18 | |
import org.mule.umo.UMOMessage; |
19 | |
import org.mule.umo.lifecycle.InitialisationException; |
20 | |
import org.mule.util.ClassUtils; |
21 | |
import org.mule.util.StringUtils; |
22 | |
|
23 | |
import java.util.Map; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 0 | public class ProcessConnector extends AbstractConnector implements MessageService |
31 | |
{ |
32 | |
|
33 | |
protected BPMS bpms; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
protected String bpmsClass; |
40 | |
|
41 | |
|
42 | |
protected String processIdField; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | protected boolean allowGlobalReceiver = false; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | 0 | protected boolean allowGlobalDispatcher = false; |
58 | |
|
59 | |
public static final String PROPERTY_ENDPOINT = "endpoint"; |
60 | |
public static final String PROPERTY_PROCESS_TYPE = "processType"; |
61 | |
public static final String PROPERTY_PROCESS_ID = "processId"; |
62 | |
public static final String PROPERTY_ACTION = "action"; |
63 | |
public static final String PROPERTY_TRANSITION = "transition"; |
64 | |
public static final String PROPERTY_PROCESS_STARTED = "started"; |
65 | |
public static final String ACTION_START = "start"; |
66 | |
public static final String ACTION_ADVANCE = "advance"; |
67 | |
public static final String ACTION_UPDATE = "update"; |
68 | |
public static final String ACTION_ABORT = "abort"; |
69 | |
public static final String PROCESS_VARIABLE_INCOMING = "incoming"; |
70 | |
public static final String PROCESS_VARIABLE_INCOMING_SOURCE = "incomingSource"; |
71 | |
public static final String PROCESS_VARIABLE_DATA = "data"; |
72 | |
|
73 | |
public static final String PROTOCOL = "bpm"; |
74 | |
public static final String GLOBAL_RECEIVER = PROTOCOL + "://*"; |
75 | |
|
76 | 0 | private MuleClient muleClient = null; |
77 | |
|
78 | |
public String getProtocol() |
79 | |
{ |
80 | 0 | return PROTOCOL; |
81 | |
} |
82 | |
|
83 | |
protected void doInitialise() throws InitialisationException |
84 | |
{ |
85 | |
try |
86 | |
{ |
87 | 0 | if (bpms == null) |
88 | |
{ |
89 | 0 | if (bpmsClass != null) |
90 | |
{ |
91 | 0 | logger.info("Instantiating BPMS from the default constructor for " + bpmsClass); |
92 | 0 | bpms = (BPMS) ClassUtils.instanciateClass(bpmsClass, new Object[0]); |
93 | |
} |
94 | |
else |
95 | |
{ |
96 | 0 | throw new ConfigurationException( |
97 | |
MessageFactory.createStaticMessage("Either the bpms or bpmsClass property must be set for this connector.")); |
98 | |
} |
99 | |
} |
100 | |
|
101 | |
|
102 | 0 | bpms.setMessageService(this); |
103 | |
|
104 | |
|
105 | |
|
106 | 0 | if ((allowGlobalDispatcher == true) && (muleClient == null)) |
107 | |
{ |
108 | 0 | muleClient = new MuleClient(false); |
109 | |
} |
110 | |
} |
111 | 0 | catch (Exception e) |
112 | |
{ |
113 | 0 | throw new InitialisationException(e, this); |
114 | 0 | } |
115 | 0 | } |
116 | |
|
117 | |
protected void doDispose() |
118 | |
{ |
119 | |
|
120 | 0 | } |
121 | |
|
122 | |
protected void doConnect() throws Exception |
123 | |
{ |
124 | |
|
125 | 0 | } |
126 | |
|
127 | |
protected void doDisconnect() throws Exception |
128 | |
{ |
129 | |
|
130 | 0 | } |
131 | |
|
132 | |
protected void doStart() throws UMOException |
133 | |
{ |
134 | |
|
135 | 0 | } |
136 | |
|
137 | |
protected void doStop() throws UMOException |
138 | |
{ |
139 | |
|
140 | 0 | } |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public ProcessMessageReceiver lookupReceiver(String processName, Object processId) |
150 | |
{ |
151 | 0 | ProcessMessageReceiver receiver = (ProcessMessageReceiver)lookupReceiver(toUrl(processName, processId)); |
152 | 0 | if (receiver == null) |
153 | |
{ |
154 | 0 | receiver = (ProcessMessageReceiver)lookupReceiver(toUrl(processName, null)); |
155 | |
} |
156 | 0 | if (receiver == null) |
157 | |
{ |
158 | 0 | receiver = (ProcessMessageReceiver)lookupReceiver(toUrl(null, null)); |
159 | |
} |
160 | 0 | return receiver; |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public String toUrl(String processName, Object processId) |
169 | |
{ |
170 | 0 | String url = getProtocol() + "://"; |
171 | 0 | if (StringUtils.isNotEmpty(processName)) |
172 | |
{ |
173 | 0 | url += processName; |
174 | 0 | if (processId != null) |
175 | |
{ |
176 | 0 | url += "/" + processId; |
177 | |
} |
178 | |
} |
179 | 0 | else if (isAllowGlobalReceiver()) |
180 | |
{ |
181 | 0 | return GLOBAL_RECEIVER; |
182 | |
} |
183 | |
else |
184 | |
{ |
185 | 0 | throw new IllegalArgumentException( |
186 | |
"No valid URL could be created for the given process name and ID: processName = " + processName + ", processId = " + processId); |
187 | |
} |
188 | 0 | return url; |
189 | |
} |
190 | |
|
191 | |
public UMOMessage generateMessage(String endpoint, |
192 | |
Object payloadObject, |
193 | |
Map messageProperties, |
194 | |
boolean synchronous) throws Exception |
195 | |
{ |
196 | 0 | String processName = (String)messageProperties.get(ProcessConnector.PROPERTY_PROCESS_TYPE); |
197 | 0 | Object processId = messageProperties.get(ProcessConnector.PROPERTY_PROCESS_ID); |
198 | |
|
199 | |
|
200 | 0 | ProcessMessageReceiver receiver = lookupReceiver(processName, processId); |
201 | 0 | if (receiver == null) |
202 | |
{ |
203 | 0 | throw new ConfigurationException(MessageFactory |
204 | |
.createStaticMessage("No corresponding receiver found for processName = " + processName |
205 | |
+ ", processId = " + processId)); |
206 | |
} |
207 | |
|
208 | 0 | if (synchronous) |
209 | |
{ |
210 | |
|
211 | 0 | return receiver.generateSynchronousEvent(endpoint, payloadObject, messageProperties); |
212 | |
} |
213 | |
else |
214 | |
{ |
215 | |
|
216 | 0 | receiver.generateAsynchronousEvent(endpoint, payloadObject, messageProperties); |
217 | 0 | return null; |
218 | |
} |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public BPMS getBpms() |
226 | |
{ |
227 | 0 | return bpms; |
228 | |
} |
229 | |
|
230 | |
public void setBpms(BPMS bpms) |
231 | |
{ |
232 | 0 | this.bpms = bpms; |
233 | 0 | } |
234 | |
|
235 | |
public String getBpmsClass() |
236 | |
{ |
237 | 0 | return bpmsClass; |
238 | |
} |
239 | |
|
240 | |
public void setBpmsClass(String bpmsClass) |
241 | |
{ |
242 | 0 | this.bpmsClass = bpmsClass; |
243 | 0 | } |
244 | |
|
245 | |
public MuleClient getMuleClient() |
246 | |
{ |
247 | 0 | return muleClient; |
248 | |
} |
249 | |
|
250 | |
public boolean isAllowGlobalDispatcher() |
251 | |
{ |
252 | 0 | return allowGlobalDispatcher; |
253 | |
} |
254 | |
|
255 | |
public void setAllowGlobalDispatcher(boolean allowGlobalDispatcher) |
256 | |
{ |
257 | 0 | this.allowGlobalDispatcher = allowGlobalDispatcher; |
258 | 0 | } |
259 | |
|
260 | |
public boolean isAllowGlobalReceiver() |
261 | |
{ |
262 | 0 | return allowGlobalReceiver; |
263 | |
} |
264 | |
|
265 | |
public void setAllowGlobalReceiver(boolean allowGlobalReceiver) |
266 | |
{ |
267 | 0 | this.allowGlobalReceiver = allowGlobalReceiver; |
268 | 0 | } |
269 | |
|
270 | |
public String getProcessIdField() |
271 | |
{ |
272 | 0 | return processIdField; |
273 | |
} |
274 | |
|
275 | |
public void setProcessIdField(String processIdField) |
276 | |
{ |
277 | 0 | this.processIdField = processIdField; |
278 | 0 | } |
279 | |
} |