2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * Modifications Copyright (C) 2018 IBM.
7 * ===================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * ============LICENSE_END=============================================
21 * ====================================================================
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 org.onap.music.eelf.logging.EELFLoggerDelegate;
35 import org.onap.music.eelf.logging.format.AppMessages;
36 import org.onap.music.eelf.logging.format.ErrorSeverity;
37 import org.onap.music.eelf.logging.format.ErrorTypes;
39 public class PropertiesListener implements ServletContextListener {
40 private Properties prop;
41 private static final String MUSIC_PROPERTIES="music.properties";
42 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertiesListener.class);
45 public void contextInitialized(ServletContextEvent servletContextEvent) {
46 prop = new Properties();
47 Properties projectProp = new Properties();
48 URL resource = getClass().getResource("/");
49 String musicPropertiesFilePath = resource.getPath().replace("WEB-INF/classes/","WEB-INF/classes/project.properties");
53 InputStream musicProps = null;
54 projectProp.load(new FileInputStream(musicPropertiesFilePath));
55 if (projectProp.containsKey(MUSIC_PROPERTIES)) {
56 musicProps = new FileInputStream(projectProp.getProperty(MUSIC_PROPERTIES));
58 musicProps = new FileInputStream(MusicUtil.getMusicPropertiesFilePath());
60 prop.load(musicProps);
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));
69 case "zookeeper.host":
70 MusicUtil.setMyZkHost(prop.getProperty(key));
72 case "cassandra.host":
73 MusicUtil.setMyCassaHost(prop.getProperty(key));
76 MusicUtil.setDefaultMusicIp(prop.getProperty(key));
79 MusicUtil.setDebug(Boolean
80 .getBoolean(prop.getProperty(key).toLowerCase()));
83 MusicUtil.setVersion(prop.getProperty(key));
86 MusicUtil.setMusicRestIp(prop.getProperty(key));
88 case MUSIC_PROPERTIES:
89 MusicUtil.setMusicPropertiesFilePath(prop.getProperty(key));
91 case "lock.lease.period":
92 MusicUtil.setDefaultLockLeasePeriod(
93 Long.parseLong(prop.getProperty(key)));
96 MusicUtil.setMyId(Integer.parseInt(prop.getProperty(key)));
99 String[] ids = prop.getProperty(key).split(":");
100 MusicUtil.setAllIds(new ArrayList<String>(Arrays.asList(ids)));
103 MusicUtil.setPublicIp(prop.getProperty(key));
105 case "all.public.ips":
106 String[] ips = prop.getProperty(key).split(":");
107 if (ips.length == 1) {
109 } else if (ips.length > 1) {
110 MusicUtil.setAllPublicIps(
111 new ArrayList<String>(Arrays.asList(ips)));
114 case "cassandra.user":
115 MusicUtil.setCassName(prop.getProperty(key));
117 case "cassandra.password":
118 MusicUtil.setCassPwd(prop.getProperty(key));
120 case "aaf.endpoint.url":
121 MusicUtil.setAafEndpointUrl(prop.getProperty(key));
123 case "cassandra.port":
124 MusicUtil.setCassandraPort(Integer.parseInt(prop.getProperty(key)));
126 case "notify.interval":
127 MusicUtil.setNotifyInterval(Integer.parseInt(prop.getProperty(key)));
129 case "notify.timeout":
130 MusicUtil.setNotifyTimeOut(Integer.parseInt(prop.getProperty(key)));
133 logger.error(EELFLoggerDelegate.errorLogger,
134 "No case found for " + key);
138 } catch (IOException e) {
139 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.IOERROR ,ErrorSeverity.CRITICAL, ErrorTypes.CONNECTIONERROR);
140 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
143 logger.info(EELFLoggerDelegate.applicationLogger,
144 "Starting MUSIC " + MusicUtil.getVersion() + " on node with id "
145 + MusicUtil.getMyId() + " and public ip "
146 + MusicUtil.getPublicIp() + "...");
147 logger.info(EELFLoggerDelegate.applicationLogger,
148 "List of all MUSIC ids:" + MusicUtil.getAllIds().toString());
149 logger.info(EELFLoggerDelegate.applicationLogger,
150 "List of all MUSIC public ips:" + MusicUtil.getAllPublicIps().toString());
154 public void contextDestroyed(ServletContextEvent servletContextEvent) {