UI initial implementation.
[validation.git] / ui / src / main / java / org / akraino / validation / ui / common / PropertyUtil.java
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.akraino.validation.ui.common;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Properties;
21
22 import org.akraino.validation.ui.config.AppConfig;
23 import org.apache.log4j.Logger;
24
25 public class PropertyUtil {
26     private static final Logger LOGGER = Logger.getLogger(PropertyUtil.class);
27     private static final String PROP_FILENAME = "app.properties";
28     private static PropertyUtil instance;
29
30     private Properties appProps;
31
32     /**
33      * Return the single instance of this object in the app.
34      *
35      * @return the singleton
36      */
37     public static synchronized PropertyUtil getInstance() {
38         if (instance == null) {
39             instance = new PropertyUtil();
40         }
41         return instance;
42     }
43
44     private PropertyUtil() {
45         InputStream input = AppConfig.class.getClassLoader().getResourceAsStream(PROP_FILENAME);
46         appProps = new Properties();
47         try {
48             appProps.load(input);
49         } catch (IOException e) {
50             LOGGER.error("Error loading properties file: " + PROP_FILENAME);
51         } finally {
52             try {
53                 input.close();
54             } catch (IOException e) {
55                 // ignore
56             }
57         }
58     }
59
60     /**
61      * Get a property from the PropertyUtil object. If the environment variable $IP is set, then any URL's referring to
62      * localhost will be rewritten to use this IP address instead.
63      *
64      * @param key the key to use to find the property
65      * @return the value
66      */
67     public String getProperty(String key) {
68         String property = appProps.getProperty(key);
69         if (property != null && property.indexOf("://localhost:") > 0) {
70             String ipAddr = System.getenv().get("IP");
71             if (ipAddr != null && !"".contentEquals(ipAddr)) {
72                 property = property.replaceAll("://localhost:", "://" + ipAddr + ":");
73             }
74         }
75         return property;
76     }
77 }