aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/music/main/PropertiesListener.java
blob: 0ed18be20ad74273200f1788ebbc06dc1b745b96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * ============LICENSE_START==========================================
 * org.onap.music
 * ===================================================================
 *  Copyright (c) 2017 AT&T Intellectual Property
 * ===================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 * ============LICENSE_END=============================================
 * ====================================================================
 */
package org.onap.music.main;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.onap.music.eelf.logging.EELFLoggerDelegate;
import org.onap.music.eelf.logging.format.AppMessages;
import org.onap.music.eelf.logging.format.ErrorSeverity;
import org.onap.music.eelf.logging.format.ErrorTypes;

public class PropertiesListener implements ServletContextListener {
    private Properties prop;

    private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PropertiesListener.class);

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        prop = new Properties();
        Properties projectProp = new Properties();
        URL resource = getClass().getResource("/");
        String musicPropertiesFilePath = resource.getPath().replace("WEB-INF/classes/","WEB-INF/classes/project.properties");

        // Open the file
        try {
            InputStream musicProps = null;
            projectProp.load(new FileInputStream(musicPropertiesFilePath));
            if (projectProp.containsKey("music.properties")) {
                musicProps = new FileInputStream(projectProp.getProperty("music.properties"));
            } else {
                musicProps = new FileInputStream(MusicUtil.getMusicPropertiesFilePath());
            }
            prop.load(musicProps);
            musicProps.close();
            prop.putAll(projectProp);
            String[] propKeys = MusicUtil.getPropkeys();
            for (int k = 0; k < propKeys.length; k++) {
                String key = propKeys[k];
                if (prop.containsKey(key) && prop.get(key) != null) {
                    logger.info(key + " : " + prop.getProperty(key));
                    switch (key) {
                        case "zookeeper.host":
                            MusicUtil.setMyZkHost(prop.getProperty(key));
                            break;
                        case "cassandra.host":
                            MusicUtil.setMyCassaHost(prop.getProperty(key));
                            break;
                        case "music.ip":
                            MusicUtil.setDefaultMusicIp(prop.getProperty(key));
                            break;
                        case "debug":
                            MusicUtil.setDebug(Boolean
                                            .getBoolean(prop.getProperty(key).toLowerCase()));
                            break;
                        case "version":
                            MusicUtil.setVersion(prop.getProperty(key));
                            break;
                        case "music.rest.ip":
                            MusicUtil.setMusicRestIp(prop.getProperty(key));
                            break;
                        case "music.properties":
                            MusicUtil.setMusicPropertiesFilePath(prop.getProperty(key));
                            break;
                        case "lock.lease.period":
                            MusicUtil.setDefaultLockLeasePeriod(
                                            Long.parseLong(prop.getProperty(key)));
                            break;
                        case "my.id":
                            MusicUtil.setMyId(Integer.parseInt(prop.getProperty(key)));
                            break;
                        case "all.ids":
                            String[] ids = prop.getProperty(key).split(":");
                            MusicUtil.setAllIds(new ArrayList<String>(Arrays.asList(ids)));
                            break;
                        case "public.ip":
                            MusicUtil.setPublicIp(prop.getProperty(key));
                            break;
                        case "all.public.ips":
                            String[] ips = prop.getProperty(key).split(":");
                            if (ips.length == 1) {
                                // Future use
                            } else if (ips.length > 1) {
                                MusicUtil.setAllPublicIps(
                                                new ArrayList<String>(Arrays.asList(ips)));
                            }
                            break;
                        case "cassandra.user":
                            MusicUtil.setCassName(prop.getProperty(key));
                            break;
                        case "cassandra.password":
                            MusicUtil.setCassPwd(prop.getProperty(key));
                            break;
                        case "aaf.endpoint.url":
                            MusicUtil.setAafEndpointUrl(prop.getProperty(key));
                            break;
                        case "cassandra.port":
                            MusicUtil.setCassandraPort(Integer.parseInt(prop.getProperty(key)));
                            break;
                        case "notify.interval":
                        	MusicUtil.setNotifyInterval(Integer.parseInt(prop.getProperty(key)));
                        	break;
                        case "notify.timeout":
                        	MusicUtil.setNotifyTimeOut(Integer.parseInt(prop.getProperty(key)));
                        	break;
                        default:
                            logger.error(EELFLoggerDelegate.errorLogger,
                                            "No case found for " + key);
                    }
                }
            }
        } catch (IOException e) {
        	logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.IOERROR  ,ErrorSeverity.CRITICAL, ErrorTypes.CONNECTIONERROR);
            logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
        }

        logger.info(EELFLoggerDelegate.applicationLogger,
                        "Starting MUSIC " + MusicUtil.getVersion() + " on node with id "
                                        + MusicUtil.getMyId() + " and public ip "
                                        + MusicUtil.getPublicIp() + "...");
        logger.info(EELFLoggerDelegate.applicationLogger,
                        "List of all MUSIC ids:" + MusicUtil.getAllIds().toString());
        logger.info(EELFLoggerDelegate.applicationLogger,
                        "List of all MUSIC public ips:" + MusicUtil.getAllPublicIps().toString());
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        prop = null;
    }
}