Reformatting

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2018-10-14 23:07:41 -04:00
parent c23af20b17
commit 93589c750e
39 changed files with 689 additions and 742 deletions

View File

@ -72,10 +72,7 @@ import java.util.Arrays;
import net.bigeon.gclc.command.ICommand; import net.bigeon.gclc.command.ICommand;
import net.bigeon.gclc.exception.CommandRunException; import net.bigeon.gclc.exception.CommandRunException;
/** /** @author Emmanuel Bigeon */
* @author Emmanuel Bigeon
*
*/
public class ForkCommandTask extends ForkTask { public class ForkCommandTask extends ForkTask {
private final ICommand command; private final ICommand command;
private final String[] args; private final String[] args;
@ -89,13 +86,6 @@ public class ForkCommandTask extends ForkTask {
this.args = Arrays.copyOf(args, args.length); this.args = Arrays.copyOf(args, args.length);
} }
/* (non-Javadoc)
* @see net.bigeon.gclc.process.Task#getName() */
@Override
public String getName() {
return command.getCommandName();
}
/* (non-Javadoc) /* (non-Javadoc)
* @see net.bigeon.gclc.process.ForkTask#doRun() */ * @see net.bigeon.gclc.process.ForkTask#doRun() */
@Override @Override
@ -103,4 +93,11 @@ public class ForkCommandTask extends ForkTask {
command.execute(out, in, args); command.execute(out, in, args);
} }
/* (non-Javadoc)
* @see net.bigeon.gclc.process.Task#getName() */
@Override
public String getName() {
return command.getCommandName();
}
} }

View File

@ -105,6 +105,20 @@ public abstract class ForkTask implements Task {
listeners.add(listener); listeners.add(listener);
} }
/** Actually run the fork. */
protected abstract void doRun() throws CommandRunException;
/** Get the excepion that caused a failure.
*
* @return the exception */
public final CommandRunException getException() {
return exception;
}
protected final Object getRunningLock() {
return runLock;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see net.bigeon.gclc.process.Task#isRunning() */ * @see net.bigeon.gclc.process.Task#isRunning() */
@Override @Override
@ -162,16 +176,6 @@ public abstract class ForkTask implements Task {
} }
} }
/** Actually run the fork. */
protected abstract void doRun() throws CommandRunException;
/** Get the excepion that caused a failure.
*
* @return the exception */
public final CommandRunException getException() {
return exception;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see net.bigeon.gclc.process.Task#setRunning(boolean) */ * @see net.bigeon.gclc.process.Task#setRunning(boolean) */
@Override @Override
@ -181,8 +185,4 @@ public abstract class ForkTask implements Task {
runLock.notifyAll(); runLock.notifyAll();
} }
} }
protected final Object getRunningLock() {
return runLock;
}
} }

View File

@ -71,10 +71,7 @@ import net.bigeon.gclc.ApplicationAttachement;
import net.bigeon.gclc.command.ICommandProvider; import net.bigeon.gclc.command.ICommandProvider;
import net.bigeon.gclc.exception.InvalidCommandName; import net.bigeon.gclc.exception.InvalidCommandName;
/** /** @author Emmanuel Bigeon */
* @author Emmanuel Bigeon
*
*/
public class ProcessAttachement implements ApplicationAttachement { public class ProcessAttachement implements ApplicationAttachement {
private final TaskPool pool; private final TaskPool pool;
@ -86,8 +83,8 @@ public class ProcessAttachement implements ApplicationAttachement {
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.ApplicationAttachement#attach(fr.bigeon.gclc.ConsoleApplication) * @see fr.bigeon.gclc.ApplicationAttachement#attach(fr.bigeon.gclc.
*/ * ConsoleApplication) */
@Override @Override
public void attach(ICommandProvider application) throws InvalidCommandName { public void attach(ICommandProvider application) throws InvalidCommandName {
application.add(new ProcessKill("kill", pool)); application.add(new ProcessKill("kill", pool));

View File

@ -89,8 +89,7 @@ public final class ProcessClear extends Command {
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager. * @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, * ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
* java.lang.String[]) */
@Override @Override
public void execute(final ConsoleOutput out, final ConsoleInput in, public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException { final String... args) throws CommandRunException {

View File

@ -89,8 +89,7 @@ public final class ProcessKill extends Command {
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager. * @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, * ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
* java.lang.String[]) */
@Override @Override
public void execute(final ConsoleOutput out, final ConsoleInput in, public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException { final String... args) throws CommandRunException {

View File

@ -95,8 +95,7 @@ public final class ProcessList extends Command {
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager. * @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, * ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
* java.lang.String[]) */
@Override @Override
public void execute(final ConsoleOutput out, final ConsoleInput in, public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException { final String... args) throws CommandRunException {
@ -107,8 +106,7 @@ public final class ProcessList extends Command {
out.println(MessageFormat.format("{0}\t{1}", string, //$NON-NLS-1$ out.println(MessageFormat.format("{0}\t{1}", string, //$NON-NLS-1$
pool.get(string).getName())); pool.get(string).getName()));
} catch (final IOException e) { } catch (final IOException e) {
throw new CommandRunException( throw new CommandRunException(CommandRunExceptionType.INTERACTION,
CommandRunExceptionType.INTERACTION,
"Unable to communicate with user", e); //$NON-NLS-1$ "Unable to communicate with user", e); //$NON-NLS-1$
} }
} }

View File

@ -103,8 +103,8 @@ public interface Task extends Runnable {
/** Set the running state. /** Set the running state.
* <p> * <p>
* This method should be only called by external objects with the false * This method should be only called by external objects with the false
* argument. Calling this method with true has unspecified behavior and * argument. Calling this method with true has unspecified behavior and could do
* could do nothing as well as restart the command for example. * nothing as well as restart the command for example.
* *
* @param running the running state */ * @param running the running state */
void setRunning(boolean running); void setRunning(boolean running);

View File

@ -95,16 +95,6 @@ public final class TaskPool {
this.autoClear = autoClear; this.autoClear = autoClear;
} }
/** Remove a task from the pool
*
* @param pid the task id */
public void remove(String pid) {
synchronized (lock) {
running.remove(pid);
count = Math.min(count, Integer.parseInt(pid));
}
}
/** Add a process in the pool. /** Add a process in the pool.
* *
* @param cmd the process * @param cmd the process
@ -168,6 +158,16 @@ public final class TaskPool {
return autoClear; return autoClear;
} }
/** Remove a task from the pool
*
* @param pid the task id */
public void remove(String pid) {
synchronized (lock) {
running.remove(pid);
count = Math.min(count, Integer.parseInt(pid));
}
}
/** Request all task to stop running. /** Request all task to stop running.
* <p> * <p>
* This call does not guaranty end of execution, it is up to the task * This call does not guaranty end of execution, it is up to the task

View File

@ -97,13 +97,12 @@ public abstract class TaskSpawner extends Command {
* @param args the arguments * @param args the arguments
* @return the process to start and add to the pool * @return the process to start and add to the pool
* @throws CommandRunException if the task creation failed */ * @throws CommandRunException if the task creation failed */
protected abstract Task createTask(ConsoleOutput out, ConsoleInput in, protected abstract Task createTask(ConsoleOutput out, ConsoleInput in, String... args)
String... args) throws CommandRunException; throws CommandRunException;
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager. * @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, * ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
* java.lang.String[]) */
@Override @Override
public final void execute(final ConsoleOutput out, final ConsoleInput in, public final void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException { final String... args) throws CommandRunException {

View File

@ -94,6 +94,26 @@ public final class ConnectingConsoleInput implements ConsoleInput {
close = true; close = true;
} }
public void connect(ConsoleInput input) {
disconnect();
synchronized (promptLock) {
connected = input;
promptLock.notifyAll();
}
}
public void disconnect() {
synchronized (connectionLock) {
if (connected != null) {
disconnection = true;
synchronized (promptLock) {
connected.interruptPrompt();
}
connected = null;
}
}
}
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#getPrompt() */ * @see fr.bigeon.gclc.manager.ConsoleInput#getPrompt() */
@Override @Override
@ -128,14 +148,6 @@ public final class ConnectingConsoleInput implements ConsoleInput {
return prompt(prompt.apply()); return prompt(prompt.apply());
} }
/* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(fr.bigeon.gclc.tools.
* StringProvider) */
@Override
public void setPrompt(StringProvider string) {
prompt = string;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(long) */ * @see fr.bigeon.gclc.manager.ConsoleInput#prompt(long) */
@Override @Override
@ -222,23 +234,11 @@ public final class ConnectingConsoleInput implements ConsoleInput {
}; };
} }
public void connect(ConsoleInput input) { /* (non-Javadoc)
disconnect(); * @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(fr.bigeon.gclc.tools.
synchronized (promptLock) { * StringProvider) */
connected = input; @Override
promptLock.notifyAll(); public void setPrompt(StringProvider string) {
} prompt = string;
}
public void disconnect() {
synchronized (connectionLock) {
if (connected != null) {
disconnection = true;
synchronized (promptLock) {
connected.interruptPrompt();
}
connected = null;
}
}
} }
} }

View File

@ -130,6 +130,25 @@ public class ConnectingConsoleOutput implements ConsoleOutput {
close = true; close = true;
} }
public synchronized void connect(ConsoleOutput output) {
this.output = output;
for (final String string : messages) {
try {
output.print(string);
} catch (final IOException e) {
LOGGER.severe("nable to print to connecting console");
LOGGER.log(Level.FINE, "Console error", e);
}
}
if (!persistent) {
messages.clear();
}
}
public synchronized void disconnect() {
output = null;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleOutput#isClosed() */ * @see fr.bigeon.gclc.manager.ConsoleOutput#isClosed() */
@Override @Override
@ -157,23 +176,4 @@ public class ConnectingConsoleOutput implements ConsoleOutput {
public void println(String message) { public void println(String message) {
addMessage(message + System.lineSeparator()); addMessage(message + System.lineSeparator());
} }
public synchronized void connect(ConsoleOutput output) {
this.output = output;
for (final String string : messages) {
try {
output.print(string);
} catch (final IOException e) {
LOGGER.severe("nable to print to connecting console");
LOGGER.log(Level.FINE, "Console error", e);
}
}
if (!persistent) {
messages.clear();
}
}
public synchronized void disconnect() {
output = null;
}
} }

View File

@ -61,12 +61,13 @@ public final class DConnexionManager<T> implements ConnexionManager<T> {
/** The lock for modification of {@link #counters}. */ /** The lock for modification of {@link #counters}. */
private final Object counterLock = new Object(); private final Object counterLock = new Object();
/** The count of connexions. */ /** The count of connexions. */
private int count = 0; private int count;
/** Default.constructor. */ /** Default.constructor. */
public DConnexionManager() { public DConnexionManager() {
// //
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see * @see
* net.bigeon.gclc.socket.ConnexionManager#addConnexion(java.lang.Object) */ * net.bigeon.gclc.socket.ConnexionManager#addConnexion(java.lang.Object) */
@ -80,11 +81,12 @@ public final class DConnexionManager<T> implements ConnexionManager<T> {
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see * @see net.bigeon.gclc.socket.ConnexionManager#disconnect(java.lang.String) */
* net.bigeon.gclc.socket.ConnexionManager#disconnect(java.lang.String) */
@Override @Override
public T disconnect(final String id) { public T disconnect(final String id) {
if (connecteds.containsKey(id)) { if (!connecteds.containsKey(id)) {
return null;
}
final T disc = connecteds.remove(id); final T disc = connecteds.remove(id);
final Object lock = locks.get(id); final Object lock = locks.get(id);
synchronized (lock) { synchronized (lock) {
@ -102,8 +104,6 @@ public final class DConnexionManager<T> implements ConnexionManager<T> {
} }
return disc; return disc;
} }
return null;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see net.bigeon.gclc.socket.ConnexionManager#get(java.lang.String) */ * @see net.bigeon.gclc.socket.ConnexionManager#get(java.lang.String) */
@ -120,8 +120,7 @@ public final class DConnexionManager<T> implements ConnexionManager<T> {
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see * @see net.bigeon.gclc.socket.ConnexionManager#isConnected(java.lang.String) */
* net.bigeon.gclc.socket.ConnexionManager#isConnected(java.lang.String) */
@Override @Override
public boolean isConnected(final String id) { public boolean isConnected(final String id) {
return connecteds.containsKey(id); return connecteds.containsKey(id);
@ -154,8 +153,8 @@ public final class DConnexionManager<T> implements ConnexionManager<T> {
@Override @Override
public void releaseDisconnexionLock(final String id) { public void releaseDisconnexionLock(final String id) {
synchronized (counterLock) { synchronized (counterLock) {
counters.put(id, Integer counters.put(id,
.valueOf(Math.max(counters.get(id).intValue() - 1, 0))); Integer.valueOf(Math.max(counters.get(id).intValue() - 1, 0)));
counterLock.notifyAll(); counterLock.notifyAll();
} }
} }

View File

@ -100,18 +100,16 @@ public final class PluggableConsoleInput implements ConsoleInput {
closed = true; closed = true;
} }
/** Connect the given input stream to the input and output to the hints /** Connect the given input stream to the input and output to the hints writing.
* writing.
* *
* @param stream the input stream * @param stream the input stream
* @param out the output for hints. * @param out the output for hints.
* @throws IOException if the input is already connected. */ * @throws IOException if the input is already connected. */
public void connect(final InputStream stream, public void connect(final InputStream stream, final PrintStream out)
final PrintStream out) throws IOException { throws IOException {
synchronized (connexionLock) { synchronized (connexionLock) {
if (connected) { if (connected) {
throw new IOException( throw new IOException("Input already connected to an input stream"); //$NON-NLS-1$
"Input already connected to an input stream"); //$NON-NLS-1$
} }
output = out; output = out;
@ -120,8 +118,8 @@ public final class PluggableConsoleInput implements ConsoleInput {
out.flush(); out.flush();
} }
final InputStreamReader streamReader = new InputStreamReader( final InputStreamReader streamReader = new InputStreamReader(stream,
stream, StandardCharsets.UTF_8); StandardCharsets.UTF_8);
final BufferedReader reader = new BufferedReader(streamReader); final BufferedReader reader = new BufferedReader(streamReader);
connexion = new ReadingRunnable(reader); connexion = new ReadingRunnable(reader);
final Thread th = new Thread(connexion, "GCLC Socket - Read input"); //$NON-NLS-1$ final Thread th = new Thread(connexion, "GCLC Socket - Read input"); //$NON-NLS-1$
@ -218,11 +216,9 @@ public final class PluggableConsoleInput implements ConsoleInput {
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String, * @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String, long) */
* long) */
@Override @Override
public String prompt(final String message, public String prompt(final String message, final long timeout) throws IOException {
final long timeout) throws IOException {
if (closed) { if (closed) {
throw new IOException(); throw new IOException();
} }
@ -240,8 +236,7 @@ public final class PluggableConsoleInput implements ConsoleInput {
long time = System.currentTimeMillis() - tic; long time = System.currentTimeMillis() - tic;
while (res == null && !interrupted && time < timeout) { while (res == null && !interrupted && time < timeout) {
try { try {
res = waitMessageOrConnexion(timeout - time, res = waitMessageOrConnexion(timeout - time, (timeout - time) / TENTH);
(timeout - time) / TENTH);
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
LOGGER.log(Level.FINE, "Interruption of thread", e); //$NON-NLS-1$ LOGGER.log(Level.FINE, "Interruption of thread", e); //$NON-NLS-1$
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
@ -255,26 +250,25 @@ public final class PluggableConsoleInput implements ConsoleInput {
return res; return res;
} }
@Override
public void setPrompt(final StringProvider prompt) {
this.prompt = prompt;
}
@Override @Override
public void setPrompt(String prompt) { public void setPrompt(String prompt) {
setPrompt(new ConstantString(prompt)); setPrompt(new ConstantString(prompt));
} }
/** Wait for a hint or connexion. @Override
public void setPrompt(final StringProvider prompt) {
this.prompt = prompt;
}
/** Wait for a hint or connection.
* *
* @param messageTimeout the timeout on the current connexion hint waiting * @param messageTimeout the timeout on the current connection hint waiting
* @param connexionTimeout the timeout on the new connexion wait * @param connexionTimeout the timeout on the new connection wait
* @return the hint, or null if not connected or timed out. * @return the hint, or null if not connected or timed out.
* @throws IOException if the reading failed. * @throws IOException if the reading failed.
* @throws InterruptedException if the wait was interrupted */ * @throws InterruptedException if the wait was interrupted */
private String waitMessageOrConnexion(final long messageTimeout, private String waitMessageOrConnexion(final long messageTimeout,
final long connexionTimeout) throws IOException, final long connexionTimeout) throws IOException, InterruptedException {
InterruptedException {
synchronized (connexionLock) { synchronized (connexionLock) {
if (connected) { if (connected) {
return connexion.getNextMessage(messageTimeout); return connexion.getNextMessage(messageTimeout);

View File

@ -60,6 +60,7 @@ public final class PluggableConsoleOutput implements ConsoleOutput {
public PluggableConsoleOutput() { public PluggableConsoleOutput() {
// //
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see java.lang.AutoCloseable#close() */ * @see java.lang.AutoCloseable#close() */
@Override @Override

View File

@ -65,8 +65,8 @@ public final class RemoteDisconnectCommand<T> extends Command {
* @param manager the manager * @param manager the manager
* @param all if all elements should be disconnected when no argument is * @param all if all elements should be disconnected when no argument is
* provided */ * provided */
public RemoteDisconnectCommand(final String name, public RemoteDisconnectCommand(final String name, final ConnexionManager<T> manager,
final ConnexionManager<T> manager, final boolean all) { final boolean all) {
super(name); super(name);
this.manager = manager; this.manager = manager;
this.all = all; this.all = all;
@ -74,8 +74,7 @@ public final class RemoteDisconnectCommand<T> extends Command {
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager. * @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, * ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
* java.lang.String[]) */
@Override @Override
public void execute(final ConsoleOutput out, final ConsoleInput in, public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException { final String... args) throws CommandRunException {
@ -89,8 +88,8 @@ public final class RemoteDisconnectCommand<T> extends Command {
if (manager.isConnected(string)) { if (manager.isConnected(string)) {
manager.disconnect(string); manager.disconnect(string);
} else { } else {
print(out, MessageFormat print(out,
.format("[WARNING] {0} is not connected", string)); //$NON-NLS-1$ MessageFormat.format("[WARNING] {0} is not connected", string)); //$NON-NLS-1$
} }
} }
} }
@ -99,16 +98,14 @@ public final class RemoteDisconnectCommand<T> extends Command {
* *
* @param out the output * @param out the output
* @param string the message * @param string the message
* @throws CommandRunException if the output exists but cannot be printed * @throws CommandRunException if the output exists but cannot be printed to */
* to */ private static void print(final ConsoleOutput out, final String string)
private void print(final ConsoleOutput out, throws CommandRunException {
final String string) throws CommandRunException {
if (out != null) { if (out != null) {
try { try {
out.println(string); out.println(string);
} catch (final IOException e) { } catch (final IOException e) {
throw new CommandRunException( throw new CommandRunException(CommandRunExceptionType.INTERACTION,
CommandRunExceptionType.INTERACTION,
"Unable to print to existing output", e); //$NON-NLS-1$ "Unable to print to existing output", e); //$NON-NLS-1$
} }
} }
@ -123,12 +120,11 @@ public final class RemoteDisconnectCommand<T> extends Command {
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#usageDetail() */ * @see fr.bigeon.gclc.command.Command#usageDetail() */
@SuppressWarnings("nls")
@Override @Override
protected String usageDetail() { protected String usageDetail() {
return MessageFormat.format( return MessageFormat.format(
" If arguments are provided the corresponding connexions are closed, " + " If arguments are provided the corresponding connexions are closed, "
"otherwise{0}{1} are.", + "otherwise{0}{1} are.",
System.lineSeparator(), all ? "all connexions" : "none"); System.lineSeparator(), all ? "all connexions" : "none");
} }
} }

View File

@ -82,13 +82,12 @@ public final class SocketConsoleApplicationShell implements Runnable {
/** THe server address. */ /** THe server address. */
private final InetAddress addr; private final InetAddress addr;
/** Create a socket application shell which will listen on the given port /** Create a socket application shell which will listen on the given port and
* and network interface. * network interface.
* *
* @param port the part * @param port the part
* @param addr the inet address */ * @param addr the inet address */
public SocketConsoleApplicationShell(final int port, public SocketConsoleApplicationShell(final int port, final InetAddress addr) {
final InetAddress addr) {
super(); super();
this.port = port; this.port = port;
this.addr = addr; this.addr = addr;
@ -110,9 +109,9 @@ public final class SocketConsoleApplicationShell implements Runnable {
} }
/** If the port provided was 0, this allows to get the actual port. /** If the port provided was 0, this allows to get the actual port.
*
* @return the local port * @return the local port
* @see java.net.ServerSocket#getLocalPort() * @see java.net.ServerSocket#getLocalPort() */
*/
public int getLocalPort() { public int getLocalPort() {
return serverSocket.getLocalPort(); return serverSocket.getLocalPort();
} }
@ -122,16 +121,14 @@ public final class SocketConsoleApplicationShell implements Runnable {
@Override @Override
public void run() { public void run() {
// Create the server // Create the server
try (ServerSocket actualServerSocket = new ServerSocket(port, 1, try (ServerSocket actualServerSocket = new ServerSocket(port, 1, addr)) {
addr)) {
serverSocket = actualServerSocket; serverSocket = actualServerSocket;
running = true; running = true;
// Create the streams // Create the streams
runSokectServer(); runSokectServer();
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.severe("Communication error between client and server"); //$NON-NLS-1$ LOGGER.severe("Communication error between client and server"); //$NON-NLS-1$
LOGGER.log(Level.FINE, LOGGER.log(Level.FINE, "Communication error between client and server", e); //$NON-NLS-1$
"Communication error between client and server", e); //$NON-NLS-1$
} }
} }
@ -141,7 +138,7 @@ public final class SocketConsoleApplicationShell implements Runnable {
private void runSokectServer() throws IOException { private void runSokectServer() throws IOException {
while (running && app.isRunning()) { while (running && app.isRunning()) {
LOGGER.info("Waiting client"); //$NON-NLS-1$ LOGGER.info("Waiting client"); //$NON-NLS-1$
try (Socket clientSocket = serverSocket.accept();) { try (Socket clientSocket = serverSocket.accept()) {
sci.connect(clientSocket); sci.connect(clientSocket);

View File

@ -68,12 +68,10 @@ public final class SocketConsoleInterface {
* @param socket the socket * @param socket the socket
* @throws IOException if the connection failed */ * @throws IOException if the connection failed */
public void connect(final Socket socket) throws IOException { public void connect(final Socket socket) throws IOException {
final PrintStream printStream = new PrintStream( final PrintStream printStream = new PrintStream(socket.getOutputStream(), true,
socket.getOutputStream(), true,
StandardCharsets.UTF_8.name()); StandardCharsets.UTF_8.name());
output.connect(printStream); output.connect(printStream);
input.connect(socket.getInputStream(), input.connect(socket.getInputStream(), printStream);
printStream);
} }
/** Disconnect the input and output of the application from the socket's. */ /** Disconnect the input and output of the application from the socket's. */

View File

@ -62,13 +62,10 @@ public class ConsoleTestApplication {
* @return create the application */ * @return create the application */
@SuppressWarnings("nls") @SuppressWarnings("nls")
public static ConsoleApplication create(final ConsoleOutput output, public static ConsoleApplication create(final ConsoleOutput output,
final ConsoleInput input, final ConsoleInput input, final ConnexionManager<Socket> manager) {
final ConnexionManager<Socket> manager) {
try { try {
final ConsoleApplication application = new ConsoleApplication( final ConsoleApplication application = new ConsoleApplication(output, input,
output, input, "Welcome to the test application. Type help or test.", "See you");
"Welcome to the test application. Type help or test.",
"See you");
application.add(new ExitCommand(EXIT, application) { application.add(new ExitCommand(EXIT, application) {
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.ExitCommand#beforeExit() */ * @see fr.bigeon.gclc.command.ExitCommand#beforeExit() */
@ -80,8 +77,7 @@ public class ConsoleTestApplication {
} }
} }
}); });
application application.add(new HelpExecutor("help", application.root));
.add(new HelpExecutor("help", application.root));
application.add(new Command("test") { application.add(new Command("test") {
/* (non-Javadoc) /* (non-Javadoc)
@ -89,8 +85,7 @@ public class ConsoleTestApplication {
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, * manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */ * java.lang.String[]) */
@Override @Override
public void execute(final ConsoleOutput out, public void execute(final ConsoleOutput out, final ConsoleInput in,
final ConsoleInput in,
final String... args) throws CommandRunException { final String... args) throws CommandRunException {
try { try {
output.println("Test command ran fine"); output.println("Test command ran fine");
@ -105,8 +100,7 @@ public class ConsoleTestApplication {
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#usageDetail() * @see fr.bigeon.gclc.command.Command#usageDetail() */
*/
@Override @Override
protected String usageDetail() { protected String usageDetail() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
@ -121,8 +115,7 @@ public class ConsoleTestApplication {
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, * manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */ * java.lang.String[]) */
@Override @Override
public void execute(final ConsoleOutput out, public void execute(final ConsoleOutput out, final ConsoleInput in,
final ConsoleInput in,
final String... args) throws CommandRunException { final String... args) throws CommandRunException {
try { try {
Thread.sleep(2000); Thread.sleep(2000);
@ -138,8 +131,7 @@ public class ConsoleTestApplication {
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.Command#usageDetail() * @see fr.bigeon.gclc.command.Command#usageDetail() */
*/
@Override @Override
protected String usageDetail() { protected String usageDetail() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
@ -147,8 +139,7 @@ public class ConsoleTestApplication {
throw new RuntimeException("Not implemented yet"); throw new RuntimeException("Not implemented yet");
} }
}); });
application.add( application.add(new RemoteDisconnectCommand<>("out", manager, true));
new RemoteDisconnectCommand<>("out", manager, true));
return application; return application;
} catch (final InvalidCommandName e) { } catch (final InvalidCommandName e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -39,6 +39,7 @@ package net.bigeon.gclc.socket;
*/ */
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -46,6 +47,7 @@ import java.io.IOException;
import java.io.PipedInputStream; import java.io.PipedInputStream;
import java.io.PipedOutputStream; import java.io.PipedOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test; import org.junit.Test;
@ -167,15 +169,17 @@ public class PluggableConsoleInputTest {
input.connect(pis, out); input.connect(pis, out);
testIn.println("tac"); testIn.println("tac");
final AtomicReference<IOException> ref = new AtomicReference<>();
final Thread th2 = new Thread(new Runnable() { final Thread th2 = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
input.prompt("Test", 5000); input.prompt("Test", 5000);
fail("Prompt should io"); // fail("Prompt should io");
} catch (final IOException e) { } catch (final IOException e) {
// ok // ok
ref.set(e);
} }
} }
}); });
@ -185,7 +189,8 @@ public class PluggableConsoleInputTest {
} }
input.close(); input.close();
th2.join();
assertNotNull("Prompt should io", ref.get());
} }
} }

View File

@ -63,10 +63,10 @@ public class RemoteDisconnectCommandTest {
@Test @Test
public final void testExecute() throws CommandRunException, IOException { public final void testExecute() throws CommandRunException, IOException {
final DConnexionManager<String> manager = new DConnexionManager<>(); final DConnexionManager<String> manager = new DConnexionManager<>();
final RemoteDisconnectCommand<String> cmd = new RemoteDisconnectCommand<>( final RemoteDisconnectCommand<String> cmd = new RemoteDisconnectCommand<>("quit",
"quit", manager, true); manager, true);
final RemoteDisconnectCommand<String> cmd2 = new RemoteDisconnectCommand<>( final RemoteDisconnectCommand<String> cmd2 = new RemoteDisconnectCommand<>("quit",
"quit", manager, false); manager, false);
manager.addConnexion("test"); manager.addConnexion("test");
cmd2.execute(null, null); cmd2.execute(null, null);

View File

@ -51,7 +51,7 @@ import org.junit.Test;
/** Test class for {@link SocketConsoleApplicationShell} /** Test class for {@link SocketConsoleApplicationShell}
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
@SuppressWarnings({"static-method", "javadoc", "nls"}) @SuppressWarnings({ "static-method", "javadoc", "nls" })
public class SocketConsoleApplicationTest { public class SocketConsoleApplicationTest {
private static final Logger LOGGER = Logger private static final Logger LOGGER = Logger
@ -60,12 +60,12 @@ public class SocketConsoleApplicationTest {
/** @param in the input /** @param in the input
* @return the string * @return the string
* @throws IOException if the input reading failed */ * @throws IOException if the input reading failed */
private String consumeToPrompt(final String server, private String consumeToPrompt(final String server, final BufferedReader in)
final BufferedReader in) throws IOException { throws IOException {
String fromServer = server; String fromServer = server;
LOGGER.fine("Server: \n" + fromServer); LOGGER.fine("Server: \n" + fromServer);
while (fromServer != null && !fromServer.equals("Bye.") && while (fromServer != null && !fromServer.equals("Bye.")
!fromServer.equals("> ")) { && !fromServer.equals("> ")) {
fromServer = in.readLine(); fromServer = in.readLine();
LOGGER.fine("Server: \n" + fromServer); LOGGER.fine("Server: \n" + fromServer);
} }
@ -81,14 +81,13 @@ public class SocketConsoleApplicationTest {
final int portNumber = 3300; final int portNumber = 3300;
try (Socket kkSocket = new Socket(hostName, portNumber); try (Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
true);
BufferedReader in = new BufferedReader( BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));) { new InputStreamReader(kkSocket.getInputStream()));) {
String fromServer; String fromServer;
int i = -1; int i = -1;
final String[] cmds = {"help", "toto", "test", "out"}; final String[] cmds = { "help", "toto", "test", "out" };
while ((fromServer = in.readLine()) != null) { while ((fromServer = in.readLine()) != null) {
i++; i++;
fromServer = consumeToPrompt(fromServer, in); fromServer = consumeToPrompt(fromServer, in);
@ -106,15 +105,13 @@ public class SocketConsoleApplicationTest {
} }
try (Socket kkSocket = new Socket(hostName, portNumber); try (Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
true);
BufferedReader in = new BufferedReader( BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));) { new InputStreamReader(kkSocket.getInputStream()));) {
String fromServer; String fromServer;
int i = 0; int i = 0;
final String[] cmds = {"help", "toto", "test", final String[] cmds = { "help", "toto", "test", ConsoleTestApplication.EXIT };
ConsoleTestApplication.EXIT};
while ((fromServer = in.readLine()) != null) { while ((fromServer = in.readLine()) != null) {
fromServer = consumeToPrompt(fromServer, in); fromServer = consumeToPrompt(fromServer, in);
if (fromServer == null || fromServer.equals("Bye.")) { if (fromServer == null || fromServer.equals("Bye.")) {
@ -129,8 +126,7 @@ public class SocketConsoleApplicationTest {
} }
i++; i++;
} }
assertEquals("Application exit command should close connection", 4, assertEquals("Application exit command should close connection", 4, i);
i);
} }
Thread.sleep(100); Thread.sleep(100);
TestServer.closeServer(); TestServer.closeServer();
@ -138,17 +134,14 @@ public class SocketConsoleApplicationTest {
server = TestServer.getServer(); server = TestServer.getServer();
Thread.sleep(1000); Thread.sleep(1000);
try (Socket kkSocket = new Socket(hostName, portNumber); try (Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
true);
BufferedReader in = new BufferedReader( BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));) { new InputStreamReader(kkSocket.getInputStream()));) {
String fromServer; String fromServer;
int i = 0; int i = 0;
final String[] cmds = {"help", "toto", "test", final String[] cmds = { "help", "toto", "test", ConsoleTestApplication.EXIT };
ConsoleTestApplication.EXIT};
while ((fromServer = in.readLine()) != null) { while ((fromServer = in.readLine()) != null) {
fromServer = consumeToPrompt(fromServer, in); fromServer = consumeToPrompt(fromServer, in);
if (fromServer == null || fromServer.equals("Bye.")) { if (fromServer == null || fromServer.equals("Bye.")) {
@ -162,8 +155,7 @@ public class SocketConsoleApplicationTest {
} }
i++; i++;
} }
assertEquals("Application exit command should close connection", 4, assertEquals("Application exit command should close connection", 4, i);
i);
} }
TestServer.closeServer(); TestServer.closeServer();
server.join(); server.join();

View File

@ -44,9 +44,8 @@ import java.util.Collection;
import net.bigeon.smu.StringEncoder; import net.bigeon.smu.StringEncoder;
/** TODO Describe TestConsoleClient.java /** TODO Describe TestConsoleClient.java
* @author Emmanuel Bigeon
* *
*/ * @author Emmanuel Bigeon */
@SuppressWarnings("nls") @SuppressWarnings("nls")
public class TestConsoleClient { public class TestConsoleClient {
@SuppressWarnings("javadoc") @SuppressWarnings("javadoc")
@ -56,8 +55,7 @@ public class TestConsoleClient {
} }
@SuppressWarnings("javadoc") @SuppressWarnings("javadoc")
private static final StringEncoder ENCODER = new StringEncoder("%", private static final StringEncoder ENCODER = new StringEncoder("%", TO_ENCODE);
TO_ENCODE);
@SuppressWarnings("javadoc") @SuppressWarnings("javadoc")
public static void main(String[] args) { public static void main(String[] args) {
@ -65,10 +63,9 @@ public class TestConsoleClient {
final int portNumber = 3300; final int portNumber = 3300;
try (Socket kkSocket = new Socket(hostName, portNumber); try (Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
true); BufferedReader in = new BufferedReader(
BufferedReader in = new BufferedReader(new InputStreamReader( new InputStreamReader(kkSocket.getInputStream()));) {
kkSocket.getInputStream()));) {
String fromServer; String fromServer;
while ((fromServer = in.readLine()) != null) { while ((fromServer = in.readLine()) != null) {

View File

@ -43,7 +43,7 @@ import net.bigeon.gclc.ConsoleApplication;
/** A test server /** A test server
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
@SuppressWarnings({"javadoc", "nls"}) @SuppressWarnings({ "javadoc", "nls" })
public class TestServer { public class TestServer {
private static SocketConsoleApplicationShell SHELL; private static SocketConsoleApplicationShell SHELL;
@ -77,8 +77,8 @@ public class TestServer {
manager = new DConnexionManager<>(); manager = new DConnexionManager<>();
SHELL = new SocketConsoleApplicationShell(3300, SHELL = new SocketConsoleApplicationShell(3300,
InetAddress.getByName("127.0.0.1")); InetAddress.getByName("127.0.0.1"));
final ConsoleApplication app = ConsoleTestApplication final ConsoleApplication app = ConsoleTestApplication.create(output, input,
.create(output, input, manager); manager);
SHELL.setInterface(new SocketConsoleInterface(input, output)); SHELL.setInterface(new SocketConsoleInterface(input, output));
SHELL.setConnexionManager(manager); SHELL.setConnexionManager(manager);
SHELL.setApplication(app); SHELL.setApplication(app);

View File

@ -77,6 +77,7 @@ package net.bigeon.gclc.swt;
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
public interface ConsoleDelayIO { public interface ConsoleDelayIO {
/** Get the input text. /** Get the input text.
*
* @return the non validated input */ * @return the non validated input */
String getInput(); String getInput();

View File

@ -123,8 +123,7 @@ public final class HistoryTextKeyListener extends KeyAdapter {
} }
// Upper arrow retrieves previous commands // Upper arrow retrieves previous commands
if (keyCode == SWT.ARROW_UP && if (keyCode == SWT.ARROW_UP && currentIndex < commands.size() - 1) {
currentIndex < commands.size() - 1) {
currentIndex++; currentIndex++;
final String cmd = commands.get(commands.size() - currentIndex - 1); final String cmd = commands.get(commands.size() - currentIndex - 1);
console.setInput(cmd); console.setInput(cmd);
@ -136,8 +135,7 @@ public final class HistoryTextKeyListener extends KeyAdapter {
currentIndex--; currentIndex--;
console.setInput(EMPTY); console.setInput(EMPTY);
} else if (currentIndex > 0) { } else if (currentIndex > 0) {
final String cmd = commands final String cmd = commands.get(commands.size() - (--currentIndex) - 1);
.get(commands.size() - (--currentIndex) - 1);
console.setInput(cmd); console.setInput(cmd);
} }
} }

View File

@ -309,8 +309,8 @@ public final class SWTConsole extends Composite
promptLock.notifyAll(); promptLock.notifyAll();
final long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
long cur = start; long cur = start;
while (prompting && start + timeout>cur) { while (prompting && start + timeout > cur) {
promptLock.wait((cur-start-timeout)/2); promptLock.wait((cur - start - timeout) / 2);
cur = System.currentTimeMillis(); cur = System.currentTimeMillis();
} }
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
@ -397,6 +397,11 @@ public final class SWTConsole extends Composite
consoleInput.setSelection(input.length()); consoleInput.setSelection(input.length());
} }
@Override
public void setPrompt(String prompt) {
setPrompt(new ConstantString(prompt));
}
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#setPrompt(java.lang.String) */ * @see fr.bigeon.gclc.ConsoleManager#setPrompt(java.lang.String) */
@Override @Override
@ -415,11 +420,6 @@ public final class SWTConsole extends Composite
}); });
} }
@Override
public void setPrompt(String prompt) {
setPrompt(new ConstantString(prompt));
}
/** @param string the text */ /** @param string the text */
public void setText(final String string) { public void setText(final String string) {
consoleInput.setText(string); consoleInput.setText(string);

View File

@ -92,8 +92,7 @@ public final class SWTConsoleView extends Composite implements ConsoleDelayIO {
/** The local implementation of the forwarding runnable /** The local implementation of the forwarding runnable
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
private final class ToSWTConsoleForwardRunnable private final class ToSWTConsoleForwardRunnable extends AOutputForwardRunnable {
extends AOutputForwardRunnable {
/** The running status */ /** The running status */
private boolean running = true; private boolean running = true;
@ -139,15 +138,13 @@ public final class SWTConsoleView extends Composite implements ConsoleDelayIO {
setLayout(new GridLayout(1, false)); setLayout(new GridLayout(1, false));
consoleOutput = new Text(this, SWT.BORDER | SWT.READ_ONLY | SWT.WRAP | consoleOutput = new Text(this,
SWT.V_SCROLL | SWT.MULTI); SWT.BORDER | SWT.READ_ONLY | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
consoleOutput.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, consoleOutput.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
1, 1));
consoleOutput.setRedraw(true); consoleOutput.setRedraw(true);
consoleInput = new Text(this, SWT.BORDER); consoleInput = new Text(this, SWT.BORDER);
consoleInput.setLayoutData( consoleInput.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
consoleInput.addKeyListener(new HistoryTextKeyListener(this)); consoleInput.addKeyListener(new HistoryTextKeyListener(this));
} }

View File

@ -73,7 +73,8 @@ import static org.junit.Assert.assertEquals;
import org.eclipse.swt.SWT; import org.eclipse.swt.SWT;
import org.junit.Test; import org.junit.Test;
/** <p> /**
* <p>
* TODO * TODO
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
@ -87,8 +88,8 @@ public class HistoryTextKeyListenerTest {
private String input = ""; private String input = "";
@Override @Override
public void validateInput() { public String getInput() {
input = ""; return input;
} }
@Override @Override
@ -97,8 +98,8 @@ public class HistoryTextKeyListenerTest {
} }
@Override @Override
public String getInput() { public void validateInput() {
return input; input = "";
} }
}; };
HistoryTextKeyListener listener = new HistoryTextKeyListener(io); HistoryTextKeyListener listener = new HistoryTextKeyListener(io);

View File

@ -91,7 +91,7 @@ import net.bigeon.gclc.manager.ConsoleOutput;
* TODO * TODO
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
@SuppressWarnings({"javadoc", "static-method", "nls", "deprecation"}) @SuppressWarnings({ "javadoc", "static-method", "nls", "deprecation" })
public class SWTConsoleShellTest { public class SWTConsoleShellTest {
protected static final long TWO_SECONDS = 2000; protected static final long TWO_SECONDS = 2000;
@ -102,8 +102,8 @@ public class SWTConsoleShellTest {
final SWTConsoleShell shell = new SWTConsoleShell(DISPLAY); final SWTConsoleShell shell = new SWTConsoleShell(DISPLAY);
final SWTConsole swtConsole = shell.getManager(); final SWTConsole swtConsole = shell.getManager();
try { try {
final ConsoleApplication appl = new ConsoleApplication(swtConsole, final ConsoleApplication appl = new ConsoleApplication(swtConsole, swtConsole,
swtConsole, "Hello", "See you"); "Hello", "See you");
appl.add(new ExitCommand("exit", appl)); appl.add(new ExitCommand("exit", appl));
appl.add(new Command("long") { appl.add(new Command("long") {
@ -112,8 +112,7 @@ public class SWTConsoleShellTest {
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, * manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */ * java.lang.String[]) */
@Override @Override
public void execute(final ConsoleOutput out, public void execute(final ConsoleOutput out, final ConsoleInput in,
final ConsoleInput in,
final String... args) throws CommandRunException { final String... args) throws CommandRunException {
try { try {
Thread.sleep(TWO_SECONDS); Thread.sleep(TWO_SECONDS);
@ -204,8 +203,8 @@ public class SWTConsoleShellTest {
swtConsole.close(); swtConsole.close();
swtConsole.setPrompt(":"); swtConsole.setPrompt(":");
try { try {
final ConsoleApplication appl = new ConsoleApplication(swtConsole, final ConsoleApplication appl = new ConsoleApplication(swtConsole, swtConsole,
swtConsole, "Hello", "See you"); "Hello", "See you");
appl.add(new ExitCommand("exit", appl)); appl.add(new ExitCommand("exit", appl));
appl.add(new Command("long") { appl.add(new Command("long") {
/* (non-Javadoc) /* (non-Javadoc)
@ -213,8 +212,7 @@ public class SWTConsoleShellTest {
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, * manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */ * java.lang.String[]) */
@Override @Override
public void execute(final ConsoleOutput out, public void execute(final ConsoleOutput out, final ConsoleInput in,
final ConsoleInput in,
final String... args) throws CommandRunException { final String... args) throws CommandRunException {
try { try {
Thread.sleep(TWO_SECONDS); Thread.sleep(TWO_SECONDS);
@ -244,8 +242,7 @@ public class SWTConsoleShellTest {
* manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, * manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */ * java.lang.String[]) */
@Override @Override
public void execute(final ConsoleOutput out, public void execute(final ConsoleOutput out, final ConsoleInput in,
final ConsoleInput in,
final String... args) throws CommandRunException { final String... args) throws CommandRunException {
try { try {
swtConsole.prompt("Test"); swtConsole.prompt("Test");

View File

@ -87,11 +87,12 @@ import net.bigeon.gclc.manager.ConsoleOutput;
import net.bigeon.gclc.utils.PipedConsoleInput; import net.bigeon.gclc.utils.PipedConsoleInput;
import net.bigeon.gclc.utils.PipedConsoleOutput; import net.bigeon.gclc.utils.PipedConsoleOutput;
/** <p> /**
* <p>
* TODO * TODO
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
@SuppressWarnings({"javadoc", "static-method", "nls", "deprecation"}) @SuppressWarnings({ "javadoc", "static-method", "nls", "deprecation" })
public class SWTConsoleViewTest { public class SWTConsoleViewTest {
protected static final long TWO_SECONDS = 2000; protected static final long TWO_SECONDS = 2000;
@ -110,15 +111,15 @@ public class SWTConsoleViewTest {
try (PipedConsoleOutput manager = new PipedConsoleOutput(); try (PipedConsoleOutput manager = new PipedConsoleOutput();
PipedConsoleInput input = new PipedConsoleInput(System.out)) { PipedConsoleInput input = new PipedConsoleInput(System.out)) {
swtConsole.setManager(manager, input); swtConsole.setManager(manager, input);
final ConsoleApplication appl = new ConsoleApplication(manager, final ConsoleApplication appl = new ConsoleApplication(manager, input,
input,
"Hello", "See you"); "Hello", "See you");
appl.add(new ExitCommand("exit", appl)); appl.add(new ExitCommand("exit", appl));
appl.add(new Command("long") { appl.add(new Command("long") {
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) * @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.
*/ * ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */
@Override @Override
public void execute(final ConsoleOutput out, final ConsoleInput in, public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException { final String... args) throws CommandRunException {

View File

@ -69,16 +69,15 @@ public class ExecSystemCommand extends Command {
super(COMMAND_DEFAULT_NAME); super(COMMAND_DEFAULT_NAME);
} }
/** @param name the name of the command (the input from the manager that /** @param name the name of the command (the input from the manager that should
* should trigger this command) */ * trigger this command) */
public ExecSystemCommand(final String name) { public ExecSystemCommand(final String name) {
super(name); super(name);
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager. * @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, * ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
* java.lang.String[]) */
@Override @Override
public void execute(final ConsoleOutput out, final ConsoleInput in, public void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException { final String... args) throws CommandRunException {
@ -90,8 +89,7 @@ public class ExecSystemCommand extends Command {
return; return;
} }
final InputStream is = proc final InputStream is = proc.getInputStream();
.getInputStream();
final Thread th = new Thread(new Runnable() { final Thread th = new Thread(new Runnable() {
@SuppressWarnings("synthetic-access") @SuppressWarnings("synthetic-access")
@ -101,8 +99,7 @@ public class ExecSystemCommand extends Command {
readToEnd(out, is); readToEnd(out, is);
is.close(); is.close();
} catch (final CommandRunException e) { } catch (final CommandRunException e) {
LOGGER.log(Level.WARNING, LOGGER.log(Level.WARNING, "Manager was closed in the meantime...", e); //$NON-NLS-1$
"Manager was closed in the meantime...", e); //$NON-NLS-1$
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.log(Level.WARNING, "Input stream was closed...", e); //$NON-NLS-1$ LOGGER.log(Level.WARNING, "Input stream was closed...", e); //$NON-NLS-1$
} }
@ -111,15 +108,13 @@ public class ExecSystemCommand extends Command {
th.start(); th.start();
in.setPrompt(""); //$NON-NLS-1$ in.setPrompt(""); //$NON-NLS-1$
final OutputStream os = proc.getOutputStream(); final OutputStream os = proc.getOutputStream();
try (BufferedWriter writer = new BufferedWriter( try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) {
new OutputStreamWriter(os))) {
while (th.isAlive()) { while (th.isAlive()) {
String user; String user;
try { try {
user = in.prompt(); user = in.prompt();
} catch (final IOException e) { } catch (final IOException e) {
throw new CommandRunException( throw new CommandRunException(CommandRunExceptionType.INTERACTION,
CommandRunExceptionType.INTERACTION,
"manager was closed", e); //$NON-NLS-1$ "manager was closed", e); //$NON-NLS-1$
} }
writer.write(user + EOL); writer.write(user + EOL);
@ -133,16 +128,15 @@ public class ExecSystemCommand extends Command {
/** @param is the input stream /** @param is the input stream
* @throws CommandRunException if the manager was closed while writing the * @throws CommandRunException if the manager was closed while writing the
* stream */ * stream */
protected void readToEnd(final ConsoleOutput out, protected void readToEnd(final ConsoleOutput out, final InputStream is)
final InputStream is) throws CommandRunException { throws CommandRunException {
int c; int c;
try { try {
while ((c = is.read()) != -1) { while ((c = is.read()) != -1) {
try { try {
out.print(Character.valueOf((char) c).toString()); out.print(Character.valueOf((char) c).toString());
} catch (final IOException e) { } catch (final IOException e) {
throw new CommandRunException( throw new CommandRunException(CommandRunExceptionType.INTERACTION,
CommandRunExceptionType.INTERACTION,
"manager was closed", e); //$NON-NLS-1$ "manager was closed", e); //$NON-NLS-1$
} }
} }
@ -164,10 +158,9 @@ public class ExecSystemCommand extends Command {
protected String usageDetail() { protected String usageDetail() {
return " The system command is a system dependend command like sh on linux or" + //$NON-NLS-1$ return " The system command is a system dependend command like sh on linux or" + //$NON-NLS-1$
System.lineSeparator() + "powershell on windows." + //$NON-NLS-1$ System.lineSeparator() + "powershell on windows." + //$NON-NLS-1$
System.lineSeparator() + System.lineSeparator() + System.lineSeparator() + System.lineSeparator()
" As an example if you give \"cat /etc/hostname\" as argument, on a linux" + //$NON-NLS-1$ + " As an example if you give \"cat /etc/hostname\" as argument, on a linux" //$NON-NLS-1$
System.lineSeparator() + + System.lineSeparator() + "system, you would get the computer name." + //$NON-NLS-1$
"system, you would get the computer name." + //$NON-NLS-1$
System.lineSeparator(); System.lineSeparator();
} }