Revert "Support SIP TLS"
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / listener / AppContextListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.common.listener;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.HashMap;
26 import java.util.Map;
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;
37
38 public class AppContextListener implements ServletContextListener {
39
40     private static Logger log = Logger.getLogger(AppContextListener.class.getName());
41
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();
58     }
59
60     public void contextDestroyed(ServletContextEvent context) {
61         log.debug("ServletContextListener destroyed");
62         ExternalConfiguration.stopListenForFileChanges();
63     }
64
65     private String setAndGetAttributeInContext(ServletContextEvent context, Map<String, String> manifestAttr, String attr) {
66         String name = manifestAttr.get(attr);
67         if (name != null) {
68             context.getServletContext().setAttribute(attr, name);
69         }
70         return name;
71     }
72
73     public static Map<String, String> getManifestInfo(ServletContext application) {
74         Map<String, String> result = new HashMap<>();
75         InputStream inputStream = null;
76         try {
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");
81             if (name != null) {
82                 result.put(Constants.APPLICATION_NAME, name);
83             }
84             String version = attr.getValue("Implementation-Version");
85             if (version != null) {
86                 result.put(Constants.APPLICATION_VERSION, version);
87             }
88         } catch (IOException e) {
89         } finally {
90             if (inputStream != null) {
91                 try {
92                     inputStream.close();
93                 } catch (IOException e) {
94                     log.info("close FileOutputStream failed - {}", e);
95                 }
96             }
97         }
98         return result;
99     }
100 }