Merge "Fix Blocker/Critical sonar issues"
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / util / ConfigHelper.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.sparky.util;
24
25 import java.io.BufferedReader;
26 import java.io.File;
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;
33 import java.util.Set;
34
35 import org.openecomp.cl.api.Logger;
36 import org.openecomp.cl.eelf.LoggerFactory;
37 import org.openecomp.sparky.logging.AaiUiMsgs;
38
39 /**
40  * The Class ConfigHelper.
41  */
42 public class ConfigHelper {
43
44   private static final Logger LOG = LoggerFactory.getInstance().getLogger(ConfigHelper.class);
45
46   /**
47    * Gets the config with prefix.
48    *
49    * @param configPrefix the config prefix
50    * @param properties the properties
51    * @return the config with prefix
52    */
53   public static Properties getConfigWithPrefix(String configPrefix, Properties properties) {
54
55     /*
56      * The idea here is collect properties groups prefixed with the same origin
57      */
58
59     Set<Object> set = properties.keySet();
60     Properties newProps = new Properties();
61
62     for (Object k : set) {
63       String ks = (String) k;
64       if (ks.startsWith(configPrefix)) {
65
66         String temp = ks.replaceFirst(configPrefix + ".", "");
67         newProps.setProperty(temp, properties.getProperty(ks));
68       }
69     }
70
71     return newProps;
72   }
73
74   /**
75    * Load config.
76    *
77    * @param fileName the file name
78    * @return the properties
79    * @throws Exception the exception
80    */
81   public static Properties loadConfig(String fileName) throws Exception {
82
83     String basePath = System.getProperty("user.dir");
84     InputStream fileInputStream = new FileInputStream(basePath + "//" + fileName);
85
86     Properties props = new Properties();
87     props.load(fileInputStream);
88
89     return props;
90   }
91
92   /**
93    * Load config from explicit path.
94    *
95    * @param fileName the file name
96    * @return the properties
97    */
98   public static Properties loadConfigFromExplicitPath(String fileName) {
99
100     Properties props = new Properties();
101
102     try {
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());
107     }
108
109     return props;
110   }
111
112   /**
113    * Property fetch.
114    *
115    * @param config the config
116    * @param propName the prop name
117    * @param defaultValue the default value
118    * @return the string
119    */
120   public static String propertyFetch(Properties config, String propName, String defaultValue) {
121     return config.getProperty(propName, defaultValue);
122   }
123
124   public static boolean isEssDevModeEnabled() {
125     return Boolean.parseBoolean(System.getProperty("isEssDevMode", "false"));
126   }
127
128   /**
129    * Gets the filepath.
130    *
131    * @param fileName the file name
132    * @param isRelativePath the is relative path
133    * @return the filepath
134    */
135   public static String getFilepath(String fileName, boolean isRelativePath) {
136
137     String filepath = null;
138
139     if (isRelativePath) {
140       filepath = System.getProperty("user.dir") + "/" + fileName;
141
142     } else {
143       filepath = fileName;
144     }
145
146     return filepath;
147
148   }
149
150   /**
151    * Gets the file contents.
152    *
153    * @param fileName the file name
154    * @return the file contents
155    * @throws IOException Signals that an I/O exception has occurred.
156    */
157   public static String getFileContents(String fileName) throws IOException {
158
159     LOG.debug(AaiUiMsgs.FILE_READ_IN_PROGRESS, fileName);
160
161     File file = new File(fileName);
162
163     if (!file.exists()) {
164       throw new FileNotFoundException("Failed to load file = " + fileName);
165     }
166
167     if (file.exists() && !file.isDirectory()) {
168       BufferedReader br = new BufferedReader(new FileReader(file));
169       try {
170         StringBuilder sb = new StringBuilder();
171         String line = br.readLine();
172
173         while (line != null) {
174           sb.append(line);
175           sb.append(System.lineSeparator());
176           line = br.readLine();
177         }
178
179         return sb.toString();
180       } finally {
181         br.close();
182       }
183     } else {
184       LOG.warn(AaiUiMsgs.FILE_NOT_FOUND, fileName);
185     }
186
187     return null;
188
189   }
190
191 }