[SDC-29] catalog 1707 rebase commit.
[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.portalsdk.core.onboarding.ueb.UebException;
33 import org.openecomp.portalsdk.core.onboarding.ueb.UebManager;
34 import org.openecomp.sdc.be.config.BeEcompErrorManager;
35 import org.openecomp.sdc.be.config.ConfigurationManager;
36 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
37 import org.openecomp.sdc.be.impl.DownloadArtifactLogic;
38 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
39 import org.openecomp.sdc.be.model.operations.api.IResourceOperation;
40 import org.openecomp.sdc.be.monitoring.BeMonitoringService;
41 import org.openecomp.sdc.common.api.Constants;
42 import org.openecomp.sdc.common.impl.ExternalConfiguration;
43 import org.openecomp.sdc.common.listener.AppContextListener;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.web.context.WebApplicationContext;
47
48 public class BEAppContextListener extends AppContextListener implements ServletContextListener {
49
50         private static final String MANIFEST_FILE_NAME = "/META-INF/MANIFEST.MF";
51         private static Logger log = LoggerFactory.getLogger(BEAppContextListener.class.getName());
52
53         private static UebManager uebManager = null;
54
55         public void contextInitialized(ServletContextEvent context) {
56
57                 super.contextInitialized(context);
58
59                 ConfigurationManager configurationManager = new ConfigurationManager(ExternalConfiguration.getConfigurationSource());
60                 log.debug("loading configuration from configDir: {} appName: {}", ExternalConfiguration.getConfigDir(), ExternalConfiguration.getAppName());
61
62                 context.getServletContext().setAttribute(Constants.CONFIGURATION_MANAGER_ATTR, configurationManager);
63
64                 WebAppContextWrapper webAppContextWrapper = new WebAppContextWrapper();
65                 context.getServletContext().setAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR, webAppContextWrapper);
66
67                 DownloadArtifactLogic downloadArtifactLogic = new DownloadArtifactLogic();
68                 context.getServletContext().setAttribute(Constants.DOWNLOAD_ARTIFACT_LOGIC_ATTR, downloadArtifactLogic);
69
70                 context.getServletContext().setAttribute(Constants.ASDC_RELEASE_VERSION_ATTR, getVersionFromManifest(context));
71
72                 // Monitoring service
73                 BeMonitoringService bms = new BeMonitoringService(context.getServletContext());
74                 bms.start(configurationManager.getConfiguration().getSystemMonitoring().getProbeIntervalInSeconds(15));
75
76                 initUebManager();
77
78                 log.debug("After executing {}", this.getClass());
79
80         }
81
82         private void initUebManager() {
83                 try {
84                         if (uebManager == null) {
85                                 uebManager = UebManager.getInstance();
86                                 uebManager.initListener(null);
87                         }
88                 } catch (UebException ex) {
89                         log.debug("Failed to initialize UebManager", ex);
90                         BeEcompErrorManager.getInstance().logInternalConnectionError("InitUebManager", "Failed to initialize listener of UebManager", ErrorSeverity.ERROR);
91                 }
92                 log.debug("After init listener of UebManager");
93         }
94
95         public void contextDestroyed(ServletContextEvent context) {
96                 if (uebManager != null) {
97                         uebManager.shutdown();
98                         uebManager = null;
99                 }
100                 super.contextDestroyed(context);
101
102         }
103
104         private IResourceOperation getResourceOperationManager(Class<? extends IResourceOperation> clazz, WebApplicationContext webContext) {
105
106                 return webContext.getBean(clazz);
107         }
108
109         private String getVersionFromManifest(ServletContextEvent context) {
110                 ServletContext servletContext = context.getServletContext();
111                 InputStream inputStream = servletContext.getResourceAsStream(MANIFEST_FILE_NAME);
112
113                 String version = null;
114                 try {
115                         Manifest mf = new Manifest(inputStream);
116                         Attributes atts = mf.getMainAttributes();
117                         version = atts.getValue(Constants.ASDC_RELEASE_VERSION_ATTR);
118                         if (version == null || version.isEmpty()) {
119                                 log.warn("failed to read ASDC version from MANIFEST.");
120                         } else {
121                                 log.info("ASDC version from MANIFEST is {}", version);
122                         }
123
124                 } catch (IOException e) {
125                         log.warn("failed to read ASDC version from MANIFEST", e.getMessage());
126                 }
127
128                 return version;
129         }
130
131 }