Package: RestApiListener
RestApiListener
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
RestApiListener() |
|
|
|
|
|
||||||||||||||||||||
contextDestroyed(ServletContextEvent) |
|
|
|
|
|
||||||||||||||||||||
contextInitialized(ServletContextEvent) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.app.api.web;
14:
15: import com.google.common.base.MoreObjects;
16: import org.eclipse.kapua.KapuaException;
17: import org.eclipse.kapua.commons.core.ServiceModuleBundle;
18: import org.eclipse.kapua.commons.jpa.JdbcConnectionUrlResolvers;
19: import org.eclipse.kapua.commons.liquibase.KapuaLiquibaseClient;
20: import org.eclipse.kapua.commons.setting.system.SystemSetting;
21: import org.eclipse.kapua.commons.setting.system.SystemSettingKey;
22: import org.slf4j.Logger;
23: import org.slf4j.LoggerFactory;
24:
25: import javax.servlet.ServletContextEvent;
26: import javax.servlet.ServletContextListener;
27:
28: /**
29: * @since 1.0.0
30: */
31: public class RestApiListener implements ServletContextListener {
32:
33: private static final Logger LOG = LoggerFactory.getLogger(RestApiListener.class);
34:
35: private static final SystemSetting SYSTEM_SETTING = SystemSetting.getInstance();
36:
37: private ServiceModuleBundle moduleBundle;
38:
39: @Override
40: public void contextInitialized(final ServletContextEvent event) {
41:
42:• if (SYSTEM_SETTING.getBoolean(SystemSettingKey.DB_SCHEMA_UPDATE, false)) {
43: try {
44: String dbUsername = SYSTEM_SETTING.getString(SystemSettingKey.DB_USERNAME);
45: String dbPassword = SYSTEM_SETTING.getString(SystemSettingKey.DB_PASSWORD);
46: String schema = MoreObjects.firstNonNull(
47: SYSTEM_SETTING.getString(SystemSettingKey.DB_SCHEMA_ENV),
48: SYSTEM_SETTING.getString(SystemSettingKey.DB_SCHEMA)
49: );
50:
51: // Loading JDBC Driver
52: String jdbcDriver = SYSTEM_SETTING.getString(SystemSettingKey.DB_JDBC_DRIVER);
53: try {
54: Class.forName(jdbcDriver);
55: } catch (ClassNotFoundException e) {
56: LOG.warn("Could not find jdbc driver: {}. Subsequent DB operation failures may occur...", SYSTEM_SETTING.getString(SystemSettingKey.DB_JDBC_DRIVER));
57: }
58:
59: // Starting Liquibase Client
60: new KapuaLiquibaseClient(JdbcConnectionUrlResolvers.resolveJdbcUrl(), dbUsername, dbPassword, schema).update();
61: } catch (Exception e) {
62: throw new ExceptionInInitializerError(e);
63: }
64: }
65:
66: // Start service modules
67: try {
68: LOG.info("Starting service modules...");
69:• if (moduleBundle == null) {
70: moduleBundle = new ServiceModuleBundle();
71: }
72: moduleBundle.startup();
73: LOG.info("Starting service modules... DONE!");
74: } catch (KapuaException e) {
75: LOG.error("Starting service modules... ERROR! Error: {}", e.getMessage(), e);
76: }
77: }
78:
79: @Override
80: public void contextDestroyed(final ServletContextEvent event) {
81: // Stop event modules
82: try {
83: LOG.info("Stopping service modules...");
84:• if (moduleBundle != null) {
85: moduleBundle.shutdown();
86: moduleBundle = null;
87: }
88: LOG.info("Stopping service modules... DONE!");
89: } catch (KapuaException e) {
90: LOG.error("Stopping service modules... ERROR! Error: {}", e.getMessage(), e);
91: }
92: }
93:
94: }