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.exception.CommandRunException;
/**
* @author Emmanuel Bigeon
*
*/
/** @author Emmanuel Bigeon */
public class ForkCommandTask extends ForkTask {
private final ICommand command;
private final String[] args;
@ -89,13 +86,6 @@ public class ForkCommandTask extends ForkTask {
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)
* @see net.bigeon.gclc.process.ForkTask#doRun() */
@Override
@ -103,4 +93,11 @@ public class ForkCommandTask extends ForkTask {
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);
}
/** 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)
* @see net.bigeon.gclc.process.Task#isRunning() */
@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)
* @see net.bigeon.gclc.process.Task#setRunning(boolean) */
@Override
@ -181,8 +185,4 @@ public abstract class ForkTask implements Task {
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.exception.InvalidCommandName;
/**
* @author Emmanuel Bigeon
*
*/
/** @author Emmanuel Bigeon */
public class ProcessAttachement implements ApplicationAttachement {
private final TaskPool pool;
@ -86,8 +83,8 @@ public class ProcessAttachement implements ApplicationAttachement {
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ApplicationAttachement#attach(fr.bigeon.gclc.ConsoleApplication)
*/
* @see fr.bigeon.gclc.ApplicationAttachement#attach(fr.bigeon.gclc.
* ConsoleApplication) */
@Override
public void attach(ICommandProvider application) throws InvalidCommandName {
application.add(new ProcessKill("kill", pool));

View File

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

View File

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

View File

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

View File

@ -103,8 +103,8 @@ public interface Task extends Runnable {
/** 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.
* 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);

View File

@ -95,16 +95,6 @@ public final class TaskPool {
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.
*
* @param cmd the process
@ -168,6 +158,16 @@ public final class TaskPool {
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.
* <p>
* 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
* @return the process to start and add to the pool
* @throws CommandRunException if the task creation failed */
protected abstract Task createTask(ConsoleOutput out, ConsoleInput in,
String... args) throws CommandRunException;
protected abstract Task createTask(ConsoleOutput out, ConsoleInput in, String... args)
throws CommandRunException;
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
* java.lang.String[]) */
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
@Override
public final void execute(final ConsoleOutput out, final ConsoleInput in,
final String... args) throws CommandRunException {

View File

@ -94,6 +94,26 @@ public final class ConnectingConsoleInput implements ConsoleInput {
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)
* @see fr.bigeon.gclc.manager.ConsoleInput#getPrompt() */
@Override
@ -128,14 +148,6 @@ public final class ConnectingConsoleInput implements ConsoleInput {
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)
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(long) */
@Override
@ -222,23 +234,11 @@ public final class ConnectingConsoleInput implements ConsoleInput {
};
}
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)
* @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(fr.bigeon.gclc.tools.
* StringProvider) */
@Override
public void setPrompt(StringProvider string) {
prompt = string;
}
}

View File

@ -130,6 +130,25 @@ public class ConnectingConsoleOutput implements ConsoleOutput {
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)
* @see fr.bigeon.gclc.manager.ConsoleOutput#isClosed() */
@Override
@ -157,23 +176,4 @@ public class ConnectingConsoleOutput implements ConsoleOutput {
public void println(String message) {
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}. */
private final Object counterLock = new Object();
/** The count of connexions. */
private int count = 0;
private int count;
/** Default.constructor. */
public DConnexionManager() {
//
}
/* (non-Javadoc)
* @see
* net.bigeon.gclc.socket.ConnexionManager#addConnexion(java.lang.Object) */
@ -80,11 +81,12 @@ public final class DConnexionManager<T> implements ConnexionManager<T> {
}
/* (non-Javadoc)
* @see
* net.bigeon.gclc.socket.ConnexionManager#disconnect(java.lang.String) */
* @see net.bigeon.gclc.socket.ConnexionManager#disconnect(java.lang.String) */
@Override
public T disconnect(final String id) {
if (connecteds.containsKey(id)) {
if (!connecteds.containsKey(id)) {
return null;
}
final T disc = connecteds.remove(id);
final Object lock = locks.get(id);
synchronized (lock) {
@ -102,8 +104,6 @@ public final class DConnexionManager<T> implements ConnexionManager<T> {
}
return disc;
}
return null;
}
/* (non-Javadoc)
* @see net.bigeon.gclc.socket.ConnexionManager#get(java.lang.String) */
@ -120,8 +120,7 @@ public final class DConnexionManager<T> implements ConnexionManager<T> {
}
/* (non-Javadoc)
* @see
* net.bigeon.gclc.socket.ConnexionManager#isConnected(java.lang.String) */
* @see net.bigeon.gclc.socket.ConnexionManager#isConnected(java.lang.String) */
@Override
public boolean isConnected(final String id) {
return connecteds.containsKey(id);
@ -154,8 +153,8 @@ public final class DConnexionManager<T> implements ConnexionManager<T> {
@Override
public void releaseDisconnexionLock(final String id) {
synchronized (counterLock) {
counters.put(id, Integer
.valueOf(Math.max(counters.get(id).intValue() - 1, 0)));
counters.put(id,
Integer.valueOf(Math.max(counters.get(id).intValue() - 1, 0)));
counterLock.notifyAll();
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -309,8 +309,8 @@ public final class SWTConsole extends Composite
promptLock.notifyAll();
final long start = System.currentTimeMillis();
long cur = start;
while (prompting && start + timeout>cur) {
promptLock.wait((cur-start-timeout)/2);
while (prompting && start + timeout > cur) {
promptLock.wait((cur - start - timeout) / 2);
cur = System.currentTimeMillis();
}
} catch (final InterruptedException e) {
@ -397,6 +397,11 @@ public final class SWTConsole extends Composite
consoleInput.setSelection(input.length());
}
@Override
public void setPrompt(String prompt) {
setPrompt(new ConstantString(prompt));
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.ConsoleManager#setPrompt(java.lang.String) */
@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 */
public void setText(final String 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
*
* @author Emmanuel Bigeon */
private final class ToSWTConsoleForwardRunnable
extends AOutputForwardRunnable {
private final class ToSWTConsoleForwardRunnable extends AOutputForwardRunnable {
/** The running status */
private boolean running = true;
@ -139,15 +138,13 @@ public final class SWTConsoleView extends Composite implements ConsoleDelayIO {
setLayout(new GridLayout(1, false));
consoleOutput = new Text(this, SWT.BORDER | SWT.READ_ONLY | SWT.WRAP |
SWT.V_SCROLL | SWT.MULTI);
consoleOutput.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
1, 1));
consoleOutput = new Text(this,
SWT.BORDER | SWT.READ_ONLY | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
consoleOutput.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
consoleOutput.setRedraw(true);
consoleInput = new Text(this, SWT.BORDER);
consoleInput.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
consoleInput.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
consoleInput.addKeyListener(new HistoryTextKeyListener(this));
}

View File

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

View File

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

View File

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