2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.common.listener;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.HashMap;
28 import java.util.jar.Attributes;
29 import java.util.jar.Manifest;
31 import javax.servlet.ServletContext;
32 import javax.servlet.ServletContextEvent;
33 import javax.servlet.ServletContextListener;
35 import org.openecomp.sdc.common.api.ConfigurationSource;
36 import org.openecomp.sdc.common.api.Constants;
37 import org.openecomp.sdc.common.impl.ExternalConfiguration;
38 import org.openecomp.sdc.common.impl.FSConfigurationSource;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class AppContextListener implements ServletContextListener {
44 private static Logger log = LoggerFactory.getLogger(AppContextListener.class.getName());
46 public void contextInitialized(ServletContextEvent context) {
48 log.debug("ServletContextListener initialized ");
50 log.debug("After read values from Manifest {}", getManifestInfo(context.getServletContext()));
52 Map<String, String> manifestAttr = getManifestInfo(context.getServletContext());
54 String appName = setAndGetAttributeInContext(context, manifestAttr, Constants.APPLICATION_NAME);
55 String appVersion = setAndGetAttributeInContext(context, manifestAttr, Constants.APPLICATION_VERSION);
57 ExternalConfiguration.setAppName(appName);
58 ExternalConfiguration.setAppVersion(appVersion);
59 String configHome = System.getProperty(Constants.CONFIG_HOME);
60 ExternalConfiguration.setConfigDir(configHome);
62 String appConfigDir = configHome + File.separator + appName;
63 // ChangeListener changeListener = new ChangeListener();
64 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
67 context.getServletContext().setAttribute(Constants.CONFIGURATION_SOURCE_ATTR, configurationSource);
69 ExternalConfiguration.setConfigurationSource(configurationSource);
71 ExternalConfiguration.listenForChanges();
75 public void contextDestroyed(ServletContextEvent context) {
77 log.debug("ServletContextListener destroyed");
78 ExternalConfiguration.stopListenForFileChanges();
81 private String setAndGetAttributeInContext(ServletContextEvent context, Map<String, String> manifestAttr,
84 String name = manifestAttr.get(attr);
86 context.getServletContext().setAttribute(attr, name);
92 public static Map<String, String> getManifestInfo(ServletContext application) {
94 Map<String, String> result = new HashMap<String, String>();
95 InputStream inputStream = null;
98 inputStream = application.getResourceAsStream("/META-INF/MANIFEST.MF");
100 Manifest manifest = new Manifest(inputStream);
102 Attributes attr = manifest.getMainAttributes();
103 String name = attr.getValue("Implementation-Title");
105 result.put(Constants.APPLICATION_NAME, name);
108 String version = attr.getValue("Implementation-Version");
109 if (version != null) {
110 result.put(Constants.APPLICATION_VERSION, version);
113 } catch (IOException e) {
116 if (inputStream != null) {
119 } catch (IOException e) {