Merge "add maven build properties to spring actuator"
[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(DelegateExecution execution, int errorCode, Exception exception) {
66                 String msg = "Exception in %s.%s ";
67                 try{
68                         msoLogger.error(exception);
69
70                         String errorVariable = "Error%s%s";
71
72                         StackTraceElement[] trace = Thread.currentThread().getStackTrace();
73                         for (StackTraceElement traceElement : trace) {
74                                 if (!traceElement.getClassName().equals(this.getClass().getName()) && !traceElement.getClassName().equals(Thread.class.getName())) {
75                                         msg = String.format(msg, traceElement.getClassName(), traceElement.getMethodName());
76                                         String shortClassName = traceElement.getClassName().substring(traceElement.getClassName().lastIndexOf(".") + 1);
77                                         errorVariable = String.format(errorVariable,  shortClassName, traceElement.getMethodName());
78                                         break;
79                                 }
80                         }
81                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, msg, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, msg.toString());
82                         execution.setVariable(errorVariable, exception.getMessage());
83                 } catch (Exception ex){
84                         //log trace, allow process to complete gracefully
85                         msoLogger.error(ex);
86                 }
87
88                 if (exception.getMessage() != null)
89                         msg = msg.concat(exception.getMessage());
90                 buildAndThrowWorkflowException(execution, errorCode, msg);
91         }
92
93         public void buildAndThrowWorkflowException(BuildingBlockExecution execution, int errorCode, String errorMessage) {
94                 if (execution instanceof DelegateExecutionImpl) {
95                         buildAndThrowWorkflowException(((DelegateExecutionImpl) execution).getDelegateExecution(), errorCode, errorMessage);
96                 }
97         }
98
99         public void buildAndThrowWorkflowException(DelegateExecution execution, int errorCode, String errorMessage) {
100                 String processKey = getProcessKey(execution);
101                 msoLogger.info("Building a WorkflowException for Subflow");
102
103                 WorkflowException exception = new WorkflowException(processKey, errorCode, errorMessage);
104                 execution.setVariable("WorkflowException", exception);
105                 execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
106                 msoLogger.info("Outgoing WorkflowException is " + exception);
107                 msoLogger.info("Throwing MSOWorkflowException");
108                 throw new BpmnError("MSOWorkflowException");
109         }
110         
111         public void buildAndThrowWorkflowException(DelegateExecution execution, String errorCode, String errorMessage) {
112                 execution.setVariable("WorkflowExceptionErrorMessage", errorMessage);
113                 throw new BpmnError(errorCode,errorMessage);
114         }
115
116         public String getProcessKey(DelegateExecution execution) {
117                 String testKey = (String) execution.getVariable("testProcessKey");
118                 if (testKey != null) {
119                         return testKey;
120                 }
121                 return execution.getProcessEngineServices().getRepositoryService()
122                                 .getProcessDefinition(execution.getProcessDefinitionId()).getKey();
123         }
124 }