Merge "fixed sonar issues in RestMusicAdminAPI.java"
[music.git] / src / main / java / org / onap / music / main / PropertiesListener.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  *  Modifications Copyright (C) 2018 IBM.
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  * 
20  * ============LICENSE_END=============================================
21  * ====================================================================
22  */
23 package org.onap.music.main;
24
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URL;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Properties;
32 import javax.servlet.ServletContextEvent;
33 import javax.servlet.ServletContextListener;
34 import org.onap.music.eelf.logging.EELFLoggerDelegate;
35 import org.onap.music.eelf.logging.format.AppMessages;
36 import org.onap.music.eelf.logging.format.ErrorSeverity;
37 import org.onap.music.eelf.logging.format.ErrorTypes;
38
39 public class PropertiesListener implements ServletContextListener {
40     private Properties prop;
41     private static final String MUSIC_PROPERTIES="music.properties";
42     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertiesListener.class);
43
44     @Override
45     public void contextInitialized(ServletContextEvent servletContextEvent) {
46         prop = new Properties();
47         Properties projectProp = new Properties();
48         URL resource = getClass().getResource("/");
49         String musicPropertiesFilePath = resource.getPath().replace("WEB-INF/classes/","WEB-INF/classes/project.properties");
50
51         // Open the file
52         try {
53             InputStream musicProps = null;
54             projectProp.load(new FileInputStream(musicPropertiesFilePath));
55             if (projectProp.containsKey(MUSIC_PROPERTIES)) {
56                 musicProps = new FileInputStream(projectProp.getProperty(MUSIC_PROPERTIES));
57             } else {
58                 musicProps = new FileInputStream(MusicUtil.getMusicPropertiesFilePath());
59             }
60             prop.load(musicProps);
61             musicProps.close();
62             prop.putAll(projectProp);
63             String[] propKeys = MusicUtil.getPropkeys();
64             for (int k = 0; k < propKeys.length; k++) {
65                 String key = propKeys[k];
66                 if (prop.containsKey(key) && prop.get(key) != null) {
67                     logger.info(key + " : " + prop.getProperty(key));
68                     switch (key) {
69                         case "zookeeper.host":
70                             MusicUtil.setMyZkHost(prop.getProperty(key));
71                             break;
72                         case "cassandra.host":
73                             MusicUtil.setMyCassaHost(prop.getProperty(key));
74                             break;
75                         case "music.ip":
76                             MusicUtil.setDefaultMusicIp(prop.getProperty(key));
77                             break;
78                         case "debug":
79                             MusicUtil.setDebug(Boolean
80                                             .getBoolean(prop.getProperty(key).toLowerCase()));
81                             break;
82                         case "version":
83                             MusicUtil.setVersion(prop.getProperty(key));
84                             break;
85                         case "music.rest.ip":
86                             MusicUtil.setMusicRestIp(prop.getProperty(key));
87                             break;
88                         case MUSIC_PROPERTIES:
89                             MusicUtil.setMusicPropertiesFilePath(prop.getProperty(key));
90                             break;
91                         case "lock.lease.period":
92                             MusicUtil.setDefaultLockLeasePeriod(
93                                             Long.parseLong(prop.getProperty(key)));
94                             break;
95                         case "my.id":
96                             MusicUtil.setMyId(Integer.parseInt(prop.getProperty(key)));
97                             break;
98                         case "all.ids":
99                             String[] ids = prop.getProperty(key).split(":");
100                             MusicUtil.setAllIds(new ArrayList<String>(Arrays.asList(ids)));
101                             break;
102                         case "public.ip":
103                             MusicUtil.setPublicIp(prop.getProperty(key));
104                             break;
105                         case "all.public.ips":
106                             String[] ips = prop.getProperty(key).split(":");
107                             if (ips.length == 1) {
108                                 // Future use
109                             } else if (ips.length > 1) {
110                                 MusicUtil.setAllPublicIps(
111                                                 new ArrayList<String>(Arrays.asList(ips)));
112                             }
113                             break;
114                         case "cassandra.user":
115                             MusicUtil.setCassName(prop.getProperty(key));
116                             break;
117                         case "cassandra.password":
118                             MusicUtil.setCassPwd(prop.getProperty(key));
119                             break;
120                         case "aaf.endpoint.url":
121                             MusicUtil.setAafEndpointUrl(prop.getProperty(key));
122                             break;
123                         case "cassandra.port":
124                             MusicUtil.setCassandraPort(Integer.parseInt(prop.getProperty(key)));
125                             break;
126                         case "notify.interval":
127                                 MusicUtil.setNotifyInterval(Integer.parseInt(prop.getProperty(key)));
128                                 break;
129                         case "notify.timeout":
130                                 MusicUtil.setNotifyTimeOut(Integer.parseInt(prop.getProperty(key)));
131                                 break;
132                         default:
133                             logger.error(EELFLoggerDelegate.errorLogger,
134                                             "No case found for " + key);
135                     }
136                 }
137             }
138         } catch (IOException e) {
139                 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.IOERROR  ,ErrorSeverity.CRITICAL, ErrorTypes.CONNECTIONERROR);
140             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
141         }
142
143         logger.info(EELFLoggerDelegate.applicationLogger,
144                         "Starting MUSIC " + MusicUtil.getVersion() + " on node with id "
145                                         + MusicUtil.getMyId() + " and public ip "
146                                         + MusicUtil.getPublicIp() + "...");
147         logger.info(EELFLoggerDelegate.applicationLogger,
148                         "List of all MUSIC ids:" + MusicUtil.getAllIds().toString());
149         logger.info(EELFLoggerDelegate.applicationLogger,
150                         "List of all MUSIC public ips:" + MusicUtil.getAllPublicIps().toString());
151     }
152
153     @Override
154     public void contextDestroyed(ServletContextEvent servletContextEvent) {
155         prop = null;
156     }
157 }