Added sonar fixes
[aai/champ.git] / champ-service / src / main / java / org / onap / champ / util / ChampProperties.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ===================================================================
8  * Modifications Copyright (C) 2019 IBM.
9  * ===================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END============================================
22  */
23 package org.onap.champ.util;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.util.Properties;
30 import org.onap.aai.cl.api.Logger;
31 import org.onap.aai.cl.eelf.LoggerFactory;
32 import org.onap.champ.service.logging.ChampMsgs;
33
34 public class ChampProperties {
35         
36         private static Logger logger = LoggerFactory.getInstance().getLogger(ChampProperties.class.getName());
37
38     private static Properties properties;
39
40     static {
41         properties = new Properties();
42         try (FileInputStream fileInputStream = new FileInputStream(
43             new File(ChampServiceConstants.CHAMP_CONFIG_FILE))
44         ) {
45             properties.load(fileInputStream);
46         } catch (IOException e) {
47                 logger.error(ChampMsgs.CHAMP_DATA_SERVICE_ERROR, "Error while loading properties ", e.getMessage());
48             Runtime.getRuntime().halt(1);
49         }
50     }
51
52     public static String get(String key) {
53         return properties.getProperty(key);
54     }
55
56     public static String get(String key, String defaultValue) {
57         return properties.getProperty(key, defaultValue);
58     }
59
60     public static void put(String key, String value) {
61         properties.setProperty(key, value);
62         try (FileOutputStream fileOut = new FileOutputStream(
63             new File(ChampServiceConstants.CHAMP_CONFIG_FILE))
64         ) {
65             properties.store(fileOut, "Added property: " + key);
66         } catch (Exception e) {
67                 logger.error(ChampMsgs.CHAMP_DATA_SERVICE_ERROR, "Error while setting properties ", e.getMessage());
68         }
69     }
70
71
72 }