re base code
[sdc.git] / catalog-fe / src / main / java / org / openecomp / sdc / fe / listen / FEAppContextListener.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.fe.listen;
22
23 import org.openecomp.sdc.common.api.Constants;
24 import org.openecomp.sdc.common.impl.ExternalConfiguration;
25 import org.openecomp.sdc.common.listener.AppContextListener;
26 import org.openecomp.sdc.fe.config.ConfigurationManager;
27 import org.openecomp.sdc.fe.impl.PluginStatusBL;
28 import org.openecomp.sdc.fe.monitoring.FeMonitoringService;
29 import org.openecomp.sdc.fe.servlets.HealthCheckService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import javax.servlet.ServletContextEvent;
34 import javax.servlet.ServletContextListener;
35 import java.util.concurrent.ExecutorService;
36 import java.util.concurrent.Executors;
37
38 public class FEAppContextListener extends AppContextListener implements ServletContextListener {
39
40         private static Logger log = LoggerFactory.getLogger(FEAppContextListener.class.getName());
41
42         public void contextInitialized(ServletContextEvent context) {
43
44                 super.contextInitialized(context);
45
46                 ConfigurationManager configurationManager = new ConfigurationManager(
47                                 ExternalConfiguration.getConfigurationSource());
48                 log.debug("loading configuration from configDir:{} appName:{}", ExternalConfiguration.getConfigDir(),
49                                 ExternalConfiguration.getAppName());
50                 context.getServletContext().setAttribute(Constants.CONFIGURATION_MANAGER_ATTR, configurationManager);
51                 
52                 PluginStatusBL pbl = new PluginStatusBL();
53                 context.getServletContext().setAttribute(Constants.PLUGIN_BL_COMPONENT, pbl);
54
55                 // Health Check service
56                 HealthCheckService hcs = new HealthCheckService(context.getServletContext());
57                 hcs.start(configurationManager.getConfiguration().getHealthCheckIntervalInSeconds(5));
58                 context.getServletContext().setAttribute(Constants.HEALTH_CHECK_SERVICE_ATTR, hcs);
59
60                 // Monitoring service
61                 FeMonitoringService fms = new FeMonitoringService(context.getServletContext());
62                 fms.start(configurationManager.getConfiguration().getSystemMonitoring().getProbeIntervalInSeconds(15));
63
64                 if (configurationManager.getConfiguration() == null) {
65                         log.debug("ERROR: configuration was not properly loaded");
66                         return;
67                 }
68
69                 ExecutorService executorPool = Executors
70                                 .newFixedThreadPool(configurationManager.getConfiguration().getThreadpoolSize());
71                 context.getServletContext().setAttribute(Constants.THREAD_EXECUTOR_ATTR, executorPool);
72
73                 log.debug("After executing {}", this.getClass());
74         }
75
76         public void contextDestroyed(ServletContextEvent context) {
77
78                 ExecutorService executorPool = (ExecutorService) context.getServletContext()
79                                 .getAttribute(Constants.THREAD_EXECUTOR_ATTR);
80                 if (executorPool != null) {
81                         executorPool.shutdown();
82                 }
83
84                 super.contextDestroyed(context);
85
86         }
87
88 }