2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.util;
27 import java.io.BufferedReader;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.Properties;
37 import org.onap.aai.cl.api.Logger;
38 import org.onap.aai.cl.eelf.LoggerFactory;
39 import org.onap.aai.sparky.logging.AaiUiMsgs;
42 * The Class ConfigHelper.
44 public class ConfigHelper {
46 private static final Logger LOG = LoggerFactory.getInstance().getLogger(ConfigHelper.class);
49 * Gets the config with prefix.
51 * @param configPrefix the config prefix
52 * @param properties the properties
53 * @return the config with prefix
55 public static Properties getConfigWithPrefix(String configPrefix, Properties properties) {
58 * The idea here is collect properties groups prefixed with the same origin
61 Set<Object> set = properties.keySet();
62 Properties newProps = new Properties();
64 for (Object k : set) {
65 String ks = (String) k;
66 if (ks.startsWith(configPrefix)) {
68 String temp = ks.replaceFirst(configPrefix + ".", "");
69 newProps.setProperty(temp, properties.getProperty(ks));
79 * @param fileName the file name
80 * @return the properties
81 * @throws Exception the exception
83 public static Properties loadConfig(String fileName) throws Exception {
85 String basePath = System.getProperty("user.dir");
86 InputStream fileInputStream = new FileInputStream(basePath + "//" + fileName);
88 Properties props = new Properties();
89 props.load(fileInputStream);
95 * Load config from explicit path.
97 * @param fileName the file name
98 * @return the properties
100 public static Properties loadConfigFromExplicitPath(String fileName) {
102 Properties props = new Properties();
105 InputStream fileInputStream = new FileInputStream(fileName);
106 props.load(fileInputStream);
107 } catch (Exception exc) {
108 LOG.warn(AaiUiMsgs.CONFIG_NOT_FOUND_VERBOSE, fileName, exc.getLocalizedMessage());
117 * @param config the config
118 * @param propName the prop name
119 * @param defaultValue the default value
122 public static String propertyFetch(Properties config, String propName, String defaultValue) {
123 return config.getProperty(propName, defaultValue);
126 public static boolean isEssDevModeEnabled() {
127 return Boolean.parseBoolean(System.getProperty("isEssDevMode", "false"));
133 * @param fileName the file name
134 * @param isRelativePath the is relative path
135 * @return the filepath
137 public static String getFilepath(String fileName, boolean isRelativePath) {
139 String filepath = null;
141 if (isRelativePath) {
142 filepath = System.getProperty("user.dir") + "/" + fileName;
153 * Gets the file contents.
155 * @param fileName the file name
156 * @return the file contents
157 * @throws IOException Signals that an I/O exception has occurred.
159 public static String getFileContents(String fileName) throws IOException {
161 LOG.debug(AaiUiMsgs.FILE_READ_IN_PROGRESS, fileName);
163 File file = new File(fileName);
165 if (!file.exists()) {
166 throw new FileNotFoundException("Failed to load file = " + fileName);
169 if (file.exists() && !file.isDirectory()) {
170 BufferedReader br = new BufferedReader(new FileReader(file));
172 StringBuilder sb = new StringBuilder();
173 String line = br.readLine();
175 while (line != null) {
177 sb.append(System.lineSeparator());
178 line = br.readLine();
181 return sb.toString();
186 LOG.warn(AaiUiMsgs.FILE_NOT_FOUND, fileName);