aboutsummaryrefslogtreecommitdiffstats
path: root/common-app-api/src/main/java/org/openecomp/sdc/common/listener/AppContextListener.java
blob: 45976c42d126791b9c1a638c01813b3860a63139 (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
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.openecomp.sdc.common.listener;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.openecomp.sdc.common.api.ConfigurationSource;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.impl.ExternalConfiguration;
import org.openecomp.sdc.common.impl.FSConfigurationSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

public class AppContextListener implements ServletContextListener {

	private static Logger log = LoggerFactory.getLogger(AppContextListener.class.getName());

	public void contextInitialized(ServletContextEvent context) {

		log.debug("ServletContextListener initialized ");

		log.debug("After read values from Manifest {}", getManifestInfo(context.getServletContext()));

		Map<String, String> manifestAttr = getManifestInfo(context.getServletContext());

		String appName = setAndGetAttributeInContext(context, manifestAttr, Constants.APPLICATION_NAME);
		String appVersion = setAndGetAttributeInContext(context, manifestAttr, Constants.APPLICATION_VERSION);

		ExternalConfiguration.setAppName(appName);
		ExternalConfiguration.setAppVersion(appVersion);
		String configHome = System.getProperty(Constants.CONFIG_HOME);
		ExternalConfiguration.setConfigDir(configHome);

		String appConfigDir = configHome + File.separator + appName;
		// ChangeListener changeListener = new ChangeListener();
		ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
				appConfigDir);

		context.getServletContext().setAttribute(Constants.CONFIGURATION_SOURCE_ATTR, configurationSource);

		ExternalConfiguration.setConfigurationSource(configurationSource);

		ExternalConfiguration.listenForChanges();

	}

	public void contextDestroyed(ServletContextEvent context) {

		log.debug("ServletContextListener destroyed");
		ExternalConfiguration.stopListenForFileChanges();
	}

	private String setAndGetAttributeInContext(ServletContextEvent context, Map<String, String> manifestAttr,
			String attr) {

		String name = manifestAttr.get(attr);
		if (name != null) {
			context.getServletContext().setAttribute(attr, name);
		}

		return name;
	}

	public static Map<String, String> getManifestInfo(ServletContext application) {

		Map<String, String> result = new HashMap<String, String>();
		InputStream inputStream = null;
		try {

			inputStream = application.getResourceAsStream("/META-INF/MANIFEST.MF");

			Manifest manifest = new Manifest(inputStream);

			Attributes attr = manifest.getMainAttributes();
			String name = attr.getValue("Implementation-Title");
			if (name != null) {
				result.put(Constants.APPLICATION_NAME, name);
			}

			String version = attr.getValue("Implementation-Version");
			if (version != null) {
				result.put(Constants.APPLICATION_VERSION, version);
			}

		} catch (IOException e) {

		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return result;

	}

}