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