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