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 javax.servlet.annotation.WebListener;
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;
40 public class PropertiesListener implements ServletContextListener {
41 private Properties prop;
43 private static EELFLoggerDelegate logger =
44 EELFLoggerDelegate.getLogger(PropertiesListener.class);
47 public void contextInitialized(ServletContextEvent servletContextEvent) {
48 prop = new Properties();
49 Properties projectProp = new Properties();
50 URL resource = getClass().getResource("/");
51 String musicPropertiesFilePath = resource.getPath().replace("WEB-INF/classes/",
52 "WEB-INF/classes/project.properties");
56 InputStream musicProps = null;
57 projectProp.load(new FileInputStream(musicPropertiesFilePath));
58 if (projectProp.containsKey("music.properties")) {
59 musicProps = new FileInputStream(projectProp.getProperty("music.properties"));
61 musicProps = new FileInputStream(MusicUtil.getMusicPropertiesFilePath());
63 prop.load(musicProps);
65 prop.putAll(projectProp);
66 String[] propKeys = MusicUtil.getPropkeys();
67 for (int k = 0; k < propKeys.length; k++) {
68 String key = propKeys[k];
69 if (prop.containsKey(key) && prop.get(key) != null) {
70 logger.info(key + " : " + prop.getProperty(key));
72 case "zookeeper.host":
73 MusicUtil.setMyZkHost(prop.getProperty(key));
75 case "cassandra.host":
76 MusicUtil.setMyCassaHost(prop.getProperty(key));
79 MusicUtil.setDefaultMusicIp(prop.getProperty(key));
82 MusicUtil.setDebug(Boolean
83 .getBoolean(prop.getProperty(key).toLowerCase()));
86 MusicUtil.setVersion(prop.getProperty(key));
89 MusicUtil.setMusicRestIp(prop.getProperty(key));
91 case "music.properties":
92 MusicUtil.setMusicPropertiesFilePath(prop.getProperty(key));
94 case "lock.lease.period":
95 MusicUtil.setDefaultLockLeasePeriod(
96 Long.parseLong(prop.getProperty(key)));
99 MusicUtil.setMyId(Integer.parseInt(prop.getProperty(key)));
102 String[] ids = prop.getProperty(key).split(":");
103 MusicUtil.setAllIds(new ArrayList<String>(Arrays.asList(ids)));
106 MusicUtil.setPublicIp(prop.getProperty(key));
108 case "all.public.ips":
109 String[] ips = prop.getProperty(key).split(":");
110 if (ips.length == 1) {
112 } else if (ips.length > 1) {
113 MusicUtil.setAllPublicIps(
114 new ArrayList<String>(Arrays.asList(ips)));
117 case "cassandra.user":
118 MusicUtil.setCassName(prop.getProperty(key));
120 case "cassandra.password":
121 MusicUtil.setCassPwd(prop.getProperty(key));
123 case "aaf.endpoint.url":
124 MusicUtil.setAafEndpointUrl(prop.getProperty(key));
127 logger.error(EELFLoggerDelegate.errorLogger,
128 "No case found for " + key);
132 } catch (IOException e) {
133 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.IOERROR ,ErrorSeverity.CRITICAL, ErrorTypes.CONNECTIONERROR);
134 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
137 logger.info(EELFLoggerDelegate.applicationLogger,
138 "Starting MUSIC " + MusicUtil.getVersion() + " on node with id "
139 + MusicUtil.getMyId() + " and public ip "
140 + MusicUtil.getPublicIp() + "...");
141 logger.info(EELFLoggerDelegate.applicationLogger,
142 "List of all MUSIC ids:" + MusicUtil.getAllIds().toString());
143 logger.info(EELFLoggerDelegate.applicationLogger,
144 "List of all MUSIC public ips:" + MusicUtil.getAllPublicIps().toString());
148 public void contextDestroyed(ServletContextEvent servletContextEvent) {