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