Skip to content

Commit

Permalink
APPNG-2476
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed Apr 26, 2023
1 parent 7a4c6a3 commit 23841cb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
30 changes: 16 additions & 14 deletions appng-cli/src/main/java/org/appng/cli/CliBootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,21 @@ public static int run(String[] args) throws IOException {
}
Properties cliConfig = getCliConfig(env, true, platformRootPath);

Server hsqlServer = HsqlStarter.startHsql(cliConfig, platformRootPath.getAbsolutePath());
boolean isHsql = null != hsqlServer;
boolean serverStarted = isHsql && ServerConstants.SERVER_STATE_ONLINE == hsqlServer.getState();
if (isHsql && !serverStarted) {
if (hsqlServer.getServerError().getClass().isAssignableFrom(BindException.class)) {
LOGGER.info("HSQL Server {} already running on port {}", hsqlServer.getProductVersion(),
hsqlServer.getPort());
} else {
LOGGER.error(
String.format("Failed to start HSQL Server %s on port %s",
hsqlServer.getProductVersion(), hsqlServer.getPort()),
hsqlServer.getServerError());
return CliCore.DATABASE_ERROR;
Server hsqlServer = null;
if (HsqlStarter.mustStartServer(cliConfig)) {
hsqlServer = HsqlStarter.startHsql(cliConfig, platformRootPath.getAbsolutePath());
boolean serverStarted = ServerConstants.SERVER_STATE_ONLINE == hsqlServer.getState();
if (!serverStarted) {
if (hsqlServer.getServerError().getClass().isAssignableFrom(BindException.class)) {
LOGGER.info("HSQL Server {} already running on port {}", hsqlServer.getProductVersion(),
hsqlServer.getPort());
} else {
LOGGER.error(
String.format("Failed to start HSQL Server %s on port %s",
hsqlServer.getProductVersion(), hsqlServer.getPort()),
hsqlServer.getServerError());
return CliCore.DATABASE_ERROR;
}
}
}

Expand All @@ -135,7 +137,7 @@ public static int run(String[] args) throws IOException {
cliWatch.stop();
LOGGER.info("duration: {}ms", cliWatch.getTotalTimeMillis());
context.close();
if (serverStarted) {
if (null != hsqlServer) {
HsqlStarter.shutdown(hsqlServer);
} else {
LOGGER.info("HSQL server was already running, shutdown not required.");
Expand Down
28 changes: 19 additions & 9 deletions appng-core/src/main/java/org/appng/core/service/HsqlStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.appng.core.domain.DatabaseConnection.DatabaseType;
import org.hsqldb.Server;

Expand All @@ -48,21 +49,30 @@ public class HsqlStarter {
public static final String CONTEXT = "hsqlContext";

/**
* Starts a HSQL {@link Server}, but only if {@link DatabaseType#HSQL} is the configured type.
* Checks if the database-type is hsql and if {@value #DATABASE_PORT} is set.
*/
public static boolean mustStartServer(Properties platformProperties) {
return DatabaseType.HSQL.name().equalsIgnoreCase(platformProperties.getProperty(DatabaseService.DATABASE_TYPE))
&& StringUtils.isNotBlank(platformProperties.getProperty(DATABASE_PORT));
}

/**
* Starts a HSQL {@link Server}, but only if {@link DatabaseType#HSQL} is the configured type and
* {@value #DATABASE_PORT} is set.
*
* @param platformProperties
* the properties read from
* {@value org.appng.core.controller.PlatformStartup#CONFIG_LOCATION}
* @param appngHome
* the home folder of appNG
* @param platformProperties
* the properties read from
* {@value org.appng.core.controller.PlatformStartup#CONFIG_LOCATION}
* @param appngHome
* the home folder of appNG
*
* @return a {@link Server}-instance, if {@link DatabaseType#HSQL} is the configured type.
* @return a {@link Server}-instance, if {@link DatabaseType#HSQL} is the configured type.
*
* @throws IOException
* in case the database folder or hsql logfiles could not be accessed
* in case the database folder or hsql logfiles could not be accessed
*/
public static Server startHsql(Properties platformProperties, String appngHome) throws IOException {
if (DatabaseType.HSQL.name().equalsIgnoreCase(platformProperties.getProperty(DatabaseService.DATABASE_TYPE))) {
if (mustStartServer(platformProperties)) {
File databaseRootPath = new File(appngHome, FOLDER_DATABASE);
FileUtils.forceMkdir(databaseRootPath);

Expand Down

0 comments on commit 23841cb

Please sign in to comment.