Update to next mechanism, with stream passed at command execution
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
@@ -52,7 +52,8 @@ import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||
import fr.bigeon.gclc.i18n.Messages;
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
import fr.bigeon.gclc.manager.ConsoleInput;
|
||||
import fr.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -62,8 +63,9 @@ import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
* A typical use case is the following:
|
||||
*
|
||||
* <pre>
|
||||
* {@link ConsoleManager} manager = new {@link fr.bigeon.gclc.manager.SystemConsoleManager SystemConsoleManager}();
|
||||
* {@link ConsoleApplication} app = new {@link ConsoleApplication}(manager, "welcome", "see you latter")};
|
||||
* {@link ConsoleOutput} out = new {@link fr.bigeon.gclc.manager.SystemConsoleOutput SystemConsoleOutput}();
|
||||
* {@link ConsoleInput} in = new {@link fr.bigeon.gclc.manager.SystemConsoleInput SystemConsoleInput}();
|
||||
* {@link ConsoleApplication} app = new {@link ConsoleApplication}(out, in, "welcome", "see you latter")};
|
||||
* app.{@link ConsoleApplication#add(ICommand) add}("my_command", new {@link ICommand MyCommand()});
|
||||
* app.{@link ConsoleApplication#start() start()};
|
||||
* </pre>
|
||||
@@ -83,8 +85,10 @@ public final class ConsoleApplication implements ICommandProvider {
|
||||
public final String header;
|
||||
/** The good bye message. */
|
||||
public final String footer;
|
||||
/** The console manager. */
|
||||
public final ConsoleManager manager;
|
||||
/** The standard output for the application. */
|
||||
private final ConsoleOutput out;
|
||||
/** The standard input for the application. */
|
||||
private final ConsoleInput in;
|
||||
/** The container of commands. */
|
||||
public final SubedCommand root;
|
||||
/** The state of this application. */
|
||||
@@ -93,15 +97,18 @@ public final class ConsoleApplication implements ICommandProvider {
|
||||
private final List<CommandRequestListener> listeners = new ArrayList<>();
|
||||
|
||||
/** Create a console application.
|
||||
*
|
||||
* @param manager the manager
|
||||
*
|
||||
* @param out the output
|
||||
* @param in the input
|
||||
* @param welcome the welcoming message
|
||||
* @param goodbye the goodbye message */
|
||||
public ConsoleApplication(final ConsoleManager manager, final String welcome,
|
||||
public ConsoleApplication(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String welcome,
|
||||
final String goodbye) {
|
||||
header = welcome;
|
||||
footer = goodbye;
|
||||
this.manager = manager;
|
||||
this.in = in;
|
||||
this.out = out;
|
||||
root = new SubedCommand(""); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
@@ -119,16 +126,27 @@ public final class ConsoleApplication implements ICommandProvider {
|
||||
* @see fr.bigeon.gclc.command.ICommandProvider#executeSub(java.lang.String,
|
||||
* java.lang.String[]) */
|
||||
@Override
|
||||
public void executeSub(final String command,
|
||||
public void executeSub(final ConsoleOutput output, final ConsoleInput input,
|
||||
final String command,
|
||||
final String... args) throws CommandRunException {
|
||||
root.executeSub(command, args);
|
||||
root.executeSub(output, input, command, args);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommandProvider#executeSub(java.lang.String, java.lang.String[])
|
||||
*/
|
||||
@Deprecated
|
||||
@Override
|
||||
public void executeSub(final String command,
|
||||
final String... args) throws CommandRunException {
|
||||
executeSub(out, in, command, args);
|
||||
}
|
||||
|
||||
/** Signify to the application that no command should be inputed anymore */
|
||||
public void exit() {
|
||||
LOGGER.fine("Request exiting application..."); //$NON-NLS-1$
|
||||
running = false;
|
||||
manager.interruptPrompt();
|
||||
in.interruptPrompt();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -145,21 +163,21 @@ public final class ConsoleApplication implements ICommandProvider {
|
||||
try {
|
||||
args = GCLCConstants.splitCommand(cmd);
|
||||
} catch (final CommandParsingException e1) {
|
||||
manager.println("Command line cannot be parsed"); //$NON-NLS-1$
|
||||
out.println("Command line cannot be parsed"); //$NON-NLS-1$
|
||||
LOGGER.log(Level.FINE, "Invalid user command " + cmd, e1); //$NON-NLS-1$
|
||||
return;
|
||||
}
|
||||
if (!args.isEmpty()) {
|
||||
try {
|
||||
executeSub(args.get(0), Arrays.copyOfRange(
|
||||
executeSub(out, in, args.get(0), Arrays.copyOfRange(
|
||||
args.toArray(new String[0]), 1, args.size()));
|
||||
} catch (final CommandRunException e) {
|
||||
LOGGER.log(Level.FINE, "Command failed: " + cmd, e); //$NON-NLS-1$
|
||||
manager.println(Messages
|
||||
out.println(Messages
|
||||
.getString("ConsoleApplication.cmd.failed", cmd)); //$NON-NLS-1$
|
||||
manager.println(e.getLocalizedMessage());
|
||||
out.println(e.getLocalizedMessage());
|
||||
if (e.getType() == CommandRunExceptionType.USAGE) {
|
||||
e.getSource().help(manager);
|
||||
e.getSource().help(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -181,7 +199,7 @@ public final class ConsoleApplication implements ICommandProvider {
|
||||
* (restarting the loop). */
|
||||
private void runLoop() {
|
||||
try {
|
||||
final String cmd = manager.prompt();
|
||||
final String cmd = in.prompt();
|
||||
if (cmd == null || cmd.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
@@ -210,7 +228,7 @@ public final class ConsoleApplication implements ICommandProvider {
|
||||
try {
|
||||
running = true;
|
||||
if (header != null) {
|
||||
manager.println(header);
|
||||
out.println(header);
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
// The manager was closed
|
||||
@@ -227,7 +245,7 @@ public final class ConsoleApplication implements ICommandProvider {
|
||||
} while (running);
|
||||
if (footer != null) {
|
||||
try {
|
||||
manager.println(footer);
|
||||
out.println(footer);
|
||||
} catch (final IOException e) {
|
||||
// The manager was closed
|
||||
running = false;
|
||||
|
||||
@@ -40,7 +40,7 @@ package fr.bigeon.gclc.command;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
import fr.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -93,7 +93,7 @@ public abstract class Command implements ICommand {
|
||||
* @see fr.bigeon.gclc.command.ICommand#help(fr.bigeon.gclc.ConsoleManager,
|
||||
* java.lang.String) */
|
||||
@Override
|
||||
public final void help(final ConsoleManager manager,
|
||||
public final void help(final ConsoleOutput manager,
|
||||
final String... args) throws IOException {
|
||||
manager.println(getCommandName());
|
||||
manager.println(brief());
|
||||
|
||||
@@ -42,8 +42,13 @@ import java.util.List;
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||
import fr.bigeon.gclc.i18n.Messages;
|
||||
import fr.bigeon.gclc.manager.ConsoleInput;
|
||||
import fr.bigeon.gclc.manager.ConsoleOutput;
|
||||
import fr.bigeon.gclc.manager.EmptyInput;
|
||||
import fr.bigeon.gclc.manager.SinkOutput;
|
||||
|
||||
/** <p>
|
||||
/**
|
||||
* <p>
|
||||
* A command provider is a map of key word to command to execute
|
||||
*
|
||||
* @author Emmanuel BIGEON */
|
||||
@@ -90,11 +95,26 @@ public class CommandProvider implements ICommandProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void executeSub(final String cmd,
|
||||
final String... args) throws CommandRunException {
|
||||
public final void executeSub(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String cmd,
|
||||
final String... args) throws CommandRunException {
|
||||
for (final ICommand command : commands) {
|
||||
if (command.getCommandName().equals(cmd)) {
|
||||
command.execute(args);
|
||||
command.execute(out, in, args);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new CommandRunException(
|
||||
Messages.getString("CommandProvider.unrecognized", cmd), null); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@Override
|
||||
public final void executeSub(final String cmd,
|
||||
final String... args) throws CommandRunException {
|
||||
for (final ICommand command : commands) {
|
||||
if (command.getCommandName().equals(cmd)) {
|
||||
command.execute(SinkOutput.INSTANCE, EmptyInput.INSTANCE, args);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,8 @@ package fr.bigeon.gclc.command;
|
||||
import java.io.IOException;
|
||||
|
||||
import fr.bigeon.gclc.ConsoleApplication;
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
import fr.bigeon.gclc.manager.ConsoleInput;
|
||||
import fr.bigeon.gclc.manager.ConsoleOutput;
|
||||
import fr.bigeon.gclc.prompt.CLIPrompterMessages;
|
||||
|
||||
/** <p>
|
||||
@@ -71,7 +72,7 @@ public final class ExitCommand implements ICommand {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(final String... args) {
|
||||
public void execute(final ConsoleOutput output, final ConsoleInput input, final String... args) {
|
||||
beforeExit();
|
||||
app.exit();
|
||||
}
|
||||
@@ -84,12 +85,11 @@ public final class ExitCommand implements ICommand {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void help(final ConsoleManager manager,
|
||||
public void help(final ConsoleOutput manager,
|
||||
final String... args) throws IOException {
|
||||
manager.println(
|
||||
CLIPrompterMessages.getString(EXIT_MAN, (Object[]) args));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String tip() {
|
||||
return CLIPrompterMessages.getString(EXIT);
|
||||
|
||||
@@ -42,7 +42,8 @@ import java.io.IOException;
|
||||
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
import fr.bigeon.gclc.manager.ConsoleInput;
|
||||
import fr.bigeon.gclc.manager.ConsoleOutput;
|
||||
import fr.bigeon.gclc.prompt.CLIPrompterMessages;
|
||||
|
||||
/** A command to print help of an other command.
|
||||
@@ -54,17 +55,13 @@ public final class HelpExecutor extends Command {
|
||||
|
||||
/** The command to execute the help of */
|
||||
private final ICommand cmd;
|
||||
/** The console manager */
|
||||
private final ConsoleManager consoleManager;
|
||||
|
||||
/** @param cmdName the command name
|
||||
* @param consoleManager the manager for the console
|
||||
* @param cmd the command to execute the help of */
|
||||
public HelpExecutor(final String cmdName, final ConsoleManager consoleManager,
|
||||
public HelpExecutor(final String cmdName,
|
||||
final ICommand cmd) {
|
||||
super(cmdName);
|
||||
this.cmd = cmd;
|
||||
this.consoleManager = consoleManager;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -78,11 +75,13 @@ public final class HelpExecutor extends Command {
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#execute(java.lang.String[]) */
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[])
|
||||
*/
|
||||
@Override
|
||||
public void execute(final String... args) throws CommandRunException {
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
try {
|
||||
cmd.help(consoleManager, args);
|
||||
cmd.help(out, args);
|
||||
} catch (final IOException e) {
|
||||
throw new CommandRunException(CommandRunExceptionType.INTERACTION,
|
||||
"Console manager closed", e, this); //$NON-NLS-1$
|
||||
|
||||
@@ -41,7 +41,8 @@ package fr.bigeon.gclc.command;
|
||||
import java.io.IOException;
|
||||
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
import fr.bigeon.gclc.manager.ConsoleInput;
|
||||
import fr.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/** The contract of commands
|
||||
* <p>
|
||||
@@ -50,10 +51,14 @@ import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
* @author Emmanuel Bigeon */
|
||||
public interface ICommand {
|
||||
|
||||
/** @param args the arguments of the command (some expect an empty array)
|
||||
* @throws CommandRunException if the execution of the command failed for
|
||||
* any reason */
|
||||
void execute(String... args) throws CommandRunException;
|
||||
/** Execute the command on the given output and input.
|
||||
*
|
||||
* @param out the normal output
|
||||
* @param in the input
|
||||
* @param args the arguments
|
||||
* @throws CommandRunException if the command failed */
|
||||
void execute(ConsoleOutput out, ConsoleInput in,
|
||||
String... args) throws CommandRunException;
|
||||
|
||||
/** @return the command's name */
|
||||
String getCommandName();
|
||||
@@ -72,10 +77,10 @@ public interface ICommand {
|
||||
* [Usage details]
|
||||
* </pre>
|
||||
*
|
||||
* @param manager the manager to print the data
|
||||
* @param output the output to print the data
|
||||
* @param args the arguments called with the help
|
||||
* @throws IOException if the manager was closed */
|
||||
void help(ConsoleManager manager, String... args) throws IOException;
|
||||
void help(ConsoleOutput output, String... args) throws IOException;
|
||||
|
||||
/** @return a tip on the command */
|
||||
String tip();
|
||||
|
||||
@@ -38,6 +38,8 @@ package fr.bigeon.gclc.command;
|
||||
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||
import fr.bigeon.gclc.manager.ConsoleInput;
|
||||
import fr.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/** <p>
|
||||
* An ICommadProvider is a provider of commands that can register commands under
|
||||
@@ -55,6 +57,23 @@ public interface ICommandProvider {
|
||||
* @throws InvalidCommandName if the command name is invalid */
|
||||
boolean add(ICommand value) throws InvalidCommandName;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* This method executes the command with the given name found. If no command
|
||||
* with this name is found, an error command is usually executed. If there
|
||||
* are several commands with the same name, the behavior is unspecified.
|
||||
* Depending on the implementation, it may run an error command or prompt
|
||||
* the user for a choice.
|
||||
*
|
||||
* @param out the output
|
||||
* @param in the input
|
||||
* @param command the name of the command the user wishes to execute
|
||||
* @param args the arguments for the command
|
||||
* @throws CommandRunException if the command failed to run */
|
||||
void executeSub(ConsoleOutput out, ConsoleInput in,
|
||||
String command,
|
||||
String... args) throws CommandRunException;
|
||||
|
||||
/** <p>
|
||||
* This method executes the command with the given name found. If no command
|
||||
* with this name is found, an error command is usually executed. If there
|
||||
@@ -65,10 +84,12 @@ public interface ICommandProvider {
|
||||
* @param command the name of the command the user wishes to execute
|
||||
* @param args the arguments for the command
|
||||
* @throws CommandRunException if the command failed to run */
|
||||
@Deprecated
|
||||
void executeSub(String command,
|
||||
String... args) throws CommandRunException;
|
||||
|
||||
/** <p>
|
||||
/**
|
||||
* <p>
|
||||
* This method provide the command with the given name found. If no command
|
||||
* with this name is found, an error command is usually returned. If there
|
||||
* are several commands with the same name, the behavior is unspecified.
|
||||
|
||||
@@ -38,7 +38,9 @@
|
||||
*/
|
||||
package fr.bigeon.gclc.command;
|
||||
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.manager.ConsoleInput;
|
||||
import fr.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/** This implement a command that does nothing.
|
||||
* <p>
|
||||
@@ -56,9 +58,11 @@ public final class MockCommand implements ICommand {
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[])
|
||||
*/
|
||||
@Override
|
||||
public void execute(final String... args) {
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
//
|
||||
}
|
||||
|
||||
@@ -73,7 +77,7 @@ public final class MockCommand implements ICommand {
|
||||
* @see fr.bigeon.gclc.command.ICommand#help(fr.bigeon.gclc.manager.
|
||||
* ConsoleManager, java.lang.String[]) */
|
||||
@Override
|
||||
public void help(final ConsoleManager manager,
|
||||
public void help(final ConsoleOutput manager,
|
||||
final String... args) {
|
||||
//
|
||||
}
|
||||
|
||||
@@ -53,7 +53,9 @@ import fr.bigeon.gclc.exception.CommandParsingException;
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import fr.bigeon.gclc.exception.InvalidParameterException;
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
import fr.bigeon.gclc.manager.ConsoleInput;
|
||||
import fr.bigeon.gclc.manager.ConsoleOutput;
|
||||
import fr.bigeon.gclc.manager.EmptyInput;
|
||||
|
||||
/** A command relying on the {@link CommandParameters} to store parameters
|
||||
* values.
|
||||
@@ -61,11 +63,6 @@ import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
* @author Emmanuel BIGEON */
|
||||
public abstract class ParametrizedCommand extends Command {
|
||||
|
||||
/** If the command may use interactive prompting for required parameters
|
||||
* that were not provided on execution. */
|
||||
private boolean interactive = true;
|
||||
/** The manager. */
|
||||
protected final ConsoleManager manager;
|
||||
/** The boolean parameters mandatory status. */
|
||||
private final Set<String> boolParams = new HashSet<>();
|
||||
/** The string parameters mandatory status. */
|
||||
@@ -76,38 +73,6 @@ public abstract class ParametrizedCommand extends Command {
|
||||
* paramters in the status maps. */
|
||||
private final boolean strict;
|
||||
|
||||
/** Create a parametrized command.
|
||||
* <p>
|
||||
* Implementation are supposed to call the
|
||||
* {@link #addBooleanParameter(String)} and
|
||||
* {@link #addStringParameter(String, boolean)} method to set the
|
||||
* parameters.
|
||||
*
|
||||
* @param manager the manager
|
||||
* @param name the name */
|
||||
public ParametrizedCommand(final ConsoleManager manager,
|
||||
final String name) {
|
||||
this(manager, name, true);
|
||||
}
|
||||
|
||||
/** Create a parametrized command.
|
||||
* <p>
|
||||
* Implementation are supposed to call the
|
||||
* {@link #addBooleanParameter(String)} and
|
||||
* {@link #addStringParameter(String, boolean)} method to set the
|
||||
* parameters.
|
||||
*
|
||||
* @param manager the manager
|
||||
* @param name the name
|
||||
* @param strict if the arguments are restricted to the declared ones */
|
||||
public ParametrizedCommand(final ConsoleManager manager, final String name,
|
||||
final boolean strict) {
|
||||
super(name);
|
||||
this.manager = manager;
|
||||
interactive = manager != null;
|
||||
this.strict = strict;
|
||||
}
|
||||
|
||||
/** Create a parametrized command.
|
||||
* <p>
|
||||
* Implementation are supposed to call the
|
||||
@@ -117,7 +82,7 @@ public abstract class ParametrizedCommand extends Command {
|
||||
*
|
||||
* @param name the name */
|
||||
public ParametrizedCommand(final String name) {
|
||||
this(null, name, true);
|
||||
this(name, true);
|
||||
}
|
||||
|
||||
/** Create a parametrized command.
|
||||
@@ -130,7 +95,8 @@ public abstract class ParametrizedCommand extends Command {
|
||||
* @param name the name
|
||||
* @param strict if the arguments are restricted to the declared ones */
|
||||
public ParametrizedCommand(final String name, final boolean strict) {
|
||||
this(null, name, strict);
|
||||
super(name);
|
||||
this.strict = strict;
|
||||
}
|
||||
|
||||
/** Add a boolean parameter to defined parmaters.
|
||||
@@ -182,15 +148,20 @@ public abstract class ParametrizedCommand extends Command {
|
||||
}
|
||||
|
||||
/** Actually performs the execution after parsing the parameters.
|
||||
*
|
||||
*
|
||||
* @param out the output
|
||||
* @param in the input
|
||||
* @param parameters the command parameters
|
||||
* @throws CommandRunException if the command failed */
|
||||
protected abstract void doExecute(CommandParameters parameters) throws CommandRunException;
|
||||
protected abstract void doExecute(ConsoleOutput out, ConsoleInput in,
|
||||
CommandParameters parameters) throws CommandRunException;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#execute(java.lang.String[]) */
|
||||
@Override
|
||||
public final void execute(final String... args) throws CommandRunException {
|
||||
public final void execute(final ConsoleOutput output,
|
||||
final ConsoleInput input,
|
||||
final String... args) throws CommandRunException {
|
||||
final CommandParameters parameters = new CommandParameters(boolParams,
|
||||
stringParams.keySet(), strict);
|
||||
try {
|
||||
@@ -203,7 +174,7 @@ public abstract class ParametrizedCommand extends Command {
|
||||
for (final Entry<String, Boolean> string : params.entrySet()) {
|
||||
if (string.getValue().booleanValue() &&
|
||||
parameters.get(string.getKey()) == null) {
|
||||
if (!interactive) {
|
||||
if (input == null || input == EmptyInput.INSTANCE) {
|
||||
throw new CommandRunException(
|
||||
CommandRunExceptionType.INTERACTION,
|
||||
"Missing required parameter " + string.getKey(), //$NON-NLS-1$
|
||||
@@ -213,26 +184,28 @@ public abstract class ParametrizedCommand extends Command {
|
||||
}
|
||||
}
|
||||
// for each needed parameters that is missing, prompt the user.
|
||||
fillParameters(toProvide, parameters);
|
||||
doExecute(parameters);
|
||||
fillParameters(input, toProvide, parameters);
|
||||
doExecute(output, input, parameters);
|
||||
}
|
||||
|
||||
/** Fill the undefined parameters.
|
||||
* <p>
|
||||
* This method prompts the user to fill the needed parameters.
|
||||
*
|
||||
* @param input the input to prompt through
|
||||
* @param parameters the parameter list to complete
|
||||
* @param toProvide the parameters to ask for
|
||||
* @throws CommandRunException if the manager was closed */
|
||||
private final void fillParameters(final List<String> toProvide,
|
||||
private final void fillParameters(final ConsoleInput input,
|
||||
final List<String> toProvide,
|
||||
final CommandParameters parameters) throws CommandRunException {
|
||||
for (final String string : toProvide) {
|
||||
String value;
|
||||
try {
|
||||
value = manager
|
||||
value = input
|
||||
.prompt(MessageFormat.format("value of {0}? ", string)); //$NON-NLS-1$
|
||||
while (value.isEmpty()) {
|
||||
value = manager.prompt(MessageFormat.format(
|
||||
value = input.prompt(MessageFormat.format(
|
||||
"value of {0}? (cannot be empty) ", //$NON-NLS-1$
|
||||
string));
|
||||
}
|
||||
@@ -266,15 +239,6 @@ public abstract class ParametrizedCommand extends Command {
|
||||
return stringParams.keySet();
|
||||
}
|
||||
|
||||
/** If the command is interactive.
|
||||
* <p>
|
||||
* An interactive command will prompt the user for missing parameters.
|
||||
*
|
||||
* @return the interactive */
|
||||
public final boolean isInteractive() {
|
||||
return interactive;
|
||||
}
|
||||
|
||||
/** Test if a parameter is needed.
|
||||
*
|
||||
* @param param the parameter name
|
||||
|
||||
@@ -52,6 +52,8 @@ import fr.bigeon.gclc.GCLCConstants;
|
||||
import fr.bigeon.gclc.exception.CommandParsingException;
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import fr.bigeon.gclc.manager.ConsoleInput;
|
||||
import fr.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/** A command that will laucnh a series of command from a file
|
||||
* <p>
|
||||
@@ -94,10 +96,13 @@ public final class ScriptExecution extends Command {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */
|
||||
@Override
|
||||
public void execute(final String... args) throws CommandRunException {
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
checkArgs(args);
|
||||
final String scriptFile = args[0];
|
||||
final String[] params = Arrays.copyOfRange(args, 1, args.length);
|
||||
@@ -114,7 +119,8 @@ public final class ScriptExecution extends Command {
|
||||
}
|
||||
final List<String> ps = GCLCConstants.splitCommand(cmdLine);
|
||||
final String command = ps.remove(0);
|
||||
application.executeSub(command, ps.toArray(new String[0]));
|
||||
application.executeSub(out, in, command,
|
||||
ps.toArray(new String[0]));
|
||||
}
|
||||
} catch (final CommandParsingException e) {
|
||||
throw new CommandRunException(MessageFormat.format(
|
||||
|
||||
@@ -41,7 +41,8 @@ import java.util.Arrays;
|
||||
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
import fr.bigeon.gclc.manager.ConsoleInput;
|
||||
import fr.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -104,17 +105,19 @@ public final class SubedCommand extends CommandProvider implements ICommand {
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.acide.Command#execute(java.lang.String[]) */
|
||||
@Override
|
||||
public void execute(final String... args) throws CommandRunException {
|
||||
public void execute(final ConsoleOutput output, final ConsoleInput input,
|
||||
final String... args) throws CommandRunException {
|
||||
if (args.length == 0 || args[0].startsWith("-")) { //$NON-NLS-1$
|
||||
if (noArgCommand != null) {
|
||||
noArgCommand.execute(args);
|
||||
noArgCommand.execute(output, input, args);
|
||||
} else {
|
||||
throw new CommandRunException("Unrecognized command", this); //$NON-NLS-1$
|
||||
}
|
||||
} else {
|
||||
|
||||
try {
|
||||
executeSub(args[0], Arrays.copyOfRange(args, 1, args.length));
|
||||
executeSub(output, input, args[0],
|
||||
Arrays.copyOfRange(args, 1, args.length));
|
||||
} catch (final CommandRunException e) {
|
||||
if (e.getSource() != null) {
|
||||
throw e;
|
||||
@@ -135,7 +138,7 @@ public final class SubedCommand extends CommandProvider implements ICommand {
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#help() */
|
||||
@Override
|
||||
public void help(final ConsoleManager manager,
|
||||
public void help(final ConsoleOutput manager,
|
||||
final String... args) throws IOException {
|
||||
if (args.length != 0 && !args[0].startsWith("-")) { //$NON-NLS-1$
|
||||
// Specific
|
||||
@@ -172,5 +175,4 @@ public final class SubedCommand extends CommandProvider implements ICommand {
|
||||
public String toString() {
|
||||
return "SubedCommand " + super.toString(); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,80 +33,29 @@
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.ConsoleManager.java
|
||||
* Created on: Dec 19, 2014
|
||||
* gclc:fr.bigeon.gclc.manager.ConsoleInput.java
|
||||
* Created on: Nov 13, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.manager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
|
||||
/** <p>
|
||||
* A console manager is in charge of the basic prompts and prints on a console.
|
||||
* <p>
|
||||
* Depending on the implementation, some manager may not support the prompt with
|
||||
* a message, in that case, the usual behavior is to prompt normally.
|
||||
/** A console application input.
|
||||
*
|
||||
* @author Emmanuel BIGEON */
|
||||
public interface ConsoleManager extends AutoCloseable {
|
||||
|
||||
/** @return the prompt prefix */
|
||||
String getPrompt();
|
||||
|
||||
/** @param text the message to print (without line break at the end).
|
||||
* @throws IOException if the manager is closed or could not read the
|
||||
* prompt */
|
||||
void print(String text) throws IOException;
|
||||
|
||||
/** Prints an end of line
|
||||
*
|
||||
* @throws IOException if the manager is closed or could not read the
|
||||
* prompt */
|
||||
void println() throws IOException;
|
||||
|
||||
/** @param message the message to print
|
||||
* @throws IOException if the manager is closed or could not read the
|
||||
* prompt */
|
||||
void println(String message) throws IOException;
|
||||
|
||||
/** @return the user inputed string
|
||||
* @throws IOException if the manager is closed or could not read the prompt
|
||||
* @throws InterruptedIOException if the prompt was interrupted */
|
||||
String prompt() throws IOException;
|
||||
|
||||
/** @param timeout the time to wait in milliseconds
|
||||
* @return the user inputed string, null if the timeout was reached
|
||||
* @throws IOException if the manager is closed or could not read the prompt
|
||||
* @throws InterruptedIOException if the prompt was interrupted */
|
||||
String prompt(long timeout) throws IOException;
|
||||
|
||||
/** @param message the message to prompt the user
|
||||
* @return the user inputed string
|
||||
* @throws IOException if the manager is closed or could not read the prompt
|
||||
* @throws InterruptedIOException if the prompt was interrupted */
|
||||
String prompt(String message) throws IOException;
|
||||
|
||||
/** @param timeout the time to wait in milliseconds
|
||||
* @param message the message to prompt the user
|
||||
* @return the user inputed string, null if the timeout was reached
|
||||
* @throws IOException if the manager is closed or could not read the prompt
|
||||
* @throws InterruptedIOException if the prompt was interrupted */
|
||||
String prompt(String message, long timeout) throws IOException;
|
||||
|
||||
/** <p>
|
||||
* Set a prompting prefix.
|
||||
*
|
||||
* @param prompt the prompt */
|
||||
void setPrompt(String prompt);
|
||||
* @author Emmanuel Bigeon */
|
||||
public interface ConsoleInput extends AutoCloseable {
|
||||
|
||||
/** Closes the manager.
|
||||
*
|
||||
*
|
||||
* @throws IOException if the close raised an exception */
|
||||
@Override
|
||||
void close() throws IOException;
|
||||
|
||||
/** @return if the manager is closed. */
|
||||
boolean isClosed();
|
||||
/** Get the prompt string.
|
||||
*
|
||||
* @return the prompt prefix */
|
||||
String getPrompt();
|
||||
|
||||
/** Indicate to the manager that is should interrompt the prompting, if
|
||||
* possible.
|
||||
@@ -116,4 +65,50 @@ public interface ConsoleManager extends AutoCloseable {
|
||||
* (from the partial prompt content to an empty string or even a null
|
||||
* pointer). */
|
||||
void interruptPrompt();
|
||||
|
||||
/** Test if the input is closed.
|
||||
* <p>
|
||||
* If this is true, {@link #prompt()} methods will return immediatly and a
|
||||
* null chain.
|
||||
*
|
||||
* @return if the manager is closed. */
|
||||
boolean isClosed();
|
||||
|
||||
/** Prompt the user.
|
||||
*
|
||||
* @return the user inputed string
|
||||
* @throws IOException if the manager is closed or could not read the prompt
|
||||
* @throws InterruptedIOException if the prompt was interrupted */
|
||||
String prompt() throws IOException;
|
||||
|
||||
/** Prompt the user, with an allotated time to answer.
|
||||
*
|
||||
* @param timeout the time to wait in milliseconds
|
||||
* @return the user inputed string, null if the timeout was reached
|
||||
* @throws IOException if the manager is closed or could not read the prompt
|
||||
* @throws InterruptedIOException if the prompt was interrupted */
|
||||
String prompt(long timeout) throws IOException;
|
||||
|
||||
/** Prompt the user, with a hint on what is prompted.
|
||||
*
|
||||
* @param message the message to prompt the user
|
||||
* @return the user inputed string
|
||||
* @throws IOException if the manager is closed or could not read the prompt
|
||||
* @throws InterruptedIOException if the prompt was interrupted */
|
||||
String prompt(String message) throws IOException;
|
||||
|
||||
/** Prompt the user, with a hint on what is prompted and an allotated time
|
||||
* to answer.
|
||||
*
|
||||
* @param timeout the time to wait in milliseconds
|
||||
* @param message the message to prompt the user
|
||||
* @return the user inputed string, null if the timeout was reached
|
||||
* @throws IOException if the manager is closed or could not read the prompt
|
||||
* @throws InterruptedIOException if the prompt was interrupted */
|
||||
String prompt(String message, long timeout) throws IOException;
|
||||
|
||||
/** Set a prompting prefix.
|
||||
*
|
||||
* @param prompt the prompt */
|
||||
void setPrompt(String prompt);
|
||||
}
|
||||
@@ -33,39 +33,45 @@
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.proc.ProcessList.java
|
||||
* Created on: May 10, 2017
|
||||
* gclc:fr.bigeon.gclc.manager.ConsoleOutput.java
|
||||
* Created on: Nov 13, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.proc;
|
||||
package fr.bigeon.gclc.manager;
|
||||
|
||||
import fr.bigeon.gclc.command.Command;
|
||||
import java.io.IOException;
|
||||
|
||||
/** An abstract command to generate a task and return the control to the user
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public abstract class TaskSpawner extends Command {
|
||||
/** The process pool */
|
||||
private final TaskPool pool;
|
||||
|
||||
/** @param name the command name
|
||||
* @param pool the pool */
|
||||
public TaskSpawner(final String name, final TaskPool pool) {
|
||||
super(name);
|
||||
this.pool = pool;
|
||||
}
|
||||
|
||||
/** @param args the arguments
|
||||
* @return the process to start and add to the pool */
|
||||
protected abstract Task createTask(String... args);
|
||||
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public interface ConsoleOutput extends AutoCloseable {
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[])
|
||||
*/
|
||||
* @see java.lang.AutoCloseable#close() */
|
||||
@Override
|
||||
public final void execute(final String... args) {
|
||||
final Task task = createTask(args);
|
||||
final Thread th = new Thread(task);
|
||||
th.start();
|
||||
pool.add(task);
|
||||
}
|
||||
void close() throws IOException;
|
||||
|
||||
/** Print a string.
|
||||
* @param text the message to print (without line break at the end).
|
||||
* @throws IOException if the manager is closed or could not read the
|
||||
* prompt */
|
||||
void print(String text) throws IOException;
|
||||
|
||||
/** Prints an end of line
|
||||
*
|
||||
* @throws IOException if the manager is closed or could not read the
|
||||
* prompt */
|
||||
void println() throws IOException;
|
||||
|
||||
/** @param message the message to print
|
||||
* @throws IOException if the manager is closed or could not read the
|
||||
* prompt */
|
||||
void println(String message) throws IOException;
|
||||
|
||||
/** @return if the manager is closed. */
|
||||
boolean isClosed();
|
||||
|
||||
|
||||
}
|
||||
119
gclc/src/main/java/fr/bigeon/gclc/manager/EmptyInput.java
Normal file
119
gclc/src/main/java/fr/bigeon/gclc/manager/EmptyInput.java
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.manager.EmptyInput.java
|
||||
* Created on: Nov 13, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.manager;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/** A console input that return empty to all prompting.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class EmptyInput implements ConsoleInput {
|
||||
|
||||
/** The empty prompter. */
|
||||
public static final ConsoleInput INSTANCE = new EmptyInput();
|
||||
/** The empty input. */
|
||||
private EmptyInput() {
|
||||
//
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#close() */
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
//
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#getPrompt() */
|
||||
@Override
|
||||
public String getPrompt() {
|
||||
return ""; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#interruptPrompt() */
|
||||
@Override
|
||||
public void interruptPrompt() {
|
||||
//
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#isClosed() */
|
||||
@Override
|
||||
public boolean isClosed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt() */
|
||||
@Override
|
||||
public String prompt() throws IOException {
|
||||
return ""; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(long) */
|
||||
@Override
|
||||
public String prompt(final long timeout) throws IOException {
|
||||
return ""; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String) */
|
||||
@Override
|
||||
public String prompt(final String message) throws IOException {
|
||||
return ""; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String,
|
||||
* long) */
|
||||
@Override
|
||||
public String prompt(final String message,
|
||||
final long timeout) throws IOException {
|
||||
return ""; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */
|
||||
@Override
|
||||
public void setPrompt(final String prompt) {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -53,13 +53,13 @@ import java.nio.charset.StandardCharsets;
|
||||
* used to test application behavior.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class PipedConsoleManager
|
||||
implements ConsoleManager {
|
||||
public final class PipedConsoleInput
|
||||
implements ConsoleInput {
|
||||
|
||||
/** The encoding between streams */
|
||||
private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
|
||||
/** THe inner manager */
|
||||
private final SystemConsoleManager innerManager;
|
||||
private final SystemConsoleInput innerManager;
|
||||
/** The stream to pipe commands into */
|
||||
private final PipedOutputStream commandInput;
|
||||
/** The reader to get application return from */
|
||||
@@ -78,7 +78,7 @@ public final class PipedConsoleManager
|
||||
/** Create a manager that will write and read through piped stream.
|
||||
*
|
||||
* @throws IOException if the piping failed for streams */
|
||||
public PipedConsoleManager() throws IOException {
|
||||
public PipedConsoleInput() throws IOException {
|
||||
commandInput = new PipedOutputStream();
|
||||
in = new PipedInputStream(commandInput);
|
||||
commandOutput = new PipedInputStream();
|
||||
@@ -86,7 +86,7 @@ public final class PipedConsoleManager
|
||||
commandBuffOutput = new BufferedReader(
|
||||
new InputStreamReader(commandOutput, StandardCharsets.UTF_8));
|
||||
outPrint = new PrintStream(out, true, UTF_8);
|
||||
innerManager = new SystemConsoleManager(outPrint, in,
|
||||
innerManager = new SystemConsoleInput(outPrint, in,
|
||||
StandardCharsets.UTF_8);
|
||||
writing = new WritingRunnable(commandInput, StandardCharsets.UTF_8);
|
||||
reading = new ReadingRunnable(commandBuffOutput);
|
||||
@@ -139,21 +139,6 @@ public final class PipedConsoleManager
|
||||
return innerManager.isClosed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(final String object) throws IOException {
|
||||
innerManager.print(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println() throws IOException {
|
||||
innerManager.println();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(final String object) throws IOException {
|
||||
innerManager.println(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String prompt() throws IOException {
|
||||
return innerManager
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc-test:fr.bigeon.gclc.test.TestConsoleManager.java
|
||||
* Created on: Nov 18, 2016
|
||||
*/
|
||||
package fr.bigeon.gclc.manager;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/** This console manager allows to enter commands and retrieve the output as an
|
||||
* input.
|
||||
* <p>
|
||||
* This console manager is used to internally pilot an application. This can be
|
||||
* used to test application behavior.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class PipedConsoleOutput
|
||||
implements ConsoleOutput {
|
||||
|
||||
/** The encoding between streams */
|
||||
private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
|
||||
/** THe inner manager */
|
||||
private final SystemConsoleOutput innerManager;
|
||||
/** The reader to get application return from */
|
||||
private final BufferedReader commandBuffOutput;
|
||||
/** The stream to get application return from */
|
||||
private final PipedInputStream commandOutput;
|
||||
/** The print writer for application to write return to */
|
||||
private final PrintStream outPrint;
|
||||
/** The reading thread */
|
||||
private final ReadingRunnable reading;
|
||||
|
||||
/** Create a manager that will write and read through piped stream.
|
||||
*
|
||||
* @throws IOException if the piping failed for streams */
|
||||
public PipedConsoleOutput() throws IOException {
|
||||
commandOutput = new PipedInputStream();
|
||||
final PipedOutputStream out = new PipedOutputStream(commandOutput);
|
||||
commandBuffOutput = new BufferedReader(
|
||||
new InputStreamReader(commandOutput, StandardCharsets.UTF_8));
|
||||
outPrint = new PrintStream(out, true, UTF_8);
|
||||
innerManager = new SystemConsoleOutput(outPrint);
|
||||
reading = new ReadingRunnable(commandBuffOutput);
|
||||
final Thread th = new Thread(reading, "read"); //$NON-NLS-1$
|
||||
th.setDaemon(true);
|
||||
th.start();
|
||||
}
|
||||
|
||||
/** @return the content of the next line written by the application
|
||||
* @throws IOException if the reading failed */
|
||||
public boolean available() throws IOException {
|
||||
return reading.hasMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
reading.setRunning(false);
|
||||
innerManager.close();
|
||||
outPrint.close();
|
||||
commandBuffOutput.close();
|
||||
commandOutput.close();
|
||||
}
|
||||
|
||||
/** @param message the message
|
||||
* @return the thread to join to wait for message delivery
|
||||
* @see fr.bigeon.gclc.manager.ReadingRunnable#getWaitForDelivery(java.lang.String) */
|
||||
public Thread getWaitForDelivery(final String message) {
|
||||
return reading.getWaitForDelivery(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClosed() {
|
||||
return innerManager.isClosed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(final String object) throws IOException {
|
||||
innerManager.print(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println() throws IOException {
|
||||
innerManager.println();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(final String object) throws IOException {
|
||||
innerManager.println(object);
|
||||
}
|
||||
|
||||
/** @return the content of the next line written by the application
|
||||
* @throws IOException if the reading failed */
|
||||
public String readNextLine() throws IOException {
|
||||
return reading.getMessage();
|
||||
}
|
||||
}
|
||||
@@ -69,7 +69,8 @@ public final class ReadingRunnable implements Runnable {
|
||||
/** @param obj the object to lock on
|
||||
* @param start the object to notify when ready to wait
|
||||
* @param message the message to wait for */
|
||||
public ToWaitRunnable(final Object obj, final Object start, final String message) {
|
||||
public ToWaitRunnable(final Object obj, final Object start,
|
||||
final String message) {
|
||||
this.obj = obj;
|
||||
this.start = start;
|
||||
this.message = message;
|
||||
@@ -214,7 +215,8 @@ public final class ReadingRunnable implements Runnable {
|
||||
}
|
||||
final Object obj = messageBlocker.get(message);
|
||||
final Object start = new Object();
|
||||
final ToWaitRunnable waitRunn = new ToWaitRunnable(obj, start, message);
|
||||
final ToWaitRunnable waitRunn = new ToWaitRunnable(obj, start,
|
||||
message);
|
||||
final Thread th = new Thread(waitRunn);
|
||||
|
||||
synchronized (start) {
|
||||
@@ -294,14 +296,18 @@ public final class ReadingRunnable implements Runnable {
|
||||
lock.notifyAll();
|
||||
}
|
||||
} catch (final InterruptedIOException e) {
|
||||
LOGGER.info("Reading interrupted"); //$NON-NLS-1$
|
||||
if (running) {
|
||||
LOGGER.info("Reading interrupted"); //$NON-NLS-1$
|
||||
}
|
||||
LOGGER.log(Level.FINER,
|
||||
"Read interruption was caused by an exception", e); //$NON-NLS-1$
|
||||
} catch (final IOException e) {
|
||||
LOGGER.severe("Unable to read from stream"); //$NON-NLS-1$
|
||||
LOGGER.log(Level.FINE, "The stream reading threw an exception", //$NON-NLS-1$
|
||||
e);
|
||||
running = false;
|
||||
if (running) {
|
||||
LOGGER.severe("Unable to read from stream"); //$NON-NLS-1$
|
||||
running = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,47 +33,59 @@
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.proc.ProcessList.java
|
||||
* Created on: May 10, 2017
|
||||
* gclc:fr.bigeon.gclc.manager.SinkOutput.java
|
||||
* Created on: Nov 13, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.proc;
|
||||
package fr.bigeon.gclc.manager;
|
||||
|
||||
import fr.bigeon.gclc.command.Command;
|
||||
import java.io.IOException;
|
||||
|
||||
/** A command that will flag a task to stop
|
||||
/** A console output that absorbs every message.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class ProcessKill extends Command {
|
||||
/** The taskpool */
|
||||
private final TaskPool pool;
|
||||
public class SinkOutput implements ConsoleOutput {
|
||||
|
||||
/** @param name the command name
|
||||
* @param pool the pool */
|
||||
public ProcessKill(final String name, final TaskPool pool) {
|
||||
super(name);
|
||||
this.pool = pool;
|
||||
/** The sink output. */
|
||||
public static final ConsoleOutput INSTANCE = new SinkOutput();
|
||||
|
||||
/** Singleton constructor. */
|
||||
private SinkOutput() {
|
||||
//
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleOutput#close() */
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
//
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */
|
||||
* @see fr.bigeon.gclc.manager.ConsoleOutput#isClosed() */
|
||||
@Override
|
||||
public void execute(final String... args) {
|
||||
pool.get(args[0]).setRunning(false);
|
||||
public boolean isClosed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||
@SuppressWarnings("nls")
|
||||
* @see fr.bigeon.gclc.manager.ConsoleOutput#print(java.lang.String) */
|
||||
@Override
|
||||
public String tip() {
|
||||
return "Request a process to stop (softly)";
|
||||
public void print(final String text) throws IOException {
|
||||
//
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
* @see fr.bigeon.gclc.manager.ConsoleOutput#println() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
return null;
|
||||
public void println() throws IOException {
|
||||
//
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleOutput#println(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public void println(final String message) throws IOException {
|
||||
//
|
||||
}
|
||||
|
||||
}
|
||||
@@ -50,7 +50,7 @@ import java.nio.charset.Charset;
|
||||
* The default constructor will use the system standart input and output.
|
||||
*
|
||||
* @author Emmanuel BIGEON */
|
||||
public final class SystemConsoleManager implements ConsoleManager {
|
||||
public final class SystemConsoleInput implements ConsoleInput {
|
||||
|
||||
/** The default prompt */
|
||||
public static final String DEFAULT_PROMPT = "> "; //$NON-NLS-1$
|
||||
@@ -74,14 +74,14 @@ public final class SystemConsoleManager implements ConsoleManager {
|
||||
|
||||
/** This default constructor relies on the system defined standart output
|
||||
* and input stream. */
|
||||
public SystemConsoleManager() {
|
||||
public SystemConsoleInput() {
|
||||
this(System.out, System.in, Charset.defaultCharset());
|
||||
}
|
||||
|
||||
/** @param out the output stream
|
||||
* @param in the input stream
|
||||
* @param charset the charset for the input */
|
||||
public SystemConsoleManager(final PrintStream out, final InputStream in,
|
||||
public SystemConsoleInput(final PrintStream out, final InputStream in,
|
||||
final Charset charset) {
|
||||
super();
|
||||
this.out = out;
|
||||
@@ -117,11 +117,11 @@ public final class SystemConsoleManager implements ConsoleManager {
|
||||
|
||||
/** Beware, in this implementation this is the same as closing the manager.
|
||||
*
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
|
||||
@Override
|
||||
public void interruptPrompt() {
|
||||
reading.interrupt();
|
||||
}
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#interruptPrompt() */
|
||||
@Override
|
||||
public void interruptPrompt() {
|
||||
reading.interrupt();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#isClosed() */
|
||||
@@ -130,30 +130,6 @@ public final class SystemConsoleManager implements ConsoleManager {
|
||||
return closed;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#print(java.lang.Object) */
|
||||
@Override
|
||||
public void print(final String object) throws IOException {
|
||||
checkOpen();
|
||||
out.print(object);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#println() */
|
||||
@Override
|
||||
public void println() throws IOException {
|
||||
checkOpen();
|
||||
out.println();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#println(java.lang.Object) */
|
||||
@Override
|
||||
public void println(final String object) throws IOException {
|
||||
checkOpen();
|
||||
out.println(object);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#prompt() */
|
||||
@Override
|
||||
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.system.SystemConsoleManager.java
|
||||
* Created on: Dec 19, 2014
|
||||
*/
|
||||
package fr.bigeon.gclc.manager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
/** A console using the input stream and print stream.
|
||||
* <p>
|
||||
* The default constructor will use the system standart input and output.
|
||||
*
|
||||
* @author Emmanuel BIGEON */
|
||||
public final class SystemConsoleOutput implements ConsoleOutput {
|
||||
|
||||
/** The default prompt */
|
||||
public static final String DEFAULT_PROMPT = "> "; //$NON-NLS-1$
|
||||
|
||||
/** The print stream */
|
||||
private final PrintStream out;
|
||||
|
||||
/** If the manager is closed */
|
||||
private boolean closed = false;
|
||||
|
||||
/** This default constructor relies on the system defined standart output
|
||||
* and input stream. */
|
||||
public SystemConsoleOutput() {
|
||||
this(System.out);
|
||||
}
|
||||
|
||||
/** @param out the output stream */
|
||||
public SystemConsoleOutput(final PrintStream out) {
|
||||
super();
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
/** @throws IOException if the stream was closed */
|
||||
private void checkOpen() throws IOException {
|
||||
if (closed) {
|
||||
throw new IOException();
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#close() */
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
closed = true;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#isClosed() */
|
||||
@Override
|
||||
public boolean isClosed() {
|
||||
return closed;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#print(java.lang.Object) */
|
||||
@Override
|
||||
public void print(final String object) throws IOException {
|
||||
checkOpen();
|
||||
out.print(object);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#println() */
|
||||
@Override
|
||||
public void println() throws IOException {
|
||||
checkOpen();
|
||||
out.println();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#println(java.lang.Object) */
|
||||
@Override
|
||||
public void println(final String object) throws IOException {
|
||||
checkOpen();
|
||||
out.println(object);
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.proc.InterruptionListener.java
|
||||
* Created on: May 10, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.proc;
|
||||
|
||||
/** A listener for interruption
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public interface InterruptionListener {
|
||||
/** Notification of an interuption of a listened object */
|
||||
void interrupted();
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.proc.ProcessList.java
|
||||
* Created on: May 10, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.proc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import fr.bigeon.gclc.command.Command;
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
|
||||
/** A command to list current processes
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class ProcessList extends Command {
|
||||
/** The process pool */
|
||||
private final TaskPool pool;
|
||||
|
||||
/** the interaction object */
|
||||
private final ConsoleManager manager;
|
||||
|
||||
/** @param name the command name
|
||||
* @param pool the pool
|
||||
* @param manager the console manager */
|
||||
public ProcessList(final String name, final TaskPool pool,
|
||||
final ConsoleManager manager) {
|
||||
super(name);
|
||||
this.pool = pool;
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */
|
||||
@Override
|
||||
public void execute(final String... args) throws CommandRunException {
|
||||
final ArrayList<String> pids = new ArrayList<>(pool.getPIDs());
|
||||
Collections.sort(pids);
|
||||
for (final String string : pids) {
|
||||
try {
|
||||
manager.println(MessageFormat.format("{0}\t{1}", string, //$NON-NLS-1$
|
||||
pool.get(string).getName()));
|
||||
} catch (final IOException e) {
|
||||
throw new CommandRunException(
|
||||
CommandRunExceptionType.INTERACTION,
|
||||
"Unable to communicate with user", e, this); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||
@SuppressWarnings("nls")
|
||||
@Override
|
||||
public String tip() {
|
||||
return "List all processes";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.proc.ThreadCommand.java
|
||||
* Created on: May 10, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.proc;
|
||||
|
||||
/** Tasks are named runnable that can be interrupted.
|
||||
* <p>
|
||||
* Good practice for those objects include an absence of interaction with the
|
||||
* user (otherwise the user may not know from which running command comes
|
||||
* information and requests) and unicity of the object to have a coherent
|
||||
* control through the {@link #setRunning(boolean)} method.
|
||||
* <p>
|
||||
* Typical cases where such command can be useful is for an application that
|
||||
* does long computations.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public interface Task extends Runnable {
|
||||
/** Add a listener for this command end of execution
|
||||
*
|
||||
* @param listener the listener */
|
||||
void addInterruptionListener(InterruptionListener listener);
|
||||
|
||||
/** Get the task name.
|
||||
*
|
||||
* @return the task name */
|
||||
String getName();
|
||||
|
||||
/** Test the running status of the task.
|
||||
*
|
||||
* @return if the command is supposed to be running */
|
||||
boolean isRunning();
|
||||
|
||||
/** Remove a listener of this command end of execution
|
||||
*
|
||||
* @param listener the listener */
|
||||
void rmInterruptionListener(InterruptionListener listener);
|
||||
|
||||
/** Set the running state.
|
||||
* <p>
|
||||
* This method should be only called by external objects with the false
|
||||
* argument. Calling this method with true has unspecified behavior and
|
||||
* could do nothing as well as restart the command for example.
|
||||
*
|
||||
* @param running the running state */
|
||||
void setRunning(boolean running);
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.proc.TaskPool.java
|
||||
* Created on: May 10, 2017
|
||||
*/
|
||||
package fr.bigeon.gclc.proc;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
||||
/** A process pool.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class TaskPool {
|
||||
/** The running processes. */
|
||||
private final Map<String, Task> running = new HashMap<>();
|
||||
/** The count for process id. */
|
||||
private int count = 0;
|
||||
/** The lock for pid attribution synchronization. */
|
||||
private final Object lock = new Object();
|
||||
|
||||
/** Default constructor. */
|
||||
public TaskPool() {
|
||||
//
|
||||
}
|
||||
|
||||
/** Add a process in the pool.
|
||||
*
|
||||
* @param cmd the process
|
||||
* @return the pid */
|
||||
public String add(final Task cmd) {
|
||||
if (cmd == null) {
|
||||
throw new IllegalArgumentException("Task cannot be null"); //$NON-NLS-1$
|
||||
}
|
||||
final String pid = getPID();
|
||||
synchronized (lock) {
|
||||
running.put(pid, cmd);
|
||||
}
|
||||
cmd.addInterruptionListener(new InterruptionListener() {
|
||||
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void interrupted() {
|
||||
synchronized (lock) {
|
||||
running.remove(pid);
|
||||
count = Math.min(count, Integer.parseInt(pid));
|
||||
}
|
||||
cmd.rmInterruptionListener(this);
|
||||
}
|
||||
});
|
||||
return pid;
|
||||
}
|
||||
|
||||
/** Get a process by it associated identifier.
|
||||
*
|
||||
* @param pid the task id
|
||||
* @return the task, if any, associated to this id */
|
||||
public Task get(final String pid) {
|
||||
synchronized (lock) {
|
||||
return running.get(pid);
|
||||
}
|
||||
}
|
||||
|
||||
/** Get the next process id.
|
||||
*
|
||||
* @return the process id */
|
||||
private String getPID() {
|
||||
synchronized (lock) {
|
||||
String pid;
|
||||
do {
|
||||
pid = Integer.toString(count++);
|
||||
} while (running.containsKey(pid));
|
||||
return pid;
|
||||
}
|
||||
}
|
||||
|
||||
/** Get the running processes' identifiers.
|
||||
*
|
||||
* @return the pids */
|
||||
public Collection<String> getPIDs() {
|
||||
return new HashSet<>(running.keySet());
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,8 @@ import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
import fr.bigeon.gclc.manager.ConsoleInput;
|
||||
import fr.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -101,32 +102,34 @@ public final class CLIPrompter {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
/** @param output the manager
|
||||
* @param choices the choices
|
||||
* @param cancel the cancel option if it exists
|
||||
* @return the number of choices plus one (or the number of choices if there
|
||||
* is a cancel)
|
||||
* @throws IOException if the manager was closed */
|
||||
private static <U> Integer listChoices(final ConsoleManager manager,
|
||||
private static <U> Integer listChoices(final ConsoleOutput output,
|
||||
final List<U> choices,
|
||||
final String cancel) throws IOException {
|
||||
int index = 0;
|
||||
for (final U u : choices) {
|
||||
manager.println(index++ + ") " + u); //$NON-NLS-1$
|
||||
output.println(index++ + ") " + u); //$NON-NLS-1$
|
||||
}
|
||||
if (cancel != null) {
|
||||
manager.println(index++ + ") " + cancel); //$NON-NLS-1$
|
||||
output.println(index++ + ") " + cancel); //$NON-NLS-1$
|
||||
}
|
||||
return Integer.valueOf(index - 1);
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param input the input
|
||||
* @param message the prompting message
|
||||
* @return the choice
|
||||
* @throws IOException if the manager was closed */
|
||||
public static boolean promptBoolean(final ConsoleManager manager,
|
||||
public static boolean promptBoolean(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final String message) throws IOException {
|
||||
String result = manager
|
||||
String result = input
|
||||
.prompt(message + CLIPrompterMessages.getString(BOOL_CHOICES));
|
||||
boolean first = true;
|
||||
final String choices = CLIPrompterMessages
|
||||
@@ -145,7 +148,7 @@ public final class CLIPrompter {
|
||||
|
||||
manager.println(CLIPrompterMessages
|
||||
.getString("promptbool.choices.invalid", choices)); //$NON-NLS-1$
|
||||
result = manager.prompt(
|
||||
result = input.prompt(
|
||||
message + CLIPrompterMessages.getString(BOOL_CHOICES));
|
||||
}
|
||||
first = false;
|
||||
@@ -157,6 +160,7 @@ public final class CLIPrompter {
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param input the input
|
||||
* @param keys the keys to be printed
|
||||
* @param choices the real choices
|
||||
* @param message the message
|
||||
@@ -165,12 +169,13 @@ public final class CLIPrompter {
|
||||
* @return the choice
|
||||
* @throws IOException if the manager was closed */
|
||||
@SuppressWarnings("boxing")
|
||||
public static <U> U promptChoice(final ConsoleManager manager,
|
||||
public static <U> U promptChoice(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final List<String> keys,
|
||||
final List<U> choices,
|
||||
final String message,
|
||||
final String cancel) throws IOException {
|
||||
final Integer index = promptChoice(manager, keys, message, cancel);
|
||||
final Integer index = promptChoice(manager, input, keys, message, cancel);
|
||||
if (index == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -178,6 +183,7 @@ public final class CLIPrompter {
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param input the input
|
||||
* @param <U> The choices labels type
|
||||
* @param <T> The real choices objects
|
||||
* @param choices the list of labels (in order to be displayed)
|
||||
@@ -186,12 +192,14 @@ public final class CLIPrompter {
|
||||
* @param cancel the cancel option if it exists (null otherwise)
|
||||
* @return the chosen object
|
||||
* @throws IOException if the manager was closed */
|
||||
public static <U, T> T promptChoice(final ConsoleManager manager,
|
||||
public static <U, T> T promptChoice(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final List<U> choices,
|
||||
final Map<U, T> choicesMap,
|
||||
final String message,
|
||||
final String cancel) throws IOException {
|
||||
final Integer res = promptChoice(manager, choices, message, cancel);
|
||||
final Integer res = promptChoice(manager, input, choices, message,
|
||||
cancel);
|
||||
if (res == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -199,13 +207,15 @@ public final class CLIPrompter {
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param input the input
|
||||
* @param <U> the type of choices
|
||||
* @param choices the list of choices
|
||||
* @param message the prompting message
|
||||
* @param cancel the cancel option, or null
|
||||
* @return the index of the choice
|
||||
* @throws IOException if the manager was closed */
|
||||
public static <U> Integer promptChoice(final ConsoleManager manager,
|
||||
public static <U> Integer promptChoice(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final List<U> choices,
|
||||
final String message,
|
||||
final String cancel) throws IOException {
|
||||
@@ -215,7 +225,7 @@ public final class CLIPrompter {
|
||||
boolean keepOn = true;
|
||||
int r = -1;
|
||||
while (keepOn) {
|
||||
result = manager.prompt(CLIPrompterMessages.getString(PROMPT));
|
||||
result = input.prompt(CLIPrompterMessages.getString(PROMPT));
|
||||
try {
|
||||
r = Integer.parseInt(result);
|
||||
if (r >= 0 && r <= index.intValue()) {
|
||||
@@ -240,6 +250,7 @@ public final class CLIPrompter {
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param input the input
|
||||
* @param <U> The choices labels type
|
||||
* @param <T> The real choices objects
|
||||
* @param choicesMap the map of label to actual objects
|
||||
@@ -247,11 +258,13 @@ public final class CLIPrompter {
|
||||
* @param cancel the cancel option (or null)
|
||||
* @return the chosen object
|
||||
* @throws IOException if the manager was closed */
|
||||
public static <U, T> T promptChoice(final ConsoleManager manager,
|
||||
public static <U, T> T promptChoice(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final Map<U, T> choicesMap,
|
||||
final String message,
|
||||
final String cancel) throws IOException {
|
||||
return promptChoice(manager, new ArrayList<>(choicesMap.keySet()),
|
||||
return promptChoice(manager, input,
|
||||
new ArrayList<>(choicesMap.keySet()),
|
||||
choicesMap, message, cancel);
|
||||
}
|
||||
|
||||
@@ -259,7 +272,7 @@ public final class CLIPrompter {
|
||||
* @param message the prompt message
|
||||
* @return the integer
|
||||
* @throws IOException if the manager was closed */
|
||||
public static int promptInteger(final ConsoleManager manager,
|
||||
public static int promptInteger(final ConsoleInput manager,
|
||||
final String message) throws IOException {
|
||||
boolean still = true;
|
||||
int r = 0;
|
||||
@@ -284,23 +297,27 @@ public final class CLIPrompter {
|
||||
/** This methods prompt the user for a list of elements
|
||||
*
|
||||
* @param manager the manager
|
||||
* @param input the input
|
||||
* @param message the message
|
||||
* @return the list of user inputs
|
||||
* @throws IOException if the manager was closed */
|
||||
public static List<String> promptList(final ConsoleManager manager,
|
||||
public static List<String> promptList(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final String message) throws IOException {
|
||||
return promptList(manager, message,
|
||||
return promptList(manager, input, message,
|
||||
CLIPrompterMessages.getString("promptlist.exit.defaultkey")); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/** This methods prompt the user for a list of elements
|
||||
*
|
||||
* @param manager the manager
|
||||
* @param input the input
|
||||
* @param message the message
|
||||
* @param ender the ending sequence for the list
|
||||
* @return the list of user inputs
|
||||
* @throws IOException if the manager was closed */
|
||||
public static List<String> promptList(final ConsoleManager manager,
|
||||
public static List<String> promptList(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final String message,
|
||||
final String ender) throws IOException {
|
||||
final List<String> strings = new ArrayList<>();
|
||||
@@ -308,7 +325,7 @@ public final class CLIPrompter {
|
||||
message + CLIPrompterMessages.getString(LIST_DISP_KEY, ender));
|
||||
String res = null;
|
||||
while (!ender.equals(res)) {
|
||||
res = manager.prompt(CLIPrompterMessages.getString(PROMPT));
|
||||
res = input.prompt(CLIPrompterMessages.getString(PROMPT));
|
||||
if (!res.equals(ender)) {
|
||||
strings.add(res);
|
||||
}
|
||||
@@ -319,23 +336,27 @@ public final class CLIPrompter {
|
||||
/** Prompt for a text with several lines.
|
||||
*
|
||||
* @param manager the manager
|
||||
* @param input the input
|
||||
* @param message the prompting message
|
||||
* @return the text
|
||||
* @throws IOException if the manager was closed */
|
||||
public static String promptLongText(final ConsoleManager manager,
|
||||
public static String promptLongText(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final String message) throws IOException {
|
||||
return promptLongText(manager, message, CLIPrompterMessages
|
||||
return promptLongText(manager, input, message, CLIPrompterMessages
|
||||
.getString("promptlongtext.exit.defaultkey")); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/** Prompt for a text with several lines.
|
||||
*
|
||||
* @param manager the manager
|
||||
* @param input the input
|
||||
* @param message the prompting message
|
||||
* @param ender the ender character
|
||||
* @return the text
|
||||
* @throws IOException if the manager was closed */
|
||||
public static String promptLongText(final ConsoleManager manager,
|
||||
public static String promptLongText(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final String message,
|
||||
final String ender) throws IOException {
|
||||
manager.println(message + CLIPrompterMessages
|
||||
@@ -343,7 +364,7 @@ public final class CLIPrompter {
|
||||
final StringBuilder res = new StringBuilder();
|
||||
String line;
|
||||
do {
|
||||
line = manager.prompt(CLIPrompterMessages.getString(PROMPT));
|
||||
line = input.prompt(CLIPrompterMessages.getString(PROMPT));
|
||||
if (!line.equals(ender)) {
|
||||
res.append(line);
|
||||
res.append(System.lineSeparator());
|
||||
@@ -353,17 +374,19 @@ public final class CLIPrompter {
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param input the input
|
||||
* @param keys the keys to be printed
|
||||
* @param choices the real choices
|
||||
* @param message the message
|
||||
* @param <U> the type of elements
|
||||
* @return the choice
|
||||
* @throws IOException if the manager was closed */
|
||||
public static <U> List<U> promptMultiChoice(final ConsoleManager manager,
|
||||
public static <U> List<U> promptMultiChoice(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final List<String> keys,
|
||||
final List<U> choices,
|
||||
final String message) throws IOException {
|
||||
final List<Integer> indices = promptMultiChoice(manager, keys, message);
|
||||
final List<Integer> indices = promptMultiChoice(manager, input, keys, message);
|
||||
final List<U> userChoices = new ArrayList<>();
|
||||
for (final Integer integer : indices) {
|
||||
userChoices.add(choices.get(integer.intValue()));
|
||||
@@ -372,6 +395,7 @@ public final class CLIPrompter {
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param input the input
|
||||
* @param <U> The choices labels type
|
||||
* @param <T> The real choices objects
|
||||
* @param choices the list of labels (in order to be displayed)
|
||||
@@ -379,11 +403,13 @@ public final class CLIPrompter {
|
||||
* @param message the prompting message
|
||||
* @return the chosen objects (or an empty list)
|
||||
* @throws IOException if the manager was closed */
|
||||
public static <U, T> List<T> promptMultiChoice(final ConsoleManager manager,
|
||||
public static <U, T> List<T> promptMultiChoice(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final List<U> choices,
|
||||
final Map<U, T> choicesMap,
|
||||
final String message) throws IOException {
|
||||
final List<Integer> chs = promptMultiChoice(manager, choices, message);
|
||||
final List<Integer> chs = promptMultiChoice(manager, input, choices,
|
||||
message);
|
||||
final List<T> userChoices = new ArrayList<>();
|
||||
for (final Integer integer : chs) {
|
||||
userChoices.add(choicesMap.get(choices.get(integer.intValue())));
|
||||
@@ -392,12 +418,14 @@ public final class CLIPrompter {
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param input the input
|
||||
* @param <U> the type of choices
|
||||
* @param choices the list of choices
|
||||
* @param message the prompting message
|
||||
* @return the indices of the choices
|
||||
* @throws IOException if the manager was closed */
|
||||
public static <U> List<Integer> promptMultiChoice(final ConsoleManager manager,
|
||||
public static <U> List<Integer> promptMultiChoice(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final List<U> choices,
|
||||
final String message) throws IOException {
|
||||
manager.println(message);
|
||||
@@ -407,7 +435,7 @@ public final class CLIPrompter {
|
||||
final List<Integer> chs = new ArrayList<>();
|
||||
while (keepOn) {
|
||||
keepOn = false;
|
||||
result = manager.prompt(CLIPrompterMessages.getString(PROMPT));
|
||||
result = input.prompt(CLIPrompterMessages.getString(PROMPT));
|
||||
final String[] vals = result
|
||||
.split(CLIPrompterMessages.getString(LIST_CHOICE_SEP));
|
||||
for (final String val : vals) {
|
||||
@@ -435,16 +463,19 @@ public final class CLIPrompter {
|
||||
}
|
||||
|
||||
/** @param manager the manager
|
||||
* @param input the input
|
||||
* @param <U> The choices labels type
|
||||
* @param <T> The real choices objects
|
||||
* @param choicesMap the map of label to actual objects
|
||||
* @param message the prompting message
|
||||
* @return the chosen objects
|
||||
* @throws IOException if the manager was closed */
|
||||
public static <U, T> List<T> promptMultiChoice(final ConsoleManager manager,
|
||||
public static <U, T> List<T> promptMultiChoice(final ConsoleOutput manager,
|
||||
final ConsoleInput input,
|
||||
final Map<U, T> choicesMap,
|
||||
final String message) throws IOException {
|
||||
return promptMultiChoice(manager, new ArrayList<>(choicesMap.keySet()),
|
||||
return promptMultiChoice(manager, input,
|
||||
new ArrayList<>(choicesMap.keySet()),
|
||||
choicesMap, message);
|
||||
}
|
||||
|
||||
@@ -453,7 +484,7 @@ public final class CLIPrompter {
|
||||
* @param reprompt the prompting message after empty input
|
||||
* @return the non empty input
|
||||
* @throws IOException if the manager was closed */
|
||||
public static String promptNonEmpty(final ConsoleManager manager,
|
||||
public static String promptNonEmpty(final ConsoleInput manager,
|
||||
final String prompt,
|
||||
final String reprompt) throws IOException {
|
||||
String res = manager.prompt(prompt);
|
||||
|
||||
@@ -42,7 +42,7 @@ import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||
import fr.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
|
||||
/** An incomplete implematation used to forward messages from a piped console.
|
||||
* <p>
|
||||
@@ -57,14 +57,14 @@ public abstract class AOutputForwardRunnable implements Runnable {
|
||||
/** The default timeout (one tenth of second). */
|
||||
private static final long DEFAULT_TIMEOUT = 100;
|
||||
/** The manager. */
|
||||
private final PipedConsoleManager manager;
|
||||
private final PipedConsoleOutput manager;
|
||||
/** The timeout */
|
||||
private final long timeout;
|
||||
|
||||
/** Create a forwarding runnable.
|
||||
*
|
||||
* @param manager the manager */
|
||||
public AOutputForwardRunnable(final PipedConsoleManager manager) {
|
||||
public AOutputForwardRunnable(final PipedConsoleOutput manager) {
|
||||
super();
|
||||
this.manager = manager;
|
||||
timeout = DEFAULT_TIMEOUT;
|
||||
@@ -79,11 +79,12 @@ public abstract class AOutputForwardRunnable implements Runnable {
|
||||
* likely to depend on the application and the use of it.
|
||||
* <p>
|
||||
* If you do not know what timeout length to use, please use the
|
||||
* {@link #AOutputForwardRunnable(PipedConsoleManager)} constructor.
|
||||
* {@link #AOutputForwardRunnable(PipedConsoleOutput)} constructor.
|
||||
*
|
||||
* @param manager the manager
|
||||
* @param timeout the timeout between message requests. */
|
||||
public AOutputForwardRunnable(final PipedConsoleManager manager, final long timeout) {
|
||||
public AOutputForwardRunnable(final PipedConsoleOutput manager,
|
||||
final long timeout) {
|
||||
super();
|
||||
this.manager = manager;
|
||||
this.timeout = timeout;
|
||||
|
||||
Reference in New Issue
Block a user