remove (all) references to System::exit, use Minosoft::shutdown now

This commit is contained in:
Bixilon 2020-12-30 16:29:20 +01:00
parent 9109f7b108
commit 89e44e611a
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 85 additions and 30 deletions

View File

@ -57,16 +57,19 @@ public final class Minosoft {
public static final HashBiMap<Integer, Connection> CONNECTIONS = HashBiMap.create();
private static final CountUpAndDownLatch START_STATUS_LATCH = new CountUpAndDownLatch(1);
public static Configuration config;
private static boolean isExiting;
public static void main(String[] args) {
MinosoftCommandLineArguments.parseCommandLineArguments(args);
Runtime.getRuntime().addShutdownHook(new Thread(() -> shutdown(ShutdownReasons.UNKNOWN), "ShutdownHook"));
Log.info("Starting...");
AsyncTaskWorker taskWorker = new AsyncTaskWorker("StartUp");
taskWorker.setFatalError((exception) -> {
Log.fatal("Critical error occurred while preparing. Exit");
if (StaticConfiguration.HEADLESS_MODE) {
System.exit(1);
shutdown(exception.getMessage(), ShutdownReasons.CRITICAL_EXCEPTION);
return;
}
try {
@ -76,7 +79,7 @@ public final class Minosoft {
StartProgressWindow.TOOLKIT_LATCH.await();
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
shutdown(e.getMessage(), ShutdownReasons.CRITICAL_EXCEPTION);
}
// hide all other gui parts
StartProgressWindow.hideDialog();
@ -99,10 +102,10 @@ public final class Minosoft {
stage.setOnCloseRequest(dialogEvent -> {
dialog.setResult(Boolean.TRUE);
dialog.close();
System.exit(1);
shutdown(exception.getMessage(), ShutdownReasons.CRITICAL_EXCEPTION);
});
dialog.showAndWait();
System.exit(1);
shutdown(exception.getMessage(), ShutdownReasons.CRITICAL_EXCEPTION);
});
});
taskWorker.addTask(new Task(progress -> {
@ -241,6 +244,33 @@ public final class Minosoft {
}
}
public static void shutdown(String message, ShutdownReasons reason) {
if (isExiting) {
return;
}
if (message == null) {
message = "Unknown :(";
}
if (reason != ShutdownReasons.CLI_HELP && reason != ShutdownReasons.CLI_WRONG_PARAMETER) {
Log.info("Exiting (reason=%s): %s", reason, message);
// disconnect from all servers
for (Object connection : CONNECTIONS.values().toArray()) {
((Connection) connection).disconnect();
}
Log.info("Disconnected from all connections!");
if (Thread.currentThread().getName().equals("ShutdownHook")) {
return;
}
}
isExiting = true;
System.exit(reason.getExitCode());
}
public static void shutdown(ShutdownReasons reason) {
shutdown(null, reason);
}
public static CountUpAndDownLatch getStartStatusLatch() {
return START_STATUS_LATCH;
}

View File

@ -0,0 +1,22 @@
package de.bixilon.minosoft;
public enum ShutdownReasons {
UNKNOWN(1),
REQUESTED_BY_USER(0),
ALL_FINE(0),
CRITICAL_EXCEPTION(1),
NO_ACCOUNT_SELECTED(1),
CLI_WRONG_PARAMETER(1),
CLI_HELP(0),
LAUNCHER_FXML_LOAD_ERROR(1);
private final int exitCode;
ShutdownReasons(int exitCode) {
this.exitCode = exitCode;
}
public int getExitCode() {
return this.exitCode;
}
}

View File

@ -13,6 +13,8 @@
package de.bixilon.minosoft.data.locale;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.ShutdownReasons;
import de.bixilon.minosoft.data.mappings.versions.Versions;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.util.Util;
@ -57,8 +59,7 @@ public class LocaleManager {
fallbackLanguage = loadLanguage("en_US");
} catch (Exception e) {
e.printStackTrace();
Log.fatal("Could not load fallback language file (en_US). Exiting...");
System.exit(1);
Minosoft.shutdown("Could not load fallback language file (en_US). Exiting...", ShutdownReasons.CRITICAL_EXCEPTION);
}
}
try {

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.gui.main;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.ShutdownReasons;
import de.bixilon.minosoft.data.accounts.Account;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
@ -51,7 +52,7 @@ public class AccountWindow implements Initializable {
GUITools.showPane("/layout/dialogs/login_mojang.fxml", Modality.APPLICATION_MODAL, LocaleManager.translate(Strings.LOGIN_MOJANG_DIALOG_TITLE));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
Minosoft.shutdown(e.getMessage(), ShutdownReasons.LAUNCHER_FXML_LOAD_ERROR);
}
}
@ -60,7 +61,7 @@ public class AccountWindow implements Initializable {
GUITools.showPane("/layout/dialogs/login_offline.fxml", Modality.APPLICATION_MODAL, LocaleManager.translate(Strings.LOGIN_OFFLINE_DIALOG_TITLE));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
Minosoft.shutdown(e.getMessage(), ShutdownReasons.LAUNCHER_FXML_LOAD_ERROR);
}
}
}

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.gui.main;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.ShutdownReasons;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
import de.bixilon.minosoft.data.mappings.versions.Version;
@ -73,7 +74,7 @@ public class Launcher {
root = loader.load();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
Minosoft.shutdown(e.getMessage(), ShutdownReasons.LAUNCHER_FXML_LOAD_ERROR);
return;
}
@ -83,7 +84,7 @@ public class Launcher {
stage.setTitle(LocaleManager.translate(Strings.MAIN_WINDOW_TITLE));
GUITools.initializeScene(scene);
stage.setOnCloseRequest(windowEvent -> System.exit(0));
stage.setOnCloseRequest(windowEvent -> Minosoft.shutdown(ShutdownReasons.REQUESTED_BY_USER));
if (exit) {
return;
}

View File

@ -19,6 +19,7 @@ import com.jfoenix.controls.JFXDialogLayout;
import com.jfoenix.controls.JFXTextField;
import com.jfoenix.validation.RequiredFieldValidator;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.ShutdownReasons;
import de.bixilon.minosoft.data.accounts.Account;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
@ -83,7 +84,7 @@ public class MainWindow implements Initializable {
JFXButton cancel = new JFXButton(ButtonType.CANCEL.getText());
cancel.setOnAction((actionEvent -> alert.close()));
JFXButton close = new JFXButton(ButtonType.OK.getText());
close.setOnAction(actionEvent -> System.exit(0));
close.setOnAction(actionEvent -> Minosoft.shutdown(ShutdownReasons.NO_ACCOUNT_SELECTED));
layout.setActions(cancel, close);
alert.setContent(layout);
@ -257,7 +258,7 @@ public class MainWindow implements Initializable {
@FXML
public void quit() {
System.exit(0);
Minosoft.shutdown(ShutdownReasons.REQUESTED_BY_USER);
}
public void refreshServers() {

View File

@ -16,6 +16,8 @@ package de.bixilon.minosoft.gui.main;
import com.jfoenix.controls.JFXAlert;
import com.jfoenix.controls.JFXDialogLayout;
import com.jfoenix.controls.JFXProgressBar;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.ShutdownReasons;
import de.bixilon.minosoft.data.locale.LocaleManager;
import de.bixilon.minosoft.data.locale.Strings;
import de.bixilon.minosoft.logging.Log;
@ -67,7 +69,7 @@ public class StartProgressWindow extends Application {
Stage stage = (Stage) progressDialog.getDialogPane().getScene().getWindow();
stage.initModality(Modality.APPLICATION_MODAL);
stage.setOnCloseRequest((request) -> System.exit(0));
stage.setOnCloseRequest((request) -> Minosoft.shutdown(ShutdownReasons.REQUESTED_BY_USER));
if (exit) {
return;
}

View File

@ -14,6 +14,8 @@
package de.bixilon.minosoft.terminal;
import com.google.common.reflect.ClassPath;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.ShutdownReasons;
import de.bixilon.minosoft.data.commands.CommandRootNode;
import de.bixilon.minosoft.data.commands.CommandStringReader;
import de.bixilon.minosoft.data.commands.parser.exceptions.CommandParseException;
@ -91,7 +93,7 @@ public class CLI {
try {
line = reader.readLine().replaceAll("\\s{2,}", "");
} catch (UserInterruptException e) {
System.exit(0);
Minosoft.shutdown(e.getMessage(), ShutdownReasons.REQUESTED_BY_USER);
return;
}
terminal.flush();
@ -101,22 +103,15 @@ public class CLI {
ROOT_NODE.execute(currentConnection, new CommandStringReader(line), new CommandStack());
} catch (CLIException | CommandParseException exception) {
Command.printError("--> " + exception.getMessage());
if (exception instanceof UnknownCommandParseException) {
Command.printError("Type help for a command list!");
}
} catch (UserInterruptException exception) {
Minosoft.shutdown(exception.getMessage(), ShutdownReasons.REQUESTED_BY_USER);
} catch (Exception exception) {
if (exception instanceof CommandParseException) {
Command.printError("--> " + exception.getMessage());
if (exception instanceof UnknownCommandParseException) {
Command.printError("Type help for a command list!");
}
continue;
}
if (exception instanceof CLIException) {
Command.printError("--> " + exception.getMessage());
continue;
}
exception.printStackTrace();
if (exception instanceof UserInterruptException) {
System.exit(0);
}
}
}
} catch (IOException e) {

View File

@ -13,6 +13,8 @@
package de.bixilon.minosoft.util;
import de.bixilon.minosoft.Minosoft;
import de.bixilon.minosoft.ShutdownReasons;
import de.bixilon.minosoft.config.StaticConfiguration;
import org.apache.commons.cli.*;
@ -43,7 +45,7 @@ public class MinosoftCommandLineArguments {
} catch (ParseException e) {
System.out.println(e.getMessage());
HELP_FORMATTER.printHelp("java -jar Minosoft.jar", OPTIONS);
System.exit(1);
Minosoft.shutdown(e.getMessage(), ShutdownReasons.CLI_WRONG_PARAMETER);
}
}
@ -54,7 +56,7 @@ public class MinosoftCommandLineArguments {
private static void registerDefaultArguments() {
registerCommandLineOption(new Option("?", "help", false, "Displays this help"), (value -> {
HELP_FORMATTER.printHelp("java -jar Minosoft.jar", OPTIONS);
System.exit(1);
Minosoft.shutdown(ShutdownReasons.CLI_HELP);
}));
registerCommandLineOption(new Option("home_folder", true, "Home of Minosoft"), (value -> StaticConfiguration.HOME_DIRECTORY = value + "/"));
registerCommandLineOption(new Option("colored_log", true, "Should the log be colored"), (value -> StaticConfiguration.COLORED_LOG = Boolean.parseBoolean(value)));