Initial OpenECOMP SDC commit
[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 import org.slf4j.MDC;
42
43 public class AppContextListener implements ServletContextListener {
44
45         private static Logger log = LoggerFactory.getLogger(AppContextListener.class.getName());
46
47         public void contextInitialized(ServletContextEvent context) {
48
49                 log.debug("ServletContextListener initialized ");
50
51                 log.debug("After read values from Manifest {}", getManifestInfo(context.getServletContext()));
52
53                 Map<String, String> manifestAttr = getManifestInfo(context.getServletContext());
54
55                 String appName = setAndGetAttributeInContext(context, manifestAttr, Constants.APPLICATION_NAME);
56                 String appVersion = setAndGetAttributeInContext(context, manifestAttr, Constants.APPLICATION_VERSION);
57
58                 ExternalConfiguration.setAppName(appName);
59                 ExternalConfiguration.setAppVersion(appVersion);
60                 String configHome = System.getProperty(Constants.CONFIG_HOME);
61                 ExternalConfiguration.setConfigDir(configHome);
62
63                 String appConfigDir = configHome + File.separator + appName;
64                 // ChangeListener changeListener = new ChangeListener();
65                 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), 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, String attr) {
82
83                 String name = manifestAttr.get(attr);
84                 if (name != null) {
85                         context.getServletContext().setAttribute(attr, name);
86                 }
87
88                 return name;
89         }
90
91         public static Map<String, String> getManifestInfo(ServletContext application) {
92
93                 Map<String, String> result = new HashMap<String, String>();
94                 InputStream inputStream = null;
95                 try {
96
97                         inputStream = application.getResourceAsStream("/META-INF/MANIFEST.MF");
98
99                         Manifest manifest = new Manifest(inputStream);
100
101                         Attributes attr = manifest.getMainAttributes();
102                         String name = attr.getValue("Implementation-Title");
103                         if (name != null) {
104                                 result.put(Constants.APPLICATION_NAME, name);
105                         }
106
107                         String version = attr.getValue("Implementation-Version");
108                         if (version != null) {
109                                 result.put(Constants.APPLICATION_VERSION, version);
110                         }
111
112                 } catch (IOException e) {
113
114                 } finally {
115                         if (inputStream != null) {
116                                 try {
117                                         inputStream.close();
118                                 } catch (IOException e) {
119                                         // TODO Auto-generated catch block
120                                         e.printStackTrace();
121                                 }
122                         }
123                 }
124
125                 return result;
126
127         }
128
129 }