Cleanup EntryPoint code

- Don't return an int from listen(). An enum is preferred.
- Make parseLine() static, and pass Parameters to it.

Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
This commit is contained in:
solonovamax 2022-10-27 20:12:39 -04:00 committed by TheKodeToad
parent 107fa6b4f7
commit 99d9868116

View File

@ -52,6 +52,7 @@
package org.prismlauncher;
import org.prismlauncher.exception.ParseException;
import org.prismlauncher.launcher.Launcher;
import org.prismlauncher.launcher.LauncherFactory;
@ -64,8 +65,8 @@ import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class EntryPoint {
public final class EntryPoint {
private static final Logger LOGGER = Logger.getLogger("EntryPoint");
private final Parameters params = new Parameters();
@ -73,16 +74,16 @@ public final class EntryPoint {
public static void main(String[] args) {
EntryPoint listener = new EntryPoint();
int retCode = listener.listen();
ExitCode exitCode = listener.listen();
if (retCode != 0) {
LOGGER.info("Exiting with " + retCode);
if (exitCode != ExitCode.NORMAL) {
LOGGER.warning("Exiting with " + exitCode);
System.exit(retCode);
System.exit(exitCode.numericalCode);
}
}
private Action parseLine(String inData) throws ParseException {
private static PreLaunchAction parseLine(String inData, Parameters params) throws ParseException {
if (inData.isEmpty())
throw new ParseException("Unexpected empty string!");
@ -97,21 +98,21 @@ public final class EntryPoint {
switch (first) {
case "launch":
return Action.LAUNCH;
return PreLaunchAction.LAUNCH;
case "abort":
return Action.ABORT;
return PreLaunchAction.ABORT;
default:
if (second == null || second.isEmpty())
throw new ParseException("Error while parsing:" + inData);
params.add(first, second);
return Action.PROCEED;
return PreLaunchAction.PROCEED;
}
}
public int listen() {
Action action = Action.PROCEED;
public ExitCode listen() {
PreLaunchAction preLaunchAction = PreLaunchAction.PROCEED;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in,
@ -119,24 +120,24 @@ public final class EntryPoint {
))) {
String line;
while (action == Action.PROCEED) {
while (preLaunchAction == PreLaunchAction.PROCEED) {
if ((line = reader.readLine()) != null) {
action = parseLine(line);
preLaunchAction = parseLine(line, this.params);
} else {
action = Action.ABORT;
preLaunchAction = PreLaunchAction.ABORT;
}
}
} catch (IOException | ParseException e) {
LOGGER.log(Level.SEVERE, "Launcher abort due to exception:", e);
return 1;
return ExitCode.ERROR;
}
// Main loop
if (action == Action.ABORT) {
if (preLaunchAction == PreLaunchAction.ABORT) {
LOGGER.info("Launch aborted by the launcher.");
return 1;
return ExitCode.ERROR;
}
try {
@ -144,22 +145,34 @@ public final class EntryPoint {
launcher.launch();
return 0;
return ExitCode.NORMAL;
} catch (IllegalArgumentException e) {
LOGGER.log(Level.SEVERE, "Wrong argument.", e);
return 1;
return ExitCode.ERROR;
} catch (Throwable e) {
LOGGER.log(Level.SEVERE, "Exception caught from launcher.", e);
return 1;
return ExitCode.ERROR;
}
}
private enum Action {
private enum PreLaunchAction {
PROCEED,
LAUNCH,
ABORT
}
private enum ExitCode {
NORMAL(0),
ERROR(1);
private final int numericalCode;
ExitCode(int numericalCode) {
this.numericalCode = numericalCode;
}
}
}