Skip to content

Commit

Permalink
Improved: Avoid confusing indirections in ‘StartupControlPanel#start’
Browse files Browse the repository at this point in the history
(OFBIZ-11137)

The ‘StartupControlPanel#start’ method is delegating its job to
smaller private methods to improve readability.  However the
conditionnals were previously hidden inside those methods with the
unfortunate consequence of making case analysis hard.

To avoid this undesirable effect, the conditionals has been moved
inside the ‘StartupControlPanel#start’ method and some delegate
methods has been inlined when their implementation was trivial.  The
parameters of the remaining delegate methods has be refined to avoid
passing around unneeded things.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1862665 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Jul 6, 2019
1 parent 6925f79 commit 1e73b05
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ private void processClientRequest(Socket client, ContainerLoader loader, AtomicR
// if the client request is shutdown, execute shutdown sequence
if(clientCommand.equals(OfbizSocketCommand.SHUTDOWN)) {
writer.flush();
StartupControlPanel.stop(loader, serverState, this);
StartupControlPanel.shutdownServer(loader, serverState, this);
System.exit(0);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,29 @@ static void start(Config config,
ContainerLoader loader = new ContainerLoader();
Thread adminServer = createAdminServer(config, serverState, loader);

createLogDirectoryIfMissing(config);
createRuntimeShutdownHook(config, loader, serverState);
loadContainers(config, loader, ofbizCommands, serverState);
printStartupMessage(config);
executeShutdownAfterLoadIfConfigured(config, loader, serverState, adminServer);
}
createLogDirectoryIfMissing(config.logDir);

/**
* Print OFBiz startup message only if the OFBiz server is not scheduled for shutdown.
* @param config contains parameters for system startup
*/
private static void printStartupMessage(Config config) {
if (!config.shutdownAfterLoad) {
String lineSeparator = System.lineSeparator();
System.out.println(lineSeparator + " ____ __________ _" +
lineSeparator + " / __ \\/ ____/ __ )(_)___" +
lineSeparator + " / / / / /_ / __ / /_ /" +
lineSeparator + "/ /_/ / __/ / /_/ / / / /_" +
lineSeparator + "\\____/_/ /_____/_/ /___/ is started and ready." +
lineSeparator);
if (config.useShutdownHook) {
createRuntimeShutdownHook(loader, serverState);
} else {
System.out.println("Shutdown hook disabled");
}
}

/**
* Shutdown the OFBiz server. This method is invoked in one of the
* following ways:
*
* - Manually if requested by the client AdminClient
* - Automatically if Config.shutdownAfterLoad is set to true
*/
static void stop(ContainerLoader loader, AtomicReference<ServerState> serverState, Thread adminServer) {
shutdownServer(loader, serverState, adminServer);
System.exit(0);
loadContainers(config, loader, ofbizCommands, serverState);

if (config.shutdownAfterLoad) {
shutdownServer(loader, serverState, adminServer);
System.exit(0);
} else {
// Print startup message.
String ls = System.lineSeparator();
System.out.println(ls + " ____ __________ _" +
ls + " / __ \\/ ____/ __ )(_)___" +
ls + " / / / / /_ / __ / /_ /" +
ls + "/ /_/ / __/ / /_/ / / / /_" +
ls + "\\____/_/ /_____/_/ /___/ is started and ready." +
ls);
}
}

/**
Expand All @@ -120,7 +110,7 @@ static void fullyTerminateSystem(StartupException e) {
System.exit(1);
}

private static void shutdownServer(ContainerLoader loader, AtomicReference<ServerState> serverState, Thread adminServer) {
static void shutdownServer(ContainerLoader loader, AtomicReference<ServerState> serverState, Thread adminServer) {
ServerState currentState;
do {
currentState = serverState.get();
Expand Down Expand Up @@ -164,30 +154,22 @@ private static Thread createAdminServer(
return adminServer;
}

private static void createLogDirectoryIfMissing(Config config) {
File logDir = new File(config.logDir);
private static void createLogDirectoryIfMissing(String logDirName) {
File logDir = new File(logDirName);
if (!logDir.exists()) {
if (logDir.mkdir()) {
System.out.println("Created OFBiz log dir [" + logDir.getAbsolutePath() + "]");
}
}
}

private static void createRuntimeShutdownHook(
Config config,
ContainerLoader loader,
AtomicReference<ServerState> serverState) {

if (config.useShutdownHook) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
shutdownServer(loader, serverState, this);
}
});
} else {
System.out.println("Shutdown hook disabled");
}
private static void createRuntimeShutdownHook(ContainerLoader loader, AtomicReference<ServerState> serverState) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
shutdownServer(loader, serverState, this);
}
});
}

private static void loadContainers(Config config,
Expand All @@ -202,15 +184,4 @@ private static void loadContainers(Config config,
}
serverState.compareAndSet(ServerState.STARTING, ServerState.RUNNING);
}

private static void executeShutdownAfterLoadIfConfigured(
Config config,
ContainerLoader loader,
AtomicReference<ServerState> serverState,
Thread adminServer) {

if (config.shutdownAfterLoad) {
stop(loader, serverState, adminServer);
}
}
}

0 comments on commit 1e73b05

Please sign in to comment.