2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.core.plugins;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.concurrent.atomic.AtomicInteger;
27 import org.camunda.bpm.engine.delegate.BpmnError;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.camunda.bpm.engine.delegate.ExecutionListener;
30 import org.camunda.bpm.engine.delegate.JavaDelegate;
31 import org.camunda.bpm.engine.impl.bpmn.behavior.ClassDelegateActivityBehavior;
32 import org.camunda.bpm.engine.impl.bpmn.parser.AbstractBpmnParseListener;
33 import org.camunda.bpm.engine.impl.bpmn.parser.BpmnParseListener;
34 import org.camunda.bpm.engine.impl.bpmn.parser.FieldDeclaration;
35 import org.camunda.bpm.engine.impl.cfg.AbstractProcessEnginePlugin;
36 import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
37 import org.camunda.bpm.engine.impl.persistence.entity.ProcessDefinitionEntity;
38 import org.camunda.bpm.engine.impl.pvm.PvmTransition;
39 import org.camunda.bpm.engine.impl.pvm.process.ActivityImpl;
40 import org.camunda.bpm.engine.impl.pvm.process.TransitionImpl;
41 import org.camunda.bpm.engine.impl.util.xml.Element;
43 import org.onap.so.bpmn.core.BPMNLogger;
44 import org.onap.so.bpmn.core.WorkflowException;
45 import org.springframework.stereotype.Component;
48 * This plugin does the following:
51 * Adds logic at the start of every Call Activity to remove any existing
52 * WorkflowException object from the execution (saving a copy of it in a
53 * different variable).
56 * Adds logic at the end of every Call Activity to generate a MSOWorkflowException
57 * event if there is a WorkflowException object in the execution.
62 public class WorkflowExceptionPlugin extends AbstractProcessEnginePlugin {
65 public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
66 List<BpmnParseListener> preParseListeners =
67 processEngineConfiguration.getCustomPreBPMNParseListeners();
69 if (preParseListeners == null) {
70 preParseListeners = new ArrayList<>();
71 processEngineConfiguration.setCustomPreBPMNParseListeners(preParseListeners);
74 preParseListeners.add(new WorkflowExceptionParseListener());
77 public static class WorkflowExceptionParseListener extends AbstractBpmnParseListener {
79 public void parseProcess(Element processElement, ProcessDefinitionEntity processDefinition) {
80 AtomicInteger triggerTaskIndex = new AtomicInteger(1);
81 List<ActivityImpl> activities = new ArrayList<>(processDefinition.getActivities());
82 recurse(activities, triggerTaskIndex);
86 * Helper method that recurses (into subprocesses) over all the listed activities.
87 * @param activities a list of workflow activities
88 * @param triggerTaskIndex the index of the next trigger task (mutable)
90 private void recurse(List<ActivityImpl> activities, AtomicInteger triggerTaskIndex) {
91 for (ActivityImpl activity : activities) {
92 String type = (String) activity.getProperty("type");
94 if ("callActivity".equals(type)) {
95 // Add a WorkflowExceptionResetListener to clear the WorkflowException
96 // variable when each Call Activity starts.
99 ExecutionListener.EVENTNAME_START,
100 new WorkflowExceptionResetListener());
102 // Add a WorkflowExceptionTriggerTask after the call activity.
103 // It must be a task because a listener cannot be used to generate
104 // an event. Throwing BpmnError from an execution listener will
105 // cause the process to die.
107 List<PvmTransition> outTransitions =
108 new ArrayList<>(activity.getOutgoingTransitions());
110 for (PvmTransition transition : outTransitions) {
111 String triggerTaskId = "WorkflowExceptionTriggerTask_" + triggerTaskIndex;
113 ActivityImpl triggerTask = activity.getFlowScope().createActivity(triggerTaskId);
115 ClassDelegateActivityBehavior behavior = new ClassDelegateActivityBehavior(
116 WorkflowExceptionTriggerTask.class.getName(),
119 triggerTask.setActivityBehavior(behavior);
120 triggerTask.setName("Workflow Exception Trigger Task " + triggerTaskIndex);
121 triggerTaskIndex.getAndIncrement();
123 TransitionImpl transitionImpl = (TransitionImpl) transition;
124 TransitionImpl triggerTaskOutTransition = triggerTask.createOutgoingTransition();
125 triggerTaskOutTransition.setDestination((ActivityImpl)transitionImpl.getDestination());
126 transitionImpl.setDestination(triggerTask);
128 } else if ("subProcess".equals(type)) {
129 recurse(new ArrayList<>(activity.getActivities()), triggerTaskIndex);
136 * If there is a WorkflowException object in the execution, this method
137 * removes it (saving a copy of it in a different variable).
139 public static class WorkflowExceptionResetListener implements ExecutionListener {
140 public void notify(DelegateExecution execution) throws Exception {
141 Object workflowException = execution.getVariable("WorkflowException");
143 if (workflowException instanceof WorkflowException) {
145 String saveName = "SavedWorkflowException" + index;
146 while (execution.getVariable(saveName) != null) {
147 saveName = "SavedWorkflowException" + (++index);
150 BPMNLogger.debug((String)execution.getVariable("isDebugLogEnabled"),
151 "WorkflowExceptionResetTask is moving WorkflowException to " + saveName);
153 execution.setVariable(saveName, workflowException);
154 execution.setVariable("WorkflowException", null);
160 * Generates an MSOWorkflowException event if there is a WorkflowException
161 * object in the execution.
163 public static class WorkflowExceptionTriggerTask implements JavaDelegate {
164 public void execute(DelegateExecution execution) throws Exception {
165 if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
166 BPMNLogger.debug((String)execution.getVariable("isDebugLogEnabled"),
167 "WorkflowExceptionTriggerTask is generating a MSOWorkflowException event");
168 throw new BpmnError("MSOWorkflowException");