Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / exception / ExceptionBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.exception;
22
23 import org.camunda.bpm.engine.delegate.BpmnError;
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.onap.so.bpmn.common.BuildingBlockExecution;
26 import org.onap.so.bpmn.common.DelegateExecutionImpl;
27 import org.onap.so.bpmn.core.WorkflowException;
28 import org.onap.so.logger.MessageEnum;
29 import org.onap.so.logger.MsoLogger;
30 import org.springframework.stereotype.Component;
31
32 @Component
33 public class ExceptionBuilder {
34         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ExceptionBuilder.class);
35
36         public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, Exception exception) {
37                 String msg = "Exception in %s.%s ";
38                 try{
39                         msoLogger.error(exception);
40                         
41                         String errorVariable = "Error%s%s";
42         
43                         StackTraceElement[] trace = Thread.currentThread().getStackTrace();
44                         for (StackTraceElement traceElement : trace) {
45                                 if (!traceElement.getClassName().equals(this.getClass().getName()) && !traceElement.getClassName().equals(Thread.class.getName())) {
46                                         msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
47                                         String shortClassName = traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
48                                         errorVariable = String.format(errorVariable,  shortClassName, traceElement.getMethodName());
49                                         break;
50                                 }
51                         }
52                         
53                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, msg.toString());
54                         execution.setVariable(errorVariable, exception.getMessage());
55                 } catch (Exception ex){
56                         //log trace, allow process to complete gracefully
57                         msoLogger.error(ex);
58                 }
59
60                 if (exception.getMessage() != null)
61                         msg = msg.concat(exception.getMessage());
62                 buildAndThrowWorkflowException(execution, errorCode, msg);
63         }
64
65         public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, String errorMessage) {
66                 if (execution instanceof DelegateExecutionImpl) {
67                         buildAndThrowWorkflowException(((DelegateExecutionImpl) execution).getDelegateExecution(), errorCode, errorMessage);
68                 }
69         }
70
71         public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, String errorMessage) {
72                 String processKey = getProcessKey(execution);
73                 msoLogger.info("Building a WorkflowException for Subflow");
74
75                 WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage);
76                 execution.setVariable("WorkflowException", exception);
77                 msoLogger.info("Outgoing WorkflowException is " + exception);
78                 msoLogger.info("Throwing MSOWorkflowException");
79                 throw new BpmnError("MSOWorkflowException");
80         }
81
82         public String getProcessKey(DelegateExecution execution) {
83                 String testKey = (String) execution.getVariable("testProcessKey");
84                 if (testKey != null) {
85                         return testKey;
86                 }
87                 return execution.getProcessEngineServices().getRepositoryService()
88                                 .getProcessDefinition(execution.getProcessDefinitionId()).getKey();
89         }
90 }