This repository has been archived by the owner on May 12, 2021. It is now read-only.
/
PICK.java
385 lines (331 loc) · 16.9 KB
/
PICK.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.ode.bpel.runtime;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.xml.namespace.QName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.ode.bpel.common.CorrelationKey;
import org.apache.ode.bpel.common.CorrelationKeySet;
import org.apache.ode.bpel.common.FaultException;
import org.apache.ode.bpel.evt.VariableModificationEvent;
import org.apache.ode.bpel.explang.EvaluationException;
import org.apache.ode.bpel.o.OElementVarType;
import org.apache.ode.bpel.o.OMessageVarType;
import org.apache.ode.bpel.o.OPickReceive;
import org.apache.ode.bpel.o.OScope;
import org.apache.ode.bpel.o.OMessageVarType.Part;
import org.apache.ode.bpel.runtime.channels.FaultData;
import org.apache.ode.bpel.runtime.channels.PickResponseChannel;
import org.apache.ode.bpel.runtime.channels.PickResponseChannelListener;
import org.apache.ode.bpel.runtime.channels.TerminationChannelListener;
import org.apache.ode.utils.DOMUtils;
import org.apache.ode.utils.xsd.Duration;
import org.apache.ode.bpel.evar.ExternalVariableModuleException;
import org.apache.ode.bpel.iapi.BpelEngineException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/**
* Template for the BPEL <code>pick</code> activity.
*/
class PICK extends ACTIVITY {
private static final long serialVersionUID = 1L;
private static final Logger __log = LoggerFactory.getLogger(PICK.class);
private OPickReceive _opick;
// if multiple alarms are set, this is the alarm the evaluates to
// the shortest absolute time until firing.
private OPickReceive.OnAlarm _alarm = null;
public PICK(ActivityInfo self, ScopeFrame scopeFrame, LinkFrame linkFrame) {
super(self, scopeFrame, linkFrame);
_opick = (OPickReceive) self.o;
}
/**
* @see org.apache.ode.jacob.JacobRunnable#run()
*/
public void run() {
PickResponseChannel pickResponseChannel = newChannel(PickResponseChannel.class);
Date timeout;
Selector[] selectors;
try {
selectors = new Selector[_opick.onMessages.size()];
int idx = 0;
for (OPickReceive.OnMessage onMessage : _opick.onMessages) {
// collect all initiated correlations
Set<OScope.CorrelationSet> matchCorrelations = new HashSet<OScope.CorrelationSet>();
matchCorrelations.addAll(onMessage.matchCorrelations);
for( OScope.CorrelationSet cset : onMessage.joinCorrelations ) {
if(getBpelRuntimeContext().isCorrelationInitialized(_scopeFrame.resolve(cset))) {
matchCorrelations.add(cset);
}
}
PartnerLinkInstance pLinkInstance = _scopeFrame.resolve(onMessage.partnerLink);
CorrelationKeySet keySet = resolveCorrelationKey(pLinkInstance, matchCorrelations);
selectors[idx] = new Selector(idx, pLinkInstance, onMessage.operation.getName(), onMessage.operation
.getOutput() == null, onMessage.messageExchangeId, keySet, onMessage.route);
idx++;
}
timeout = null;
for (OPickReceive.OnAlarm onAlarm : _opick.onAlarms) {
Date dt = onAlarm.forExpr != null ? offsetFromNow(getBpelRuntimeContext().getExpLangRuntime()
.evaluateAsDuration(onAlarm.forExpr, getEvaluationContext())) : getBpelRuntimeContext()
.getExpLangRuntime().evaluateAsDate(onAlarm.untilExpr, getEvaluationContext()).getTime();
if (timeout == null || timeout.compareTo(dt) > 0) {
timeout = dt;
_alarm = onAlarm;
}
}
getBpelRuntimeContext().select(pickResponseChannel, timeout, _opick.createInstanceFlag, selectors);
} catch (FaultException e) {
__log.error("",e);
FaultData fault = createFault(e.getQName(), _opick, e.getMessage());
dpe(_opick.outgoingLinks);
_self.parent.completed(fault, CompensationHandler.emptySet());
return;
} catch (EvaluationException e) {
String msg = "Unexpected evaluation error evaluating alarm.";
__log.error(msg, e);
throw new InvalidProcessException(msg, e);
}
// Dead path all the alarms that have no chace of coming first.
for (OPickReceive.OnAlarm oa : _opick.onAlarms) {
if (!oa.equals(_alarm)) {
dpe(oa.activity);
}
}
instance(new WAITING(pickResponseChannel));
}
/**
* Resolves the correlation key from the given PartnerLinkInstance and a match type correlation(non-initiate or
* already initialized join correlation).
*
* @param pLinkInstance the partner link instance
* @param matchCorrelations the match type correlation
* @return returns the resolved CorrelationKey
* @throws FaultException thrown when the correlation is not initialized and createInstance flag is not set
*/
private CorrelationKeySet resolveCorrelationKey(PartnerLinkInstance pLinkInstance, Set<OScope.CorrelationSet> matchCorrelations) throws FaultException {
CorrelationKeySet keySet = new CorrelationKeySet(); // is empty for the case of the createInstance activity
if (matchCorrelations.isEmpty() && !_opick.createInstanceFlag) {
// Adding a route for opaque correlation. In this case,
// correlation is on "out-of-band" session-id
String sessionId = getBpelRuntimeContext().fetchMySessionId(pLinkInstance);
keySet.add(new CorrelationKey("-1", new String[] { sessionId }));
} else if (!matchCorrelations.isEmpty()) {
for( OScope.CorrelationSet cset : matchCorrelations ) {
CorrelationKey key = null;
if(!getBpelRuntimeContext().isCorrelationInitialized(
_scopeFrame.resolve(cset))) {
if (!_opick.createInstanceFlag) {
throw new FaultException(_opick.getOwner().constants.qnCorrelationViolation,
"Correlation not initialized.");
}
} else {
key = getBpelRuntimeContext().readCorrelation(_scopeFrame.resolve(cset));
assert key != null;
}
if( key != null ) {
keySet.add(key);
}
}
}
return keySet;
}
/**
* Calculate a duration offset from right now.
*
* @param duration
* the offset
* @return the resulting date.
*/
private static Date offsetFromNow(Duration duration) {
Calendar cal = Calendar.getInstance();
duration.addTo(cal);
return cal.getTime();
}
@SuppressWarnings("unchecked")
private void initVariable(String mexId, OPickReceive.OnMessage onMessage) {
// This is allowed, if there is no parts in the message for example.
if (onMessage.variable == null) return;
Element msgEl;
try {
// At this point, not being able to get the request is most probably
// a mex that hasn't properly replied to (process issue).
msgEl = getBpelRuntimeContext().getMyRequest(mexId);
} catch (BpelEngineException e) {
__log.error("The message exchange seems to be in an unconsistent state, you're " +
"probably missing a reply on a request/response interaction.");
_self.parent.failure(e.toString(), null);
return;
}
Collection<String> partNames = (Collection<String>) onMessage.operation.getInput().getMessage().getParts().keySet();
// Let's do some sanity checks here so that we don't get weird errors in assignment later.
// The engine should have checked to make sure that the messages that are delivered conform
// to the correct format; but you know what they say, don't trust anyone.
if (!(onMessage.variable.type instanceof OMessageVarType)) {
String errmsg = "Non-message variable for receive: should have been picked up by static analysis.";
__log.error(errmsg);
throw new InvalidProcessException(errmsg);
}
OMessageVarType vartype = (OMessageVarType) onMessage.variable.type;
// Check that each part contains what we expect.
for (String pName : partNames) {
QName partName = new QName(null, pName);
Element msgPart = DOMUtils.findChildByName(msgEl, partName);
Part part = vartype.parts.get(pName);
if (part == null) {
String errmsg = "Inconsistent WSDL, part " + pName + " not found in message type " + vartype.messageType;
__log.error(errmsg);
throw new InvalidProcessException(errmsg);
}
if (msgPart == null) {
String errmsg = "Message missing part: " + pName;
__log.error(errmsg);
throw new InvalidContextException(errmsg);
}
if (part.type instanceof OElementVarType) {
OElementVarType ptype = (OElementVarType) part.type;
Element e = DOMUtils.getFirstChildElement(msgPart);
if (e == null) {
String errmsg = "Message (element) part " + pName + " did not contain child element.";
__log.error(errmsg);
throw new InvalidContextException(errmsg);
}
QName qn = new QName(e.getNamespaceURI(), e.getLocalName());
if(!qn.equals(ptype.elementType)) {
String errmsg = "Message (element) part " + pName + " did not contain correct child element: expected "
+ ptype.elementType + " but got " + qn;
__log.error(errmsg);
throw new InvalidContextException(errmsg);
}
}
}
VariableInstance vinst = _scopeFrame.resolve(onMessage.variable);
try {
initializeVariable(vinst, msgEl);
} catch (ExternalVariableModuleException e) {
__log.error("Exception while initializing external variable", e);
_self.parent.failure(e.toString(), null);
return;
}
// Generating event
VariableModificationEvent se = new VariableModificationEvent(vinst.declaration.name);
se.setNewValue(msgEl);
if (_opick.debugInfo != null)
se.setLineNo(_opick.debugInfo.startLine);
sendEvent(se);
}
private class WAITING extends BpelJacobRunnable {
private static final long serialVersionUID = 1L;
private PickResponseChannel _pickResponseChannel;
private WAITING(PickResponseChannel pickResponseChannel) {
this._pickResponseChannel = pickResponseChannel;
}
public void run() {
object(false, new PickResponseChannelListener(_pickResponseChannel) {
private static final long serialVersionUID = -8237296827418738011L;
public void onRequestRcvd(int selectorIdx, String mexId) {
OPickReceive.OnMessage onMessage = _opick.onMessages.get(selectorIdx);
// dead path the non-selected onMessage blocks.
for (OPickReceive.OnMessage onmsg : _opick.onMessages) {
if (!onmsg.equals(onMessage)) {
dpe(onmsg.activity);
}
}
// dead-path the alarm (if any)
if (_alarm != null) {
dpe(_alarm.activity);
}
getBpelRuntimeContext().cancelOutstandingRequests(_pickResponseChannel.export());
FaultData fault;
initVariable(mexId, onMessage);
try {
VariableInstance vinst = _scopeFrame.resolve(onMessage.variable);
for (OScope.CorrelationSet cset : onMessage.initCorrelations) {
initializeCorrelation(_scopeFrame.resolve(cset), vinst);
}
for( OScope.CorrelationSet cset : onMessage.joinCorrelations ) {
// will be ignored if already initialized
initializeCorrelation(_scopeFrame.resolve(cset), vinst);
}
if (onMessage.partnerLink.hasPartnerRole()) {
// Trying to initialize partner epr based on a
// message-provided epr/session.
if (!getBpelRuntimeContext().isPartnerRoleEndpointInitialized(
_scopeFrame.resolve(onMessage.partnerLink))
|| !onMessage.partnerLink.initializePartnerRole) {
Node fromEpr = getBpelRuntimeContext().getSourceEPR(mexId);
if (fromEpr != null) {
if (__log.isDebugEnabled())
__log.debug("Received callback EPR " + DOMUtils.domToString(fromEpr)
+ " saving it on partner link " + onMessage.partnerLink.getName());
getBpelRuntimeContext().writeEndpointReference(
_scopeFrame.resolve(onMessage.partnerLink), (Element) fromEpr);
}
}
String partnersSessionId = getBpelRuntimeContext().getSourceSessionId(mexId);
if (partnersSessionId != null)
getBpelRuntimeContext().initializePartnersSessionId(
_scopeFrame.resolve(onMessage.partnerLink), partnersSessionId);
}
// this request is now waiting for a reply
getBpelRuntimeContext().processOutstandingRequest(_scopeFrame.resolve(onMessage.partnerLink),
onMessage.operation.getName(), onMessage.messageExchangeId, mexId);
} catch (FaultException e) {
__log.error("",e);
fault = createFault(e.getQName(), onMessage);
_self.parent.completed(fault, CompensationHandler.emptySet());
dpe(onMessage.activity);
return;
}
// load 'onMessage' activity
// Because we are done with all the DPE, we can simply
// re-use our control
// channels for the child.
ActivityInfo child = new ActivityInfo(genMonotonic(), onMessage.activity, _self.self, _self.parent);
instance(createChild(child, _scopeFrame, _linkFrame));
}
public void onTimeout() {
// Dead path all the onMessage activiites (the other alarms
// have already been DPE'ed)
for (OPickReceive.OnMessage onMessage : _opick.onMessages) {
dpe(onMessage.activity);
}
// Because we are done with all the DPE, we can simply
// re-use our control
// channels for the child.
ActivityInfo child = new ActivityInfo(genMonotonic(), _alarm.activity, _self.self, _self.parent);
instance(createChild(child, _scopeFrame, _linkFrame));
}
public void onCancel() {
_self.parent.completed(null, CompensationHandler.emptySet());
}
}.or(new TerminationChannelListener(_self.self) {
private static final long serialVersionUID = 4399496341785922396L;
public void terminate() {
getBpelRuntimeContext().cancel(_pickResponseChannel);
instance(WAITING.this);
}
}));
}
}
}