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