Added prompting interruption, add exception in parameterized commands
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
@@ -37,6 +37,7 @@
|
||||
package fr.bigeon.gclc;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -106,18 +107,13 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
return root.add(cmd);
|
||||
}
|
||||
|
||||
/** Add a command request listener.
|
||||
* <p>
|
||||
* A listener can listen several times to the same application.
|
||||
*
|
||||
* @param listener the listener to add. */
|
||||
/** @param listener the command listener */
|
||||
public final void addListener(CommandRequestListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see
|
||||
* fr.bigeon.gclc.command.ICommandProvider#executeSub(java.lang.String,
|
||||
* @see fr.bigeon.gclc.command.ICommandProvider#executeSub(java.lang.String,
|
||||
* java.lang.String[]) */
|
||||
@Override
|
||||
public final void executeSub(String command,
|
||||
@@ -125,10 +121,11 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
root.executeSub(command, args);
|
||||
}
|
||||
|
||||
/** Exit this running application before next command prompt */
|
||||
/** Signify to the application that no command should be inputed anymore */
|
||||
public final void exit() {
|
||||
LOGGER.fine("Request exiting application..."); //$NON-NLS-1$
|
||||
running = false;
|
||||
manager.interruptPrompt();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -143,8 +140,8 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
return manager;
|
||||
}
|
||||
|
||||
/** @param cmd the command to interpret
|
||||
* @throws IOException if the manager was closed */
|
||||
/** @param cmd the command
|
||||
* @throws IOException if the command could not be parsed */
|
||||
public final void interpretCommand(String cmd) throws IOException {
|
||||
List<String> args;
|
||||
try {
|
||||
@@ -170,7 +167,7 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/** @param listener the listener to remove (once) */
|
||||
/** @param listener the command listener to remove */
|
||||
public final void removeListener(CommandRequestListener listener) {
|
||||
for (int i = 0; i < listeners.size(); i++) {
|
||||
if (listeners.get(i) == listener) {
|
||||
@@ -180,14 +177,23 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/** Launches the prompting application */
|
||||
/** Start the application */
|
||||
public final void start() {
|
||||
try {
|
||||
running = true;
|
||||
if (header != null) {
|
||||
manager.println(header);
|
||||
}
|
||||
do {
|
||||
} catch (IOException e) {
|
||||
// The manager was closed
|
||||
running = false;
|
||||
LOGGER.log(Level.WARNING,
|
||||
"The console manager was closed. Closing the application as no one can reach it.", //$NON-NLS-1$
|
||||
e);
|
||||
return;
|
||||
}
|
||||
do {
|
||||
try {
|
||||
final String cmd = manager.prompt();
|
||||
if (cmd == null || cmd.isEmpty()) {
|
||||
continue;
|
||||
@@ -196,22 +202,34 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
listener.commandRequest(cmd);
|
||||
}
|
||||
interpretCommand(cmd);
|
||||
} while (running);
|
||||
if (footer != null) {
|
||||
manager.println(footer);
|
||||
} catch (InterruptedIOException e) {
|
||||
LOGGER.log(Level.INFO,
|
||||
"Prompt interrupted. It is likely the application is closing.", //$NON-NLS-1$
|
||||
e);
|
||||
} catch (IOException e) {
|
||||
// The manager was closed
|
||||
running = false;
|
||||
LOGGER.log(Level.WARNING,
|
||||
"The console manager was closed. Closing the application as no one can reach it.", //$NON-NLS-1$
|
||||
e);
|
||||
}
|
||||
} while (running);
|
||||
if (footer != null) {
|
||||
try {
|
||||
manager.println(footer);
|
||||
} catch (IOException e) {
|
||||
// The manager was closed
|
||||
running = false;
|
||||
LOGGER.log(Level.WARNING,
|
||||
"The console manager was closed.", //$NON-NLS-1$
|
||||
e);
|
||||
}
|
||||
running = false;
|
||||
LOGGER.fine("Exiting application."); //$NON-NLS-1$
|
||||
} catch (IOException e) {
|
||||
// The manager was closed
|
||||
running = false;
|
||||
LOGGER.log(Level.WARNING,
|
||||
"The console manager was closed. Closing the application as no one can reach it.", //$NON-NLS-1$
|
||||
e);
|
||||
}
|
||||
running = false;
|
||||
LOGGER.fine("Exiting application."); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/** @return if the application is running */
|
||||
/** @return the running status */
|
||||
public boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
|
||||
@@ -154,8 +154,9 @@ public abstract class ParametrizedCommand extends Command {
|
||||
}
|
||||
}
|
||||
|
||||
/** @param parameters the command parameters */
|
||||
protected abstract void doExecute(CommandParameters parameters);
|
||||
/** @param parameters the command parameters
|
||||
* @throws CommandRunException if the command failed */
|
||||
protected abstract void doExecute(CommandParameters parameters) throws CommandRunException;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#execute(java.lang.String[]) */
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
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.
|
||||
@@ -69,15 +70,15 @@ public interface ConsoleManager {
|
||||
void println(String message) throws IOException;
|
||||
|
||||
/** @return the user inputed string
|
||||
* @throws IOException if the manager is closed or could not read the
|
||||
* prompt */
|
||||
String prompt() throws IOException;
|
||||
* @throws IOException if the manager is closed or could not read the prompt
|
||||
* @throws InterruptedIOException if the prompt was interrupted */
|
||||
String prompt() throws IOException, InterruptedIOException;
|
||||
|
||||
/** @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 */
|
||||
String prompt(String message) throws IOException;
|
||||
* @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, InterruptedIOException;
|
||||
|
||||
/** <p>
|
||||
* Set a prompting prefix.
|
||||
@@ -92,4 +93,7 @@ public interface ConsoleManager {
|
||||
|
||||
/** @return if the manager is closed. */
|
||||
boolean isClosed();
|
||||
|
||||
/** Indicate to the manager that is should interrompt the prompting */
|
||||
void interruptPrompt();
|
||||
}
|
||||
|
||||
54
gclc/src/main/java/fr/bigeon/gclc/manager/ReadRunnable.java
Normal file
54
gclc/src/main/java/fr/bigeon/gclc/manager/ReadRunnable.java
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.manager.ReadRunnable.java
|
||||
* Created on: Nov 21, 2016
|
||||
*/
|
||||
package fr.bigeon.gclc.manager;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class ReadRunnable implements Runnable {
|
||||
|
||||
/** The logger */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(ReadRunnable.class.getName());
|
||||
/** The result */
|
||||
private String result = "";
|
||||
/** The input buffer */
|
||||
private final BufferedReader in;
|
||||
|
||||
/** @param in the input buffer */
|
||||
protected ReadRunnable(BufferedReader in) {
|
||||
super();
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Runnable#run() */
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
result = in.readLine();
|
||||
while (result != null && result.length() > 0 &&
|
||||
result.charAt(0) == 0) {
|
||||
result = result.substring(1);
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Unable to read prompt", e); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
/** @return the result */
|
||||
public String getResult() {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,6 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/** A console using the input stream and print stream.
|
||||
@@ -75,6 +74,8 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||
/** If the manager is closed */
|
||||
private boolean closed = false;
|
||||
|
||||
private Thread reading;
|
||||
|
||||
/** This default constructor relies on the system defined standart output
|
||||
* and input stream. */
|
||||
public SystemConsoleManager() {
|
||||
@@ -140,18 +141,24 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||
@Override
|
||||
public String prompt(String message) throws IOException {
|
||||
checkOpen();
|
||||
String result = EMPTY;
|
||||
out.print(message);
|
||||
try {
|
||||
// ReadRunnable rr = new ReadRunnable(in);
|
||||
// reading = new Thread(rr, "prompt"); //$NON-NLS-1$
|
||||
// reading.start();
|
||||
// try {
|
||||
// reading.join();
|
||||
// } catch (final InterruptedException e) {
|
||||
// LOGGER.log(Level.SEVERE, "Prompt reading interrupted", e); //$NON-NLS-1$
|
||||
// throw new InterruptedIOException("Prompt interruption"); //$NON-NLS-1$
|
||||
// }
|
||||
String result = EMPTY;
|
||||
result = in.readLine();
|
||||
while (result != null && result.length() > 0 &&
|
||||
result.charAt(0) == 0) {
|
||||
result = result.substring(1);
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Unable to read prompt", e); //$NON-NLS-1$
|
||||
}
|
||||
return result;
|
||||
// return rr.getResult();
|
||||
}
|
||||
|
||||
/** @param prompt the prompt to set */
|
||||
@@ -174,4 +181,13 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||
return closed;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
|
||||
@Override
|
||||
public void interruptPrompt() {
|
||||
if (reading != null) {
|
||||
reading.interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user