2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.mso.bpmn.core;
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
29 import org.camunda.bpm.engine.ProcessEngineException;
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.camunda.bpm.engine.delegate.Expression;
33 import org.openecomp.mso.logger.MsoLogger;
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
40 * Required fields:<br/><br/>
41 * file: the resource file path<br/>
42 * inputVariable: the input variable name<br/>
43 * outputVariable: the output variable name<br/>
45 public class ReadFileTask extends BaseTask {
47 private Expression file;
48 private Expression inputVariable;
49 private Expression outputVariable;
51 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
53 public void execute(DelegateExecution execution) throws Exception {
54 if (msoLogger.isDebugEnabled()) {
55 msoLogger.debug("Started Executing " + getTaskName());
58 String theInputVariable =
59 getStringField(inputVariable, execution, "inputVariable");
60 String theOutputVariable =
61 getOutputField(outputVariable, execution, "outputVariable");
62 String theFile =getStringField(file, execution, "file");
64 if (msoLogger.isDebugEnabled()) {
65 msoLogger.debug("inputVariable = " + theInputVariable
66 + " outputVariable = " + theOutputVariable
67 + "file = " + theFile);
70 Boolean shouldFail = (Boolean) execution.getVariable("shouldFail");
72 if (shouldFail != null && shouldFail) {
73 throw new ProcessEngineException(getClass().getSimpleName() + " Failed");
76 Object value = execution.getVariable(theInputVariable);
79 InputStream xmlStream = null;
82 xmlStream = getClass().getResourceAsStream(theFile);
84 if (xmlStream == null) {
85 throw new IOException("Resource not found: " + theFile);
88 BufferedReader reader = new BufferedReader(new InputStreamReader(xmlStream));
89 StringBuilder output = new StringBuilder();
92 while ((line = reader.readLine()) != null) {
99 value = output.toString();
102 if (xmlStream != null) {
105 } catch (Exception e) {
106 msoLogger.debug("Exception ", e);
111 execution.setVariable(theInputVariable, value);
112 execution.setVariable(theOutputVariable, value);
113 msoLogger.debug ("ServiceInput - " + execution.getVariable("gServiceInput"));
114 if (msoLogger.isDebugEnabled()) {
115 msoLogger.debug("Done Executing " + getTaskName());