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 {
81 String basePath = System.getProperty("user.dir");
82 InputStream fileInputStream = new FileInputStream(basePath + "//" + fileName);
84 Properties props = new Properties();
85 props.load(fileInputStream);
91 * Load config from explicit path.
93 * @param fileName the file name
94 * @return the properties
96 public static Properties loadConfigFromExplicitPath(String fileName) {
98 Properties props = new Properties();
101 InputStream fileInputStream = new FileInputStream(fileName);
102 props.load(fileInputStream);
103 } catch (Exception exc) {
104 LOG.warn(AaiUiMsgs.CONFIG_NOT_FOUND_VERBOSE, fileName, exc.getLocalizedMessage());
113 * @param config the config
114 * @param propName the prop name
115 * @param defaultValue the default value
118 public static String propertyFetch(Properties config, String propName, String defaultValue) {
119 return config.getProperty(propName, defaultValue);
122 public static boolean isEssDevModeEnabled() {
123 return Boolean.parseBoolean(System.getProperty("isEssDevMode", "false"));
129 * @param fileName the file name
130 * @param isRelativePath the is relative path
131 * @return the filepath
133 public static String getFilepath(String fileName, boolean isRelativePath) {
135 String filepath = null;
137 if (isRelativePath) {
138 filepath = System.getProperty("user.dir") + "/" + fileName;
149 * Gets the file contents.
151 * @param fileName the file name
152 * @return the file contents
153 * @throws IOException Signals that an I/O exception has occurred.
155 public static String getFileContents(String fileName) throws IOException {
157 LOG.debug(AaiUiMsgs.FILE_READ_IN_PROGRESS, fileName);
159 File file = new File(fileName);
161 if (!file.exists()) {
162 throw new FileNotFoundException("Failed to load file = " + fileName);
165 if (file.exists() && !file.isDirectory()) {
166 BufferedReader br = new BufferedReader(new FileReader(file));
168 StringBuilder sb = new StringBuilder();
169 String line = br.readLine();
171 while (line != null) {
173 sb.append(System.lineSeparator());
174 line = br.readLine();
177 return sb.toString();
182 LOG.warn(AaiUiMsgs.FILE_NOT_FOUND, fileName);