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=========================================================
20 package org.openecomp.sdc.common.listener;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.HashMap;
27 import java.util.jar.Attributes;
28 import java.util.jar.Manifest;
29 import javax.servlet.ServletContext;
30 import javax.servlet.ServletContextEvent;
31 import javax.servlet.ServletContextListener;
32 import org.openecomp.sdc.common.api.ConfigurationSource;
33 import org.openecomp.sdc.common.api.Constants;
34 import org.openecomp.sdc.common.impl.ExternalConfiguration;
35 import org.openecomp.sdc.common.impl.FSConfigurationSource;
36 import org.openecomp.sdc.common.log.wrappers.Logger;
38 public class AppContextListener implements ServletContextListener {
40 private static Logger log = Logger.getLogger(AppContextListener.class.getName());
42 public void contextInitialized(ServletContextEvent context) {
43 log.debug("ServletContextListener initialized ");
44 log.debug("After read values from Manifest {}", getManifestInfo(context.getServletContext()));
45 Map<String, String> manifestAttr = getManifestInfo(context.getServletContext());
46 String appName = setAndGetAttributeInContext(context, manifestAttr, Constants.APPLICATION_NAME);
47 String appVersion = setAndGetAttributeInContext(context, manifestAttr, Constants.APPLICATION_VERSION);
48 ExternalConfiguration.setAppName(appName);
49 ExternalConfiguration.setAppVersion(appVersion);
50 String configHome = System.getProperty(Constants.CONFIG_HOME);
51 ExternalConfiguration.setConfigDir(configHome);
52 String appConfigDir = configHome + File.separator + appName;
53 // ChangeListener changeListener = new ChangeListener();
54 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
55 context.getServletContext().setAttribute(Constants.CONFIGURATION_SOURCE_ATTR, configurationSource);
56 ExternalConfiguration.setConfigurationSource(configurationSource);
57 ExternalConfiguration.listenForChanges();
60 public void contextDestroyed(ServletContextEvent context) {
61 log.debug("ServletContextListener destroyed");
62 ExternalConfiguration.stopListenForFileChanges();
65 private String setAndGetAttributeInContext(ServletContextEvent context, Map<String, String> manifestAttr, String attr) {
66 String name = manifestAttr.get(attr);
68 context.getServletContext().setAttribute(attr, name);
73 public static Map<String, String> getManifestInfo(ServletContext application) {
74 Map<String, String> result = new HashMap<>();
75 InputStream inputStream = null;
77 inputStream = application.getResourceAsStream("/META-INF/MANIFEST.MF");
78 Manifest manifest = new Manifest(inputStream);
79 Attributes attr = manifest.getMainAttributes();
80 String name = attr.getValue("Implementation-Title");
82 result.put(Constants.APPLICATION_NAME, name);
84 String version = attr.getValue("Implementation-Version");
85 if (version != null) {
86 result.put(Constants.APPLICATION_VERSION, version);
88 } catch (IOException e) {
90 if (inputStream != null) {
93 } catch (IOException e) {
94 log.info("close FileOutputStream failed - {}", e);