389fdc05182ee4c26878a389e7e0fb817b7caea7
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / ReadFileTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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.openecomp.mso.bpmn.core;
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27
28 import org.camunda.bpm.engine.ProcessEngineException;
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.camunda.bpm.engine.delegate.Expression;
31
32 import org.openecomp.mso.logger.MsoLogger;
33
34 /**
35  * Conditionally reads the contents of a resource file as a string and stores it
36  * in an execution variable.  The file is read only if the value of the input
37  * variable is null.
38  * <p>
39  * Required fields:<br/><br/>
40  * &nbsp;&nbsp;&nbsp;&nbsp;file: the resource file path<br/>
41  * &nbsp;&nbsp;&nbsp;&nbsp;inputVariable: the input variable name<br/>
42  * &nbsp;&nbsp;&nbsp;&nbsp;outputVariable: the output variable name<br/>
43  */
44 public class ReadFileTask extends BaseTask {
45         
46         private Expression file;
47         private Expression inputVariable;
48         private Expression outputVariable;
49         
50         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
51
52         public void execute(DelegateExecution execution) throws Exception {
53                 if (msoLogger.isDebugEnabled()) {
54                         msoLogger.debug("Started Executing " + getTaskName());
55                 }
56
57                 String theInputVariable =
58                         getStringField(inputVariable, execution, "inputVariable");
59                 String theOutputVariable =
60                         getOutputField(outputVariable, execution, "outputVariable");
61                 String theFile =getStringField(file, execution, "file");
62
63                 if (msoLogger.isDebugEnabled()) {
64                         msoLogger.debug("inputVariable = " + theInputVariable
65                                 + " outputVariable = " + theOutputVariable
66                                 + "file = " + theFile);
67                 }
68
69                 Boolean shouldFail = (Boolean) execution.getVariable("shouldFail");
70
71                 if (shouldFail != null && shouldFail) {
72                         throw new ProcessEngineException(getClass().getSimpleName() + " Failed");
73                 }
74
75                 Object value = execution.getVariable(theInputVariable);
76
77                 if (value == null) {
78                         InputStream xmlStream = null;
79
80                         try {
81                                 xmlStream = getClass().getResourceAsStream(theFile);
82
83                                 if (xmlStream == null) {
84                                         throw new IOException("Resource not found: " + theFile);
85                                 }
86
87                                 BufferedReader reader = new BufferedReader(new InputStreamReader(xmlStream));
88                                 StringBuilder output = new StringBuilder();
89                                 String line;
90
91                                 while ((line = reader.readLine()) != null) {
92                                         output.append(line);
93                                 }
94
95                                 xmlStream.close();
96                                 xmlStream = null;
97
98                                 value = output.toString();
99
100                         } finally {
101                                 if (xmlStream != null) {
102                                         try {
103                                                 xmlStream.close();
104                                         } catch (Exception e) {
105                                                 // Do nothing
106                                         }
107                                 }
108                         }
109                 }
110                 execution.setVariable(theInputVariable, value);
111                 execution.setVariable(theOutputVariable, value);
112                 System.out.println("ServiceInput - " + execution.getVariable("gServiceInput"));
113                 if (msoLogger.isDebugEnabled()) {
114                         msoLogger.debug("Done Executing " + getTaskName());
115                 }
116         }
117 }