/drools-core/src/main/java/org/drools/workflow/instance/node/FaultNodeInstance.java

https://github.com/bobmcwhirter/drools · Java · 87 lines · 56 code · 11 blank · 20 comment · 9 complexity · f5e6c1ed342fcec77998569f51addd1f MD5 · raw file

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