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