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 * ====================================================================
22 package org.onap.music.main;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
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;
38 public class PropertiesListener implements ServletContextListener {
39 private Properties prop;
41 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertiesListener.class);
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");
52 InputStream musicProps = null;
53 projectProp.load(new FileInputStream(musicPropertiesFilePath));
54 if (projectProp.containsKey("music.properties")) {
55 musicProps = new FileInputStream(projectProp.getProperty("music.properties"));
57 musicProps = new FileInputStream(MusicUtil.getMusicPropertiesFilePath());
59 prop.load(musicProps);
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));
68 case "zookeeper.host":
69 MusicUtil.setMyZkHost(prop.getProperty(key));
71 case "cassandra.host":
72 MusicUtil.setMyCassaHost(prop.getProperty(key));
75 MusicUtil.setDefaultMusicIp(prop.getProperty(key));
78 MusicUtil.setDebug(Boolean
79 .getBoolean(prop.getProperty(key).toLowerCase()));
82 MusicUtil.setVersion(prop.getProperty(key));
85 MusicUtil.setMusicRestIp(prop.getProperty(key));
87 case "music.properties":
88 MusicUtil.setMusicPropertiesFilePath(prop.getProperty(key));
90 case "lock.lease.period":
91 MusicUtil.setDefaultLockLeasePeriod(
92 Long.parseLong(prop.getProperty(key)));
95 MusicUtil.setMyId(Integer.parseInt(prop.getProperty(key)));
98 String[] ids = prop.getProperty(key).split(":");
99 MusicUtil.setAllIds(new ArrayList<String>(Arrays.asList(ids)));
102 MusicUtil.setPublicIp(prop.getProperty(key));
104 case "all.public.ips":
105 String[] ips = prop.getProperty(key).split(":");
106 if (ips.length == 1) {
108 } else if (ips.length > 1) {
109 MusicUtil.setAllPublicIps(
110 new ArrayList<String>(Arrays.asList(ips)));
113 case "cassandra.user":
114 MusicUtil.setCassName(prop.getProperty(key));
116 case "cassandra.password":
117 MusicUtil.setCassPwd(prop.getProperty(key));
119 case "aaf.endpoint.url":
120 MusicUtil.setAafEndpointUrl(prop.getProperty(key));
122 case "cassandra.port":
123 MusicUtil.setCassandraPort(Integer.parseInt(prop.getProperty(key)));
125 case "notify.interval":
126 MusicUtil.setNotifyInterval(Integer.parseInt(prop.getProperty(key)));
128 case "notify.timeout":
129 MusicUtil.setNotifyTimeOut(Integer.parseInt(prop.getProperty(key)));
132 logger.error(EELFLoggerDelegate.errorLogger,
133 "No case found for " + key);
137 } catch (IOException e) {
138 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.IOERROR ,ErrorSeverity.CRITICAL, ErrorTypes.CONNECTIONERROR);
139 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
142 logger.info(EELFLoggerDelegate.applicationLogger,
143 "Starting MUSIC " + MusicUtil.getVersion() + " on node with id "
144 + MusicUtil.getMyId() + " and public ip "
145 + MusicUtil.getPublicIp() + "...");
146 logger.info(EELFLoggerDelegate.applicationLogger,
147 "List of all MUSIC ids:" + MusicUtil.getAllIds().toString());
148 logger.info(EELFLoggerDelegate.applicationLogger,
149 "List of all MUSIC public ips:" + MusicUtil.getAllPublicIps().toString());
153 public void contextDestroyed(ServletContextEvent servletContextEvent) {