fixing warnings from checkstyle in common-app-api
[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
21 package org.openecomp.sdc.common.listener;
22
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;
28
29 import javax.servlet.ServletContext;
30 import javax.servlet.ServletContextEvent;
31 import javax.servlet.ServletContextListener;
32 import java.io.File;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.HashMap;
36 import java.util.Map;
37 import java.util.jar.Attributes;
38 import java.util.jar.Manifest;
39
40 public class AppContextListener implements ServletContextListener {
41
42     private static Logger log = Logger.getLogger(AppContextListener.class.getName());
43
44     public void contextInitialized(ServletContextEvent context) {
45
46         log.debug("ServletContextListener initialized ");
47
48         log.debug("After read values from Manifest {}", getManifestInfo(context.getServletContext()));
49
50         Map<String, String> manifestAttr = getManifestInfo(context.getServletContext());
51
52         String appName = setAndGetAttributeInContext(context, manifestAttr, Constants.APPLICATION_NAME);
53         String appVersion = setAndGetAttributeInContext(context, manifestAttr, Constants.APPLICATION_VERSION);
54
55         ExternalConfiguration.setAppName(appName);
56         ExternalConfiguration.setAppVersion(appVersion);
57         String configHome = System.getProperty(Constants.CONFIG_HOME);
58         ExternalConfiguration.setConfigDir(configHome);
59
60         String appConfigDir = configHome + File.separator + appName;
61         // ChangeListener changeListener = new ChangeListener();
62         ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
63                 appConfigDir);
64
65         context.getServletContext().setAttribute(Constants.CONFIGURATION_SOURCE_ATTR, configurationSource);
66
67         ExternalConfiguration.setConfigurationSource(configurationSource);
68
69         ExternalConfiguration.listenForChanges();
70
71     }
72
73     public void contextDestroyed(ServletContextEvent context) {
74
75         log.debug("ServletContextListener destroyed");
76         ExternalConfiguration.stopListenForFileChanges();
77     }
78
79     private String setAndGetAttributeInContext(ServletContextEvent context, Map<String, String> manifestAttr,
80                                                String attr) {
81
82         String name = manifestAttr.get(attr);
83         if (name != null) {
84             context.getServletContext().setAttribute(attr, name);
85         }
86
87         return name;
88     }
89
90     public static Map<String, String> getManifestInfo(ServletContext application) {
91
92         Map<String, String> result = new HashMap<>();
93         InputStream inputStream = null;
94         try {
95
96             inputStream = application.getResourceAsStream("/META-INF/MANIFEST.MF");
97
98             Manifest manifest = new Manifest(inputStream);
99
100             Attributes attr = manifest.getMainAttributes();
101             String name = attr.getValue("Implementation-Title");
102             if (name != null) {
103                 result.put(Constants.APPLICATION_NAME, name);
104             }
105
106             String version = attr.getValue("Implementation-Version");
107             if (version != null) {
108                 result.put(Constants.APPLICATION_VERSION, version);
109             }
110
111         } catch (IOException e) {
112
113         } finally {
114             if (inputStream != null) {
115                 try {
116                     inputStream.close();
117                 } catch (IOException e) {
118                     log.info("close FileOutputStream failed - {}", e);
119                 }
120             }
121         }
122
123         return result;
124
125     }
126
127 }