From 83fd710e5ca2d3e2da896d89e3518d3666decb43 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Thu, 13 Sep 2018 13:50:39 +0200 Subject: [PATCH] Added a recording mechanism for all commands executed. --- .../ui/control/command/CommandExecutor.java | 32 ++++++++++++++--- .../executables/ExecutableContext.java | 34 +++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/main/java/nl/andrewlalis/ui/control/command/executables/ExecutableContext.java diff --git a/src/main/java/nl/andrewlalis/ui/control/command/CommandExecutor.java b/src/main/java/nl/andrewlalis/ui/control/command/CommandExecutor.java index d66bb01..4d3eaa0 100644 --- a/src/main/java/nl/andrewlalis/ui/control/command/CommandExecutor.java +++ b/src/main/java/nl/andrewlalis/ui/control/command/CommandExecutor.java @@ -1,8 +1,8 @@ package nl.andrewlalis.ui.control.command; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; +import nl.andrewlalis.ui.control.command.executables.ExecutableContext; + +import java.util.*; import java.util.logging.Logger; /** @@ -23,8 +23,20 @@ public class CommandExecutor { */ private Map commands; + /** + * A list of all the executables which have failed to execute. + */ + private List failedExecutables; + + /** + * A list of all executables which have been run successfully. + */ + private List successfulExecutables; + public CommandExecutor() { this.commands = new HashMap<>(); + this.failedExecutables = new ArrayList<>(); + this.successfulExecutables = new ArrayList<>(); } /** @@ -64,12 +76,24 @@ public class CommandExecutor { public void executeCommand(String commandName, String[] args) { if (this.commands.containsKey(commandName)) { logger.info("Command executed: " + commandName + ' ' + Arrays.toString(args)); - if (!this.commands.get(commandName).execute(args)) { + Executable executable = this.commands.get(commandName); + ExecutableContext context = new ExecutableContext(executable, args); + if (!executable.execute(args)) { logger.warning("Command did not execute successfully."); + this.failedExecutables.add(context); + } else { + this.successfulExecutables.add(context); } } else { logger.warning(commandName + " is not a valid command."); } } + /** + * Retries all failed executables, and if successful, removes them from the queue. + */ + public void rerunFailedExecutables() { + this.failedExecutables.removeIf(ExecutableContext::runAgain); + } + } diff --git a/src/main/java/nl/andrewlalis/ui/control/command/executables/ExecutableContext.java b/src/main/java/nl/andrewlalis/ui/control/command/executables/ExecutableContext.java new file mode 100644 index 0000000..fc69f2c --- /dev/null +++ b/src/main/java/nl/andrewlalis/ui/control/command/executables/ExecutableContext.java @@ -0,0 +1,34 @@ +package nl.andrewlalis.ui.control.command.executables; + +import nl.andrewlalis.ui.control.command.Executable; + +/** + * An object to record a specific executable's execution context (args given), with the ability to re-run the executable + * if a failure occurs. + */ +public class ExecutableContext { + + /** + * The executable object, without any contextual information. + */ + private Executable executable; + + /** + * A list of arguments given to the executable when it was called. + */ + private String[] args; + + public ExecutableContext(Executable executable, String[] args) { + this.executable = executable; + this.args = args; + } + + /** + * Runs the stored executable again with the same arguments it was originally given. + * @return True if the execution was successful, or false otherwise. + */ + public boolean runAgain() { + return this.executable.execute(this.args); + } + +}