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 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 protected boolean allowGlobalReceiver = false;
51
52
53
54
55
56
57 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 private MuleClient muleClient = null;
77
78 public String getProtocol()
79 {
80 return PROTOCOL;
81 }
82
83 protected void doInitialise() throws InitialisationException
84 {
85 try
86 {
87 if (bpms == null)
88 {
89 if (bpmsClass != null)
90 {
91 logger.info("Instantiating BPMS from the default constructor for " + bpmsClass);
92 bpms = (BPMS) ClassUtils.instanciateClass(bpmsClass, new Object[0]);
93 }
94 else
95 {
96 throw new ConfigurationException(
97 MessageFactory.createStaticMessage("Either the bpms or bpmsClass property must be set for this connector."));
98 }
99 }
100
101
102 bpms.setMessageService(this);
103
104
105
106 if ((allowGlobalDispatcher == true) && (muleClient == null))
107 {
108 muleClient = new MuleClient(false);
109 }
110 }
111 catch (Exception e)
112 {
113 throw new InitialisationException(e, this);
114 }
115 }
116
117 protected void doDispose()
118 {
119
120 }
121
122 protected void doConnect() throws Exception
123 {
124
125 }
126
127 protected void doDisconnect() throws Exception
128 {
129
130 }
131
132 protected void doStart() throws UMOException
133 {
134
135 }
136
137 protected void doStop() throws UMOException
138 {
139
140 }
141
142
143
144
145
146
147
148
149 public ProcessMessageReceiver lookupReceiver(String processName, Object processId)
150 {
151 ProcessMessageReceiver receiver = (ProcessMessageReceiver)lookupReceiver(toUrl(processName, processId));
152 if (receiver == null)
153 {
154 receiver = (ProcessMessageReceiver)lookupReceiver(toUrl(processName, null));
155 }
156 if (receiver == null)
157 {
158 receiver = (ProcessMessageReceiver)lookupReceiver(toUrl(null, null));
159 }
160 return receiver;
161 }
162
163
164
165
166
167
168 public String toUrl(String processName, Object processId)
169 {
170 String url = getProtocol() + "://";
171 if (StringUtils.isNotEmpty(processName))
172 {
173 url += processName;
174 if (processId != null)
175 {
176 url += "/" + processId;
177 }
178 }
179 else if (isAllowGlobalReceiver())
180 {
181 return GLOBAL_RECEIVER;
182 }
183 else
184 {
185 throw new IllegalArgumentException(
186 "No valid URL could be created for the given process name and ID: processName = " + processName + ", processId = " + processId);
187 }
188 return url;
189 }
190
191 public UMOMessage generateMessage(String endpoint,
192 Object payloadObject,
193 Map messageProperties,
194 boolean synchronous) throws Exception
195 {
196 String processName = (String)messageProperties.get(ProcessConnector.PROPERTY_PROCESS_TYPE);
197 Object processId = messageProperties.get(ProcessConnector.PROPERTY_PROCESS_ID);
198
199
200 ProcessMessageReceiver receiver = lookupReceiver(processName, processId);
201 if (receiver == null)
202 {
203 throw new ConfigurationException(MessageFactory
204 .createStaticMessage("No corresponding receiver found for processName = " + processName
205 + ", processId = " + processId));
206 }
207
208 if (synchronous)
209 {
210
211 return receiver.generateSynchronousEvent(endpoint, payloadObject, messageProperties);
212 }
213 else
214 {
215
216 receiver.generateAsynchronousEvent(endpoint, payloadObject, messageProperties);
217 return null;
218 }
219 }
220
221
222
223
224
225 public BPMS getBpms()
226 {
227 return bpms;
228 }
229
230 public void setBpms(BPMS bpms)
231 {
232 this.bpms = bpms;
233 }
234
235 public String getBpmsClass()
236 {
237 return bpmsClass;
238 }
239
240 public void setBpmsClass(String bpmsClass)
241 {
242 this.bpmsClass = bpmsClass;
243 }
244
245 public MuleClient getMuleClient()
246 {
247 return muleClient;
248 }
249
250 public boolean isAllowGlobalDispatcher()
251 {
252 return allowGlobalDispatcher;
253 }
254
255 public void setAllowGlobalDispatcher(boolean allowGlobalDispatcher)
256 {
257 this.allowGlobalDispatcher = allowGlobalDispatcher;
258 }
259
260 public boolean isAllowGlobalReceiver()
261 {
262 return allowGlobalReceiver;
263 }
264
265 public void setAllowGlobalReceiver(boolean allowGlobalReceiver)
266 {
267 this.allowGlobalReceiver = allowGlobalReceiver;
268 }
269
270 public String getProcessIdField()
271 {
272 return processIdField;
273 }
274
275 public void setProcessIdField(String processIdField)
276 {
277 this.processIdField = processIdField;
278 }
279 }