2020-03-02 14:01:12 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package ch.ethz.seb.sebserver.webservice;
|
|
|
|
|
|
|
|
import javax.annotation.PreDestroy;
|
|
|
|
|
|
|
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
|
|
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
2021-12-07 10:41:54 +01:00
|
|
|
import org.springframework.context.ApplicationContext;
|
2020-03-02 14:01:12 +01:00
|
|
|
import org.springframework.context.ApplicationEventPublisher;
|
|
|
|
import org.springframework.context.ApplicationListener;
|
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import ch.ethz.seb.sebserver.SEBServerInit;
|
|
|
|
import ch.ethz.seb.sebserver.SEBServerInitEvent;
|
|
|
|
import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile;
|
2021-01-27 09:17:45 +01:00
|
|
|
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.WebserviceInfoDAO;
|
2023-10-31 09:24:07 +01:00
|
|
|
import ch.ethz.seb.sebserver.webservice.servicelayer.session.impl.SEBClientPingServiceFactory;
|
2020-03-02 14:01:12 +01:00
|
|
|
|
|
|
|
@Component
|
|
|
|
@WebServiceProfile
|
|
|
|
@Import(DataSourceAutoConfiguration.class)
|
|
|
|
public class WebserviceInit implements ApplicationListener<ApplicationReadyEvent> {
|
|
|
|
|
2021-12-07 10:41:54 +01:00
|
|
|
private final ApplicationContext applicationContext;
|
2020-03-02 14:01:12 +01:00
|
|
|
private final SEBServerInit sebServerInit;
|
|
|
|
private final Environment environment;
|
|
|
|
private final WebserviceInfo webserviceInfo;
|
|
|
|
private final AdminUserInitializer adminUserInitializer;
|
|
|
|
private final ApplicationEventPublisher applicationEventPublisher;
|
2021-01-27 09:17:45 +01:00
|
|
|
private final WebserviceInfoDAO webserviceInfoDAO;
|
2021-06-16 13:27:58 +02:00
|
|
|
private final DBIntegrityChecker dbIntegrityChecker;
|
2022-02-01 10:56:53 +01:00
|
|
|
private final SEBServerMigrationStrategy sebServerMigrationStrategy;
|
2023-10-31 09:24:07 +01:00
|
|
|
private final SEBClientPingServiceFactory sebClientPingServiceFactory;
|
2020-03-02 14:01:12 +01:00
|
|
|
|
|
|
|
protected WebserviceInit(
|
|
|
|
final SEBServerInit sebServerInit,
|
|
|
|
final WebserviceInfo webserviceInfo,
|
|
|
|
final AdminUserInitializer adminUserInitializer,
|
2021-01-27 09:17:45 +01:00
|
|
|
final ApplicationEventPublisher applicationEventPublisher,
|
2021-06-16 13:27:58 +02:00
|
|
|
final WebserviceInfoDAO webserviceInfoDAO,
|
2021-12-07 10:41:54 +01:00
|
|
|
final DBIntegrityChecker dbIntegrityChecker,
|
2022-02-01 10:56:53 +01:00
|
|
|
final ApplicationContext applicationContext,
|
2023-10-31 09:24:07 +01:00
|
|
|
final SEBServerMigrationStrategy sebServerMigrationStrategy,
|
|
|
|
final SEBClientPingServiceFactory sebClientPingServiceFactory) {
|
2020-03-02 14:01:12 +01:00
|
|
|
|
2021-12-07 10:41:54 +01:00
|
|
|
this.applicationContext = applicationContext;
|
2020-03-02 14:01:12 +01:00
|
|
|
this.sebServerInit = sebServerInit;
|
2021-12-07 10:41:54 +01:00
|
|
|
this.environment = applicationContext.getEnvironment();
|
2020-03-02 14:01:12 +01:00
|
|
|
this.webserviceInfo = webserviceInfo;
|
|
|
|
this.adminUserInitializer = adminUserInitializer;
|
|
|
|
this.applicationEventPublisher = applicationEventPublisher;
|
2021-01-27 09:17:45 +01:00
|
|
|
this.webserviceInfoDAO = webserviceInfoDAO;
|
2021-06-16 13:27:58 +02:00
|
|
|
this.dbIntegrityChecker = dbIntegrityChecker;
|
2022-02-01 10:56:53 +01:00
|
|
|
this.sebServerMigrationStrategy = sebServerMigrationStrategy;
|
2023-10-31 09:24:07 +01:00
|
|
|
this.sebClientPingServiceFactory = sebClientPingServiceFactory;
|
2021-12-07 10:41:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public ApplicationContext getApplicationContext() {
|
|
|
|
return this.applicationContext;
|
2020-03-02 14:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onApplicationEvent(final ApplicationReadyEvent event) {
|
|
|
|
|
|
|
|
this.sebServerInit.init();
|
|
|
|
|
2021-12-07 10:41:54 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> *********************************************************");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> *** Webservice starting up... ***");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> *********************************************************");
|
2021-01-27 09:17:45 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> ");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Register Webservice: {}", this.webserviceInfo.getWebserviceUUID());
|
|
|
|
|
2022-02-07 16:36:50 +01:00
|
|
|
if (this.webserviceInfoDAO.isInitialized()) {
|
|
|
|
this.registerWebservice();
|
2021-01-27 09:17:45 +01:00
|
|
|
|
2022-02-07 16:36:50 +01:00
|
|
|
// Apply migration if needed and possible
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> ");
|
|
|
|
this.sebServerMigrationStrategy.applyMigration();
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> ");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Apply migration if needed and possible
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> ");
|
|
|
|
this.sebServerMigrationStrategy.applyMigration();
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> ");
|
|
|
|
|
|
|
|
this.registerWebservice();
|
|
|
|
}
|
2022-02-01 10:56:53 +01:00
|
|
|
|
2020-03-02 14:01:12 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> ");
|
2020-03-04 15:20:31 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Initialize Services...");
|
2020-03-02 14:01:12 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> ");
|
|
|
|
|
|
|
|
this.applicationEventPublisher.publishEvent(new SEBServerInitEvent(this));
|
|
|
|
|
2021-12-07 10:41:54 +01:00
|
|
|
// Run the data base integrity checks and fixes if configured
|
|
|
|
this.dbIntegrityChecker.checkIntegrity();
|
|
|
|
|
|
|
|
// Create an initial admin account if requested and not already in the data-base
|
|
|
|
this.adminUserInitializer.initAdminAccount();
|
|
|
|
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> *********************************************************");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> *** Webservice Info: ***");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> *********************************************************");
|
2020-03-02 14:01:12 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("---->");
|
|
|
|
|
2021-11-18 08:21:03 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> JDBC connection pool max size: {}",
|
|
|
|
this.environment.getProperty("spring.datasource.hikari.maximumPoolSize"));
|
|
|
|
|
2021-07-12 11:27:52 +02:00
|
|
|
if (this.webserviceInfo.isDistributed()) {
|
2021-12-07 10:41:54 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> ");
|
2021-07-12 11:27:52 +02:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Distributed Setup: {}", this.webserviceInfo.getWebserviceUUID());
|
2021-12-07 10:41:54 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Connection update time: {}",
|
2021-12-01 11:03:03 +01:00
|
|
|
this.environment.getProperty("sebserver.webservice.distributed.connectionUpdate", "2000"));
|
2021-07-12 11:27:52 +02:00
|
|
|
}
|
|
|
|
|
2023-10-31 09:24:07 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> ");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Working with ping service: {}",
|
|
|
|
this.sebClientPingServiceFactory.getWorkingServiceType());
|
|
|
|
|
2022-06-01 15:39:12 +02:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> ");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Server address: {}", this.environment.getProperty("server.address"));
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Server port: {}", this.environment.getProperty("server.port"));
|
|
|
|
SEBServerInit.INIT_LOGGER.info("---->");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Local-Host address: {}", this.webserviceInfo.getLocalHostAddress());
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Local-Host name: {}", this.webserviceInfo.getLocalHostName());
|
|
|
|
SEBServerInit.INIT_LOGGER.info("---->");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Remote-Host address: {}", this.webserviceInfo.getLoopbackHostAddress());
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Remote-Host name: {}", this.webserviceInfo.getLoopbackHostName());
|
2020-03-02 14:01:12 +01:00
|
|
|
|
|
|
|
SEBServerInit.INIT_LOGGER.info("---->");
|
2021-04-30 15:59:15 +02:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Context Path: {}", this.webserviceInfo.getContextPath());
|
2020-03-02 14:01:12 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> External-Host URL: {}", this.webserviceInfo.getExternalServerURL());
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> LMS-External-Address-Alias: {}",
|
|
|
|
this.webserviceInfo.getLmsExternalAddressAlias());
|
|
|
|
SEBServerInit.INIT_LOGGER.info("---->");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> HTTP Scheme {}", this.webserviceInfo.getHttpScheme());
|
|
|
|
SEBServerInit.INIT_LOGGER.info("---->");
|
2022-06-21 10:12:07 +02:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Access-Tokens:");
|
|
|
|
SEBServerInit.INIT_LOGGER.info(
|
|
|
|
"----> admin API access token validity: " + this.webserviceInfo.getAdminAccessTokenValSec() + "s");
|
|
|
|
SEBServerInit.INIT_LOGGER.info(
|
|
|
|
"----> admin API refresh token validity: " + this.webserviceInfo.getAdminRefreshTokenValSec() + "s");
|
|
|
|
SEBServerInit.INIT_LOGGER.info(
|
|
|
|
"----> exam API access token validity: " + this.webserviceInfo.getExamAPITokenValiditySeconds() + "s");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> ");
|
2020-03-02 14:01:12 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Property Override Test: {}", this.webserviceInfo.getTestProperty());
|
|
|
|
|
2021-12-07 10:41:54 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("---->");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> *********************************************************");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> *** Webservice successfully started up! ***");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> *********************************************************");
|
2020-03-02 14:01:12 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 16:36:50 +01:00
|
|
|
private boolean registerWebservice() {
|
|
|
|
boolean registered = false;
|
|
|
|
try {
|
|
|
|
final String webserviceUUID = this.webserviceInfo.getWebserviceUUID();
|
2022-06-01 15:39:12 +02:00
|
|
|
final String hostAddress = this.webserviceInfo.getLocalHostAddress();
|
2022-02-07 16:36:50 +01:00
|
|
|
registered = this.webserviceInfoDAO.register(webserviceUUID, hostAddress);
|
|
|
|
if (registered) {
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Successfully register Webservice instance. uuid: {}, address: {}",
|
|
|
|
webserviceUUID, hostAddress);
|
|
|
|
}
|
|
|
|
} catch (final Exception e) {
|
|
|
|
SEBServerInit.INIT_LOGGER.error("----> Failed to register webservice: ", e);
|
|
|
|
}
|
|
|
|
return registered;
|
|
|
|
}
|
|
|
|
|
2020-03-02 14:01:12 +01:00
|
|
|
@PreDestroy
|
|
|
|
public void gracefulShutdown() {
|
2021-12-07 10:41:54 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("*********************************************************");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("**** Gracefully Shutdown of SEB Server instance {}",
|
2020-03-02 14:01:12 +01:00
|
|
|
this.webserviceInfo.getHostAddress());
|
2021-01-27 09:17:45 +01:00
|
|
|
SEBServerInit.INIT_LOGGER.info("---->");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Unregister Webservice: {}", this.webserviceInfo.getWebserviceUUID());
|
2021-07-12 13:40:12 +02:00
|
|
|
|
2021-01-27 09:17:45 +01:00
|
|
|
this.webserviceInfoDAO.unregister(this.webserviceInfo.getWebserviceUUID());
|
|
|
|
|
|
|
|
SEBServerInit.INIT_LOGGER.info("---->");
|
|
|
|
SEBServerInit.INIT_LOGGER.info("----> Webservice down");
|
2020-03-02 14:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|