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