More Unit tests for Sonar coverage
[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  * ===================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22 package org.onap.music.main;
23
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Properties;
31 import javax.servlet.ServletContextEvent;
32 import javax.servlet.ServletContextListener;
33 import javax.servlet.annotation.WebListener;
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 @WebListener
40 public class PropertiesListener implements ServletContextListener {
41     private Properties prop;
42
43     private static EELFLoggerDelegate logger =
44                     EELFLoggerDelegate.getLogger(PropertiesListener.class);
45
46     @Override
47     public void contextInitialized(ServletContextEvent servletContextEvent) {
48         prop = new Properties();
49         Properties projectProp = new Properties();
50         URL resource = getClass().getResource("/");
51         String musicPropertiesFilePath = resource.getPath().replace("WEB-INF/classes/",
52                         "WEB-INF/classes/project.properties");
53
54         // Open the file
55         try {
56             InputStream musicProps = null;
57             projectProp.load(new FileInputStream(musicPropertiesFilePath));
58             if (projectProp.containsKey("music.properties")) {
59                 musicProps = new FileInputStream(projectProp.getProperty("music.properties"));
60             } else {
61                 musicProps = new FileInputStream(MusicUtil.getMusicPropertiesFilePath());
62             }
63             prop.load(musicProps);
64             musicProps.close();
65             prop.putAll(projectProp);
66             String[] propKeys = MusicUtil.getPropkeys();
67             for (int k = 0; k < propKeys.length; k++) {
68                 String key = propKeys[k];
69                 if (prop.containsKey(key) && prop.get(key) != null) {
70                     logger.info(key + " : " + prop.getProperty(key));
71                     switch (key) {
72                         case "zookeeper.host":
73                             MusicUtil.setMyZkHost(prop.getProperty(key));
74                             break;
75                         case "cassandra.host":
76                             MusicUtil.setMyCassaHost(prop.getProperty(key));
77                             break;
78                         case "music.ip":
79                             MusicUtil.setDefaultMusicIp(prop.getProperty(key));
80                             break;
81                         case "debug":
82                             MusicUtil.setDebug(Boolean
83                                             .getBoolean(prop.getProperty(key).toLowerCase()));
84                             break;
85                         case "version":
86                             MusicUtil.setVersion(prop.getProperty(key));
87                             break;
88                         case "music.rest.ip":
89                             MusicUtil.setMusicRestIp(prop.getProperty(key));
90                             break;
91                         case "music.properties":
92                             MusicUtil.setMusicPropertiesFilePath(prop.getProperty(key));
93                             break;
94                         case "lock.lease.period":
95                             MusicUtil.setDefaultLockLeasePeriod(
96                                             Long.parseLong(prop.getProperty(key)));
97                             break;
98                         case "my.id":
99                             MusicUtil.setMyId(Integer.parseInt(prop.getProperty(key)));
100                             break;
101                         case "all.ids":
102                             String[] ids = prop.getProperty(key).split(":");
103                             MusicUtil.setAllIds(new ArrayList<String>(Arrays.asList(ids)));
104                             break;
105                         case "public.ip":
106                             MusicUtil.setPublicIp(prop.getProperty(key));
107                             break;
108                         case "all.public.ips":
109                             String[] ips = prop.getProperty(key).split(":");
110                             if (ips.length == 1) {
111                                 // Future use
112                             } else if (ips.length > 1) {
113                                 MusicUtil.setAllPublicIps(
114                                                 new ArrayList<String>(Arrays.asList(ips)));
115                             }
116                             break;
117                         case "cassandra.user":
118                             MusicUtil.setCassName(prop.getProperty(key));
119                             break;
120                         case "cassandra.password":
121                             MusicUtil.setCassPwd(prop.getProperty(key));
122                             break;
123                         case "aaf.endpoint.url":
124                             MusicUtil.setAafEndpointUrl(prop.getProperty(key));
125                             break;
126                         default:
127                             logger.error(EELFLoggerDelegate.errorLogger,
128                                             "No case found for " + key);
129                     }
130                 }
131             }
132         } catch (IOException e) {
133                 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.IOERROR  ,ErrorSeverity.CRITICAL, ErrorTypes.CONNECTIONERROR);
134             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
135         }
136
137         logger.info(EELFLoggerDelegate.applicationLogger,
138                         "Starting MUSIC " + MusicUtil.getVersion() + " on node with id "
139                                         + MusicUtil.getMyId() + " and public ip "
140                                         + MusicUtil.getPublicIp() + "...");
141         logger.info(EELFLoggerDelegate.applicationLogger,
142                         "List of all MUSIC ids:" + MusicUtil.getAllIds().toString());
143         logger.info(EELFLoggerDelegate.applicationLogger,
144                         "List of all MUSIC public ips:" + MusicUtil.getAllPublicIps().toString());
145     }
146
147     @Override
148     public void contextDestroyed(ServletContextEvent servletContextEvent) {
149         prop = null;
150     }
151 }