2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (c) 2019 Samsung
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.fe.listen;
24 import java.security.KeyStoreException;
25 import java.security.NoSuchAlgorithmException;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
29 import javax.net.ssl.SSLContext;
30 import javax.net.ssl.SSLException;
31 import javax.servlet.ServletContextEvent;
32 import javax.servlet.ServletContextListener;
34 import org.apache.http.config.Registry;
35 import org.apache.http.config.RegistryBuilder;
36 import org.apache.http.conn.socket.ConnectionSocketFactory;
37 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
38 import org.apache.http.conn.ssl.NoopHostnameVerifier;
39 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
40 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
41 import org.apache.http.impl.client.CloseableHttpClient;
42 import org.apache.http.impl.client.HttpClients;
43 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
44 import org.apache.http.ssl.SSLContextBuilder;
45 import org.openecomp.sdc.common.api.Constants;
46 import org.openecomp.sdc.common.impl.ExternalConfiguration;
47 import org.openecomp.sdc.common.listener.AppContextListener;
48 import org.openecomp.sdc.fe.config.ConfigurationManager;
49 import org.openecomp.sdc.fe.impl.PluginStatusBL;
50 import org.openecomp.sdc.fe.monitoring.FeMonitoringService;
51 import org.openecomp.sdc.fe.servlets.HealthCheckService;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 public class FEAppContextListener extends AppContextListener implements ServletContextListener {
57 private static final int HEALTH_CHECK_INTERVAL = 5;
58 private static final int PROBE_INTERVAL = 15;
59 private static Logger log = LoggerFactory.getLogger(FEAppContextListener.class.getName());
61 public void contextInitialized(ServletContextEvent context) {
63 super.contextInitialized(context);
65 ConfigurationManager configurationManager = new ConfigurationManager(
66 ExternalConfiguration.getConfigurationSource());
67 log.debug("loading configuration from configDir:{} appName:{}", ExternalConfiguration.getConfigDir(),
68 ExternalConfiguration.getAppName());
69 context.getServletContext().setAttribute(Constants.CONFIGURATION_MANAGER_ATTR, configurationManager);
72 PluginStatusBL pbl = new PluginStatusBL(buildRestClient());
73 context.getServletContext().setAttribute(Constants.PLUGIN_BL_COMPONENT, pbl);
74 } catch (SSLException e) {
75 log.debug("ERROR: Build rest client failed because ", e);
79 // Health Check service
80 HealthCheckService hcs = new HealthCheckService(context.getServletContext());
81 hcs.start(configurationManager.getConfiguration().getHealthCheckIntervalInSeconds(HEALTH_CHECK_INTERVAL));
82 context.getServletContext().setAttribute(Constants.HEALTH_CHECK_SERVICE_ATTR, hcs);
85 FeMonitoringService fms = new FeMonitoringService(context.getServletContext());
86 fms.start(configurationManager.getConfiguration().getSystemMonitoring().getProbeIntervalInSeconds(PROBE_INTERVAL));
88 if (configurationManager.getConfiguration() == null) {
89 log.debug("ERROR: configuration was not properly loaded");
93 ExecutorService executorPool = Executors
94 .newFixedThreadPool(configurationManager.getConfiguration().getThreadpoolSize());
95 context.getServletContext().setAttribute(Constants.THREAD_EXECUTOR_ATTR, executorPool);
97 log.debug("After executing {}", this.getClass());
100 public void contextDestroyed(ServletContextEvent context) {
102 ExecutorService executorPool = (ExecutorService) context.getServletContext()
103 .getAttribute(Constants.THREAD_EXECUTOR_ATTR);
104 if (executorPool != null) {
105 executorPool.shutdown();
108 super.contextDestroyed(context);
112 private CloseableHttpClient buildRestClient() throws SSLException {
113 SSLContextBuilder builder = new SSLContextBuilder();
115 builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
116 SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
117 SSLContext.getDefault(), NoopHostnameVerifier.INSTANCE);
118 Registry<ConnectionSocketFactory> registry =
119 RegistryBuilder.<ConnectionSocketFactory>create()
120 .register("http", new PlainConnectionSocketFactory()).register("https", sslsf)
122 PoolingHttpClientConnectionManager cm =
123 new PoolingHttpClientConnectionManager(registry);
124 return HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(cm).build();
125 } catch (NoSuchAlgorithmException | KeyStoreException e) {
126 throw new SSLException(e);