1 package org.jbpm.bpel.def; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.HashMap ; 6 import java.util.HashSet ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.Map ; 10 11 import javax.wsdl.Message; 12 import javax.xml.namespace.QName ; 13 14 import org.jbpm.bpel.data.def.ElementTypeInfo; 15 import org.jbpm.bpel.data.def.MessageTypeInfo; 16 import org.jbpm.bpel.data.def.VariableDefinition; 17 import org.jbpm.bpel.data.def.VariableTypeInfo; 18 import org.jbpm.bpel.exe.Fault; 19 import org.jbpm.bpel.exe.ScopeInstance; 20 import org.jbpm.bpel.service.def.CorrelationSetDefinition; 21 import org.jbpm.bpel.service.def.PartnerLinkDefinition; 22 import org.jbpm.bpel.wsdl.util.WsdlUtil; 23 import org.jbpm.bpel.xml.BpelConstants; 24 import org.jbpm.graph.def.DelegationException; 25 import org.jbpm.graph.def.Node; 26 import org.jbpm.graph.exe.ExecutionContext; 27 import org.jbpm.graph.exe.Token; 28 29 35 public class Scope extends CompositeActivity { 36 protected Activity root; 37 private transient List nodes = null; 38 39 private static final long serialVersionUID = 1L; 40 41 public static final HandlerType COMPENSATION = new HandlerType("compensation"); 42 43 44 public static final HandlerType CATCH_ALL = new HandlerType("catchAll"); 45 46 47 public static final HandlerType TERMINATION = new HandlerType("termination"); 48 49 50 public static final QName Q_FORCED_TERMINATION = new QName (BpelConstants.NS_BPWS, "forcedTermination"); 51 52 public static final String NORMAL_FLOW_TOKEN = "normalFlow"; 53 public static final String IMPLICIT_TOKEN = "implicit"; 54 public static final String EVENTS_TOKEN = "events"; 55 public static final String EVENT_TOKEN = "event"; 56 57 private Map variables = new HashMap (); 58 private Map partnerLinks = new HashMap (); 59 private Map correlationSets = new HashMap (); 60 61 private ScopeHandler compensationHandler; 62 private ScopeHandler terminationHandler; 63 private Collection messageHandlers = new HashSet (); 64 private Collection alarmHandlers = new HashSet (); 65 private List faultHandlers = new ArrayList (); 66 private ScopeHandler catchAllHandler; 67 68 private boolean isolated; 69 private boolean implicit = false; 70 71 public Scope() { 72 super(); 73 } 74 75 public Scope(String name) { 76 super(name); 77 } 78 79 private long id; 81 82 public void setId(long id) { 83 this.id = id; 84 } 85 86 public long getId() { 87 return id; 88 } 89 90 92 public void execute(ExecutionContext context) { 93 Token scopeToken = new Token(context.getToken(), name); 95 initScopeData(scopeToken); 96 97 enableEvents(scopeToken); 98 getRoot().enter(createScopeContext(scopeToken)); 99 } 100 101 public void setNegativeLinkStatus(Token token) { 102 getRoot().setNegativeLinkStatus(token); 103 super.setNegativeLinkStatus(token); 104 } 105 106 public void complete(ScopeInstance parent) { 107 Token token = parent.getToken(); 109 token.getNode().leave(new ExecutionContext(token)); 110 } 111 112 public void initScopeData(Token token) { 113 ScopeInstance.create(token); 115 116 for(Iterator it = variables.values().iterator(); it.hasNext();) { 118 ((VariableDefinition) it.next()).createInstance(token); 119 } 120 for(Iterator it = correlationSets.values().iterator(); it.hasNext();) { 122 ((CorrelationSetDefinition) it.next()).createInstance(token); 123 } 124 for(Iterator it = partnerLinks.values().iterator(); it.hasNext();) { 126 ((PartnerLinkDefinition) it.next()).createInstance(token); 127 } 128 } 129 130 public void disableEvents(Token eventToken) { 131 for(Iterator it = messageHandlers.iterator(); it.hasNext();) 132 ((MessageHandler)it.next()).getReceiver().close(eventToken); 133 134 for(Iterator it = alarmHandlers.iterator(); it.hasNext();) 135 ((AlarmHandler)it.next()).getAlarm().cancelTimer(eventToken); 136 } 137 138 public void enableEvents(Token scopeToken) { 139 if ( !messageHandlers.isEmpty() || !alarmHandlers.isEmpty() ) { 140 Token eventToken = new Token(scopeToken, EVENTS_TOKEN); 141 142 new Token(eventToken, IMPLICIT_TOKEN); 146 147 for(Iterator it = messageHandlers.iterator(); it.hasNext();) 148 ((MessageHandler)it.next()).getReceiver().receive(eventToken); 149 150 for(Iterator it = alarmHandlers.iterator(); it.hasNext();) 151 ((AlarmHandler)it.next()).getAlarm().startTimer(eventToken); 152 } 153 } 154 155 public ExecutionContext createScopeContext(Token scopeToken) { 156 Token normalFlowToken = new Token(scopeToken, NORMAL_FLOW_TOKEN); 157 new Token(scopeToken, IMPLICIT_TOKEN); 161 return new ExecutionContext(normalFlowToken); 162 } 163 164 165 public void accept(BpelVisitor visitor) { 166 visitor.visit(this); 167 } 168 169 171 public VariableDefinition findVariable(String varName) { 172 VariableDefinition variable = getVariable(varName); 173 return variable != null ? variable : super.findVariable(varName); 174 } 175 176 public CorrelationSetDefinition findCorrelationSet(String csName) { 177 CorrelationSetDefinition cs = getCorrelationSet(csName); 178 return cs != null ? cs : super.findCorrelationSet(csName); 179 } 180 181 public PartnerLinkDefinition findPartnerLink(String plinkName) { 182 PartnerLinkDefinition plink = getPartnerLink(plinkName); 183 return plink != null ? plink : super.findPartnerLink(plinkName); 184 } 185 186 188 public void raiseException(Throwable exception, ExecutionContext executionContext) throws DelegationException { 189 if(exception instanceof Fault) { 190 Token token = executionContext.getToken(); 191 ScopeInstance.get(token).faulted((Fault)exception); 192 } 193 else { 194 super.raiseException(exception, executionContext); 195 } 196 } 197 198 200 public Activity getRoot() { 201 return root; 202 } 203 204 public void setRoot(Activity root) { 205 this.root = root; 206 } 207 208 public void addVariable(VariableDefinition variable) { 209 variables.put(variable.getName(), variable); 210 } 211 212 216 public Map getVariables() { 217 return variables; 218 } 219 220 public void setVariables(Map variables) { 221 this.variables = variables; 222 } 223 224 public VariableDefinition getVariable(String variableName) { 225 return (VariableDefinition) variables.get(variableName); 226 } 227 228 public void addCorrelationSet(CorrelationSetDefinition correlation) { 229 correlationSets.put(correlation.getName(), correlation); 230 } 231 232 238 public CorrelationSetDefinition getCorrelationSet(String correlationSetName) { 239 return (CorrelationSetDefinition) correlationSets.get(correlationSetName); 240 } 241 242 public void setCorrelationSets(Map correlationSets) { 243 this.correlationSets = correlationSets; 244 } 245 246 public Collection getPartnerLinks() { 247 return partnerLinks.values(); 248 } 249 250 public void setPartnerLinks(Map partnerLinks) { 251 this.partnerLinks = partnerLinks; 252 } 253 254 public void addPartnerLink(PartnerLinkDefinition partnerLink) { 255 partnerLinks.put(partnerLink.getName(), partnerLink); 256 } 257 258 public PartnerLinkDefinition getPartnerLink(String plinkName) { 259 return (PartnerLinkDefinition) partnerLinks.get(plinkName); 260 } 261 262 263 public void addFaultHandler(FaultHandler faultHandler) { 264 faultHandler.compositeActivity = this; 265 faultHandlers.add(faultHandler); 266 } 267 268 284 public ScopeHandler getFaultHandler(Fault fault) { 285 ScopeHandler handler; 286 QName faultName = fault.getName(); 287 VariableTypeInfo faultTypeInfo = fault.getTypeInfo(); 288 289 if (faultTypeInfo == null) { 290 handler = findFaultHandler(faultName); 291 } 292 else { 293 handler = findFaultHandler(faultTypeInfo, faultName); 294 if (handler == null) { 295 handler = findFaultHandler(faultTypeInfo); 296 } 297 } 298 if (handler == null) { 299 handler = catchAllHandler; 300 } 301 return handler; 302 } 303 304 316 protected FaultHandler findFaultHandler(QName faultName) { 317 Iterator faultHandlerIt = faultHandlers.iterator(); 318 while (faultHandlerIt.hasNext()) { 319 FaultHandler handler = (FaultHandler) faultHandlerIt.next(); 320 if (faultName.equals(handler.getFaultName()) && 321 handler.getFaultVariable() == null) { 322 return handler; 324 } 325 } 326 return null; 327 } 328 329 347 protected FaultHandler findFaultHandler(VariableTypeInfo faultTypeInfo, QName faultName) { 348 FaultHandler selectedHandler = null; 349 Iterator handlerIt = faultHandlers.iterator(); 350 while (handlerIt.hasNext()) { 351 FaultHandler handler = (FaultHandler) handlerIt.next(); 352 if (!faultName.equals(handler.getFaultName())) { 353 continue; 355 } 356 VariableDefinition handlerVariable = handler.getFaultVariable(); 357 if (handlerVariable != null) { 358 VariableTypeInfo handlerTypeInfo = handlerVariable.getTypeInfo(); 359 if (handlerTypeInfo.equals(faultTypeInfo)) { 360 362 selectedHandler = handler; 363 break; 364 } 365 else if (faultTypeInfo instanceof MessageTypeInfo && 366 handlerTypeInfo instanceof ElementTypeInfo) { 367 Message message = ((MessageTypeInfo) faultTypeInfo).getMessage(); 369 QName elementName = WsdlUtil.getDocLitElementName(message); 370 if (handlerTypeInfo.getName().equals(elementName)) { 372 374 selectedHandler = handler; 375 } 376 } 377 } 378 else if (selectedHandler == null) { 379 381 selectedHandler = handler; 382 } 383 } 384 return selectedHandler; 385 } 386 387 403 protected FaultHandler findFaultHandler(VariableTypeInfo faultTypeInfo) { 404 FaultHandler selectedHandler = null; 405 Iterator handlerIt = faultHandlers.iterator(); 406 while (handlerIt.hasNext()) { 407 FaultHandler handler = (FaultHandler) handlerIt.next(); 408 if (handler.getFaultName() != null) { 409 continue; 411 } 412 VariableTypeInfo handlerTypeInfo = handler.getFaultVariable().getTypeInfo(); 414 if (handlerTypeInfo.equals(faultTypeInfo)) { 415 417 selectedHandler = handler; 418 break; 419 } 420 else if (faultTypeInfo instanceof MessageTypeInfo && 421 handlerTypeInfo instanceof ElementTypeInfo) { 422 Message message = ((MessageTypeInfo) faultTypeInfo).getMessage(); 424 QName elementName = WsdlUtil.getDocLitElementName(message); 425 if (handlerTypeInfo.getName().equals(elementName)) { 427 429 selectedHandler = handler; 430 } 431 } 432 } 433 return selectedHandler; 434 } 435 436 public List getFaultHandlers() { 437 return faultHandlers; 438 } 439 440 public Collection getMessageHandlers() { 441 return messageHandlers; 442 } 443 444 public void addMessageHandler(MessageHandler messageHandler) { 445 messageHandler.compositeActivity = this; 446 messageHandlers.add(messageHandler); 447 } 448 449 public Collection getAlarmHandlers() { 450 return alarmHandlers; 451 } 452 453 public void addAlarmHandler(AlarmHandler alarmHandler) { 454 alarmHandler.compositeActivity = this; 455 alarmHandlers.add(alarmHandler); 456 } 457 458 public void setHandler(HandlerType type, ScopeHandler handler) { 459 ScopeHandler previousHandler = null; 460 handler.compositeActivity = this; 461 462 if (type == COMPENSATION) { 463 previousHandler = compensationHandler; 464 compensationHandler = handler; 465 } else if (type == CATCH_ALL) { 466 previousHandler = catchAllHandler; 467 catchAllHandler = handler; 468 } else if (type == TERMINATION) { 469 previousHandler = terminationHandler; 470 terminationHandler = handler; 471 } 472 473 if(previousHandler != null) previousHandler.compositeActivity = null; 474 } 475 476 public ScopeHandler getHandler(HandlerType type) { 477 if (type == COMPENSATION) { 478 return compensationHandler; 479 } else if (type == CATCH_ALL) { 480 return catchAllHandler; 481 } else if (type == TERMINATION) { 482 return terminationHandler; 483 } 484 485 return null; 486 } 487 488 490 public boolean isIsolated() { 491 return isolated; 492 } 493 494 public void setIsolated(boolean isolated) { 495 this.isolated = isolated; 496 } 497 498 public void setImplicit(boolean implicit) { 499 this.implicit = implicit; 500 } 501 502 public boolean isImplicit() { 503 return implicit; 504 } 505 506 507 public static class HandlerType { 508 509 private final String name; 510 511 515 HandlerType(String name) { 516 this.name = name; 517 } 518 519 523 public String toString() { 524 return name; 525 } 526 } 527 528 530 531 public Node addNode(Node node) { 532 root = (Activity) node; 533 root.compositeActivity = this; 534 return root; 535 } 536 537 538 public Node removeNode(Node node) { 539 if(root.equals(node)) { 540 root.compositeActivity = null; 541 root = null; 542 } 543 544 return node; 545 } 546 547 548 public void reorderNode(int oldIndex, int newIndex) { 549 } 551 552 553 public List getNodes() { 554 if(nodes == null && root!=null) { 555 nodes = new ArrayList (); 556 nodes.add(root); 557 } 558 return nodes; 559 } 560 } | Popular Tags |