2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.util;
25 import java.io.BufferedReader;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.Properties;
35 import org.onap.aai.cl.api.Logger;
36 import org.onap.aai.cl.eelf.LoggerFactory;
37 import org.onap.aai.sparky.logging.AaiUiMsgs;
40 * The Class ConfigHelper.
42 public class ConfigHelper {
44 private static final Logger LOG = LoggerFactory.getInstance().getLogger(ConfigHelper.class);
47 * Gets the config with prefix.
49 * @param configPrefix the config prefix
50 * @param properties the properties
51 * @return the config with prefix
53 public static Properties getConfigWithPrefix(String configPrefix, Properties properties) {
56 * The idea here is collect properties groups prefixed with the same origin
59 Set<Object> set = properties.keySet();
60 Properties newProps = new Properties();
62 for (Object k : set) {
63 String ks = (String) k;
64 if (ks.startsWith(configPrefix)) {
66 String temp = ks.replaceFirst(configPrefix + ".", "");
67 newProps.setProperty(temp, properties.getProperty(ks));
77 * @param fileName the file name
78 * @return the properties
79 * @throws Exception the exception
81 public static Properties loadConfig(String fileName) throws Exception {
83 String basePath = System.getProperty("user.dir");
84 InputStream fileInputStream = new FileInputStream(basePath + "//" + fileName);
86 Properties props = new Properties();
87 props.load(fileInputStream);
93 * Load config from explicit path.
95 * @param fileName the file name
96 * @return the properties
98 public static Properties loadConfigFromExplicitPath(String fileName) {
100 Properties props = new Properties();
103 InputStream fileInputStream = new FileInputStream(fileName);
104 props.load(fileInputStream);
105 } catch (Exception exc) {
106 LOG.warn(AaiUiMsgs.CONFIG_NOT_FOUND_VERBOSE, fileName, exc.getLocalizedMessage());
115 * @param config the config
116 * @param propName the prop name
117 * @param defaultValue the default value
120 public static String propertyFetch(Properties config, String propName, String defaultValue) {
121 return config.getProperty(propName, defaultValue);
124 public static boolean isEssDevModeEnabled() {
125 return Boolean.parseBoolean(System.getProperty("isEssDevMode", "false"));
131 * @param fileName the file name
132 * @param isRelativePath the is relative path
133 * @return the filepath
135 public static String getFilepath(String fileName, boolean isRelativePath) {
137 String filepath = null;
139 if (isRelativePath) {
140 filepath = System.getProperty("user.dir") + "/" + fileName;
151 * Gets the file contents.
153 * @param fileName the file name
154 * @return the file contents
155 * @throws IOException Signals that an I/O exception has occurred.
157 public static String getFileContents(String fileName) throws IOException {
159 LOG.debug(AaiUiMsgs.FILE_READ_IN_PROGRESS, fileName);
161 File file = new File(fileName);
163 if (!file.exists()) {
164 throw new FileNotFoundException("Failed to load file = " + fileName);
167 if (file.exists() && !file.isDirectory()) {
168 BufferedReader br = new BufferedReader(new FileReader(file));
170 StringBuilder sb = new StringBuilder();
171 String line = br.readLine();
173 while (line != null) {
175 sb.append(System.lineSeparator());
176 line = br.readLine();
179 return sb.toString();
184 LOG.warn(AaiUiMsgs.FILE_NOT_FOUND, fileName);