2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sparky.util;
23 import java.io.BufferedReader;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.Properties;
33 import org.onap.aai.cl.api.Logger;
34 import org.onap.aai.cl.eelf.LoggerFactory;
35 import org.onap.aai.sparky.logging.AaiUiMsgs;
38 * The Class ConfigHelper.
40 public class ConfigHelper {
42 private static final Logger LOG = LoggerFactory.getInstance().getLogger(ConfigHelper.class);
45 * Gets the config with prefix.
47 * @param configPrefix the config prefix
48 * @param properties the properties
49 * @return the config with prefix
51 public static Properties getConfigWithPrefix(String configPrefix, Properties properties) {
54 * The idea here is collect properties groups prefixed with the same origin
57 Set<Object> set = properties.keySet();
58 Properties newProps = new Properties();
60 for (Object k : set) {
61 String ks = (String) k;
62 if (ks.startsWith(configPrefix)) {
64 String temp = ks.replaceFirst(configPrefix + ".", "");
65 newProps.setProperty(temp, properties.getProperty(ks));
75 * @param fileName the file name
76 * @return the properties
77 * @throws Exception the exception
79 public static Properties loadConfig(String fileName) throws Exception {
80 String basePath = System.getProperty("user.dir");
81 try (InputStream fileInputStream = new FileInputStream(basePath + "//" + fileName)) {
82 Properties props = new Properties();
83 props.load(fileInputStream);
89 * Load config from explicit path.
91 * @param fileName the file name
92 * @return the properties
94 public static Properties loadConfigFromExplicitPath(String fileName) {
96 Properties props = new Properties();
97 try (InputStream fileInputStream = new FileInputStream(fileName)) {
98 props.load(fileInputStream);
99 } catch (Exception exc) {
100 LOG.warn(AaiUiMsgs.CONFIG_NOT_FOUND_VERBOSE, fileName, exc.getLocalizedMessage());
108 * @param config the config
109 * @param propName the prop name
110 * @param defaultValue the default value
113 public static String propertyFetch(Properties config, String propName, String defaultValue) {
114 return config.getProperty(propName, defaultValue);
117 public static boolean isEssDevModeEnabled() {
118 return Boolean.parseBoolean(System.getProperty("isEssDevMode", "false"));
124 * @param fileName the file name
125 * @param isRelativePath the is relative path
126 * @return the filepath
128 public static String getFilepath(String fileName, boolean isRelativePath) {
130 String filepath = null;
132 if (isRelativePath) {
133 filepath = System.getProperty("user.dir") + "/" + fileName;
144 * Gets the file contents.
146 * @param fileName the file name
147 * @return the file contents
148 * @throws IOException Signals that an I/O exception has occurred.
150 public static String getFileContents(String fileName) throws IOException {
152 LOG.debug(AaiUiMsgs.FILE_READ_IN_PROGRESS, fileName);
154 File file = new File(fileName);
156 if (!file.exists()) {
157 throw new FileNotFoundException("Failed to load file = " + fileName);
160 if (file.exists() && !file.isDirectory()) {
161 BufferedReader br = new BufferedReader(new FileReader(file));
163 StringBuilder sb = new StringBuilder();
164 String line = br.readLine();
166 while (line != null) {
168 sb.append(System.lineSeparator());
169 line = br.readLine();
172 return sb.toString();
177 LOG.warn(AaiUiMsgs.FILE_NOT_FOUND, fileName);