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