Containerization feature of SO
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / onap / so / bpmn / core / UrnPropertiesReader.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.bpmn.core;
22
23 import java.util.Optional;
24
25 import org.camunda.bpm.engine.delegate.DelegateExecution;
26 import org.onap.so.logger.MsoLogger;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.context.annotation.Configuration;
29 import org.springframework.core.env.Environment;
30 import org.springframework.stereotype.Component;
31
32
33 /**
34  * Read the URN property value from the execution object or from the spring environment object
35  */
36 @Component
37 @Configuration
38 public class UrnPropertiesReader {
39     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL,UrnPropertiesReader.class);
40     private static Environment environment;
41
42     @Autowired
43         public void setEnvironment(Environment environment) {
44         this.environment = environment;
45         }
46         /**
47      * Return the URN property value
48      * if property is present in the execution object, return the same
49      * else search in the environment object. If found, add it to the execution object and return the value
50      * otherwise return null
51      *
52      * @param variableName URN property name
53      * @param execution    The flow's execution instance.
54      * @return URN property value
55      */
56     public static String getVariable(String variableName, DelegateExecution execution) {
57         Object value = execution.getVariable(variableName);
58         if (value != null) {
59             LOGGER.trace("Retrieved value for the URN variable, " + variableName + ", from the execution object: " + String.valueOf(value));
60             return String.valueOf(value);
61         }
62         String variableValue = null;
63         if (environment != null && environment.getProperty(variableName) != null) {
64             variableValue = environment.getProperty(variableName);
65             LOGGER.trace("Retrieved value for the URN variable, " + variableName + ", from the environment variable: " + variableValue);
66             execution.setVariable(variableName, variableValue);
67             return variableValue;
68         }
69         return variableValue;
70     }
71
72     /**
73      * Return the URN property value from the environment object
74      * @param variableName URN property name
75      * @return URN property value
76      */
77
78     public static String getVariable(String variableName){
79         if (environment != null) {
80             return environment.getProperty(variableName);
81         } else {
82             return null;
83         }
84     }
85     
86     public static String getVariable(String variableName, String defaultValue) {
87         return Optional.ofNullable(getVariable(variableName)).orElse(defaultValue); 
88     }
89 }