[SDC] sync
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / listen / BEAppContextListener.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.be.listen;
22
23 import org.openecomp.sdc.be.config.ConfigurationManager;
24 import org.openecomp.sdc.be.impl.DownloadArtifactLogic;
25 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
26 import org.openecomp.sdc.be.monitoring.BeMonitoringService;
27 import org.openecomp.sdc.common.api.Constants;
28 import org.openecomp.sdc.common.impl.ExternalConfiguration;
29 import org.openecomp.sdc.common.listener.AppContextListener;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.web.context.WebApplicationContext;
33
34 import javax.servlet.ServletContext;
35 import javax.servlet.ServletContextEvent;
36 import javax.servlet.ServletContextListener;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.util.jar.Attributes;
40 import java.util.jar.Manifest;
41
42 public class BEAppContextListener extends AppContextListener implements ServletContextListener {
43
44         private static final String MANIFEST_FILE_NAME = "/META-INF/MANIFEST.MF";
45         private static Logger log = LoggerFactory.getLogger(BEAppContextListener.class.getName());
46         
47         public void contextInitialized(ServletContextEvent context) {
48
49                 super.contextInitialized(context);
50
51                 ConfigurationManager configurationManager = new ConfigurationManager(ExternalConfiguration.getConfigurationSource());
52                 log.debug("loading configuration from configDir: {} appName: {}", ExternalConfiguration.getConfigDir(), ExternalConfiguration.getAppName());
53
54                 context.getServletContext().setAttribute(Constants.CONFIGURATION_MANAGER_ATTR, configurationManager);
55
56                 WebAppContextWrapper webAppContextWrapper = new WebAppContextWrapper();
57                 context.getServletContext().setAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR, webAppContextWrapper);
58
59                 DownloadArtifactLogic downloadArtifactLogic = new DownloadArtifactLogic();
60                 context.getServletContext().setAttribute(Constants.DOWNLOAD_ARTIFACT_LOGIC_ATTR, downloadArtifactLogic);
61
62                 context.getServletContext().setAttribute(Constants.ASDC_RELEASE_VERSION_ATTR, getVersionFromManifest(context));
63
64                 // Monitoring service
65                 BeMonitoringService bms = new BeMonitoringService(context.getServletContext());
66                 bms.start(configurationManager.getConfiguration().getSystemMonitoring().getProbeIntervalInSeconds(15));
67
68                 log.debug("After executing {}", this.getClass());
69
70         }
71         
72         private String getVersionFromManifest(ServletContextEvent context) {
73                 ServletContext servletContext = context.getServletContext();
74                 InputStream inputStream = servletContext.getResourceAsStream(MANIFEST_FILE_NAME);
75
76                 String version = null;
77                 try {
78                         Manifest mf = new Manifest(inputStream);
79                         Attributes atts = mf.getMainAttributes();
80                         version = atts.getValue(Constants.ASDC_RELEASE_VERSION_ATTR);
81                         if (version == null || version.isEmpty()) {
82                                 log.warn("failed to read ASDC version from MANIFEST.");
83                         } else {
84                                 log.info("ASDC version from MANIFEST is {}", version);
85                         }
86
87                 } catch (IOException e) {
88                         log.warn("failed to read ASDC version from MANIFEST", e.getMessage());
89                 }
90
91                 return version;
92         }
93
94 }