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;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
28 import org.camunda.bpm.engine.ProcessEngineException;
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.camunda.bpm.engine.delegate.Expression;
32 import org.openecomp.mso.logger.MsoLogger;
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
39 * Required fields:<br/><br/>
40 * file: the resource file path<br/>
41 * inputVariable: the input variable name<br/>
42 * outputVariable: the output variable name<br/>
44 public class ReadFileTask extends BaseTask {
46 private Expression file;
47 private Expression inputVariable;
48 private Expression outputVariable;
50 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
52 public void execute(DelegateExecution execution) throws Exception {
53 if (msoLogger.isDebugEnabled()) {
54 msoLogger.debug("Started Executing " + getTaskName());
57 String theInputVariable =
58 getStringField(inputVariable, execution, "inputVariable");
59 String theOutputVariable =
60 getOutputField(outputVariable, execution, "outputVariable");
61 String theFile =getStringField(file, execution, "file");
63 if (msoLogger.isDebugEnabled()) {
64 msoLogger.debug("inputVariable = " + theInputVariable
65 + " outputVariable = " + theOutputVariable
66 + "file = " + theFile);
69 Boolean shouldFail = (Boolean) execution.getVariable("shouldFail");
71 if (shouldFail != null && shouldFail) {
72 throw new ProcessEngineException(getClass().getSimpleName() + " Failed");
75 Object value = execution.getVariable(theInputVariable);
78 InputStream xmlStream = null;
81 xmlStream = getClass().getResourceAsStream(theFile);
83 if (xmlStream == null) {
84 throw new IOException("Resource not found: " + theFile);
87 BufferedReader reader = new BufferedReader(new InputStreamReader(xmlStream));
88 StringBuilder output = new StringBuilder();
91 while ((line = reader.readLine()) != null) {
98 value = output.toString();
101 if (xmlStream != null) {
104 } catch (Exception e) {
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());