String correlatorId = BpelProcess.genCorrelatorId(_plinkDef, operation.getName());
CorrelatorDAO correlator = _process.getProcessDAO().getCorrelator(correlatorId);
CorrelationKey[] keys;
MessageRouteDAO messageRoute = null;
// We need to compute the correlation keys (based on the operation
// we can infer which correlation keys to compute - this is merely a set
// consisting of each correlationKey used in each correlation sets
// that is ever referenced in an <receive>/<onMessage> on this
// partnerlink/operation.
try {
keys = computeCorrelationKeys(mex);
} catch (InvalidMessageException ime) {
// We'd like to do a graceful exit here, no sense in rolling back due to a
// a message format problem.
__log.debug("Unable to evaluate correlation keys, invalid message format. ",ime);
mex.setFailure(MessageExchange.FailureType.FORMAT_ERROR, ime.getMessage(), null);
return;
}
String mySessionId = mex.getProperty(MessageExchange.PROPERTY_SEP_MYROLE_SESSIONID);
String partnerSessionId = mex.getProperty(MessageExchange.PROPERTY_SEP_PARTNERROLE_SESSIONID);
if (__log.isDebugEnabled()) {
__log.debug("INPUTMSG: " + correlatorId + ": MSG RCVD keys="
+ ArrayUtils.makeCollection(HashSet.class, keys) + " mySessionId=" + mySessionId
+ " partnerSessionId=" + partnerSessionId);
}
CorrelationKey matchedKey = null;
// Try to find a route for one of our keys.
for (CorrelationKey key : keys) {
messageRoute = correlator.findRoute(key);
if (messageRoute != null) {
if (__log.isDebugEnabled()) {
__log.debug("INPUTMSG: " + correlatorId + ": ckey " + key + " route is to " + messageRoute);
}
matchedKey = key;
break;
}
}
// TODO - ODE-58
// If no luck, and this operation qualifies for create-instance
// treatment, then create a new process
// instance.
if (messageRoute == null && isCreateInstnace) {
if (__log.isDebugEnabled()) {
__log.debug("INPUTMSG: " + correlatorId + ": routing failed, CREATING NEW INSTANCE");
}
ProcessDAO processDAO = _process.getProcessDAO();
if (_process._pconf.getState() == ProcessState.RETIRED) {
throw new InvalidProcessException("Process is retired.", InvalidProcessException.RETIRED_CAUSE_CODE);
}
if (!_process.processInterceptors(mex, InterceptorInvoker.__onNewInstanceInvoked)) {
__log.debug("Not creating a new instance for mex " + mex + "; interceptor prevented!");
return;
}
ProcessInstanceDAO newInstance = processDAO.createInstance(correlator);
BpelRuntimeContextImpl instance = _process
.createRuntimeContext(newInstance, new PROCESS(_process.getOProcess()), mex);
// send process instance event
NewProcessInstanceEvent evt = new NewProcessInstanceEvent(new QName(_process.getOProcess().targetNamespace,
_process.getOProcess().getName()), _process.getProcessDAO().getProcessId(), newInstance.getInstanceId());
evt.setPortType(mex.getPortType().getQName());
evt.setOperation(operation.getName());
evt.setMexId(mex.getMessageExchangeId());
_process._debugger.onEvent(evt);
_process.saveEvent(evt, newInstance);
mex.setCorrelationStatus(MyRoleMessageExchange.CorrelationStatus.CREATE_INSTANCE);
mex.getDAO().setInstance(newInstance);
instance.execute();
} else if (messageRoute != null) {
if (__log.isDebugEnabled()) {
__log.debug("INPUTMSG: " + correlatorId + ": ROUTING to instance "
+ messageRoute.getTargetInstance().getInstanceId());
}
ProcessInstanceDAO instanceDao = messageRoute.getTargetInstance();
// Reload process instance for DAO.
BpelRuntimeContextImpl instance = _process.createRuntimeContext(instanceDao, null, null);
instance.inputMsgMatch(messageRoute.getGroupId(), messageRoute.getIndex(), mex);
// Kill the route so some new message does not get routed to
// same process instance.
correlator.removeRoutes(messageRoute.getGroupId(), instanceDao);
// send process instance event
CorrelationMatchEvent evt = new CorrelationMatchEvent(new QName(_process.getOProcess().targetNamespace,
_process.getOProcess().getName()), _process.getProcessDAO().getProcessId(),
instanceDao.getInstanceId(), matchedKey);
evt.setPortType(mex.getPortType().getQName());
evt.setOperation(operation.getName());
evt.setMexId(mex.getMessageExchangeId());
_process._debugger.onEvent(evt);
// store event
_process.saveEvent(evt, instanceDao);
// EXPERIMENTAL -- LOCK
// instanceDao.lock();
mex.setCorrelationStatus(MyRoleMessageExchange.CorrelationStatus.MATCHED);
mex.getDAO().setInstance(messageRoute.getTargetInstance());
instance.execute();
} else {
if (__log.isDebugEnabled()) {
__log.debug("INPUTMSG: " + correlatorId + ": SAVING to DB (no match) ");
}