Added a recording mechanism for all commands executed.

This commit is contained in:
Andrew Lalis 2018-09-13 13:50:39 +02:00
parent 003e84d960
commit 83fd710e5c
2 changed files with 62 additions and 4 deletions

View File

@ -1,8 +1,8 @@
package nl.andrewlalis.ui.control.command; package nl.andrewlalis.ui.control.command;
import java.util.Arrays; import nl.andrewlalis.ui.control.command.executables.ExecutableContext;
import java.util.HashMap;
import java.util.Map; import java.util.*;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
@ -23,8 +23,20 @@ public class CommandExecutor {
*/ */
private Map<String, Executable> commands; private Map<String, Executable> commands;
/**
* A list of all the executables which have failed to execute.
*/
private List<ExecutableContext> failedExecutables;
/**
* A list of all executables which have been run successfully.
*/
private List<ExecutableContext> successfulExecutables;
public CommandExecutor() { public CommandExecutor() {
this.commands = new HashMap<>(); 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) { public void executeCommand(String commandName, String[] args) {
if (this.commands.containsKey(commandName)) { if (this.commands.containsKey(commandName)) {
logger.info("Command executed: " + commandName + ' ' + Arrays.toString(args)); 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."); logger.warning("Command did not execute successfully.");
this.failedExecutables.add(context);
} else {
this.successfulExecutables.add(context);
} }
} else { } else {
logger.warning(commandName + " is not a valid command."); 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);
}
} }

View File

@ -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);
}
}