6249040342025379d45ac6f8234636bc748aba39
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / ReadConfigTask.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.IOException;
25 import java.io.InputStream;
26 import java.util.Properties;
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  * Reads the contents of a resource file as a string and stores it in an
36  * execution variable.
37  * <p>
38  * Required fields:<br/><br/>
39  * &nbsp;&nbsp;&nbsp;&nbsp;file: the resource file path<br/>
40  * &nbsp;&nbsp;&nbsp;&nbsp;outputVariable: the output variable name<br/>
41  */
42 public class ReadConfigTask extends BaseTask {
43         
44         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
45         private static Properties properties = null;
46
47         private Expression propertiesFile;
48
49         public void execute(DelegateExecution execution) throws Exception {
50                 if (msoLogger.isDebugEnabled()) {
51                         msoLogger.debug("Started Executing " + getTaskName());
52                 }
53
54                 String thePropertiesFile =
55                         getStringField(propertiesFile, execution, "propertiesFile");
56
57                 if (msoLogger.isDebugEnabled()) {
58                         msoLogger.debug("propertiesFile = " + thePropertiesFile);
59                 }
60
61                 Boolean shouldFail = (Boolean) execution.getVariable("shouldFail");
62
63                 if (shouldFail != null && shouldFail) {
64                         throw new ProcessEngineException(getClass().getSimpleName() + " Failed");
65                 }
66
67                 synchronized (ReadConfigTask.class) {
68                         if (properties == null) {
69                                 properties = new Properties();
70
71                                 InputStream stream = null;
72
73                                 try {
74                                         stream = getClass().getResourceAsStream(thePropertiesFile);
75
76                                         if (stream == null) {
77                                                 throw new IOException("Resource not found: " + thePropertiesFile);
78                                         }
79
80                                         properties.load(stream);
81
82                                         stream.close();
83                                         stream = null;
84
85                                 } finally {
86                                         if (stream != null) {
87                                                 try {
88                                                         stream.close();
89                                                 } catch (Exception e) {
90                                                     msoLogger.debug("Exception:", e);
91                                                 }
92                                         }
93                                 }
94                         }
95                 }
96
97                 for (Object objectKey : properties.keySet()) {
98                         String key = (String) objectKey;
99                         String value = properties.getProperty(key);
100
101                         if (msoLogger.isDebugEnabled()) {
102                                 msoLogger.debug("Setting variable '" + key + "' to '" + value + "'");
103                         }
104
105                         execution.setVariable(key, value);
106                 }
107
108                 if (msoLogger.isDebugEnabled()) {
109                         msoLogger.debug("Done Executing " + getTaskName());
110                 }
111         }
112 }