Initial code Import.
[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
36 @WebListener
37 public class PropertiesListener implements ServletContextListener {
38     private Properties prop;
39
40     private static EELFLoggerDelegate logger =
41                     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/",
49                         "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                         default:
124                             logger.error(EELFLoggerDelegate.errorLogger,
125                                             "No case found for " + key);
126                     }
127                 }
128             }
129         } catch (IOException e) {
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 }