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.
26 package org.openecomp.sparky.util;
28 import java.io.BufferedReader;
30 import java.io.FileInputStream;
31 import java.io.FileNotFoundException;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.Properties;
38 import org.openecomp.cl.api.Logger;
39 import org.openecomp.cl.eelf.LoggerFactory;
40 import org.openecomp.sparky.logging.AaiUiMsgs;
43 * The Class ConfigHelper.
45 public class ConfigHelper {
47 private static final Logger LOG = LoggerFactory.getInstance().getLogger(ConfigHelper.class);
50 * Gets the config with prefix.
52 * @param configPrefix the config prefix
53 * @param properties the properties
54 * @return the config with prefix
56 public static Properties getConfigWithPrefix(String configPrefix, Properties properties) {
59 * The idea here is collect properties groups prefixed with the same origin
62 Set<Object> set = properties.keySet();
63 Properties newProps = new Properties();
65 for (Object k : set) {
66 String ks = (String) k;
67 if (ks.startsWith(configPrefix)) {
69 String temp = ks.replaceFirst(configPrefix + ".", "");
70 newProps.setProperty(temp, properties.getProperty(ks));
80 * @param fileName the file name
81 * @return the properties
82 * @throws Exception the exception
84 public static Properties loadConfig(String fileName) throws Exception {
86 String basePath = System.getProperty("user.dir");
87 InputStream fileInputStream = new FileInputStream(basePath + "//" + fileName);
89 Properties props = new Properties();
90 props.load(fileInputStream);
96 * Load config from explicit path.
98 * @param fileName the file name
99 * @return the properties
101 public static Properties loadConfigFromExplicitPath(String fileName) {
103 Properties props = new Properties();
106 InputStream fileInputStream = new FileInputStream(fileName);
107 props.load(fileInputStream);
108 } catch (Exception exc) {
109 LOG.warn(AaiUiMsgs.CONFIG_NOT_FOUND_VERBOSE, fileName, exc.getLocalizedMessage());
118 * @param config the config
119 * @param propName the prop name
120 * @param defaultValue the default value
123 public static String propertyFetch(Properties config, String propName, String defaultValue) {
124 return config.getProperty(propName, defaultValue);
127 public static boolean isEssDevModeEnabled() {
128 return Boolean.parseBoolean(System.getProperty("isEssDevMode", "false"));
134 * @param fileName the file name
135 * @param isRelativePath the is relative path
136 * @return the filepath
138 public static String getFilepath(String fileName, boolean isRelativePath) {
140 String filepath = null;
142 if (isRelativePath) {
143 filepath = System.getProperty("user.dir") + "/" + fileName;
154 * Gets the file contents.
156 * @param fileName the file name
157 * @return the file contents
158 * @throws IOException Signals that an I/O exception has occurred.
160 public static String getFileContents(String fileName) throws IOException {
162 LOG.debug(AaiUiMsgs.FILE_READ_IN_PROGRESS, fileName);
164 File file = new File(fileName);
166 if (!file.exists()) {
167 throw new FileNotFoundException("Failed to load file = " + fileName);
170 if (file.exists() && !file.isDirectory()) {
171 BufferedReader br = new BufferedReader(new FileReader(file));
173 StringBuilder sb = new StringBuilder();
174 String line = br.readLine();
176 while (line != null) {
178 sb.append(System.lineSeparator());
179 line = br.readLine();
182 return sb.toString();
187 LOG.warn(AaiUiMsgs.FILE_NOT_FOUND, fileName);