1
2
3
4
5
6
7
8
9
10
11 package org.mule.providers.bpm;
12
13 import org.mule.config.MuleProperties;
14 import org.mule.config.i18n.MessageFactory;
15 import org.mule.impl.MuleMessage;
16 import org.mule.providers.AbstractMessageDispatcher;
17 import org.mule.providers.NullPayload;
18 import org.mule.umo.UMOEvent;
19 import org.mule.umo.UMOMessage;
20 import org.mule.umo.endpoint.UMOImmutableEndpoint;
21 import org.mule.umo.provider.DispatchException;
22 import org.mule.util.StringUtils;
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27
28
29
30
31 public class ProcessMessageDispatcher extends AbstractMessageDispatcher
32 {
33 private ProcessConnector connector;
34
35 public ProcessMessageDispatcher(UMOImmutableEndpoint endpoint)
36 {
37 super(endpoint);
38 this.connector = (ProcessConnector)endpoint.getConnector();
39 }
40
41
42
43
44
45
46 public UMOMessage doSend(UMOEvent event) throws Exception
47 {
48 Object process = processAction(event);
49
50 if (process != null)
51 {
52 UMOMessage msg = new MuleMessage(process);
53 msg.setProperty(ProcessConnector.PROPERTY_PROCESS_ID, connector.getBpms().getId(process));
54 return msg;
55 }
56 else
57 {
58 throw new DispatchException(MessageFactory
59 .createStaticMessage("Synchronous process invocation must return the new process state."),
60 event.getMessage(), event.getEndpoint());
61 }
62 }
63
64
65
66
67 public void doDispatch(UMOEvent event) throws Exception
68 {
69 processAction(event);
70 }
71
72 protected Object processAction(UMOEvent event) throws Exception
73 {
74
75 Object process = null;
76
77
78 Map processVariables = new HashMap();
79 if (event != null)
80 {
81 String propertyName;
82 for (Iterator iterator = event.getMessage().getPropertyNames().iterator(); iterator.hasNext();)
83 {
84 propertyName = (String)iterator.next();
85 processVariables.put(propertyName, event.getMessage().getProperty(propertyName));
86 }
87
88 Object payload = event.getTransformedMessage();
89 if (payload != null && !(payload instanceof NullPayload))
90 {
91
92 processVariables.put(ProcessConnector.PROCESS_VARIABLE_INCOMING, payload);
93
94
95
96 String originatingEndpoint = event.getMessage().getStringProperty(
97 MuleProperties.MULE_ORIGINATING_ENDPOINT_PROPERTY, null);
98 if (StringUtils.isNotEmpty(originatingEndpoint))
99 {
100 processVariables.put(ProcessConnector.PROCESS_VARIABLE_INCOMING_SOURCE,
101 originatingEndpoint);
102 }
103 }
104 }
105
106
107 Object processType = event.getProperty(ProcessConnector.PROPERTY_PROCESS_TYPE,
108 true);
109 processVariables.remove(ProcessConnector.PROPERTY_PROCESS_TYPE);
110
111
112
113 Object processId;
114 String processIdField = connector.getProcessIdField();
115 if (StringUtils.isNotEmpty(processIdField))
116 {
117 processId = event.getProperty(processIdField,
118 }
119
120
121 processId = event.getProperty(ProcessConnector.PROPERTY_PROCESS_ID,
122 processVariables.remove(ProcessConnector.PROPERTY_PROCESS_ID);
123
124
125 String action = event.getMessage().getStringProperty(ProcessConnector.PROPERTY_ACTION,
126 ProcessConnector.ACTION_ADVANCE);
127 processVariables.remove(ProcessConnector.PROPERTY_ACTION);
128
129 Object transition = event.getMessage().getProperty(ProcessConnector.PROPERTY_TRANSITION);
130 processVariables.remove(ProcessConnector.PROPERTY_TRANSITION);
131
132
133
134 String temp;
135 temp = event.getEndpoint().getEndpointURI().getHost();
136 if (StringUtils.isNotEmpty(temp))
137 {
138 processType = temp;
139 }
140 temp = event.getEndpoint().getEndpointURI().getPath();
141 if (StringUtils.isNotEmpty(temp))
142 {
143
144 if (temp.startsWith("/"))
145 {
146 temp = StringUtils.right(temp, temp.length() - 1);
147 }
148
149 if (temp.indexOf("/") != -1)
150 {
151 throw new IllegalArgumentException("Unexpected format in the path of the URL: " + temp);
152 }
153 processId = temp;
154 }
155
156
157
158
159 if (processId == null || action.equals(ProcessConnector.ACTION_START))
160 {
161 if (processType != null)
162 {
163 process = connector.getBpms().startProcess(processType, transition, processVariables);
164 if ((process != null) && logger.isInfoEnabled())
165 {
166 logger.info("New process started, ID = " + connector.getBpms().getId(process));
167 }
168 }
169 else
170 {
171 throw new IllegalArgumentException("Process type is missing, cannot start a new process.");
172 }
173 }
174
175
176 else if (action.equals(ProcessConnector.ACTION_UPDATE))
177 {
178 if (processId != null)
179 {
180 process = connector.getBpms().updateProcess(processId, processVariables);
181 if ((process != null) && logger.isInfoEnabled())
182 {
183 logger.info("Process variables updated, ID = " + connector.getBpms().getId(process));
184 }
185 }
186 else
187 {
188 throw new IllegalArgumentException("Process ID is missing, cannot update process.");
189 }
190 }
191
192
193 else if (action.equals(ProcessConnector.ACTION_ABORT))
194 {
195 if (processId != null)
196 {
197 connector.getBpms().abortProcess(processId);
198 process = NullPayload.getInstance();
199 logger.info("Process aborted, ID = " + processId);
200 }
201 else
202 {
203 throw new IllegalArgumentException("Process ID is missing, cannot abort process.");
204 }
205 }
206
207
208 else
209 {
210 if (processId != null)
211 {
212 process = connector.getBpms().advanceProcess(processId, transition, processVariables);
213 if ((process != null) && logger.isInfoEnabled())
214 {
215 logger.info("Process advanced, ID = " + connector.getBpms().getId(process)
216 + ", new state = " + connector.getBpms().getState(process));
217 }
218 }
219 else
220 {
221 throw new IllegalArgumentException("Process ID is missing, cannot advance process.");
222 }
223 }
224
225 return process;
226 }
227
228 protected UMOMessage doReceive(long timeout) throws Exception
229 {
230 throw new UnsupportedOperationException(
231 "doReceive() is not implemented by the ProcessMessageDispatcher");
232 }
233
234 protected void doConnect() throws Exception
235 {
236
237 }
238
239 protected void doDisconnect() throws Exception
240 {
241
242 }
243
244 protected void doDispose()
245 {
246
247 }
248
249 }