2 * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.akraino.validation.ui.common;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Properties;
22 import org.akraino.validation.ui.config.AppConfig;
23 import org.apache.log4j.Logger;
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;
30 private Properties appProps;
33 * Return the single instance of this object in the app.
35 * @return the singleton
37 public static synchronized PropertyUtil getInstance() {
38 if (instance == null) {
39 instance = new PropertyUtil();
44 private PropertyUtil() {
45 InputStream input = AppConfig.class.getClassLoader().getResourceAsStream(PROP_FILENAME);
46 appProps = new Properties();
49 } catch (IOException e) {
50 LOGGER.error("Error loading properties file: " + PROP_FILENAME);
54 } catch (IOException e) {
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.
64 * @param key the key to use to find the property
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 + ":");