Changed try to try with resource
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / ReadFileTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.mso.bpmn.core;
23
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28
29 import org.camunda.bpm.engine.ProcessEngineException;
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.camunda.bpm.engine.delegate.Expression;
32
33 import org.openecomp.mso.logger.MsoLogger;
34
35 /**
36  * Conditionally reads the contents of a resource file as a string and stores it
37  * in an execution variable.  The file is read only if the value of the input
38  * variable is null.
39  * <p>
40  * Required fields:<br/><br/>
41  * &nbsp;&nbsp;&nbsp;&nbsp;file: the resource file path<br/>
42  * &nbsp;&nbsp;&nbsp;&nbsp;inputVariable: the input variable name<br/>
43  * &nbsp;&nbsp;&nbsp;&nbsp;outputVariable: the output variable name<br/>
44  */
45 public class ReadFileTask extends BaseTask {
46         
47         private Expression file;
48         private Expression inputVariable;
49         private Expression outputVariable;
50         
51         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
52
53         public void execute(DelegateExecution execution) throws Exception {
54                 if (msoLogger.isDebugEnabled()) {
55                         msoLogger.debug("Started Executing " + getTaskName());
56                 }
57
58                 String theInputVariable =
59                         getStringField(inputVariable, execution, "inputVariable");
60                 String theOutputVariable =
61                         getOutputField(outputVariable, execution, "outputVariable");
62                 String theFile =getStringField(file, execution, "file");
63
64                 if (msoLogger.isDebugEnabled()) {
65                         msoLogger.debug("inputVariable = " + theInputVariable
66                                 + " outputVariable = " + theOutputVariable
67                                 + "file = " + theFile);
68                 }
69
70                 Boolean shouldFail = (Boolean) execution.getVariable("shouldFail");
71
72                 if (shouldFail != null && shouldFail) {
73                         throw new ProcessEngineException(getClass().getSimpleName() + " Failed");
74                 }
75
76                 Object value = execution.getVariable(theInputVariable);
77
78                 if (value == null) {
79                         try (InputStream xmlStream = getClass().getResourceAsStream(theFile);
80                                 BufferedReader reader = new BufferedReader(new InputStreamReader(xmlStream))) {
81                                 StringBuilder output = new StringBuilder();
82                                 String line;
83
84                                 while ((line = reader.readLine()) != null) {
85                                         output.append(line);
86                                 }
87                                 value = output.toString();
88
89                         } catch (Exception e) {
90                                 msoLogger.debug("Exception at readResourceFile stream: " + e);
91                         }
92                 }
93                 execution.setVariable(theInputVariable, value);
94                 execution.setVariable(theOutputVariable, value);
95                 msoLogger.debug ("ServiceInput - " + execution.getVariable("gServiceInput"));
96                 if (msoLogger.isDebugEnabled()) {
97                         msoLogger.debug("Done Executing " + getTaskName());
98                 }
99         }
100 }