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