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