/jbpm-flow/src/main/java/org/jbpm/workflow/instance/node/FaultNodeInstance.java

https://github.com/wintonxu/jbpm · Java · 101 lines · 69 code · 12 blank · 20 comment · 14 complexity · 869ff1ca6f4e7ddff66277959c4bab85 MD5 · raw file

  1. /**
  2. * Copyright 2005 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jbpm.workflow.instance.node;
  17. import java.util.Collection;
  18. import org.drools.runtime.process.NodeInstance;
  19. import org.drools.runtime.process.WorkflowProcessInstance;
  20. import org.jbpm.process.core.context.exception.ExceptionScope;
  21. import org.jbpm.process.core.context.variable.VariableScope;
  22. import org.jbpm.process.instance.ProcessInstance;
  23. import org.jbpm.process.instance.context.exception.ExceptionScopeInstance;
  24. import org.jbpm.process.instance.context.variable.VariableScopeInstance;
  25. import org.jbpm.workflow.core.node.FaultNode;
  26. import org.jbpm.workflow.instance.NodeInstanceContainer;
  27. import org.jbpm.workflow.instance.impl.NodeInstanceImpl;
  28. /**
  29. * Runtime counterpart of a fault node.
  30. *
  31. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  32. */
  33. public class FaultNodeInstance extends NodeInstanceImpl {
  34. private static final long serialVersionUID = 510l;
  35. protected FaultNode getFaultNode() {
  36. return (FaultNode) getNode();
  37. }
  38. public void internalTrigger(final NodeInstance from, String type) {
  39. if (!org.jbpm.workflow.core.Node.CONNECTION_DEFAULT_TYPE.equals(type)) {
  40. throw new IllegalArgumentException(
  41. "A FaultNode only accepts default incoming connections!");
  42. }
  43. String faultName = getFaultName();
  44. ExceptionScopeInstance exceptionScopeInstance = getExceptionScopeInstance(faultName);
  45. NodeInstanceContainer nodeInstanceContainer = (NodeInstanceContainer) getNodeInstanceContainer();
  46. nodeInstanceContainer.removeNodeInstance(this);
  47. if (getFaultNode().isTerminateParent()) {
  48. if (nodeInstanceContainer instanceof CompositeNodeInstance) {
  49. ((CompositeNodeInstance) nodeInstanceContainer).cancel();
  50. } else if (nodeInstanceContainer instanceof WorkflowProcessInstance) {
  51. Collection<NodeInstance> nodeInstances = ((WorkflowProcessInstance) nodeInstanceContainer).getNodeInstances();
  52. for (NodeInstance nodeInstance: nodeInstances) {
  53. ((org.jbpm.workflow.instance.NodeInstance) nodeInstance).cancel();
  54. }
  55. }
  56. }
  57. if (exceptionScopeInstance != null) {
  58. handleException(faultName, exceptionScopeInstance);
  59. } else {
  60. ((ProcessInstance) getProcessInstance()).setState(ProcessInstance.STATE_ABORTED);
  61. }
  62. }
  63. protected ExceptionScopeInstance getExceptionScopeInstance(String faultName) {
  64. return (ExceptionScopeInstance)
  65. resolveContextInstance(ExceptionScope.EXCEPTION_SCOPE, faultName);
  66. }
  67. protected String getFaultName() {
  68. return getFaultNode().getFaultName();
  69. }
  70. protected Object getFaultData() {
  71. Object value = null;
  72. String faultVariable = getFaultNode().getFaultVariable();
  73. if (faultVariable != null) {
  74. VariableScopeInstance variableScopeInstance = (VariableScopeInstance)
  75. resolveContextInstance(VariableScope.VARIABLE_SCOPE, faultVariable);
  76. if (variableScopeInstance != null) {
  77. value = variableScopeInstance.getVariable(faultVariable);
  78. } else {
  79. System.err.println("Could not find variable scope for variable " + faultVariable);
  80. System.err.println("when trying to execute fault node " + getFaultNode().getName());
  81. System.err.println("Continuing without setting value.");
  82. }
  83. }
  84. return value;
  85. }
  86. protected void handleException(String faultName, ExceptionScopeInstance exceptionScopeInstance) {
  87. exceptionScopeInstance.handleException(faultName, getFaultData());
  88. }
  89. }