Changed try to try with resource
[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                                 try (InputStream stream = getClass().getResourceAsStream(thePropertiesFile)) {
71
72                                         properties.load(stream);
73
74                                 } catch (Exception e) {
75                                         msoLogger.debug("Exception at readResourceFile stream: " + e);
76                                 }
77                         }
78                 }
79
80                 for (Object objectKey : properties.keySet()) {
81                         String key = (String) objectKey;
82                         String value = properties.getProperty(key);
83
84                         if (msoLogger.isDebugEnabled()) {
85                                 msoLogger.debug("Setting variable '" + key + "' to '" + value + "'");
86                         }
87
88                         execution.setVariable(key, value);
89                 }
90
91                 if (msoLogger.isDebugEnabled()) {
92                         msoLogger.debug("Done Executing " + getTaskName());
93                 }
94         }
95 }