2 * ============LICENSE_START==========================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
23 package org.onap.music.main;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
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;
41 public class PropertiesListener implements ServletContextListener {
42 private Properties prop;
44 private static EELFLoggerDelegate logger =
45 EELFLoggerDelegate.getLogger(PropertiesListener.class);
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");
57 InputStream musicProps = null;
58 projectProp.load(new FileInputStream(musicPropertiesFilePath));
59 if (projectProp.containsKey("music.properties")) {
60 musicProps = new FileInputStream(projectProp.getProperty("music.properties"));
62 musicProps = new FileInputStream(MusicUtil.getMusicPropertiesFilePath());
64 prop.load(musicProps);
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));
73 case "zookeeper.host":
74 MusicUtil.setMyZkHost(prop.getProperty(key));
76 case "cassandra.host":
77 MusicUtil.setMyCassaHost(prop.getProperty(key));
80 MusicUtil.setDefaultMusicIp(prop.getProperty(key));
83 MusicUtil.setDebug(Boolean
84 .getBoolean(prop.getProperty(key).toLowerCase()));
87 MusicUtil.setVersion(prop.getProperty(key));
90 MusicUtil.setMusicRestIp(prop.getProperty(key));
92 case "music.properties":
93 MusicUtil.setMusicPropertiesFilePath(prop.getProperty(key));
95 case "lock.lease.period":
96 MusicUtil.setDefaultLockLeasePeriod(
97 Long.parseLong(prop.getProperty(key)));
100 MusicUtil.setMyId(Integer.parseInt(prop.getProperty(key)));
103 String[] ids = prop.getProperty(key).split(":");
104 MusicUtil.setAllIds(new ArrayList<String>(Arrays.asList(ids)));
107 MusicUtil.setPublicIp(prop.getProperty(key));
109 case "all.public.ips":
110 String[] ips = prop.getProperty(key).split(":");
111 if (ips.length == 1) {
113 } else if (ips.length > 1) {
114 MusicUtil.setAllPublicIps(
115 new ArrayList<String>(Arrays.asList(ips)));
118 case "cassandra.user":
119 MusicUtil.setCassName(prop.getProperty(key));
121 case "cassandra.password":
122 MusicUtil.setCassPwd(prop.getProperty(key));
124 case "aaf.endpoint.url":
125 MusicUtil.setAafEndpointUrl(prop.getProperty(key));
128 logger.error(EELFLoggerDelegate.errorLogger,
129 "No case found for " + key);
133 } catch (IOException e) {
134 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.IOERROR ,ErrorSeverity.CRITICAL, ErrorTypes.CONNECTIONERROR);
135 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
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());
149 public void contextDestroyed(ServletContextEvent servletContextEvent) {