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;
23 import org.openecomp.sdc.common.api.ConfigurationSource;
24 import org.openecomp.sdc.common.api.Constants;
25 import org.openecomp.sdc.common.impl.ExternalConfiguration;
26 import org.openecomp.sdc.common.impl.FSConfigurationSource;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
29 import javax.servlet.ServletContext;
30 import javax.servlet.ServletContextEvent;
31 import javax.servlet.ServletContextListener;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.HashMap;
37 import java.util.jar.Attributes;
38 import java.util.jar.Manifest;
40 public class AppContextListener implements ServletContextListener {
42 private static Logger log = Logger.getLogger(AppContextListener.class.getName());
44 public void contextInitialized(ServletContextEvent context) {
46 log.debug("ServletContextListener initialized ");
48 log.debug("After read values from Manifest {}", getManifestInfo(context.getServletContext()));
50 Map<String, String> manifestAttr = getManifestInfo(context.getServletContext());
52 String appName = setAndGetAttributeInContext(context, manifestAttr, Constants.APPLICATION_NAME);
53 String appVersion = setAndGetAttributeInContext(context, manifestAttr, Constants.APPLICATION_VERSION);
55 ExternalConfiguration.setAppName(appName);
56 ExternalConfiguration.setAppVersion(appVersion);
57 String configHome = System.getProperty(Constants.CONFIG_HOME);
58 ExternalConfiguration.setConfigDir(configHome);
60 String appConfigDir = configHome + File.separator + appName;
61 // ChangeListener changeListener = new ChangeListener();
62 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
65 context.getServletContext().setAttribute(Constants.CONFIGURATION_SOURCE_ATTR, configurationSource);
67 ExternalConfiguration.setConfigurationSource(configurationSource);
69 ExternalConfiguration.listenForChanges();
73 public void contextDestroyed(ServletContextEvent context) {
75 log.debug("ServletContextListener destroyed");
76 ExternalConfiguration.stopListenForFileChanges();
79 private String setAndGetAttributeInContext(ServletContextEvent context, Map<String, String> manifestAttr,
82 String name = manifestAttr.get(attr);
84 context.getServletContext().setAttribute(attr, name);
90 public static Map<String, String> getManifestInfo(ServletContext application) {
92 Map<String, String> result = new HashMap<>();
93 InputStream inputStream = null;
96 inputStream = application.getResourceAsStream("/META-INF/MANIFEST.MF");
98 Manifest manifest = new Manifest(inputStream);
100 Attributes attr = manifest.getMainAttributes();
101 String name = attr.getValue("Implementation-Title");
103 result.put(Constants.APPLICATION_NAME, name);
106 String version = attr.getValue("Implementation-Version");
107 if (version != null) {
108 result.put(Constants.APPLICATION_VERSION, version);
111 } catch (IOException e) {
114 if (inputStream != null) {
117 } catch (IOException e) {
118 log.info("close FileOutputStream failed - {}" , e);