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.openecomp.mso.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.openecomp.mso.bpmn.core.BPMNLogger;
44 import org.openecomp.mso.bpmn.core.WorkflowException;
47 * This plugin does the following:
50 * Adds logic at the start of every Call Activity to remove any existing
51 * WorkflowException object from the execution (saving a copy of it in a
52 * different variable).
55 * Adds logic at the end of every Call Activity to generate a MSOWorkflowException
56 * event if there is a WorkflowException object in the execution.
60 public class WorkflowExceptionPlugin extends AbstractProcessEnginePlugin {
63 public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
64 List<BpmnParseListener> preParseListeners =
65 processEngineConfiguration.getCustomPreBPMNParseListeners();
67 if (preParseListeners == null) {
68 preParseListeners = new ArrayList<BpmnParseListener>();
69 processEngineConfiguration.setCustomPreBPMNParseListeners(preParseListeners);
72 preParseListeners.add(new WorkflowExceptionParseListener());
75 public static class WorkflowExceptionParseListener extends AbstractBpmnParseListener {
77 public void parseProcess(Element processElement, ProcessDefinitionEntity processDefinition) {
78 AtomicInteger triggerTaskIndex = new AtomicInteger(1);
79 List<ActivityImpl> activities = new ArrayList<ActivityImpl>(processDefinition.getActivities());
80 recurse(activities, triggerTaskIndex);
84 * Helper method that recurses (into subprocesses) over all the listed activities.
85 * @param activities a list of workflow activities
86 * @param triggerTaskIndex the index of the next trigger task (mutable)
88 private void recurse(List<ActivityImpl> activities, AtomicInteger triggerTaskIndex) {
89 for (ActivityImpl activity : activities) {
90 String type = (String) activity.getProperty("type");
92 if ("callActivity".equals(type)) {
93 // Add a WorkflowExceptionResetListener to clear the WorkflowException
94 // variable when each Call Activity starts.
97 ExecutionListener.EVENTNAME_START,
98 new WorkflowExceptionResetListener());
100 // Add a WorkflowExceptionTriggerTask after the call activity.
101 // It must be a task because a listener cannot be used to generate
102 // an event. Throwing BpmnError from an execution listener will
103 // cause the process to die.
105 List<PvmTransition> outTransitions =
106 new ArrayList<PvmTransition>(activity.getOutgoingTransitions());
108 for (PvmTransition transition : outTransitions) {
109 String triggerTaskId = "WorkflowExceptionTriggerTask_" + triggerTaskIndex;
111 ActivityImpl triggerTask = activity.getFlowScope().createActivity(triggerTaskId);
113 ClassDelegateActivityBehavior behavior = new ClassDelegateActivityBehavior(
114 WorkflowExceptionTriggerTask.class.getName(),
115 new ArrayList<FieldDeclaration>(0));
117 triggerTask.setActivityBehavior(behavior);
118 triggerTask.setName("Workflow Exception Trigger Task " + triggerTaskIndex);
119 triggerTaskIndex.getAndIncrement();
121 TransitionImpl transitionImpl = (TransitionImpl) transition;
122 TransitionImpl triggerTaskOutTransition = triggerTask.createOutgoingTransition();
123 triggerTaskOutTransition.setDestination((ActivityImpl)transitionImpl.getDestination());
124 transitionImpl.setDestination(triggerTask);
126 } else if ("subProcess".equals(type)) {
127 recurse(new ArrayList<ActivityImpl>(activity.getActivities()), triggerTaskIndex);
134 * If there is a WorkflowException object in the execution, this method
135 * removes it (saving a copy of it in a different variable).
137 public static class WorkflowExceptionResetListener implements ExecutionListener {
138 public void notify(DelegateExecution execution) throws Exception {
139 Object workflowException = execution.getVariable("WorkflowException");
141 if (workflowException instanceof WorkflowException) {
143 String saveName = "SavedWorkflowException" + index;
144 while (execution.getVariable(saveName) != null) {
145 saveName = "SavedWorkflowException" + (++index);
148 BPMNLogger.debug((String)execution.getVariable("isDebugLogEnabled"),
149 "WorkflowExceptionResetTask is moving WorkflowException to " + saveName);
151 execution.setVariable(saveName, workflowException);
152 execution.setVariable("WorkflowException", null);
158 * Generates an MSOWorkflowException event if there is a WorkflowException
159 * object in the execution.
161 public static class WorkflowExceptionTriggerTask implements JavaDelegate {
162 public void execute(DelegateExecution execution) throws Exception {
163 if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
164 BPMNLogger.debug((String)execution.getVariable("isDebugLogEnabled"),
165 "WorkflowExceptionTriggerTask is generating a MSOWorkflowException event");
166 throw new BpmnError("MSOWorkflowException");