Compare commits
124 Commits
gclc-2.0.1
...
gclc-2.1.2
| Author | SHA1 | Date | |
|---|---|---|---|
| a0a1dfe91e | |||
| 04902c9868 | |||
| 9cf30ef7d2 | |||
| 3960b10e8e | |||
| d3bb6fa5a0 | |||
| b6b6ab9d36 | |||
| 92a664d193 | |||
| c230a23f81 | |||
| e5c3bb0152 | |||
| 24e57eca2f | |||
| da8765dd7b | |||
| ce4254941a | |||
| f459f78a75 | |||
| 8cd0b01490 | |||
| 698b3d0e5e | |||
| 9ad4a1e456 | |||
| e560fcb92d | |||
| 56f3d604a1 | |||
| 73317186df | |||
| 80bd7c0ac9 | |||
| dc71070dab | |||
| 78b5926af1 | |||
| a3d2c2c07e | |||
| 9df812273c | |||
| e953c2e659 | |||
| 0f4fd6109d | |||
| 69a8fd2533 | |||
| 0b772ddeb3 | |||
| 55f13ae004 | |||
| 295075ca37 | |||
| 75bbba9884 | |||
| 82daa84bdf | |||
| 10a0858d81 | |||
| dfd3645497 | |||
| bd2da741f2 | |||
| 314db5b82f | |||
| 981668d3a7 | |||
| 9c8866827d | |||
| 43c9faaee7 | |||
| 8436e8926c | |||
| 0cef23e17b | |||
| 6b2a25674d | |||
| eec660e089 | |||
| 901469792d | |||
| 79ee5394d4 | |||
| acf4484eb3 | |||
| 012ef6e668 | |||
| 583f3146fd | |||
| 8b52ba5d49 | |||
| a3051c6f03 | |||
| 18b98997d3 | |||
| 0e1c69dab7 | |||
| c206b5b22c | |||
| c4bbfd8434 | |||
| b671474f64 | |||
| 6443790532 | |||
| 2df99debf8 | |||
| d87aabd465 | |||
| 0562faad70 | |||
| bb06b2a799 | |||
| 626f557aa0 | |||
| 185d19bfcf | |||
| 7fd1bf90bd | |||
| 1fa1f168c8 | |||
| 09e9c69fe0 | |||
| 9f273f7595 | |||
| 143eb7036a | |||
| 9c64c708eb | |||
| 8449eddab8 | |||
| d0a286d45a | |||
| d6698a22fb | |||
| 5e1050a9d7 | |||
| 9374c5a681 | |||
| 4c1e28b04f | |||
| 80933bb636 | |||
| 48b4375565 | |||
| ba7b80689c | |||
| bd311d185e | |||
| aa26173f26 | |||
| df688b1a85 | |||
| bcd0faceef | |||
| 5e5cc2a1cd | |||
| b24b72f3e2 | |||
| ad79e3ccb6 | |||
| ddd818d93b | |||
| 6993513f38 | |||
| b9a7142c5c | |||
| 85ef68f6ae | |||
| dfca63b179 | |||
| 787f951ff7 | |||
| a364b6442b | |||
| 211b965e9e | |||
| e1d030c6c5 | |||
| 8bc2b13a92 | |||
| c1050b1b4f | |||
| a3e4758d1b | |||
| c436b5af2f | |||
| 3272261be1 | |||
| f82f0b9f16 | |||
| 1fa9ca213e | |||
| 265f90526a | |||
| 71b325508f | |||
| 98857e425a | |||
| 8c6b1d8884 | |||
| 2972e82f4a | |||
| 0ab39b24f6 | |||
| 171f79518d | |||
| 0e422a81ce | |||
| ea4164fbfb | |||
| e2f45c77d9 | |||
| 55bb54ca43 | |||
| cf29eb37cc | |||
| 579e85dcb3 | |||
| 7e36a378c4 | |||
| 80242c79e8 | |||
| ab9a4c474b | |||
| b8e5ea0b78 | |||
| 2637d99bce | |||
| cfd3450afe | |||
| 20d5a84269 | |||
| 637cfb8f43 | |||
| d7fbdfb66b | |||
| 22741104f5 | |||
| 45c5375118 |
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
# maven files
|
||||
target/
|
||||
# Eclipse files
|
||||
.settings/
|
||||
.classpath
|
||||
.project
|
||||
@@ -1,38 +1,4 @@
|
||||
|
||||
<!-- process, Distribution repositories and basic setup for Emmanuel Bigeon
|
||||
projects -->
|
||||
<!-- Copyright (C) 2014-2018 E. Bigeon -->
|
||||
<!-- mailto:emmanuel@bigeon.fr -->
|
||||
<!-- -->
|
||||
<!-- This software is governed by the CeCILL license under French law and -->
|
||||
<!-- abiding by the rules of distribution of free software. You can use, -->
|
||||
<!-- modify and/or redistribute the software under the terms of the CeCILL -->
|
||||
<!-- license as circulated by CEA, CNRS and INRIA at the following URL -->
|
||||
<!-- "http://www.cecill.info". -->
|
||||
<!-- -->
|
||||
<!-- As a counterpart to the access to the source code and rights to copy, -->
|
||||
<!-- modify and redistribute granted by the license, users are provided only -->
|
||||
<!-- with a limited warranty and the software's author, the holder of the -->
|
||||
<!-- economic rights, and the successive licensors have only limited -->
|
||||
<!-- liability. -->
|
||||
<!-- -->
|
||||
<!-- In this respect, the user's attention is drawn to the risks associated -->
|
||||
<!-- with loading, using, modifying and/or developing or reproducing the -->
|
||||
<!-- software by the user in light of its specific status of free software, -->
|
||||
<!-- that may mean that it is complicated to manipulate, and that also -->
|
||||
<!-- therefore means that it is reserved for developers and experienced -->
|
||||
<!-- professionals having in-depth computer knowledge. Users are therefore -->
|
||||
<!-- encouraged to load and test the software's suitability as regards their -->
|
||||
<!-- requirements in conditions enabling the security of their systems and/or -->
|
||||
<!-- data to be ensured and, more generally, to use and operate it in the -->
|
||||
<!-- same conditions as regards security. -->
|
||||
<!-- -->
|
||||
<!-- The fact that you are presently reading this means that you have had -->
|
||||
<!-- knowledge of the CeCILL license and that you accept its terms. -->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.bigeon.config</groupId>
|
||||
@@ -42,7 +8,7 @@
|
||||
|
||||
<groupId>net.bigeon.gclc</groupId>
|
||||
<artifactId>process</artifactId>
|
||||
<version>0.0.4-SNAPSHOT</version>
|
||||
<version>0.0.7-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>process</name>
|
||||
@@ -83,11 +49,22 @@
|
||||
<dependency>
|
||||
<groupId>net.bigeon</groupId>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>2.0.9</version>
|
||||
<version>2.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.bigeon.test</groupId>
|
||||
<artifactId>junitmt</artifactId>
|
||||
<version>1.0.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.10.3</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||
<artifactId>dsm-maven-plugin</artifactId>
|
||||
|
||||
@@ -132,16 +132,23 @@ public class CommandForeground extends ParametrizedCommand {
|
||||
protected void doExecute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final CommandParameters parameters) throws CommandRunException {
|
||||
String string = parameters.get("pid");
|
||||
final List<String> additionals = parameters.getAdditionals();
|
||||
if (string == null && !additionals.isEmpty()) {
|
||||
if (string == null) {
|
||||
final List<String> additionals = parameters.getAdditionals();
|
||||
if (additionals.isEmpty()) {
|
||||
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||
"Missing process id");
|
||||
}
|
||||
string = additionals.get(0);
|
||||
}
|
||||
if (string == null) {
|
||||
Integer pid;
|
||||
try {
|
||||
pid = Integer.valueOf(string);
|
||||
} catch (final NumberFormatException e) {
|
||||
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||
"Missing process id");
|
||||
"PID should be an integer");
|
||||
}
|
||||
// Join the command.
|
||||
final Task cmd = pool.get(string);
|
||||
final Task cmd = pool.get(pid);
|
||||
if (!(cmd instanceof ForkTask)) {
|
||||
throw new CommandRunException("No such forked process");
|
||||
}
|
||||
@@ -151,7 +158,8 @@ public class CommandForeground extends ParametrizedCommand {
|
||||
delai = Long.parseLong(delaiOpt) * MILLIS_IN_A_SEC;
|
||||
}
|
||||
if (delai < 0) {
|
||||
throw new CommandRunException("Join delai cannot be negative");
|
||||
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||
"Join delai cannot be negative");
|
||||
}
|
||||
((ForkTask) cmd).join(out, in, delai);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
package net.bigeon.gclc.process;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
/*-
|
||||
* #%L
|
||||
* process
|
||||
@@ -43,6 +44,7 @@ import net.bigeon.gclc.command.Command;
|
||||
import net.bigeon.gclc.command.ICommand;
|
||||
import net.bigeon.gclc.command.ICommandProvider;
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
@@ -95,12 +97,18 @@ public class CommandFork extends Command {
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
if (args.length < 1) {
|
||||
throw new CommandRunException("No command to fork");
|
||||
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||
"No command to fork");
|
||||
}
|
||||
final String string = args[0];
|
||||
final ICommand cmd = provider.get(string);
|
||||
if (cmd == null) {
|
||||
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||
MessageFormat.format("No such command {0}", string));
|
||||
}
|
||||
final ICommand cmd = provider.get(args[0]);
|
||||
final String[] inner = Arrays.copyOfRange(args, 1, args.length);
|
||||
final ForkTask task = new ForkCommandTask(cmd, inner, lines);
|
||||
final Thread th = new Thread(task);
|
||||
final Thread th = new Thread(task, MessageFormat.format("fork [{0}]", string));
|
||||
pool.add(task);
|
||||
th.start();
|
||||
}
|
||||
|
||||
@@ -85,7 +85,9 @@ public abstract class ForkTask implements Task {
|
||||
/** THe listeners. */
|
||||
private final Set<InterruptionListener> listeners = new HashSet<>();
|
||||
/** The running state. */
|
||||
private boolean running = false;
|
||||
private boolean running = true;
|
||||
/** The running state. */
|
||||
private boolean started = false;
|
||||
/** The connecting input for this task */
|
||||
protected final ConnectingConsoleInput in = new ConnectingConsoleInput();
|
||||
/** The connecting output for this task */
|
||||
@@ -169,7 +171,7 @@ public abstract class ForkTask implements Task {
|
||||
@Override
|
||||
public final void run() {
|
||||
synchronized (runLock) {
|
||||
running = true;
|
||||
started = true;
|
||||
}
|
||||
try {
|
||||
doRun();
|
||||
@@ -192,4 +194,11 @@ public abstract class ForkTask implements Task {
|
||||
runLock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/** @return the started */
|
||||
public boolean isStarted() {
|
||||
synchronized (runLock) {
|
||||
return started;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ public final class ProcessClear extends Command {
|
||||
@Override
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) {
|
||||
for (final String id : pool.getPIDs()) {
|
||||
for (final Integer id : pool.getPIDs()) {
|
||||
if (!pool.get(id).isRunning()) {
|
||||
pool.remove(id);
|
||||
}
|
||||
@@ -103,14 +103,15 @@ public final class ProcessClear extends Command {
|
||||
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||
@Override
|
||||
public String tip() {
|
||||
return "Request a process to stop (softly)";
|
||||
return "Remove non running processes from pool";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
return null;
|
||||
return " All processes in the pool have their running status checked and the "
|
||||
+ "non running ones are removed from the list of tasks.";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -69,6 +69,8 @@ package net.bigeon.gclc.process;
|
||||
* #L%
|
||||
*/
|
||||
import net.bigeon.gclc.command.Command;
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
@@ -91,8 +93,12 @@ public final class ProcessKill extends Command {
|
||||
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
|
||||
@Override
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) {
|
||||
pool.get(args[0]).setRunning(false);
|
||||
final String... args) throws CommandRunException {
|
||||
if (args.length < 1) {
|
||||
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||
"A pid should be specified.");
|
||||
}
|
||||
pool.get(Integer.parseInt(args[0])).setRunning(false);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -106,7 +112,13 @@ public final class ProcessKill extends Command {
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
return null;
|
||||
return " <pid> is the identification of the process to request the stop to in the pool";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.command.Command#usagePattern() */
|
||||
@Override
|
||||
protected String usagePattern() {
|
||||
return super.usagePattern() + " <pid>";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,9 +99,9 @@ public final class ProcessList extends Command {
|
||||
@Override
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
final ArrayList<String> pids = new ArrayList<>(pool.getPIDs());
|
||||
final ArrayList<Integer> pids = new ArrayList<>(pool.getPIDs());
|
||||
Collections.sort(pids);
|
||||
for (final String string : pids) {
|
||||
for (final Integer string : pids) {
|
||||
try {
|
||||
out.println(MessageFormat.format("{0}\t{1}", string, //$NON-NLS-1$
|
||||
pool.get(string).getName()));
|
||||
@@ -124,7 +124,7 @@ public final class ProcessList extends Command {
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
return null;
|
||||
return " No argument is considered by this command";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ import java.util.Map;
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class TaskPool {
|
||||
/** The running processes. */
|
||||
private final Map<String, Task> running = new HashMap<>();
|
||||
private final Map<Integer, Task> running = new HashMap<>();
|
||||
/** The count for process id. */
|
||||
private int count = 0;
|
||||
/** The lock for pid attribution synchronization. */
|
||||
@@ -99,11 +99,11 @@ public final class TaskPool {
|
||||
*
|
||||
* @param cmd the process
|
||||
* @return the pid */
|
||||
public String add(final Task cmd) {
|
||||
public int add(final Task cmd) {
|
||||
if (cmd == null) {
|
||||
throw new IllegalArgumentException("Task cannot be null"); //$NON-NLS-1$
|
||||
}
|
||||
final String pid;
|
||||
final int pid;
|
||||
synchronized (lock) {
|
||||
pid = getPID();
|
||||
running.put(pid, cmd);
|
||||
@@ -124,21 +124,21 @@ public final class TaskPool {
|
||||
*
|
||||
* @param pid the task id
|
||||
* @return the task, if any, associated to this id */
|
||||
public Task get(final String pid) {
|
||||
public Task get(final int pid) {
|
||||
synchronized (lock) {
|
||||
return running.get(pid);
|
||||
return running.get(Integer.valueOf(pid));
|
||||
}
|
||||
}
|
||||
|
||||
/** Get the next process id.
|
||||
*
|
||||
* @return the process id */
|
||||
private String getPID() {
|
||||
private int getPID() {
|
||||
synchronized (lock) {
|
||||
String pid;
|
||||
int pid;
|
||||
do {
|
||||
pid = Integer.toString(count++);
|
||||
} while (running.containsKey(pid));
|
||||
pid = count++;
|
||||
} while (running.containsKey(Integer.valueOf(pid)));
|
||||
return pid;
|
||||
}
|
||||
}
|
||||
@@ -146,7 +146,7 @@ public final class TaskPool {
|
||||
/** Get the running processes' identifiers.
|
||||
*
|
||||
* @return the pids */
|
||||
public Collection<String> getPIDs() {
|
||||
public Collection<Integer> getPIDs() {
|
||||
return new HashSet<>(running.keySet());
|
||||
}
|
||||
|
||||
@@ -158,10 +158,10 @@ public final class TaskPool {
|
||||
/** Remove a task from the pool
|
||||
*
|
||||
* @param pid the task id */
|
||||
public void remove(final String pid) {
|
||||
public void remove(final int pid) {
|
||||
synchronized (lock) {
|
||||
running.remove(pid);
|
||||
count = Math.min(count, Integer.parseInt(pid));
|
||||
running.remove(Integer.valueOf(pid));
|
||||
count = Math.min(count, (pid));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,12 +68,11 @@ package net.bigeon.gclc.process.io;
|
||||
* #L%
|
||||
*/
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.tools.ConstantString;
|
||||
import net.bigeon.gclc.tools.StringProvider;
|
||||
|
||||
/** A console input that can be connected to and diconnected from.
|
||||
*
|
||||
@@ -82,24 +81,29 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
||||
|
||||
/** The empty string provider. */
|
||||
private static final ConstantString EMPTY_STRING = new ConstantString("");
|
||||
/** The logger. */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(ConnectingConsoleInput.class.getName());
|
||||
|
||||
/** If the input is closed. */
|
||||
private boolean close = false;
|
||||
private boolean close = false;
|
||||
/** The prompt string. */
|
||||
private StringProvider prompt = EMPTY_STRING;
|
||||
/** If the input is currently in prompting state. */
|
||||
private boolean prompting = false;
|
||||
/** The synchronization lock for the rompting status. */
|
||||
private final Object promptLock = new Object();
|
||||
private Supplier<String> prompt = EMPTY_STRING;
|
||||
/** If the input is currently in prompting state.
|
||||
* <p>
|
||||
* To change it you should be in a promptLock. */
|
||||
private boolean prompting = false;
|
||||
/** The synchronization lock for the prompting status. */
|
||||
private final Object promptLock = new Object();
|
||||
/** The synchronization lock for the connection status. */
|
||||
private final Object connectionLock = new Object();
|
||||
/** The connected console input. */
|
||||
private ConsoleInput connected = null;
|
||||
/** The connection state. */
|
||||
private boolean disconnection = false;
|
||||
private final Object connectionLock = new Object();
|
||||
/** The connected console input.
|
||||
* <p>
|
||||
* To use it, you should be in a promptLock and connectionLock. */
|
||||
private ConsoleInput connected = null;
|
||||
/** The connection state.
|
||||
* <p>
|
||||
* To read or modify it, you should be in a connectionLock synchronize block. */
|
||||
private boolean disconnection = false;
|
||||
|
||||
private boolean interrupting = false;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#close() */
|
||||
@@ -114,20 +118,23 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
||||
public void connect(final ConsoleInput input) {
|
||||
disconnect();
|
||||
synchronized (promptLock) {
|
||||
connected = input;
|
||||
promptLock.notifyAll();
|
||||
synchronized (connectionLock) {
|
||||
connected = input;
|
||||
connectionLock.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Disconnect the current input. */
|
||||
public void disconnect() {
|
||||
synchronized (connectionLock) {
|
||||
if (connected != null) {
|
||||
disconnection = true;
|
||||
synchronized (promptLock) {
|
||||
synchronized (promptLock) {
|
||||
if (connected != null) {
|
||||
disconnection = true;
|
||||
connected.interruptPrompt();
|
||||
connected = null;
|
||||
}
|
||||
connected = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -135,7 +142,7 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#getPrompt() */
|
||||
@Override
|
||||
public StringProvider getPrompt() {
|
||||
public Supplier<String> getPrompt() {
|
||||
return prompt;
|
||||
}
|
||||
|
||||
@@ -143,12 +150,18 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#interruptPrompt() */
|
||||
@Override
|
||||
public void interruptPrompt() {
|
||||
synchronized (promptLock) {
|
||||
prompting = false;
|
||||
if (connected != null) {
|
||||
connected.interruptPrompt();
|
||||
synchronized (connectionLock) {
|
||||
synchronized (promptLock) {
|
||||
if (prompting) {
|
||||
interrupting = true;
|
||||
}
|
||||
connectionLock.notifyAll();
|
||||
if (connected != null) {
|
||||
connected.interruptPrompt();
|
||||
}
|
||||
prompting = false;
|
||||
promptLock.notifyAll();
|
||||
}
|
||||
promptLock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,14 +176,14 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt() */
|
||||
@Override
|
||||
public String prompt() throws IOException {
|
||||
return prompt(prompt.apply());
|
||||
return prompt(prompt.get());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(long) */
|
||||
@Override
|
||||
public String prompt(final long timeout) throws IOException {
|
||||
return prompt(prompt.apply(), timeout);
|
||||
return prompt(prompt.get(), timeout);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -180,32 +193,7 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
||||
synchronized (promptLock) {
|
||||
prompting = true;
|
||||
}
|
||||
while (true) {
|
||||
synchronized (promptLock) {
|
||||
if (!prompting) {
|
||||
return null;
|
||||
}
|
||||
if (connected == null) {
|
||||
try {
|
||||
promptLock.wait();
|
||||
} catch (final InterruptedException e) {
|
||||
LOGGER.log(Level.WARNING, "Inerruption of console thread", e);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
} else {
|
||||
final String res = connected.prompt(message);
|
||||
synchronized (connectionLock) {
|
||||
if (disconnection) {
|
||||
disconnection = false;
|
||||
} else if (prompting) {
|
||||
return res;
|
||||
} else {
|
||||
// prompt interrupted, lose the result.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return doPrompt(message, 0, 0);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -215,36 +203,103 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
||||
if (timeout <= 0) {
|
||||
return prompt(message);
|
||||
}
|
||||
final long end = System.currentTimeMillis() + timeout;
|
||||
synchronized (promptLock) {
|
||||
prompting = true;
|
||||
}
|
||||
while (true) {
|
||||
synchronized (promptLock) {
|
||||
if (!prompting) {
|
||||
return null;
|
||||
}
|
||||
if (connected == null) {
|
||||
try {
|
||||
promptLock.wait();
|
||||
} catch (final InterruptedException e) {
|
||||
LOGGER.log(Level.WARNING, "Inerruption of console thread", e);
|
||||
Thread.currentThread().interrupt();
|
||||
final long tic = System.currentTimeMillis();
|
||||
return doPrompt(message, timeout, tic);
|
||||
}
|
||||
|
||||
private String doPrompt(final String message, final long timeout, final long tic)
|
||||
throws IOException {
|
||||
do {
|
||||
if (!checkPrompt()) {
|
||||
break;
|
||||
}
|
||||
getConnection(getTimeoutLeft(tic, timeout));
|
||||
boolean connect;
|
||||
ConsoleInput actualConnected;
|
||||
synchronized (connectionLock) {
|
||||
connect = connected != null;
|
||||
actualConnected = connected;
|
||||
}
|
||||
if (!connect) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
final String res = actualPrompt(message, timeout, tic, actualConnected);
|
||||
synchronized (promptLock) {
|
||||
if (prompting) {
|
||||
prompting = false;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
} catch (final InterruptedIOException e) {
|
||||
// The inner console was interrupted. This can mean we are
|
||||
// disconnecting or actually interrupted.
|
||||
if (disconnection) {
|
||||
disconnection = false;
|
||||
} else {
|
||||
final String res = connected.prompt(message,
|
||||
end - System.currentTimeMillis());
|
||||
synchronized (connectionLock) {
|
||||
if (disconnection) {
|
||||
disconnection = false;
|
||||
} else if (prompting) {
|
||||
return res;
|
||||
} else {
|
||||
// prompt interrupted, lose the result.
|
||||
}
|
||||
}
|
||||
interrupting = false;
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} while (checkTimeout(tic, timeout));
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String actualPrompt(final String message, final long timeout,
|
||||
final long tic, final ConsoleInput actualConnected) throws IOException {
|
||||
final long timeoutLeft = getTimeoutLeft(tic, timeout);
|
||||
if (timeoutLeft == 0) {
|
||||
return actualConnected.prompt(message);
|
||||
}
|
||||
return actualConnected.prompt(message, timeoutLeft);
|
||||
}
|
||||
|
||||
private static boolean checkTimeout(final long tic, final long timeout) {
|
||||
return timeout <= 0 || tic + timeout > System.currentTimeMillis();
|
||||
}
|
||||
|
||||
private static long getTimeoutLeft(final long tic, final long timeout) {
|
||||
if (timeout > 0) {
|
||||
return Math.max(timeout + tic - System.currentTimeMillis(), 1);
|
||||
}
|
||||
if (timeout < 0) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Test if we are in prompting state.
|
||||
*
|
||||
* @return if the process is currently in prompting state.
|
||||
* @throws InterruptedIOException if the prompting state has been interrupted */
|
||||
private boolean checkPrompt() throws InterruptedIOException {
|
||||
synchronized (promptLock) {
|
||||
if (!prompting && interrupting) {
|
||||
interrupting = false;
|
||||
throw new InterruptedIOException("Prompt was interrupted");
|
||||
}
|
||||
return prompting;
|
||||
}
|
||||
}
|
||||
|
||||
private void getConnection(final long timeout) throws InterruptedIOException {
|
||||
boolean connect;
|
||||
synchronized (connectionLock) {
|
||||
connect = connected != null;
|
||||
if (!connect) {
|
||||
try {
|
||||
connectionLock.wait(timeout);
|
||||
} catch (final InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
if (interrupting) {
|
||||
interrupting = false;
|
||||
throw new InterruptedIOException("Prompt ws interrupted");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,19 +307,14 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */
|
||||
@Override
|
||||
public void setPrompt(final String prompt) {
|
||||
this.prompt = new StringProvider() {
|
||||
@Override
|
||||
public String apply() {
|
||||
return prompt;
|
||||
}
|
||||
};
|
||||
this.prompt = new ConstantString(prompt);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(fr.bigeon.gclc.tools.
|
||||
* StringProvider) */
|
||||
@Override
|
||||
public void setPrompt(final StringProvider string) {
|
||||
public void setPrompt(final Supplier<String> string) {
|
||||
prompt = string;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.process;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import net.bigeon.gclc.process.mocks.ForkTaskMock;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class CommandForegroundTest {
|
||||
|
||||
/**
|
||||
* Test method for {@link net.bigeon.gclc.process.CommandForeground#CommandForeground(java.lang.String, net.bigeon.gclc.process.TaskPool)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCommandForeground() {
|
||||
final CommandForeground pl = new CommandForeground("fg", new TaskPool());
|
||||
assertEquals("Command name should be kept as specified", "fg",
|
||||
pl.getCommandName());
|
||||
}
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.process.CommandForeground#doExecute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, net.bigeon.gclc.command.CommandParameters)}.
|
||||
*
|
||||
* @throws CommandRunException if a valid command failed */
|
||||
@Test
|
||||
public void testDoExecuteConsoleOutputConsoleInputCommandParameters()
|
||||
throws CommandRunException {
|
||||
final TaskPool pool = new TaskPool(false);
|
||||
final CommandForeground pl = new CommandForeground("fg", pool);
|
||||
|
||||
try {
|
||||
pl.execute(null, null, "-delai", "1");
|
||||
fail("PID should be mandatory");
|
||||
} catch (final CommandRunException e) {
|
||||
assertEquals("No pid specified is a usage error",
|
||||
CommandRunExceptionType.USAGE, e.getType());
|
||||
}
|
||||
try {
|
||||
pl.execute(null, null, "-pid", "invalid", "-delai", "1");
|
||||
fail("PID should be a number");
|
||||
} catch (final CommandRunException e) {
|
||||
assertEquals("Invalid pid specified is a usage error",
|
||||
CommandRunExceptionType.USAGE, e.getType());
|
||||
}
|
||||
try {
|
||||
pl.execute(null, null, "-pid", "2", "-delai", "1");
|
||||
fail("PID should exist");
|
||||
} catch (final CommandRunException e) {
|
||||
assertEquals("Inexistent pid specified is a run error",
|
||||
CommandRunExceptionType.EXECUTION, e.getType());
|
||||
}
|
||||
|
||||
final ForkTaskMock cmd = new ForkTaskMock();
|
||||
final int id = pool.add(cmd);
|
||||
pl.execute(null, null, "-pid", Integer.toString(id), "-delai", "1");
|
||||
|
||||
try {
|
||||
pl.execute(null, null, "-pid", Integer.toString(id), "-delai", "-1");
|
||||
fail("delai should be a posistive number");
|
||||
} catch (final CommandRunException e) {
|
||||
assertEquals("Negative delai specified is a usage error",
|
||||
CommandRunExceptionType.USAGE, e.getType());
|
||||
}
|
||||
|
||||
assert cmd.getRunCall() == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link net.bigeon.gclc.process.CommandForeground#tip()}.
|
||||
*/
|
||||
@Test
|
||||
public void testTip() {
|
||||
final CommandForeground pl = new CommandForeground("fg", new TaskPool());
|
||||
assertNotNull("Command tip should be defined", pl.tip());
|
||||
assertNotNull("Command usage should be defined", pl.usagePattern());
|
||||
assertNotNull("Command usage should be defined", pl.usageDetail());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.process;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.command.ICommandProvider;
|
||||
import net.bigeon.gclc.command.SubedCommand;
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import net.bigeon.gclc.exception.InvalidCommandName;
|
||||
import net.bigeon.gclc.process.mocks.CommandMock;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class CommandForkTest {
|
||||
|
||||
/**
|
||||
* Test method for {@link net.bigeon.gclc.process.CommandFork#CommandFork(java.lang.String, net.bigeon.gclc.command.ICommandProvider, net.bigeon.gclc.process.TaskPool)}.
|
||||
*/
|
||||
@Test
|
||||
public void testCommandForkStringICommandProviderTaskPool() {
|
||||
final TaskPool pool = new TaskPool();
|
||||
final ICommandProvider provider = new SubedCommand("test");
|
||||
final CommandFork pl = new CommandFork("fork", provider, pool);
|
||||
assertEquals("Command name should be preserved", "fork", pl.getCommandName());
|
||||
}
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.process.CommandFork#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}.
|
||||
*
|
||||
* @throws InvalidCommandName if the test init failed
|
||||
* @throws CommandRunException if the command execution failed in the test */
|
||||
@Test
|
||||
public void testExecute() throws InvalidCommandName, CommandRunException {
|
||||
final TaskPool pool = new TaskPool(false);
|
||||
final ICommandProvider provider = new SubedCommand("test");
|
||||
final CommandFork pl = new CommandFork("fork", provider, pool);
|
||||
|
||||
try {
|
||||
pl.execute(null, null);
|
||||
fail("No command specified should through an exception");
|
||||
} catch (final CommandRunException e) {
|
||||
assertEquals("No command specified is a usage error",
|
||||
CommandRunExceptionType.USAGE, e.getType());
|
||||
}
|
||||
try {
|
||||
pl.execute(null, null, "invalid");
|
||||
fail("Invalid command specified should through an exception");
|
||||
} catch (final CommandRunException e) {
|
||||
assertEquals("Invalid command specified is a usage error",
|
||||
CommandRunExceptionType.USAGE, e.getType());
|
||||
}
|
||||
|
||||
final CommandMock mock = new CommandMock();
|
||||
provider.add(mock);
|
||||
|
||||
pl.execute(null, null, mock.getCommandName());
|
||||
|
||||
final ForkTask task = (ForkTask) pool.get(pool.getPIDs().iterator().next());
|
||||
task.join(null, null, 1000);
|
||||
|
||||
assertEquals("Command should be executed when forked", 1, mock.getExecuteCall());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link net.bigeon.gclc.process.CommandFork#tip()}.
|
||||
*/
|
||||
@Test
|
||||
public void testTip() {
|
||||
final TaskPool pool = new TaskPool();
|
||||
final ICommandProvider provider = new SubedCommand("test");
|
||||
final CommandFork pl = new CommandFork("fork", provider, pool);
|
||||
assertNotNull("Command tip should be defined", pl.tip());
|
||||
assertNotNull("Command usage should be defined", pl.usagePattern());
|
||||
assertNotNull("Command usage should be defined", pl.usageDetail());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.process;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.process.mocks.CommandMock;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class ForkCommandTaskTest {
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.process.ForkCommandTask#ForkCommandTask(net.bigeon.gclc.command.ICommand, java.lang.String[], int)}.
|
||||
*
|
||||
* @throws InterruptedException if there is an error in the thread join */
|
||||
@Test
|
||||
public void testForkCommandTask() throws InterruptedException {
|
||||
final CommandMock cmd = new CommandMock();
|
||||
final ForkCommandTask task = new ForkCommandTask(cmd, new String[0], 1);
|
||||
|
||||
final Thread th = new Thread(task);
|
||||
th.start();
|
||||
th.join();
|
||||
|
||||
assertEquals("Task should be executed once", 1, cmd.getExecuteCall());
|
||||
assertEquals("Names should be consistent between task and command",
|
||||
cmd.getCommandName(), task.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.process;
|
||||
|
||||
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 java.io.IOException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import net.bigeon.gclc.manager.PipedConsoleInput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
|
||||
/** @author Emmanuel Bigeon */
|
||||
public class ForkTaskTest {
|
||||
|
||||
@Test
|
||||
public void testGenericForkTask() throws IOException {
|
||||
final ForkTask task = new ForkTask(5) {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "name";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doRun() throws CommandRunException {
|
||||
String msg;
|
||||
try {
|
||||
msg = in.prompt();
|
||||
} catch (final IOException e) {
|
||||
throw new CommandRunException(CommandRunExceptionType.INTERACTION,
|
||||
"Unable to prompt user");
|
||||
}
|
||||
out.println(msg);
|
||||
}
|
||||
};
|
||||
|
||||
final Thread execThread = new Thread(task, "Task");
|
||||
execThread.start();
|
||||
try {
|
||||
execThread.join(100);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try (PipedConsoleOutput pco = new PipedConsoleOutput();
|
||||
PipedConsoleInput pci = new PipedConsoleInput(null)) {
|
||||
while (!pco.available()) {
|
||||
pci.type("ok");
|
||||
task.join(pco, pci, 1000);
|
||||
}
|
||||
assertEquals("Positive execution", "ok", pco.readNextLine());
|
||||
}
|
||||
assertFalse("Running state should be updated by task on its completion",
|
||||
task.isRunning());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTaskInterruption() throws IOException, InterruptedException {
|
||||
final ForkTask task = new ForkTask(5) {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "name";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doRun() throws CommandRunException {
|
||||
while (isRunning()) {
|
||||
String msg;
|
||||
try {
|
||||
msg = in.prompt(1000);
|
||||
} catch (final IOException e) {
|
||||
throw new CommandRunException(CommandRunExceptionType.INTERACTION,
|
||||
"Unable to prompt user");
|
||||
}
|
||||
if (msg != null) {
|
||||
out.println(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
final Thread execThread = new Thread(task, "Task");
|
||||
execThread.start();
|
||||
execThread.join(100);
|
||||
|
||||
final AtomicBoolean interrupted = new AtomicBoolean(false);
|
||||
final AtomicBoolean interrupted2 = new AtomicBoolean(false);
|
||||
final InterruptionListener listener = new InterruptionListener() {
|
||||
|
||||
@Override
|
||||
public void interrupted() {
|
||||
interrupted.set(true);
|
||||
}
|
||||
};
|
||||
final InterruptionListener listener2 = new InterruptionListener() {
|
||||
|
||||
@Override
|
||||
public void interrupted() {
|
||||
interrupted2.set(true);
|
||||
}
|
||||
};
|
||||
task.addInterruptionListener(listener);
|
||||
task.addInterruptionListener(listener2);
|
||||
task.rmInterruptionListener(listener2);
|
||||
|
||||
assertFalse("Interruption should not be notified before actual interruption",
|
||||
interrupted.get());
|
||||
assertTrue("Task should be started", task.isStarted());
|
||||
task.setRunning(false);
|
||||
try (PipedConsoleOutput pco = new PipedConsoleOutput();
|
||||
PipedConsoleInput pci = new PipedConsoleInput(null)) {
|
||||
task.join(pco, pci, 2000);
|
||||
}
|
||||
execThread.join();
|
||||
assertTrue("Interruption should be notified to listeners", interrupted.get());
|
||||
|
||||
assertFalse("Running state should be updated by task on its completion",
|
||||
task.isRunning());
|
||||
assertFalse("Interruption should not be notified to removed listeners",
|
||||
interrupted2.get());
|
||||
|
||||
task.rmInterruptionListener(listener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailingTask() throws InterruptedException {
|
||||
final ForkTask task = new ForkTask(5) {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "name";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doRun() throws CommandRunException {
|
||||
throw new CommandRunException("Error");
|
||||
}
|
||||
};
|
||||
|
||||
final Thread execThread = new Thread(task, "Task");
|
||||
execThread.start();
|
||||
execThread.join();
|
||||
assertNotNull("Exception should be forwarded", task.getException());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.process;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.command.SubedCommand;
|
||||
import net.bigeon.gclc.exception.InvalidCommandName;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class ProcessAttachementTest {
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.process.ProcessAttachement#attach(net.bigeon.gclc.command.ICommandProvider)}.
|
||||
*
|
||||
* @throws InvalidCommandName if the command has already some subcommand that is
|
||||
* incompatible */
|
||||
@Test
|
||||
public void testAttach() throws InvalidCommandName {
|
||||
final TaskPool pool = new TaskPool();
|
||||
final ProcessAttachement attachement = new ProcessAttachement(pool);
|
||||
final SubedCommand testPoint = new SubedCommand("test");
|
||||
attachement.attach(testPoint);
|
||||
assertNotNull("Commands should be added", testPoint.get("kill"));
|
||||
assertNotNull("Commands should be added", testPoint.get("list"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.process;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.process.mocks.TaskMock;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class ProcessClearTest {
|
||||
|
||||
/**
|
||||
* Test method for {@link net.bigeon.gclc.process.ProcessClear#ProcessClear(java.lang.String, net.bigeon.gclc.process.TaskPool)}.
|
||||
*/
|
||||
@Test
|
||||
public void testProcessClear() {
|
||||
final ProcessClear pl = new ProcessClear("clear", new TaskPool());
|
||||
assertEquals("Command name should be kept as specified", "clear",
|
||||
pl.getCommandName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link net.bigeon.gclc.process.ProcessClear#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}.
|
||||
*/
|
||||
@Test
|
||||
public void testExecute() {
|
||||
final TaskPool pool = new TaskPool();
|
||||
final TaskMock task = new TaskMock();
|
||||
pool.add(task);
|
||||
final ProcessClear pl = new ProcessClear("clear", pool);
|
||||
pl.execute(null, null);
|
||||
assertTrue("cleared pool should be empty", pool.getPIDs().isEmpty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link net.bigeon.gclc.process.ProcessClear#tip()}.
|
||||
*/
|
||||
@Test
|
||||
public void testTip() {
|
||||
final ProcessClear pl = new ProcessClear("clear", new TaskPool());
|
||||
assertNotNull("Command tip should be defined", pl.tip());
|
||||
assertNotNull("Command usage should be defined", pl.usageDetail());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.process;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import net.bigeon.gclc.process.mocks.TaskMock;
|
||||
|
||||
/** @author Emmanuel Bigeon */
|
||||
public class ProcessKillTest {
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.process.ProcessKill#ProcessKill(java.lang.String, net.bigeon.gclc.process.TaskPool)}. */
|
||||
@Test
|
||||
public void testProcessKill() {
|
||||
final ProcessList pl = new ProcessList("kill", new TaskPool());
|
||||
assertEquals("Command name should be kept as specified", "kill",
|
||||
pl.getCommandName());
|
||||
}
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.process.ProcessKill#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}.
|
||||
*
|
||||
* @throws CommandRunException if an error occured in test */
|
||||
@Test
|
||||
public void testExecute() throws CommandRunException {
|
||||
final TaskPool pool = new TaskPool();
|
||||
final TaskMock task = new TaskMock();
|
||||
final int id = pool.add(task);
|
||||
final ProcessKill pl = new ProcessKill("kill", pool);
|
||||
task.reset();
|
||||
try {
|
||||
pl.execute(null, null);
|
||||
fail("Execution with no pid should raise exception");
|
||||
} catch (final CommandRunException e) {
|
||||
assertEquals("Usage exception expected", CommandRunExceptionType.USAGE,
|
||||
e.getType());
|
||||
}
|
||||
pl.execute(null, null, Integer.toString(id));
|
||||
assertEquals("Task should be shutdown", 1, task.getNumberSetRunning());
|
||||
task.reset();
|
||||
pl.execute(null, null, Integer.toString(id));
|
||||
assertEquals("Task should be shutdown", 1, task.getNumberSetRunning());
|
||||
}
|
||||
|
||||
/** Test method for {@link net.bigeon.gclc.process.ProcessKill#tip()}. */
|
||||
@Test
|
||||
public void testTip() {
|
||||
final ProcessKill pl = new ProcessKill("kill", new TaskPool());
|
||||
assertNotNull("Command tip should be defined", pl.tip());
|
||||
assertNotNull("Command usage should be defined", pl.usagePattern());
|
||||
assertNotNull("Command usage should be defined", pl.usageDetail());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.process;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.process.mocks.TaskMock;
|
||||
|
||||
/** @author Emmanuel Bigeon */
|
||||
public class ProcessListTest {
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.process.ProcessList#ProcessList(java.lang.String, net.bigeon.gclc.process.TaskPool)}. */
|
||||
@Test
|
||||
public void testProcessList() {
|
||||
final ProcessList pl = new ProcessList("list", new TaskPool());
|
||||
assertEquals("Command name should be kept as specified", "list",
|
||||
pl.getCommandName());
|
||||
}
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.process.ProcessList#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}.
|
||||
*
|
||||
* @throws CommandRunException if an error occured
|
||||
* @throws IOException if an error in initialization of console occured */
|
||||
@Test
|
||||
public void testExecute() throws CommandRunException, IOException {
|
||||
final TaskPool pool = new TaskPool();
|
||||
final TaskMock task = new TaskMock();
|
||||
pool.add(task);
|
||||
final ProcessList pl = new ProcessList("list", pool);
|
||||
final PipedConsoleOutput out = new PipedConsoleOutput();
|
||||
pl.execute(out, null);
|
||||
|
||||
assertTrue("Task should be contained in list",
|
||||
out.readNextLine().contains(task.getName()));
|
||||
|
||||
out.close();
|
||||
|
||||
try {
|
||||
pl.execute(out, null);
|
||||
fail("Printing element to closed console should raise interaction exception");
|
||||
} catch (final CommandRunException e) {
|
||||
assertEquals("Console printing error should be an interaction error",
|
||||
CommandRunExceptionType.INTERACTION, e.getType());
|
||||
}
|
||||
}
|
||||
|
||||
/** Test method for {@link net.bigeon.gclc.process.ProcessList#tip()}. */
|
||||
@Test
|
||||
public void testTip() {
|
||||
final ProcessList pl = new ProcessList("list", new TaskPool());
|
||||
assertNotNull("Command tip should be defined", pl.tip());
|
||||
assertNotNull("Command usage should be defined", pl.usageDetail());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.process;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.command.SubedCommand;
|
||||
import net.bigeon.gclc.exception.InvalidCommandName;
|
||||
|
||||
/** @author Emmanuel Bigeon */
|
||||
public class ScreenAttachementTest {
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.process.ScreenAttachement#attach(net.bigeon.gclc.command.ICommandProvider)}.
|
||||
*
|
||||
* @throws InvalidCommandName if an error occured in attachement */
|
||||
@Test
|
||||
public void testAttach() throws InvalidCommandName {
|
||||
final TaskPool pool = new TaskPool();
|
||||
final ScreenAttachement attachement = new ScreenAttachement(pool, 15);
|
||||
final SubedCommand testPoint = new SubedCommand("test");
|
||||
attachement.attach(testPoint);
|
||||
assertNotNull("Commands should be added", testPoint.get("terminate"));
|
||||
assertNotNull("Commands should be added", testPoint.get("list"));
|
||||
assertNotNull("Commands should be added", testPoint.get("clear"));
|
||||
assertNotNull("Commands should be added", testPoint.get("fg"));
|
||||
assertNotNull("Commands should be added", testPoint.get("fork"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package net.bigeon.gclc.process;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.process.mocks.TaskMock;
|
||||
|
||||
public class TaskPoolTest {
|
||||
|
||||
public TaskMock task = new TaskMock();
|
||||
public TaskPool pool = new TaskPool(false);
|
||||
|
||||
@Test
|
||||
public void testTaskPool() {
|
||||
assertFalse("task pool should not auto clear if specified",
|
||||
pool.isAutoClearing());
|
||||
pool = new TaskPool();
|
||||
assertTrue("Default task pool should not auto clear", pool.isAutoClearing());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
final int id = pool.add(task);
|
||||
assertTrue("Added task pid should be in pool", pool.getPIDs().contains(id));
|
||||
assertEquals("Added task should be in pool", task, pool.get(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet() {
|
||||
assertNull("Inexistent task id should provide null as task in pool",
|
||||
pool.get(60));
|
||||
final int id = pool.add(task);
|
||||
assertEquals("Added task should be in pool at its id", task, pool.get(id));
|
||||
assertNull("Added task should be in pool at its id", pool.get(id + 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPIDs() {
|
||||
assertTrue("new pool should be empty", pool.getPIDs().isEmpty());
|
||||
final int id = pool.add(task);
|
||||
assertNull("Added task should be in pool at its id", pool.get(id + 1));
|
||||
assertEquals(
|
||||
"Pool task addition should change the pid collection size accordingly", 1,
|
||||
pool.getPIDs().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemove() {
|
||||
assertTrue("new pool should be empty", pool.getPIDs().isEmpty());
|
||||
pool.remove(60);
|
||||
assertTrue("removal of inexistent task should be NOP", pool.getPIDs().isEmpty());
|
||||
final int id = pool.add(task);
|
||||
assertEquals(
|
||||
"Pool task addition should change the pid collection size accordingly", 1,
|
||||
pool.getPIDs().size());
|
||||
pool.remove(id + 1);
|
||||
assertEquals("removal of inexistent task should be NOP", 1,
|
||||
pool.getPIDs().size());
|
||||
pool.remove(id);
|
||||
assertTrue("removal of task should reduce the number of tasks",
|
||||
pool.getPIDs().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShutdown() {
|
||||
final int id = pool.add(task);
|
||||
task.reset();
|
||||
pool.shutdown();
|
||||
assertEquals("Shutdown should request shut down of tasks, only once", 1,
|
||||
task.getNumberSetRunning());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.process;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/** @author Emmanuel Bigeon */
|
||||
public class TaskSpawnerTest {
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.process.TaskSpawner#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}.
|
||||
*
|
||||
* @throws CommandRunException if an error occurred */
|
||||
@Test
|
||||
public void testExecute() throws CommandRunException {
|
||||
final Task task = new Task() {
|
||||
@Override
|
||||
public void run() {
|
||||
//
|
||||
}
|
||||
@Override
|
||||
public void setRunning(final boolean running) {
|
||||
//
|
||||
}
|
||||
@Override
|
||||
public void rmInterruptionListener(final InterruptionListener listener) {
|
||||
//
|
||||
}
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public String getName() {
|
||||
return "abc";
|
||||
}
|
||||
@Override
|
||||
public void addInterruptionListener(final InterruptionListener listener) {
|
||||
//
|
||||
}
|
||||
};
|
||||
final TaskPool pool = new TaskPool();
|
||||
assertTrue("Pool not empty", pool.getPIDs().isEmpty());
|
||||
final ExecutorService service = Executors.newSingleThreadExecutor();
|
||||
final TaskSpawner spawner = new TaskSpawner("name", pool, service) {
|
||||
|
||||
@Override
|
||||
public String tip() {
|
||||
return "tip";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
return "no details";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Task createTask(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
return task;
|
||||
}
|
||||
};
|
||||
spawner.execute(null, null);
|
||||
assertEquals("Added the task", 1, pool.getPIDs().size());
|
||||
final Integer id = pool.getPIDs().iterator().next();
|
||||
assertEquals("Added task is not the one", task, pool.get(id));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package net.bigeon.gclc.process.io;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.manager.PipedConsoleInput;
|
||||
import net.bigeon.gclc.manager.StreamConsoleInput;
|
||||
import net.bigeon.gclc.tools.ConstantString;
|
||||
import net.bigeon.test.junitmt.ATestRunnable;
|
||||
import net.bigeon.test.junitmt.FunctionalTestRunnable;
|
||||
import net.bigeon.test.junitmt.TestFunction;
|
||||
import net.bigeon.test.junitmt.ThreadTest;
|
||||
|
||||
public class ConnectingConsoleInputTest {
|
||||
|
||||
@Test
|
||||
public void testConnect() throws IOException {
|
||||
final ConnectingConsoleInput in = new ConnectingConsoleInput();
|
||||
final PipedConsoleInput pci = new PipedConsoleInput(null);
|
||||
assertNull("Inputs should not transmit before connection", in.prompt(1000));
|
||||
in.connect(pci);
|
||||
pci.type("Some message");
|
||||
assertEquals("Messages should be buffered", "Some message", in.prompt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisconnect() throws IOException {
|
||||
final ConnectingConsoleInput in = new ConnectingConsoleInput();
|
||||
final PipedConsoleInput pci = new PipedConsoleInput(null);
|
||||
assertNull("Inputs should not transmit before connection", in.prompt(1000));
|
||||
in.connect(pci);
|
||||
pci.type("Some message");
|
||||
assertEquals("Messages should be buffered", "Some message", in.prompt());
|
||||
in.disconnect();
|
||||
assertNull("Disconnected inputs should not transmit anymore", in.prompt(1000));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsClosed() throws IOException {
|
||||
final ConnectingConsoleInput in = new ConnectingConsoleInput();
|
||||
assertFalse("New input should not be closed", in.isClosed());
|
||||
final PipedConsoleInput pci = new PipedConsoleInput(null);
|
||||
in.connect(pci);
|
||||
pci.close();
|
||||
assertFalse("Connected input closing should not close connecting", in.isClosed());
|
||||
final PipedConsoleInput pci2 = new PipedConsoleInput(null);
|
||||
in.connect(pci2);
|
||||
in.close();
|
||||
assertTrue("Input should close on request", in.isClosed());
|
||||
assertFalse("Connected input should not be closed by connecting closure",
|
||||
pci2.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCoveragePrompt() {
|
||||
final ConnectingConsoleInput in = new ConnectingConsoleInput();
|
||||
in.setPrompt("test");
|
||||
assertEquals("Prompt should be set correctly", "test",
|
||||
((ConstantString) in.getPrompt()).get());
|
||||
final ConstantString prompt = new ConstantString("other");
|
||||
in.setPrompt(prompt);
|
||||
assertEquals("Prompt should be set correctly", prompt, in.getPrompt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPromptSequence() throws IOException, InterruptedException {
|
||||
final ConnectingConsoleInput in = new ConnectingConsoleInput();
|
||||
// Unconnected
|
||||
final AtomicBoolean ended = new AtomicBoolean(false);
|
||||
final TestFunction one = new TestFunction() {
|
||||
|
||||
@Override
|
||||
public void apply() throws Exception {
|
||||
try {
|
||||
final String res = in.prompt("m1", -1);
|
||||
fail("interruption of infinite waiting prompt should cause error, but was "
|
||||
+ res);
|
||||
} catch (final InterruptedIOException e) {
|
||||
// ok
|
||||
}
|
||||
try {
|
||||
final String res = in.prompt("m2", 25000);
|
||||
fail("interruption of finite waiting prompt should cause error, but was "
|
||||
+ res);
|
||||
} catch (final InterruptedIOException e) {
|
||||
// ok
|
||||
}
|
||||
synchronized (ended) {
|
||||
ended.set(true);
|
||||
try {
|
||||
assertNull("Overtime should return null", in.prompt("m3", 200));
|
||||
} catch (final InterruptedIOException e) {
|
||||
fail("Unexpected interruption error in overtime");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
final ATestRunnable runnable = new FunctionalTestRunnable(one);
|
||||
final Thread th = new Thread(runnable);
|
||||
final Thread inter = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!ended.get()) {
|
||||
try {
|
||||
th.join(100);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
synchronized (ended) {
|
||||
if (!ended.get()) {
|
||||
in.interruptPrompt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
th.start();
|
||||
inter.start();
|
||||
ThreadTest.assertRuns(th, runnable);
|
||||
inter.join();
|
||||
ended.set(false);
|
||||
|
||||
final PipedOutputStream os = new PipedOutputStream();
|
||||
final PipedInputStream pis = new PipedInputStream(os);
|
||||
in.connect(new StreamConsoleInput(null, pis, StandardCharsets.UTF_8));
|
||||
final ATestRunnable runnable2 = new FunctionalTestRunnable(one);
|
||||
final Thread th2 = new Thread(runnable2);
|
||||
final Thread inter2 = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!ended.get()) {
|
||||
try {
|
||||
th2.join(100);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
synchronized (ended) {
|
||||
if (!ended.get()) {
|
||||
in.interruptPrompt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
th2.start();
|
||||
inter2.start();
|
||||
ThreadTest.assertRuns(th2, runnable2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package net.bigeon.gclc.process.io;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
|
||||
public class ConnectingConsoleOutputTest {
|
||||
|
||||
@Test
|
||||
public void testConnect() throws IOException {
|
||||
final ConnectingConsoleOutput out = new ConnectingConsoleOutput(
|
||||
ConnectingConsoleOutput.QUEUE, 10);
|
||||
final PipedConsoleOutput output = new PipedConsoleOutput();
|
||||
out.println("Some message");
|
||||
assertFalse("Message should not be transmitted", output.available());
|
||||
// Connect
|
||||
out.connect(output);
|
||||
|
||||
// Connected outputs should receive data
|
||||
final String res = output.readNextLine();
|
||||
assertEquals("Message should be correctly transmitted", "Some message", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisconnect() throws IOException {
|
||||
final ConnectingConsoleOutput out = new ConnectingConsoleOutput(
|
||||
ConnectingConsoleOutput.QUEUE, 10);
|
||||
final PipedConsoleOutput output = new PipedConsoleOutput();
|
||||
// NOP disconnection when no connection
|
||||
out.disconnect();
|
||||
// Connect
|
||||
out.connect(output);
|
||||
|
||||
// Connected outputs should receive data
|
||||
out.println("Some message");
|
||||
final String res = output.readNextLine();
|
||||
assertEquals("Message should be correctly transmitted", "Some message", res);
|
||||
out.disconnect();
|
||||
out.println("Some message");
|
||||
assertFalse("Message should not be transmitted", output.available());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsClosed() throws IOException {
|
||||
final ConnectingConsoleOutput out = new ConnectingConsoleOutput(
|
||||
ConnectingConsoleOutput.QUEUE, 10);
|
||||
final PipedConsoleOutput output = new PipedConsoleOutput();
|
||||
assertFalse("New output should not be closed", out.isClosed());
|
||||
// Connect
|
||||
out.connect(output);
|
||||
|
||||
output.close();
|
||||
|
||||
assertFalse("Output should not close when connected outputs do", out.isClosed());
|
||||
final PipedConsoleOutput output2 = new PipedConsoleOutput();
|
||||
out.connect(output2);
|
||||
out.close();
|
||||
assertTrue("Output should close on request", out.isClosed());
|
||||
assertFalse("Output should not close connected output when they do",
|
||||
output2.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrint() throws IOException {
|
||||
final ConnectingConsoleOutput out = new ConnectingConsoleOutput(
|
||||
ConnectingConsoleOutput.PERSIST, 10);
|
||||
|
||||
out.print("Some text");
|
||||
out.println();
|
||||
final PipedConsoleOutput output = new PipedConsoleOutput();
|
||||
out.connect(output);
|
||||
String res = output.readNextLine();
|
||||
assertEquals("Message should be correctly transmitted", "Some text", res);
|
||||
out.disconnect();
|
||||
|
||||
// Persist should be reprinted on any connect
|
||||
out.connect(output);
|
||||
res = output.readNextLine();
|
||||
assertEquals("Message should be correctly transmitted", "Some text", res);
|
||||
out.disconnect();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.process.mocks;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import net.bigeon.gclc.command.ICommand;
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/** @author Emmanuel Bigeon */
|
||||
public class CommandMock implements ICommand {
|
||||
|
||||
private int executeCall = 0;
|
||||
private int tipCall = 0;
|
||||
private int helpCall = 0;
|
||||
private int commandNameCall = 0;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.command.ICommand#execute(net.bigeon.gclc.manager.
|
||||
* ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
|
||||
@Override
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
executeCall++;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.command.ICommand#getCommandName() */
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
commandNameCall++;
|
||||
return "name";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see
|
||||
* net.bigeon.gclc.command.ICommand#help(net.bigeon.gclc.manager.ConsoleOutput,
|
||||
* java.lang.String[]) */
|
||||
@Override
|
||||
public void help(final ConsoleOutput output, final String... args)
|
||||
throws IOException {
|
||||
helpCall++;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.command.ICommand#tip() */
|
||||
@Override
|
||||
public String tip() {
|
||||
tipCall++;
|
||||
return "tip";
|
||||
}
|
||||
|
||||
/** @return the executeCall */
|
||||
public int getExecuteCall() {
|
||||
return executeCall;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.process.mocks;
|
||||
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.process.ForkTask;
|
||||
import net.bigeon.gclc.process.Task;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class ForkTaskMock extends ForkTask implements Task {
|
||||
|
||||
private int runCall = 0;
|
||||
|
||||
/** Default constructor. */
|
||||
public ForkTaskMock() {
|
||||
super(10);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.process.Task#getName()
|
||||
*/
|
||||
@Override
|
||||
public String getName() {
|
||||
return "name";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.process.ForkTask#doRun()
|
||||
*/
|
||||
@Override
|
||||
protected void doRun() throws CommandRunException {
|
||||
runCall++;
|
||||
}
|
||||
|
||||
/** @return the runCall */
|
||||
public int getRunCall() {
|
||||
return runCall;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package net.bigeon.gclc.process.mocks;
|
||||
|
||||
import net.bigeon.gclc.process.InterruptionListener;
|
||||
import net.bigeon.gclc.process.Task;
|
||||
|
||||
public class TaskMock implements Task {
|
||||
|
||||
private int runCall = 0;
|
||||
private int setRunningCall = 0;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
runCall++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterruptionListener(final InterruptionListener listener) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "name";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rmInterruptionListener(final InterruptionListener listener) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRunning(final boolean running) {
|
||||
setRunningCall++;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void reset() {
|
||||
runCall = 0;
|
||||
setRunningCall = 0;
|
||||
}
|
||||
|
||||
/** @return the number of calls to set running method */
|
||||
public int getNumberSetRunning() {
|
||||
return setRunningCall;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/** Mock definitions for testing.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
package net.bigeon.gclc.process.mocks;
|
||||
@@ -1,63 +1,22 @@
|
||||
|
||||
<!-- GCLC Socket, Socket implementation of GCLC -->
|
||||
<!-- Copyright (C) 2014-2017 E. Bigeon -->
|
||||
<!-- mailto:emmanuel@bigeon.fr -->
|
||||
<!-- -->
|
||||
<!-- This software is governed by the CeCILL license under French law and -->
|
||||
<!-- abiding by the rules of distribution of free software. You can use, -->
|
||||
<!-- modify and/or redistribute the software under the terms of the CeCILL -->
|
||||
<!-- license as circulated by CEA, CNRS and INRIA at the following URL -->
|
||||
<!-- "http://www.cecill.info". -->
|
||||
<!-- -->
|
||||
<!-- As a counterpart to the access to the source code and rights to copy, -->
|
||||
<!-- modify and redistribute granted by the license, users are provided only -->
|
||||
<!-- with a limited warranty and the software's author, the holder of the -->
|
||||
<!-- economic rights, and the successive licensors have only limited -->
|
||||
<!-- liability. -->
|
||||
<!-- -->
|
||||
<!-- In this respect, the user's attention is drawn to the risks associated -->
|
||||
<!-- with loading, using, modifying and/or developing or reproducing the -->
|
||||
<!-- software by the user in light of its specific status of free software, -->
|
||||
<!-- that may mean that it is complicated to manipulate, and that also -->
|
||||
<!-- therefore means that it is reserved for developers and experienced -->
|
||||
<!-- professionals having in-depth computer knowledge. Users are therefore -->
|
||||
<!-- encouraged to load and test the software's suitability as regards their -->
|
||||
<!-- requirements in conditions enabling the security of their systems and/or -->
|
||||
<!-- data to be ensured and, more generally, to use and operate it in the -->
|
||||
<!-- same conditions as regards security. -->
|
||||
<!-- -->
|
||||
<!-- The fact that you are presently reading this means that you have had -->
|
||||
<!-- knowledge of the CeCILL license and that you accept its terms. -->
|
||||
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>socket</artifactId>
|
||||
<version>1.1.12-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<url>https://bigeon.net/projects/gclc.html</url>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.scm.id>git.code.bigeon.net</project.scm.id>
|
||||
<license.licenseName>cecill_2.1</license.licenseName>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.bigeon</groupId>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>2.0.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.bigeon</groupId>
|
||||
<artifactId>smu</artifactId>
|
||||
<version>1.0.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<parent>
|
||||
<groupId>net.bigeon.config</groupId>
|
||||
<artifactId>ebigeon-config</artifactId>
|
||||
<version>1.8.21</version>
|
||||
<artifactId>ebigeon-public-conf</artifactId>
|
||||
<version>1.0.10</version>
|
||||
</parent>
|
||||
<groupId>net.bigeon.gclc</groupId>
|
||||
<artifactId>socket</artifactId>
|
||||
<version>1.1.19-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>GCLC Socket</name>
|
||||
<description>Socket implementation of GCLC</description>
|
||||
<url>https://bigeon.net/projects/gclc.html</url>
|
||||
<inceptionYear>2016</inceptionYear>
|
||||
<organization>
|
||||
<name>Bigeon</name>
|
||||
<url>https://bigeon.net/</url>
|
||||
</organization>
|
||||
<licenses>
|
||||
<license>
|
||||
<distribution>manual</distribution>
|
||||
@@ -65,15 +24,6 @@
|
||||
<url>https://cecill.info/licences/Licence_CeCILL_V2.1-en.html</url>
|
||||
</license>
|
||||
</licenses>
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||
<artifactId>dsm-maven-plugin</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
<developers>
|
||||
<developer>
|
||||
<email>emmanuel@bigeon.fr</email>
|
||||
@@ -84,16 +34,44 @@
|
||||
</roles>
|
||||
</developer>
|
||||
</developers>
|
||||
<name>GCLC Socket</name>
|
||||
<description>Socket implementation of GCLC</description>
|
||||
<scm>
|
||||
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
|
||||
<tag>HEAD</tag>
|
||||
</scm>
|
||||
<groupId>net.bigeon.gclc</groupId>
|
||||
<inceptionYear>2016</inceptionYear>
|
||||
<organization>
|
||||
<name>Bigeon</name>
|
||||
<url>https://bigeon.net/</url>
|
||||
</organization>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<license.licenseName>cecill_2.1</license.licenseName>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.bigeon</groupId>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>2.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.bigeon</groupId>
|
||||
<artifactId>smu</artifactId>
|
||||
<version>1.0.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.bigeon.test</groupId>
|
||||
<artifactId>junitmt</artifactId>
|
||||
<version>1.0.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.10.3</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||
<artifactId>dsm-maven-plugin</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
</project>
|
||||
|
||||
@@ -143,7 +143,8 @@ public final class DConnexionManager<T> implements ConnexionManager<T> {
|
||||
*
|
||||
* @return a new ID */
|
||||
private String newID() {
|
||||
return "Client " + count++; //$NON-NLS-1$
|
||||
final int c = count++;
|
||||
return "Client " + c; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.socket;
|
||||
|
||||
/** Interface for listener of brutal disconnection from a pluggable
|
||||
* input/output.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
@FunctionalInterface
|
||||
public interface DisconnexionListener {
|
||||
/** Indicate a brutal disconnection */
|
||||
void disconnected();
|
||||
}
|
||||
@@ -41,14 +41,17 @@ import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.tools.ConstantString;
|
||||
import net.bigeon.gclc.tools.StringProvider;
|
||||
import net.bigeon.gclc.utils.ReadingRunnable;
|
||||
|
||||
/** A console input where the stream can be plugged.
|
||||
@@ -75,7 +78,7 @@ public final class PluggableConsoleInput implements ConsoleInput {
|
||||
/** If the element is closed. */
|
||||
private boolean closed = false;
|
||||
/** The default prompt. */
|
||||
private StringProvider prompt = new ConstantString("> "); //$NON-NLS-1$
|
||||
private Supplier<String> prompt = new ConstantString("> "); //$NON-NLS-1$
|
||||
/** If the input is plugged or buffering. */
|
||||
private boolean connected = false;
|
||||
/** The current connexion (if any). */
|
||||
@@ -87,6 +90,8 @@ public final class PluggableConsoleInput implements ConsoleInput {
|
||||
/** The output for hints. */
|
||||
private PrintStream output;
|
||||
|
||||
private final Set<DisconnexionListener> listeners = new HashSet<>();
|
||||
|
||||
// Locks
|
||||
/** The lock for connexion and disconnexion of actual streams. */
|
||||
private final Object connexionLock = new Object();
|
||||
@@ -119,6 +124,7 @@ public final class PluggableConsoleInput implements ConsoleInput {
|
||||
if (prompting) {
|
||||
// print the hint, to indicate we are waiting for a user input.
|
||||
out.print(hint);
|
||||
out.println();
|
||||
out.flush();
|
||||
}
|
||||
final InputStreamReader streamReader = new InputStreamReader(stream,
|
||||
@@ -146,7 +152,7 @@ public final class PluggableConsoleInput implements ConsoleInput {
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#getPrompt() */
|
||||
@Override
|
||||
public StringProvider getPrompt() {
|
||||
public Supplier<String> getPrompt() {
|
||||
return prompt;
|
||||
}
|
||||
|
||||
@@ -175,14 +181,14 @@ public final class PluggableConsoleInput implements ConsoleInput {
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt() */
|
||||
@Override
|
||||
public String prompt() throws IOException {
|
||||
return prompt(prompt.apply());
|
||||
return prompt(prompt.get());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(long) */
|
||||
@Override
|
||||
public String prompt(final long timeout) throws IOException {
|
||||
return prompt(prompt.apply(), timeout);
|
||||
return prompt(prompt.get(), timeout);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -264,7 +270,7 @@ public final class PluggableConsoleInput implements ConsoleInput {
|
||||
* @see net.bigeon.gclc.manager.ConsoleInput#setPrompt(net.bigeon.gclc.tools.
|
||||
* StringProvider) */
|
||||
@Override
|
||||
public void setPrompt(final StringProvider prompt) {
|
||||
public void setPrompt(final Supplier<String> prompt) {
|
||||
this.prompt = prompt;
|
||||
}
|
||||
|
||||
@@ -279,10 +285,48 @@ public final class PluggableConsoleInput implements ConsoleInput {
|
||||
final long connexionTimeout) throws IOException, InterruptedException {
|
||||
synchronized (connexionLock) {
|
||||
if (connected) {
|
||||
return connexion.getNextMessage(messageTimeout);
|
||||
try {
|
||||
return connexion.getNextMessage(messageTimeout);
|
||||
} catch (final InterruptedIOException e) {
|
||||
throw e;
|
||||
} catch (final IOException e) {
|
||||
LOGGER.log(Level.INFO, "Communication was abrubptly interrupted", e);
|
||||
brutalDisconnection();
|
||||
}
|
||||
}
|
||||
connexionLock.wait(connexionTimeout);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private void brutalDisconnection() {
|
||||
// clean up the disconnection
|
||||
disconnect();
|
||||
// notify listeners
|
||||
for (final DisconnexionListener listener : listeners) {
|
||||
listener.disconnected();
|
||||
}
|
||||
}
|
||||
|
||||
/** Add a listener to the list of listeners.
|
||||
*
|
||||
* @param e the listener
|
||||
* @return if the listener was added
|
||||
* @see java.util.Set#add(java.lang.Object) */
|
||||
public boolean add(final DisconnexionListener e) {
|
||||
return listeners.add(e);
|
||||
}
|
||||
|
||||
/** Remove a listener from the list of listeners.
|
||||
*
|
||||
* @param o the listener
|
||||
* @return if the listener was removed
|
||||
* @see java.util.Set#remove(java.lang.Object) */
|
||||
public boolean remove(final DisconnexionListener o) {
|
||||
return listeners.remove(o);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -50,7 +50,10 @@ import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
/** A {@link Command} to disconnect elements from a {@link ConnexionManager}.
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
* @param <T> the type of connected object */
|
||||
* @param <T> the type of connected object
|
||||
* @deprecated since 1.1.17, this has been moved to
|
||||
* {@link RemoteDisconnectCommand}. */
|
||||
@Deprecated
|
||||
public final class RemoteDisconnectCommand<T> extends Command {
|
||||
|
||||
/** The connexion manager. */
|
||||
@@ -123,8 +126,7 @@ public final class RemoteDisconnectCommand<T> extends Command {
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
return MessageFormat.format(
|
||||
" If arguments are provided the corresponding connexions are closed, "
|
||||
+ "otherwise{0}{1} are.",
|
||||
System.lineSeparator(), all ? "all connexions" : "none");
|
||||
" If arguments are provided the corresponding connexions are closed, otherwise\n{0} are.",
|
||||
all ? "all connexions" : "none");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,6 +77,8 @@ public final class SocketConsoleApplicationShell implements Runnable {
|
||||
/** The application. */
|
||||
private ConsoleApplication app;
|
||||
|
||||
private final Object initLock = new Object();
|
||||
|
||||
/** The server socket. */
|
||||
private ServerSocket serverSocket;
|
||||
/** THe server address. */
|
||||
@@ -123,7 +125,10 @@ public final class SocketConsoleApplicationShell implements Runnable {
|
||||
// Create the server
|
||||
try (ServerSocket actualServerSocket = new ServerSocket(port, 1, addr)) {
|
||||
serverSocket = actualServerSocket;
|
||||
running = true;
|
||||
synchronized (initLock) {
|
||||
running = true;
|
||||
initLock.notifyAll();
|
||||
}
|
||||
// Create the streams
|
||||
runSokectServer();
|
||||
} catch (final IOException e) {
|
||||
@@ -194,4 +199,23 @@ public final class SocketConsoleApplicationShell implements Runnable {
|
||||
LOGGER.log(Level.SEVERE, "Exception in closing socket server", e); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
/** A method to wait for the socket server initialization.
|
||||
*
|
||||
* @param timeout the timeout */
|
||||
public void waitStartUp(final long timeout) {
|
||||
synchronized (initLock) {
|
||||
final long tic = System.currentTimeMillis() + timeout;
|
||||
long tac;
|
||||
while (!running && (tac = System.currentTimeMillis()) < tic) {
|
||||
final long idle = tic - tac;
|
||||
try {
|
||||
initLock.wait(idle);
|
||||
} catch (final InterruptedException e) {
|
||||
LOGGER.log(Level.INFO, "Thread interruption", e);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* gclc-socket:net.bigeon.gclc.socket.RemoteDisconnectCommand.java
|
||||
* Created on: Nov 18, 2017
|
||||
*/
|
||||
package net.bigeon.gclc.socket.cmd;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC Socket
|
||||
* %%
|
||||
* Copyright (C) 2016 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.bigeon.gclc.command.Command;
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
import net.bigeon.gclc.socket.ConnexionManager;
|
||||
|
||||
/** A {@link Command} to disconnect elements from a {@link ConnexionManager}.
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
* @param <T> the type of connected object */
|
||||
public final class ConnexionListCommand<T> extends Command {
|
||||
|
||||
/** The connexion manager. */
|
||||
private final ConnexionManager<T> manager;
|
||||
|
||||
/** Create the connexion listing command.
|
||||
*
|
||||
* @param name the command name
|
||||
* @param manager the manager */
|
||||
public ConnexionListCommand(final String name, final ConnexionManager<T> manager) {
|
||||
super(name);
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @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 {
|
||||
final Collection<String> coll = manager.getConnected();
|
||||
try {
|
||||
for (final String string : coll) {
|
||||
out.println(string);
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
throw new CommandRunException(CommandRunExceptionType.INTERACTION,
|
||||
"User cannot be notified", e);
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||
@Override
|
||||
public String tip() {
|
||||
return "List current connexions."; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* gclc-socket:net.bigeon.gclc.socket.RemoteDisconnectCommand.java
|
||||
* Created on: Nov 18, 2017
|
||||
*/
|
||||
package net.bigeon.gclc.socket.cmd;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC Socket
|
||||
* %%
|
||||
* Copyright (C) 2016 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
import java.io.IOException;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Collection;
|
||||
|
||||
import net.bigeon.gclc.command.Command;
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
import net.bigeon.gclc.socket.ConnexionManager;
|
||||
|
||||
/** A {@link Command} to disconnect elements from a {@link ConnexionManager}.
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
* @param <T> the type of connected object */
|
||||
public final class RemoteDisconnectCommand<T> extends Command {
|
||||
|
||||
/** The connexion manager. */
|
||||
private final ConnexionManager<T> manager;
|
||||
/** If all connexion should be disconnected when no argument have been
|
||||
* specified. */
|
||||
private final boolean all;
|
||||
|
||||
/** Create the disconnection command.
|
||||
*
|
||||
* @param name the command name
|
||||
* @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) {
|
||||
super(name);
|
||||
this.manager = manager;
|
||||
this.all = all;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @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 {
|
||||
if (args.length == 0 && all) {
|
||||
final Collection<String> coll = manager.getConnected();
|
||||
for (final String string : coll) {
|
||||
manager.disconnect(string);
|
||||
}
|
||||
}
|
||||
for (final String string : args) {
|
||||
if (manager.isConnected(string)) {
|
||||
manager.disconnect(string);
|
||||
} else {
|
||||
print(out,
|
||||
MessageFormat.format("[WARNING] {0} is not connected", string)); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Print a message if the output is defined.
|
||||
*
|
||||
* @param out the output
|
||||
* @param string the message
|
||||
* @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,
|
||||
"Unable to print to existing output", e); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||
@Override
|
||||
public String tip() {
|
||||
return "Close a connexion."; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
final StringBuilder builder = new StringBuilder(
|
||||
" If arguments are provided the corresponding connexions are closed, otherwise\n");
|
||||
if (all) {
|
||||
builder.append("all connections");
|
||||
} else {
|
||||
builder.append("none");
|
||||
}
|
||||
builder.append(" are.");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.socket.cmd;
|
||||
@@ -90,7 +90,7 @@ public class PluggableConsoleInputTest {
|
||||
|
||||
}
|
||||
try {
|
||||
Object obj = new Object();
|
||||
final Object obj = new Object();
|
||||
synchronized (obj) {
|
||||
obj.wait(100);
|
||||
}
|
||||
@@ -115,7 +115,7 @@ public class PluggableConsoleInputTest {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Object obj = new Object();
|
||||
final Object obj = new Object();
|
||||
synchronized (obj) {
|
||||
obj.wait(200);
|
||||
}
|
||||
@@ -168,7 +168,7 @@ public class PluggableConsoleInputTest {
|
||||
});
|
||||
th.start();
|
||||
|
||||
Object obj = new Object();
|
||||
final Object obj = new Object();
|
||||
synchronized (obj) {
|
||||
while (!input.isPrompting()) {
|
||||
obj.wait(10);
|
||||
@@ -211,8 +211,8 @@ public class PluggableConsoleInputTest {
|
||||
@Test
|
||||
public final void testGetPrompt() {
|
||||
final PluggableConsoleInput input = new PluggableConsoleInput();
|
||||
assertEquals("Default prompt invalid", "> ", input.getPrompt().apply());
|
||||
assertEquals("Default prompt invalid", "> ", input.getPrompt().get());
|
||||
input.setPrompt("test");
|
||||
assertEquals("Prompt setting failed", "test", input.getPrompt().apply());
|
||||
assertEquals("Prompt setting failed", "test", input.getPrompt().get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -76,8 +77,8 @@ public class SocketConsoleApplicationTest {
|
||||
public void testIntegration() throws IOException, InterruptedException {
|
||||
Thread server;
|
||||
server = TestServer.getServer();
|
||||
final String hostName = "127.0.0.1";
|
||||
final int portNumber = 3300;
|
||||
final InetAddress hostName = InetAddress.getLocalHost();
|
||||
final int portNumber = TestServer.PORT;
|
||||
|
||||
try (Socket kkSocket = new Socket(hostName, portNumber);
|
||||
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
|
||||
|
||||
@@ -46,6 +46,7 @@ import net.bigeon.gclc.ConsoleApplication;
|
||||
@SuppressWarnings({ "javadoc", "nls" })
|
||||
public class TestServer {
|
||||
|
||||
public static final int PORT = 12343;
|
||||
private static SocketConsoleApplicationShell SHELL;
|
||||
private static ConnexionManager<Socket> manager;
|
||||
|
||||
@@ -65,6 +66,7 @@ public class TestServer {
|
||||
if (server == null) {
|
||||
server = new Thread(getShell(), "gclcServer");
|
||||
server.start();
|
||||
getShell().waitStartUp(500);
|
||||
}
|
||||
return server;
|
||||
}
|
||||
@@ -75,29 +77,22 @@ public class TestServer {
|
||||
input.setPrompt("> \n");
|
||||
output = new PluggableConsoleOutput();
|
||||
manager = new DConnexionManager<>();
|
||||
SHELL = new SocketConsoleApplicationShell(3300,
|
||||
InetAddress.getByName("127.0.0.1"));
|
||||
SHELL = new SocketConsoleApplicationShell(PORT, InetAddress.getLocalHost());
|
||||
final ConsoleApplication app = ConsoleTestApplication.create(output, input,
|
||||
manager);
|
||||
SHELL.setInterface(new SocketConsoleInterface(input, output));
|
||||
SHELL.setConnexionManager(manager);
|
||||
SHELL.setApplication(app);
|
||||
final Thread th = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
app.start();
|
||||
}
|
||||
});
|
||||
final Thread th = new Thread(() -> app.start());
|
||||
th.start();
|
||||
try {
|
||||
Object waiting = new Object();
|
||||
final Object waiting = new Object();
|
||||
synchronized (waiting) {
|
||||
while (!input.isPrompting()) {
|
||||
waiting.wait(50);
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
} catch (final InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* gclc-socket:net.bigeon.gclc.socket.RemoteDisconnectCommandTest.java
|
||||
* Created on: Nov 18, 2017
|
||||
*/
|
||||
package net.bigeon.gclc.socket;
|
||||
package net.bigeon.gclc.socket.cmd;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
@@ -46,7 +46,8 @@ import java.io.IOException;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.utils.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.socket.DConnexionManager;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -56,7 +57,7 @@ import net.bigeon.gclc.utils.PipedConsoleOutput;
|
||||
public class RemoteDisconnectCommandTest {
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.socket.RemoteDisconnectCommand#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}.
|
||||
* {@link net.bigeon.gclc.socket.cmd.RemoteDisconnectCommand#execute(net.bigeon.gclc.manager.ConsoleOutput, net.bigeon.gclc.manager.ConsoleInput, java.lang.String[])}.
|
||||
*
|
||||
* @throws CommandRunException if the command unexpectedly failed.
|
||||
* @throws IOException if the output could not be written to */
|
||||
4
gclc-swt/.gitignore
vendored
4
gclc-swt/.gitignore
vendored
@@ -1,4 +0,0 @@
|
||||
/target/
|
||||
/.settings/
|
||||
/.classpath
|
||||
/.project
|
||||
@@ -1,20 +1,18 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.bigeon.config</groupId>
|
||||
<artifactId>swt-config</artifactId>
|
||||
<version>1.8.11</version>
|
||||
<artifactId>swt-public-conf</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</parent>
|
||||
<groupId>net.bigeon.gclc</groupId>
|
||||
<artifactId>swt</artifactId>
|
||||
<version>1.1.6-SNAPSHOT</version>
|
||||
<version>1.2.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>GCLC swt</name>
|
||||
<description>A swt window for console applications</description>
|
||||
<inceptionYear>2015</inceptionYear>
|
||||
<url>https://bigeon.net/projects/gclc.html</url>
|
||||
<inceptionYear>2015</inceptionYear>
|
||||
<organization>
|
||||
<name>Bigeon</name>
|
||||
<url>https://bigeon.net/</url>
|
||||
@@ -50,17 +48,28 @@
|
||||
<dependency>
|
||||
<groupId>net.bigeon</groupId>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>2.0.9</version>
|
||||
<version>2.1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.bigeon</groupId>
|
||||
<artifactId>collections</artifactId>
|
||||
<version>1.1.4</version>
|
||||
<version>1.2.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>2.27.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.10.3</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||
<artifactId>dsm-maven-plugin</artifactId>
|
||||
|
||||
@@ -30,11 +30,13 @@
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc-swt:net.bigeon.gclc.swt.SWTConsole.java
|
||||
* gclc-swt:net.bigeon.gclc.swt.SWTConsoleShell.java
|
||||
* Created on: Apr 18, 2015
|
||||
*/
|
||||
package net.bigeon.gclc.swt;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC swt
|
||||
@@ -68,411 +70,78 @@ package net.bigeon.gclc.swt;
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.layout.GridData;
|
||||
import org.eclipse.swt.layout.GridLayout;
|
||||
import org.eclipse.swt.widgets.Composite;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.swt.widgets.Label;
|
||||
import org.eclipse.swt.widgets.Text;
|
||||
|
||||
import net.bigeon.gclc.ConsoleApplication;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
import net.bigeon.gclc.tools.ConstantString;
|
||||
import net.bigeon.gclc.tools.StringProvider;
|
||||
import net.bigeon.gclc.manager.PipedConsoleInput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.swt.io.ConsoleInputManager;
|
||||
import net.bigeon.gclc.swt.io.ConsoleOutputManager;
|
||||
import net.bigeon.gclc.swt.io.ConsolePromptManager;
|
||||
|
||||
/** A SWT component to connect to gclc {@link ConsoleApplication}.
|
||||
/** A shell containing a {@link SWTConsoleView}
|
||||
* <p>
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class SWTConsole extends Composite
|
||||
implements ConsoleDelayIO, ConsoleInput, ConsoleOutput {
|
||||
private static final int TWO = 2;
|
||||
private static final String ERROR_SYNCHRO_PROMPT = "Error in synchronization of prompting";
|
||||
/** The number of columns of the layout. */
|
||||
private static final int LAYOUT_NB_COLUMNS = 2;
|
||||
/** The cmd prefix in the output console. */
|
||||
private static final String CMD_PREFIX = "[CMD] "; //$NON-NLS-1$
|
||||
/** The class logger. */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(SWTConsole.class.getName());
|
||||
/** The empty string constant. */
|
||||
private static final String EMPTY = ""; //$NON-NLS-1$
|
||||
public final class SWTConsole extends Composite {
|
||||
|
||||
/** The console output text field. */
|
||||
private final Text consoleOutput;
|
||||
/** The console input text field. */
|
||||
private final Text consoleInput;
|
||||
/** The prompt label. */
|
||||
private final Label lblPromptlabel;
|
||||
/** The prompt text. */
|
||||
private StringProvider prompt = new ConstantString("> "); //$NON-NLS-1$
|
||||
/** The command entered by the user. */
|
||||
private String command = null;
|
||||
/** If the prompt should be active. */
|
||||
private boolean prompting = false;
|
||||
/** The object for thread synchronization with the prompt. */
|
||||
private final Object promptLock = new Object();
|
||||
private ConsoleInputManager inputManager;
|
||||
private ConsoleOutputManager outputManager;
|
||||
private ConsolePromptManager promptManager;
|
||||
|
||||
/** Create the composite.
|
||||
/** Create the shell.
|
||||
*
|
||||
* @param parent the prent composite
|
||||
* @param style the composite style */
|
||||
* @param parent the containing composite
|
||||
* @param style the shell style */
|
||||
public SWTConsole(final Composite parent, final int style) {
|
||||
super(parent, style);
|
||||
|
||||
setLayout(new GridLayout(LAYOUT_NB_COLUMNS, 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, LAYOUT_NB_COLUMNS, 1));
|
||||
consoleOutput.setRedraw(true);
|
||||
|
||||
lblPromptlabel = new Label(this, SWT.NONE);
|
||||
lblPromptlabel.setText(prompt.apply());
|
||||
|
||||
consoleInput = new Text(this, SWT.BORDER);
|
||||
consoleInput.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
|
||||
consoleInput.addKeyListener(new HistoryTextKeyListener(this));
|
||||
|
||||
setLayout(new GridLayout(2, false));
|
||||
createContents();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.swt.widgets.Composite#checkSubclass() */
|
||||
@Override
|
||||
protected void checkSubclass() {
|
||||
// Disable the check that prevents subclassing of SWT components
|
||||
/** Create contents of the shell. */
|
||||
private void createContents() {
|
||||
final Text output = new Text(this,
|
||||
SWT.WRAP | SWT.READ_ONLY | SWT.MULTI | SWT.V_SCROLL);
|
||||
output.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
|
||||
final Label prompt = new Label(this, SWT.NONE);
|
||||
prompt.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
|
||||
final Text input = new Text(this, SWT.NONE);
|
||||
input.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
|
||||
|
||||
inputManager = new ConsoleInputManager(input);
|
||||
outputManager = new ConsoleOutputManager(output);
|
||||
promptManager = new ConsolePromptManager(prompt);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#close() */
|
||||
@Override
|
||||
public void close() {
|
||||
synchronized (promptLock) {
|
||||
prompting = false;
|
||||
promptLock.notifyAll();
|
||||
}
|
||||
if (consoleInput.isDisposed()) {
|
||||
return;
|
||||
}
|
||||
consoleInput.setEnabled(false);
|
||||
consoleOutput.setEnabled(false);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.swt.ConsoleDelayIO#getInput() */
|
||||
@Override
|
||||
public String getInput() {
|
||||
return consoleInput.getText();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#getPrompt() */
|
||||
@Override
|
||||
public StringProvider getPrompt() {
|
||||
return prompt;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
|
||||
@Override
|
||||
public void interruptPrompt() {
|
||||
synchronized (promptLock) {
|
||||
promptLock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#isClosed() */
|
||||
@Override
|
||||
public boolean isClosed() {
|
||||
return isDisposed();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#print(java.lang.String) */
|
||||
@Override
|
||||
public void print(final String text) throws IOException {
|
||||
if (isDisposed()) {
|
||||
throw new IOException();
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
if (!consoleOutput.isDisposed()) {
|
||||
consoleOutput.append(text);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#println() */
|
||||
@Override
|
||||
public void println() throws IOException {
|
||||
if (isDisposed()) {
|
||||
throw new IOException();
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
if (!consoleOutput.isDisposed()) {
|
||||
consoleOutput.append(System.lineSeparator());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#println(java.lang.String) */
|
||||
@Override
|
||||
public void println(final String message) throws IOException {
|
||||
if (isDisposed()) {
|
||||
throw new IOException();
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
if (!consoleOutput.isDisposed()) {
|
||||
consoleOutput.append(message + System.lineSeparator());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#prompt() */
|
||||
@Override
|
||||
public String prompt() throws IOException {
|
||||
synchronized (promptLock) {
|
||||
initPrompt(prompt.apply());
|
||||
promptLock.notifyAll();
|
||||
try {
|
||||
while (prompting) {
|
||||
promptLock.wait();
|
||||
}
|
||||
} catch (final InterruptedException e) {
|
||||
LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e);
|
||||
command = null;
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
if (isDisposed()) {
|
||||
throw new IOException("Input closed"); //$NON-NLS-1$
|
||||
}
|
||||
return command;
|
||||
}
|
||||
|
||||
/** Initialize the prompting.
|
||||
/** Connect the console parts to the shell.
|
||||
*
|
||||
* @param message the prompt message
|
||||
* @throws IOException if the console is disposed */
|
||||
private void initPrompt(final String message) throws IOException {
|
||||
if (isDisposed()) {
|
||||
throw new IOException();
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
if (!consoleOutput.isDisposed()) {
|
||||
lblPromptlabel.setText(message);
|
||||
// relayout
|
||||
SWTConsole.this.layout();
|
||||
consoleInput.setEnabled(true);
|
||||
consoleInput.setFocus();
|
||||
}
|
||||
}
|
||||
});
|
||||
prompting = true;
|
||||
command = null;
|
||||
* @param input the input
|
||||
* @param output the output
|
||||
* @param promptStream the stream where the prompts are forwarded. */
|
||||
public void connect(final PipedConsoleInput input, final PipedConsoleOutput output,
|
||||
final BufferedReader promptStream) {
|
||||
inputManager.setManager(input);
|
||||
outputManager.setManager(output);
|
||||
promptManager.setStream(promptStream);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(long) */
|
||||
@Override
|
||||
public String prompt(final long timeout) throws IOException {
|
||||
synchronized (promptLock) {
|
||||
initPrompt(prompt.apply());
|
||||
promptLock.notifyAll();
|
||||
try {
|
||||
waitPromptResolution(timeout);
|
||||
} catch (final InterruptedException e) {
|
||||
LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e);
|
||||
command = null;
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
return command;
|
||||
/** @return the inputManager */
|
||||
public ConsoleInputManager getInputManager() {
|
||||
return inputManager;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#prompt(java.lang.String) */
|
||||
@Override
|
||||
public String prompt(final String message) throws IOException {
|
||||
synchronized (promptLock) {
|
||||
initPrompt(message);
|
||||
promptLock.notifyAll();
|
||||
try {
|
||||
while (prompting) {
|
||||
promptLock.wait();
|
||||
}
|
||||
if (isDisposed()) {
|
||||
throw new IOException();
|
||||
}
|
||||
} catch (final InterruptedException e) {
|
||||
LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e);
|
||||
command = null;
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
resetPrompt();
|
||||
}
|
||||
}
|
||||
return command;
|
||||
/** @return the outputManager */
|
||||
public ConsoleOutputManager getOutputManager() {
|
||||
return outputManager;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#prompt(java.lang.String, long) */
|
||||
@Override
|
||||
public String prompt(final String message, final long timeout) throws IOException {
|
||||
synchronized (promptLock) {
|
||||
initPrompt(message);
|
||||
promptLock.notifyAll();
|
||||
try {
|
||||
waitPromptResolution(timeout);
|
||||
} catch (final InterruptedException e) {
|
||||
LOGGER.log(Level.WARNING, ERROR_SYNCHRO_PROMPT, e);
|
||||
command = null;
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
resetPrompt();
|
||||
}
|
||||
}
|
||||
return command;
|
||||
/** @return the promptManager */
|
||||
public ConsolePromptManager getPromptManager() {
|
||||
return promptManager;
|
||||
}
|
||||
|
||||
/** Wait the prompt resolution by user.
|
||||
*
|
||||
* @param timeout the timeout for the wait
|
||||
* @throws InterruptedException if the thread is interrupted
|
||||
* @throws IOException if the console is disposed. */
|
||||
private void waitPromptResolution(final long timeout)
|
||||
throws InterruptedException, IOException {
|
||||
final long start = System.currentTimeMillis();
|
||||
long cur = start;
|
||||
while (prompting && start + timeout > cur) {
|
||||
promptLock.wait((cur - start - timeout) / TWO);
|
||||
cur = System.currentTimeMillis();
|
||||
}
|
||||
if (isDisposed()) {
|
||||
throw new IOException();
|
||||
}
|
||||
}
|
||||
|
||||
/** Reset the prompt message. */
|
||||
private void resetPrompt() {
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
if (!consoleOutput.isDisposed()) {
|
||||
lblPromptlabel.setText(prompt.apply());
|
||||
// relayout
|
||||
SWTConsole.this.layout();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.swt.widgets.Composite#setFocus() */
|
||||
@Override
|
||||
public boolean setFocus() {
|
||||
return consoleInput.setFocus();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.swt.ConsoleDelayIO#setInput(java.lang.String) */
|
||||
@Override
|
||||
public void setInput(final String input) {
|
||||
consoleInput.setText(input);
|
||||
consoleInput.setSelection(input.length());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */
|
||||
@Override
|
||||
public void setPrompt(final String prompt) {
|
||||
setPrompt(new ConstantString(prompt));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.ConsoleManager#setPrompt(java.lang.String) */
|
||||
@Override
|
||||
public void setPrompt(final StringProvider prompt) {
|
||||
this.prompt = prompt;
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
if (!consoleOutput.isDisposed()) {
|
||||
lblPromptlabel.setText(prompt.apply());
|
||||
// relayout
|
||||
SWTConsole.this.layout();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** Set the input text.
|
||||
*
|
||||
* @param string the text */
|
||||
public void setText(final String string) {
|
||||
consoleInput.setText(string);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.swt.ConsoleDelayIO#validateInput() */
|
||||
@Override
|
||||
public void validateInput() {
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
consoleInput.setEnabled(false);
|
||||
}
|
||||
});
|
||||
synchronized (promptLock) {
|
||||
while (!prompting) {
|
||||
try {
|
||||
promptLock.wait();
|
||||
} catch (final InterruptedException e) {
|
||||
LOGGER.log(Level.SEVERE, "Interruption while waiting prompt", e); //$NON-NLS-1$
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
command = consoleInput.getText();
|
||||
prompting = false;
|
||||
consoleInput.setText(EMPTY);
|
||||
consoleOutput.append(CMD_PREFIX + command + System.lineSeparator());
|
||||
}
|
||||
});
|
||||
promptLock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,118 +0,0 @@
|
||||
/*
|
||||
* GCLC swt, provide a swt window for console applications
|
||||
* Copyright (C) 2015-2017 E. Bigeon
|
||||
* mailto:emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc-swt:net.bigeon.gclc.swt.SWTConsoleShell.java
|
||||
* Created on: Apr 18, 2015
|
||||
*/
|
||||
package net.bigeon.gclc.swt;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC swt
|
||||
* %%
|
||||
* Copyright (C) 2015 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.layout.FillLayout;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.swt.widgets.Shell;
|
||||
|
||||
/** A shell containing a {@link SWTConsole}
|
||||
* <p>
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class SWTConsoleShell extends Shell {
|
||||
|
||||
/** The console component */
|
||||
private SWTConsole console;
|
||||
|
||||
/** Create the shell.
|
||||
*
|
||||
* @param display the display */
|
||||
public SWTConsoleShell(final Display display, final int style) {
|
||||
super(display, style);
|
||||
setLayout(new FillLayout(SWT.HORIZONTAL));
|
||||
|
||||
createContents();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void checkSubclass() {
|
||||
// Disable the check that prevents subclassing of SWT components
|
||||
}
|
||||
|
||||
/** Create contents of the shell. */
|
||||
private void createContents() {
|
||||
console = new SWTConsole(this, SWT.NONE);
|
||||
setText("Console Application"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.swt.widgets.Shell#dispose() */
|
||||
@Override
|
||||
public void dispose() {
|
||||
super.dispose();
|
||||
console.close();
|
||||
}
|
||||
|
||||
/** @return the input and output. */
|
||||
public SWTConsole getManager() {
|
||||
return console;
|
||||
}
|
||||
}
|
||||
@@ -46,13 +46,13 @@ package net.bigeon.gclc.swt;
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
@@ -63,71 +63,33 @@ package net.bigeon.gclc.swt;
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.layout.GridData;
|
||||
import org.eclipse.swt.layout.GridLayout;
|
||||
import org.eclipse.swt.widgets.Composite;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.swt.widgets.Text;
|
||||
|
||||
import net.bigeon.gclc.ConsoleApplication;
|
||||
import net.bigeon.gclc.utils.AOutputForwardRunnable;
|
||||
import net.bigeon.gclc.utils.PipedConsoleInput;
|
||||
import net.bigeon.gclc.utils.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleInput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.swt.io.ConsoleInputManager;
|
||||
import net.bigeon.gclc.swt.io.ConsoleOutputManager;
|
||||
import net.bigeon.gclc.swt.tools.HistoryTextKeyListener;
|
||||
|
||||
/** A SWT component to connect to gclc {@link ConsoleApplication}
|
||||
* <p>
|
||||
/** A SWT component to connect to gclc {@link ConsoleApplication}.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class SWTConsoleView extends Composite implements ConsoleDelayIO {
|
||||
/** The local implementation of the forwarding runnable
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
private final class ToSWTConsoleForwardRunnable extends AOutputForwardRunnable {
|
||||
/** The running status */
|
||||
private boolean running = true;
|
||||
|
||||
/** @param manager the manager */
|
||||
public ToSWTConsoleForwardRunnable(final PipedConsoleOutput manager) {
|
||||
super(manager);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void forwardLine(final String m) {
|
||||
appendConsoleOutput(System.lineSeparator() + m);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isRunning() {
|
||||
return running && !isDisposed();
|
||||
}
|
||||
|
||||
/** @param running the running to set */
|
||||
public void setRunning(final boolean running) {
|
||||
this.running = running;
|
||||
}
|
||||
}
|
||||
|
||||
/** The class logger */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(SWTConsoleView.class.getName());
|
||||
/** The console output text field */
|
||||
private final Text consoleOutput;
|
||||
/** The console input text field */
|
||||
private final Text consoleInput;
|
||||
/** The input. */
|
||||
private PipedConsoleInput input;
|
||||
/** The forwarding runnable */
|
||||
private ToSWTConsoleForwardRunnable forward;
|
||||
public final class SWTConsoleView extends Composite {
|
||||
/** The input manager. */
|
||||
private final ConsoleInputManager inManager;
|
||||
/** The output manager. */
|
||||
private final ConsoleOutputManager outManager;
|
||||
|
||||
/** Create the composite.
|
||||
*
|
||||
@@ -138,52 +100,23 @@ public final class SWTConsoleView extends Composite implements ConsoleDelayIO {
|
||||
|
||||
setLayout(new GridLayout(1, false));
|
||||
|
||||
consoleOutput = new Text(this,
|
||||
final Text 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);
|
||||
outManager = new ConsoleOutputManager(consoleOutput);
|
||||
|
||||
consoleInput = new Text(this, SWT.BORDER);
|
||||
final Text consoleInput = new Text(this, SWT.BORDER);
|
||||
consoleInput.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
|
||||
consoleInput.addKeyListener(new HistoryTextKeyListener(this));
|
||||
}
|
||||
|
||||
/** @param next the next message */
|
||||
public void appendConsoleOutput(final String next) {
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
consoleOutput.append(next);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void checkSubclass() {
|
||||
// Disable the check that prevents subclassing of SWT components
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.swt.ConsoleDelayIO#getInput() */
|
||||
@Override
|
||||
public String getInput() {
|
||||
return consoleInput.getText();
|
||||
inManager = new ConsoleInputManager(consoleInput);
|
||||
consoleInput.addKeyListener(new HistoryTextKeyListener(inManager));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.swt.widgets.Composite#setFocus() */
|
||||
@Override
|
||||
public boolean setFocus() {
|
||||
return consoleInput.setFocus();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.swt.ConsoleDelayIO#setInput(java.lang.String) */
|
||||
@Override
|
||||
public void setInput(final String input) {
|
||||
consoleInput.setText(input);
|
||||
consoleInput.setSelection(input.length());
|
||||
return inManager.getText().setFocus();
|
||||
}
|
||||
|
||||
/** Set the input and output.
|
||||
@@ -192,29 +125,21 @@ public final class SWTConsoleView extends Composite implements ConsoleDelayIO {
|
||||
* @param input the input */
|
||||
public void setManager(final PipedConsoleOutput manager,
|
||||
final PipedConsoleInput input) {
|
||||
this.input = input;
|
||||
if (forward != null) {
|
||||
forward.setRunning(false);
|
||||
}
|
||||
forward = new ToSWTConsoleForwardRunnable(manager);
|
||||
final Thread th = new Thread(forward, "gclcToSWT"); //$NON-NLS-1$
|
||||
th.start();
|
||||
outManager.setManager(manager);
|
||||
inManager.setManager(input);
|
||||
}
|
||||
|
||||
/** @param string the text */
|
||||
public void setText(final String string) {
|
||||
consoleInput.setText(string);
|
||||
/** Get the manager of console input.
|
||||
*
|
||||
* @return the input manager */
|
||||
public ConsoleInputManager getInputManager() {
|
||||
return inManager;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void validateInput() {
|
||||
try {
|
||||
input.type(getInput());
|
||||
} catch (final IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Unable to input value to console", e); //$NON-NLS-1$
|
||||
}
|
||||
/** Get the manager of console output.
|
||||
*
|
||||
* @return the output manager */
|
||||
public ConsoleOutputManager getOutputManager() {
|
||||
return outManager;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,9 @@
|
||||
* gclc-swt:net.bigeon.gclc.swt.ConsoleDelayIO.java
|
||||
* Created on: Nov 19, 2016
|
||||
*/
|
||||
package net.bigeon.gclc.swt;
|
||||
package net.bigeon.gclc.swt.api;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
@@ -86,6 +88,8 @@ public interface ConsoleDelayIO {
|
||||
* @param input the input to set */
|
||||
void setInput(String input);
|
||||
|
||||
/** Actually send the input as the prompt next input. */
|
||||
void validateInput();
|
||||
/** Actually send the input as the prompt next input.
|
||||
*
|
||||
* @throws IOException if an error occurred */
|
||||
void validateInput() throws IOException;
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt.api;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC swt
|
||||
* %%
|
||||
* Copyright (C) 2015 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
/** The common interface for console display.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public interface ConsoleOutputDisplay {
|
||||
|
||||
/** Append a line to the display.
|
||||
*
|
||||
* @param m the line content */
|
||||
void appendLine(String m);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt.api;
|
||||
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt.io;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC swt
|
||||
* %%
|
||||
* Copyright (C) 2015 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.swt.widgets.Text;
|
||||
|
||||
import net.bigeon.gclc.manager.PipedConsoleInput;
|
||||
import net.bigeon.gclc.swt.api.ConsoleDelayIO;
|
||||
|
||||
/** The object managing the console input.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class ConsoleInputManager implements ConsoleDelayIO {
|
||||
|
||||
private final Text text;
|
||||
private PipedConsoleInput input;
|
||||
|
||||
/** Create a managing object.
|
||||
*
|
||||
* @param text the text */
|
||||
public ConsoleInputManager(final Text text) {
|
||||
super();
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.swt.ConsoleDelayIO#setInput(java.lang.String) */
|
||||
@Override
|
||||
public void setInput(final String string) {
|
||||
text.setText(string);
|
||||
text.setSelection(string.length());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.swt.ConsoleDelayIO#getInput() */
|
||||
@Override
|
||||
public String getInput() {
|
||||
return text.getText();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.swt.ConsoleDelayIO#validateInput() */
|
||||
@Override
|
||||
public void validateInput() throws IOException {
|
||||
input.type(text.getText());
|
||||
}
|
||||
|
||||
/** Set the input to control.
|
||||
*
|
||||
* @param input the input */
|
||||
public void setManager(final PipedConsoleInput input) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
/** Get the text component containing the currently set input.
|
||||
*
|
||||
* @return the text */
|
||||
public Text getText() {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt.io;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC swt
|
||||
* %%
|
||||
* Copyright (C) 2015 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import org.eclipse.swt.widgets.Text;
|
||||
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.swt.api.ConsoleOutputDisplay;
|
||||
import net.bigeon.gclc.swt.tools.TextAppendingRunnable;
|
||||
import net.bigeon.gclc.swt.tools.ToSWTConsoleForwardRunnable;
|
||||
|
||||
/** The manager for console output to insert in a text.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class ConsoleOutputManager implements ConsoleOutputDisplay {
|
||||
/** The SWT component displaying the output content. */
|
||||
private final Text text;
|
||||
/** The forwarding runnable. */
|
||||
private ToSWTConsoleForwardRunnable forward;
|
||||
/** The forwarding thread. */
|
||||
private Thread forwardThread;
|
||||
|
||||
/** Create the manager.
|
||||
*
|
||||
* @param text the text to display the output in. */
|
||||
public ConsoleOutputManager(final Text text) {
|
||||
super();
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.swt.ConsoleOutputDisplay#appendLine(java.lang.String) */
|
||||
@Override
|
||||
public void appendLine(final String next) {
|
||||
text.getDisplay().syncExec(new TextAppendingRunnable(text, next));
|
||||
}
|
||||
|
||||
/** Set the output.
|
||||
*
|
||||
* @param output the output to set */
|
||||
public void setManager(final PipedConsoleOutput output) {
|
||||
if (forward != null) {
|
||||
if (forward.getOuput() == output) {
|
||||
return;
|
||||
}
|
||||
forward.setRunning(false);
|
||||
}
|
||||
if (output == null) {
|
||||
forward = null;
|
||||
forwardThread = null;
|
||||
return;
|
||||
}
|
||||
forward = new ToSWTConsoleForwardRunnable(output, this, text);
|
||||
forwardThread = new Thread(forward, "gclcToSWT"); //$NON-NLS-1$
|
||||
forwardThread.start();
|
||||
}
|
||||
|
||||
/** Get the current thread forwarding the console output to the SWT components.
|
||||
*
|
||||
* @return the forwardThread */
|
||||
public Thread getForwardThread() {
|
||||
return forwardThread;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt.io;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC swt
|
||||
* %%
|
||||
* Copyright (C) 2015 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import java.io.BufferedReader;
|
||||
|
||||
import org.eclipse.swt.widgets.Label;
|
||||
|
||||
import net.bigeon.gclc.swt.tools.PromptReadingRunnable;
|
||||
|
||||
/** The manager for the console prompt updates.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class ConsolePromptManager {
|
||||
|
||||
/** The label to set the prompts at. */
|
||||
private final Label label;
|
||||
|
||||
/** The current prompt reading runnable. */
|
||||
private PromptReadingRunnable promptRead;
|
||||
|
||||
/** Create the manager.
|
||||
*
|
||||
* @param label the label to update with prompts */
|
||||
public ConsolePromptManager(final Label label) {
|
||||
super();
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
/** Set the prompt.
|
||||
* <p>
|
||||
* This method sets the prompt on the label as requested, without changing the
|
||||
* actual prompt of the console input.
|
||||
*
|
||||
* @param string the text */
|
||||
public void setPrompt(final String string) {
|
||||
label.setText(string);
|
||||
}
|
||||
|
||||
/** Set the input to control.
|
||||
*
|
||||
* @param promptStream the input */
|
||||
public void setStream(final BufferedReader promptStream) {
|
||||
if (promptRead != null) {
|
||||
if (promptRead.getReader() == promptStream) {
|
||||
return;
|
||||
}
|
||||
promptRead.setRunning(false);
|
||||
}
|
||||
if (promptStream == null) {
|
||||
promptRead = null;
|
||||
return;
|
||||
}
|
||||
promptRead = new PromptReadingRunnable(promptStream, label);
|
||||
final Thread th = new Thread(promptRead, "Prompt To Label");
|
||||
th.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt.io;
|
||||
@@ -33,7 +33,11 @@
|
||||
* gclc-swt:net.bigeon.gclc.swt.HistoryTextKeyListener.java
|
||||
* Created on: Jun 9, 2016
|
||||
*/
|
||||
package net.bigeon.gclc.swt;
|
||||
package net.bigeon.gclc.swt.tools;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
@@ -72,8 +76,9 @@ import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.events.KeyAdapter;
|
||||
import org.eclipse.swt.events.KeyEvent;
|
||||
|
||||
import net.bigeon.collections.ArrayRibbon;
|
||||
import net.bigeon.collections.Ribbon;
|
||||
import net.bigeon.collections.ribbon.ArrayRibbon;
|
||||
import net.bigeon.gclc.swt.api.ConsoleDelayIO;
|
||||
|
||||
/** A key listener to validate commands and manage the history of commands.
|
||||
*
|
||||
@@ -83,7 +88,9 @@ public final class HistoryTextKeyListener extends KeyAdapter {
|
||||
/** The size of commands history. */
|
||||
private static final int DEFAULT_HISTORY_SIZE = 10;
|
||||
/** The empty string constant. */
|
||||
private static final String EMPTY = ""; //$NON-NLS-1$
|
||||
private static final String EMPTY = ""; //$NON-NLS-1$
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(HistoryTextKeyListener.class.getName());
|
||||
/** The history ribbon. */
|
||||
private final Ribbon<String> commands;
|
||||
/** The current index in history search. */
|
||||
@@ -118,7 +125,11 @@ public final class HistoryTextKeyListener extends KeyAdapter {
|
||||
if (!input.isEmpty()) {
|
||||
commands.add(input);
|
||||
}
|
||||
console.validateInput();
|
||||
try {
|
||||
console.validateInput();
|
||||
} catch (final IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Unable to write to console", e);
|
||||
}
|
||||
currentIndex = -1;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt.tools;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC swt
|
||||
* %%
|
||||
* Copyright (C) 2015 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.eclipse.swt.widgets.Label;
|
||||
|
||||
/** The runnable forwarding prompts from a buffered input to a label.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public class PromptReadingRunnable implements Runnable {
|
||||
|
||||
/** The logger for this class. */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(PromptReadingRunnable.class.getName());
|
||||
|
||||
/** The reader providing the succession of prompts */
|
||||
private final BufferedReader reader;
|
||||
/** The label. */
|
||||
private final Label view;
|
||||
/** The running status. */
|
||||
private boolean running = true;
|
||||
|
||||
/** The updating runnable implementation.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
private static class LabelUpdater implements Runnable {
|
||||
/** The label to update. */
|
||||
private final Label label;
|
||||
/** The text to set. */
|
||||
private final String content;
|
||||
|
||||
/** Create the updating runnable.
|
||||
*
|
||||
* @param label the label
|
||||
* @param content the content */
|
||||
public LabelUpdater(final Label label, final String content) {
|
||||
super();
|
||||
this.label = label;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Runnable#run() */
|
||||
@Override
|
||||
public void run() {
|
||||
label.setText(content);
|
||||
}
|
||||
}
|
||||
|
||||
/** Create the prompt flow updating runnable.
|
||||
*
|
||||
* @param reader the reader
|
||||
* @param view the view to update on lines. */
|
||||
public PromptReadingRunnable(final BufferedReader reader, final Label view) {
|
||||
super();
|
||||
this.reader = reader;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Runnable#run() */
|
||||
@Override
|
||||
public void run() {
|
||||
String prompt;
|
||||
try {
|
||||
while (isRunning() && (prompt = reader.readLine()) != null) {
|
||||
view.getDisplay().syncExec(new LabelUpdater(view, prompt));
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOGGER.log(Level.INFO, "Prompt cannot be read, or was prematuraly closed", e);
|
||||
}
|
||||
}
|
||||
|
||||
/** Get the running state.
|
||||
*
|
||||
* @return the running */
|
||||
public synchronized boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
|
||||
/** Set the running state.
|
||||
*
|
||||
* @param running the running to set */
|
||||
public synchronized void setRunning(final boolean running) {
|
||||
this.running = running;
|
||||
}
|
||||
|
||||
/** Get the prompt flow.
|
||||
*
|
||||
* @return the reader */
|
||||
public BufferedReader getReader() {
|
||||
return reader;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt.tools;
|
||||
|
||||
import org.eclipse.swt.widgets.Text;
|
||||
|
||||
/** A runnable appending text to the content of a {@link Text} component.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public class TextAppendingRunnable implements Runnable {
|
||||
/** The text to append on a line (possibly new). */
|
||||
private final String next;
|
||||
/** The {@link Text} component. */
|
||||
private final Text text;
|
||||
|
||||
/** Create the appending runnable.
|
||||
*
|
||||
* @param text the component to update
|
||||
* @param next the text to append */
|
||||
public TextAppendingRunnable(final Text text, final String next) {
|
||||
this.text = text;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Runnable#run() */
|
||||
@Override
|
||||
public void run() {
|
||||
final String initialText = text.getText();
|
||||
if (initialText != null && !initialText.isEmpty()) {
|
||||
text.append(System.lineSeparator());
|
||||
}
|
||||
text.append(next);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt.tools;
|
||||
|
||||
import org.eclipse.swt.widgets.Widget;
|
||||
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.manager.forwarding.AOutputForwardRunnable;
|
||||
import net.bigeon.gclc.swt.api.ConsoleOutputDisplay;
|
||||
|
||||
/** The local implementation of the forwarding runnable.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class ToSWTConsoleForwardRunnable extends AOutputForwardRunnable {
|
||||
/** The running status. */
|
||||
private boolean running = true;
|
||||
/** The console output. */
|
||||
private final PipedConsoleOutput out;
|
||||
/** The console output display. */
|
||||
private final ConsoleOutputDisplay display;
|
||||
/** The actual SWT component. */
|
||||
private final Widget element;
|
||||
|
||||
/** Create the forwarding runnable.
|
||||
*
|
||||
* @param manager the manager
|
||||
* @param display the display
|
||||
* @param element the composite */
|
||||
public ToSWTConsoleForwardRunnable(final PipedConsoleOutput manager,
|
||||
final ConsoleOutputDisplay display,
|
||||
final Widget element) {
|
||||
super(manager);
|
||||
out = manager;
|
||||
this.display = display;
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see
|
||||
* net.bigeon.gclc.utils.AOutputForwardRunnable#forwardLine(java.lang.String) */
|
||||
@Override
|
||||
protected void forwardLine(final String m) {
|
||||
display.appendLine(m);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.utils.AOutputForwardRunnable#isRunning() */
|
||||
@Override
|
||||
protected boolean isRunning() {
|
||||
return running && !element.isDisposed();
|
||||
}
|
||||
|
||||
/** Set the running status.
|
||||
*
|
||||
* @param running the running to set */
|
||||
public void setRunning(final boolean running) {
|
||||
this.running = running;
|
||||
}
|
||||
|
||||
/** Get the output.
|
||||
*
|
||||
* @return the currently forwarded output */
|
||||
public PipedConsoleOutput getOuput() {
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
/** Tool classes for the library. External code should not rely on classes in
|
||||
* there as they can be removed without notice.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
package net.bigeon.gclc.swt.tools;
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC swt
|
||||
* %%
|
||||
* Copyright (C) 2015 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.swt.widgets.Text;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.manager.PipedConsoleInput;
|
||||
import net.bigeon.gclc.swt.io.ConsoleInputManager;
|
||||
|
||||
/** @author Emmanuel Bigeon */
|
||||
public class ConsoleInputManagerTest {
|
||||
|
||||
private final Text text = mock(Text.class);
|
||||
private final ConsoleInputManager cim = new ConsoleInputManager(text);
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.swt.io.ConsoleInputManager#setInput(java.lang.String)}. */
|
||||
@Test
|
||||
public void testSetText() {
|
||||
cim.setInput("Text");
|
||||
verify(text).setText("Text");
|
||||
}
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.swt.io.ConsoleInputManager#validateInput()}.
|
||||
*
|
||||
* @throws IOException if an error occurred */
|
||||
@Test
|
||||
public void testValidateInput() throws IOException {
|
||||
// Expected calls
|
||||
when(text.getText()).thenReturn("abc");
|
||||
|
||||
try {
|
||||
cim.validateInput();
|
||||
fail("No input should cause exception for input validation");
|
||||
} catch (final Exception e) {
|
||||
// ok
|
||||
}
|
||||
final PipedConsoleInput input = mock(PipedConsoleInput.class);
|
||||
cim.setManager(input);
|
||||
|
||||
cim.validateInput();
|
||||
verify(input).type("abc");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testValidateInputWithError() throws IOException {
|
||||
final PipedConsoleInput input = mock(PipedConsoleInput.class);
|
||||
cim.setManager(input);
|
||||
// Expected calls
|
||||
when(text.getText()).thenReturn("abc");
|
||||
doThrow(new IOException()).when(input).type("abc");
|
||||
|
||||
try {
|
||||
cim.validateInput();
|
||||
fail("IO exception should be forwarded");
|
||||
} catch (final IOException e) {
|
||||
// ok
|
||||
}
|
||||
}
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.swt.io.ConsoleInputManager#getText()}. */
|
||||
@Test
|
||||
public void testGetText() {
|
||||
assertEquals("Text component should be preserved", text, cim.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetInput() {
|
||||
when(text.getText()).thenReturn("abc");
|
||||
assertEquals("Text component should be preserved", "abc", cim.getInput());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC swt
|
||||
* %%
|
||||
* Copyright (C) 2015 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.swt.widgets.Text;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.swt.io.ConsoleOutputManager;
|
||||
|
||||
/** @author Emmanuel Bigeon */
|
||||
public class ConsoleOutputManagerTest {
|
||||
private final Text text = mock(Text.class);
|
||||
private final ConsoleOutputManager com = new ConsoleOutputManager(text);
|
||||
private final Display display = mock(Display.class);
|
||||
{
|
||||
when(text.getDisplay()).thenReturn(display);
|
||||
doAnswer(new Answer<Object>() {
|
||||
/* (non-Javadoc)
|
||||
* @see
|
||||
* org.mockito.stubbing.Answer#answer(org.mockito.invocation.InvocationOnMock) */
|
||||
@Override
|
||||
public Object answer(final InvocationOnMock invocation) throws Throwable {
|
||||
final Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}
|
||||
}).when(display).syncExec(any(Runnable.class));
|
||||
}
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.swt.io.ConsoleOutputManager#appendLine(java.lang.String)}. */
|
||||
@Test
|
||||
public void testAppendConsoleOutput() {
|
||||
when(text.getText()).thenReturn("", "abc",
|
||||
"abc" + System.lineSeparator() + "def");
|
||||
com.appendLine("abc");
|
||||
verify(text).append("abc");
|
||||
com.appendLine("def");
|
||||
verify(text).append(System.lineSeparator());
|
||||
verify(text).append("def");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetManager() throws IOException, InterruptedException {
|
||||
final PipedConsoleOutput output = mock(PipedConsoleOutput.class);
|
||||
com.setManager(output);
|
||||
com.setManager(output);
|
||||
|
||||
try (PipedConsoleOutput output2 = new PipedConsoleOutput()) {
|
||||
com.setManager(output2);
|
||||
output2.println("line");
|
||||
final Thread forwardThread = com.getForwardThread();
|
||||
forwardThread.join(500);
|
||||
com.setManager(null);
|
||||
forwardThread.join();
|
||||
verify(text).append("line");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC swt
|
||||
* %%
|
||||
* Copyright (C) 2015 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
|
||||
import org.eclipse.swt.widgets.Label;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.swt.io.ConsolePromptManager;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class ConsolePromptManagerTest {
|
||||
|
||||
private final Label label = mock(Label.class);
|
||||
|
||||
private final ConsolePromptManager manager = new ConsolePromptManager(label);
|
||||
|
||||
/**
|
||||
* Test method for {@link net.bigeon.gclc.swt.io.ConsolePromptManager#setPrompt(java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetPrompt() {
|
||||
manager.setPrompt("abc");
|
||||
verify(label).setText("abc");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link net.bigeon.gclc.swt.io.ConsolePromptManager#setStream(java.io.BufferedReader)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetStream() {
|
||||
BufferedReader output = mock(BufferedReader.class);
|
||||
manager.setStream(output);
|
||||
manager.setStream(output);
|
||||
output = mock(BufferedReader.class);
|
||||
manager.setStream(output);
|
||||
manager.setStream(null);
|
||||
manager.setStream(output);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -69,10 +69,15 @@ package net.bigeon.gclc.swt;
|
||||
* #L%
|
||||
*/
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.events.KeyEvent;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.swt.api.ConsoleDelayIO;
|
||||
import net.bigeon.gclc.swt.tools.HistoryTextKeyListener;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
@@ -81,10 +86,10 @@ import org.junit.Test;
|
||||
public class HistoryTextKeyListenerTest {
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.swt.HistoryTextKeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)}. */
|
||||
* {@link net.bigeon.gclc.swt.tools.HistoryTextKeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)}. */
|
||||
@Test
|
||||
public final void testKeyPressedKeyEvent() {
|
||||
ConsoleDelayIO io = new ConsoleDelayIO() {
|
||||
final ConsoleDelayIO io = new ConsoleDelayIO() {
|
||||
private String input = "";
|
||||
|
||||
@Override
|
||||
@@ -93,7 +98,7 @@ public class HistoryTextKeyListenerTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInput(String input) {
|
||||
public void setInput(final String input) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
@@ -102,7 +107,7 @@ public class HistoryTextKeyListenerTest {
|
||||
input = "";
|
||||
}
|
||||
};
|
||||
HistoryTextKeyListener listener = new HistoryTextKeyListener(io);
|
||||
final HistoryTextKeyListener listener = new HistoryTextKeyListener(io);
|
||||
|
||||
// no effects
|
||||
assertEquals("", io.getInput());
|
||||
@@ -139,4 +144,12 @@ public class HistoryTextKeyListenerTest {
|
||||
assertEquals("cmd arg2", io.getInput());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeyPressed() {
|
||||
final ConsoleDelayIO io = mock(ConsoleDelayIO.class);
|
||||
final KeyEvent event = mock(KeyEvent.class);
|
||||
event.keyCode = 'a';
|
||||
final HistoryTextKeyListener listener = new HistoryTextKeyListener(io);
|
||||
listener.keyPressed(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* GCLC swt
|
||||
* %%
|
||||
* Copyright (C) 2015 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.swt.widgets.Label;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import net.bigeon.gclc.swt.tools.PromptReadingRunnable;
|
||||
|
||||
/** @author Emmanuel Bigeon */
|
||||
public class PromptReadingRunnableTest {
|
||||
private final Label view = mock(Label.class);
|
||||
private final Display display = mock(Display.class);
|
||||
{
|
||||
when(view.getDisplay()).thenReturn(display);
|
||||
doAnswer(new Answer<Object>() {
|
||||
/* (non-Javadoc)
|
||||
* @see
|
||||
* org.mockito.stubbing.Answer#answer(org.mockito.invocation.InvocationOnMock) */
|
||||
@Override
|
||||
public Object answer(final InvocationOnMock invocation) throws Throwable {
|
||||
final Runnable runnable = invocation.getArgument(0);
|
||||
runnable.run();
|
||||
return null;
|
||||
}
|
||||
}).when(display).syncExec(any(Runnable.class));
|
||||
}
|
||||
|
||||
/** Test method for {@link net.bigeon.gclc.swt.tools.PromptReadingRunnable#run()}.
|
||||
*
|
||||
* @throws IOException if an error occurred */
|
||||
@Test
|
||||
public void testRun() throws IOException {
|
||||
final BufferedReader reader = mock(BufferedReader.class);
|
||||
when(reader.readLine()).thenReturn("abc", "def", null);
|
||||
|
||||
final PromptReadingRunnable runnable = new PromptReadingRunnable(reader, view);
|
||||
|
||||
runnable.run();
|
||||
}
|
||||
|
||||
/** Test method for {@link net.bigeon.gclc.swt.tools.PromptReadingRunnable#run()}.
|
||||
*
|
||||
* @throws IOException if an error occurred */
|
||||
@Test
|
||||
public void testRunWithIO() throws IOException {
|
||||
final BufferedReader reader = mock(BufferedReader.class);
|
||||
when(reader.readLine()).thenThrow(new IOException());
|
||||
|
||||
final PromptReadingRunnable runnable = new PromptReadingRunnable(reader, view);
|
||||
final Logger logger = Logger.getLogger(PromptReadingRunnable.class.getName());
|
||||
final Level back = logger.getLevel();
|
||||
logger.setLevel(Level.OFF);
|
||||
|
||||
runnable.run();
|
||||
logger.setLevel(back);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +1,5 @@
|
||||
/*
|
||||
* GCLC swt, provide a swt window for console applications
|
||||
* Copyright (C) 2015-2017 E. Bigeon
|
||||
* mailto:emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc-swt:net.bigeon.gclc.swt.SWTConsoleShellTest.java
|
||||
* Created on: Jun 8, 2016
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt;
|
||||
|
||||
@@ -46,13 +14,13 @@ package net.bigeon.gclc.swt;
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
@@ -63,304 +31,45 @@ package net.bigeon.gclc.swt;
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.swt.widgets.Composite;
|
||||
import org.eclipse.swt.widgets.Shell;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.ConsoleApplication;
|
||||
import net.bigeon.gclc.command.Command;
|
||||
import net.bigeon.gclc.command.base.ExitCommand;
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.exception.InvalidCommandName;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleInput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
@SuppressWarnings({ "javadoc", "static-method", "nls", "deprecation" })
|
||||
/** @author Emmanuel Bigeon */
|
||||
public class SWTConsoleShellTest {
|
||||
|
||||
protected static final long TWO_SECONDS = 2000;
|
||||
private static final Display DISPLAY = Display.getDefault();
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.swt.SWTConsole#SWTConsole(Composite, int)}. */
|
||||
@Test
|
||||
public void testGeneric() {
|
||||
final SWTConsoleShell shell = new SWTConsoleShell(DISPLAY, SWT.SHELL_TRIM);
|
||||
final SWTConsole swtConsole = shell.getManager();
|
||||
try {
|
||||
final ConsoleApplication appl = new ConsoleApplication(swtConsole, swtConsole,
|
||||
"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[]) */
|
||||
@Override
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
try {
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String tip() {
|
||||
return "a long running command";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
// TODO Auto-generated method stub
|
||||
// return null;
|
||||
throw new RuntimeException("Not implemented yet");
|
||||
}
|
||||
});
|
||||
// shell.pack();
|
||||
shell.open();
|
||||
shell.setVisible(false);
|
||||
final Thread applThread = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
appl.start();
|
||||
}
|
||||
});
|
||||
final Thread testThread = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
swtConsole.setText("test"); //$NON-NLS-1$
|
||||
swtConsole.validateInput();
|
||||
}
|
||||
});
|
||||
try {
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
shell.dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
applThread.start();
|
||||
testThread.start();
|
||||
while (!shell.isDisposed()) {
|
||||
if (!DISPLAY.readAndDispatch()) {
|
||||
DISPLAY.sleep();
|
||||
}
|
||||
}
|
||||
// DISPLAY.dispose();
|
||||
assertTrue(swtConsole.isClosed());
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
assertFalse(appl.isRunning());
|
||||
} catch (final InvalidCommandName e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
public void testSWTConsoleShell() {
|
||||
final SWTConsole console = new SWTConsole(new Shell(), SWT.NONE);
|
||||
assertNotNull("View should be initialized with managers",
|
||||
console.getInputManager());
|
||||
assertNotNull("View should be initialized with managers",
|
||||
console.getOutputManager());
|
||||
assertNotNull("View should be initialized with managers",
|
||||
console.getPromptManager());
|
||||
}
|
||||
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.swt.SWTConsole#connect(PipedConsoleInput, PipedConsoleOutput, java.io.BufferedReader)}. */
|
||||
@Test
|
||||
public void testConsoleClose() {
|
||||
final SWTConsoleShell shell = new SWTConsoleShell(DISPLAY, SWT.SHELL_TRIM);
|
||||
final SWTConsole swtConsole = shell.getManager();
|
||||
swtConsole.close();
|
||||
swtConsole.setPrompt(":");
|
||||
try {
|
||||
final ConsoleApplication appl = new ConsoleApplication(swtConsole, swtConsole,
|
||||
"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[]) */
|
||||
@Override
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
try {
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String tip() {
|
||||
return "a long running command";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
// TODO Auto-generated method stub
|
||||
// return null;
|
||||
throw new RuntimeException("Not implemented yet");
|
||||
}
|
||||
});
|
||||
appl.add(new Command("test") {
|
||||
/* (non-Javadoc)
|
||||
* @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 {
|
||||
try {
|
||||
swtConsole.prompt("Test");
|
||||
} catch (final IOException e) {
|
||||
throw new CommandRunException("No input", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String tip() {
|
||||
return "a prompting running command";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
// TODO Auto-generated method stub
|
||||
// return null;
|
||||
throw new RuntimeException("Not implemented yet");
|
||||
}
|
||||
});
|
||||
// shell.pack();
|
||||
shell.open();
|
||||
shell.setVisible(false);
|
||||
final Thread applThread = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
appl.start();
|
||||
}
|
||||
});
|
||||
final Thread testThread = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
swtConsole.setText("test"); //$NON-NLS-1$
|
||||
swtConsole.validateInput();
|
||||
}
|
||||
});
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
swtConsole.setText("ok"); //$NON-NLS-1$
|
||||
}
|
||||
});
|
||||
swtConsole.validateInput();
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
swtConsole.setText("long"); //$NON-NLS-1$
|
||||
}
|
||||
});
|
||||
swtConsole.validateInput();
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
swtConsole.setText("test"); //$NON-NLS-1$
|
||||
}
|
||||
});
|
||||
swtConsole.validateInput();
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
swtConsole.setText("test"); //$NON-NLS-1$
|
||||
}
|
||||
});
|
||||
swtConsole.validateInput();
|
||||
try {
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
shell.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
applThread.start();
|
||||
testThread.start();
|
||||
while (!shell.isDisposed()) {
|
||||
if (!DISPLAY.readAndDispatch()) {
|
||||
DISPLAY.sleep();
|
||||
}
|
||||
}
|
||||
swtConsole.setPrompt(">");
|
||||
try {
|
||||
swtConsole.prompt();
|
||||
fail("Prompting when closed should fail!");
|
||||
} catch (final IOException e) {
|
||||
assertNotNull(e);
|
||||
}
|
||||
// DISPLAY.dispose();
|
||||
assertTrue(swtConsole.isClosed());
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
assertFalse(appl.isRunning());
|
||||
} catch (final InvalidCommandName e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
public void testConnect() {
|
||||
final SWTConsole console = new SWTConsole(new Shell(), SWT.NONE);
|
||||
// Disconnection should work.
|
||||
console.connect(null, null, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,37 +1,5 @@
|
||||
/*
|
||||
* GCLC swt, provide a swt window for console applications
|
||||
* Copyright (C) 2015-2017 E. Bigeon
|
||||
* mailto:emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
* gclc-swt:net.bigeon.gclc.swt.SWTConsoleShellTest.java
|
||||
* Created on: Jun 8, 2016
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.swt;
|
||||
|
||||
@@ -46,13 +14,13 @@ package net.bigeon.gclc.swt;
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
@@ -63,142 +31,48 @@ package net.bigeon.gclc.swt;
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.swt.widgets.Shell;
|
||||
import org.junit.Test;
|
||||
|
||||
import net.bigeon.gclc.ConsoleApplication;
|
||||
import net.bigeon.gclc.command.Command;
|
||||
import net.bigeon.gclc.command.base.ExitCommand;
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.exception.InvalidCommandName;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
import net.bigeon.gclc.utils.PipedConsoleInput;
|
||||
import net.bigeon.gclc.utils.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleInput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
@SuppressWarnings({ "javadoc", "static-method", "nls", "deprecation" })
|
||||
/** @author Emmanuel Bigeon */
|
||||
public class SWTConsoleViewTest {
|
||||
|
||||
protected static final long TWO_SECONDS = 2000;
|
||||
private static final Display DISPLAY = Display.getDefault();
|
||||
|
||||
/** Test method for {@link net.bigeon.gclc.swt.SWTConsoleView#setFocus()}. */
|
||||
@Test
|
||||
public void testGeneric() {
|
||||
final Shell shell = new Shell(DISPLAY);
|
||||
final SWTConsoleView swtConsole = new SWTConsoleView(shell, SWT.NONE);
|
||||
try (PipedConsoleOutput manager = new PipedConsoleOutput();
|
||||
PipedConsoleInput input = new PipedConsoleInput(System.out)) {
|
||||
swtConsole.setManager(manager, input);
|
||||
} catch (final IOException e2) {
|
||||
assertNull(e2);
|
||||
}
|
||||
try (PipedConsoleOutput manager = new PipedConsoleOutput();
|
||||
PipedConsoleInput input = new PipedConsoleInput(System.out)) {
|
||||
swtConsole.setManager(manager, input);
|
||||
final ConsoleApplication appl = new ConsoleApplication(manager, input,
|
||||
"Hello", "See you");
|
||||
appl.add(new ExitCommand("exit", appl));
|
||||
appl.add(new Command("long") {
|
||||
public void testSetFocus() {
|
||||
final SWTConsoleView view = new SWTConsoleView(new Shell(), SWT.NONE);
|
||||
assertFalse("Non visible component set focus should fail", view.setFocus());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @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 {
|
||||
try {
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.swt.SWTConsoleView#SWTConsoleView(org.eclipse.swt.widgets.Composite, int)}. */
|
||||
@Test
|
||||
public void testSWTConsoleView() {
|
||||
final SWTConsoleView view = new SWTConsoleView(new Shell(), SWT.NONE);
|
||||
assertNotNull("View should be initialized with managers", view.getInputManager());
|
||||
assertNotNull("View should be initialized with managers",
|
||||
view.getOutputManager());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String tip() {
|
||||
return "a long running command";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
// TODO Auto-generated method stub
|
||||
// return null;
|
||||
throw new RuntimeException("Not implemented yet");
|
||||
}
|
||||
});
|
||||
// shell.pack();
|
||||
shell.open();
|
||||
shell.setVisible(false);
|
||||
final Thread applThread = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
appl.start();
|
||||
}
|
||||
});
|
||||
final Thread testThread = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
swtConsole.setText("test"); //$NON-NLS-1$
|
||||
swtConsole.validateInput();
|
||||
}
|
||||
});
|
||||
try {
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
shell.dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
applThread.start();
|
||||
testThread.start();
|
||||
while (!shell.isDisposed()) {
|
||||
if (!DISPLAY.readAndDispatch()) {
|
||||
DISPLAY.sleep();
|
||||
}
|
||||
}
|
||||
} catch (final InvalidCommandName e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (final IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
/** Test method for
|
||||
* {@link net.bigeon.gclc.swt.SWTConsoleView#setManager(PipedConsoleOutput, PipedConsoleInput)}. */
|
||||
@Test
|
||||
public void testSetManager() {
|
||||
final SWTConsoleView view = new SWTConsoleView(new Shell(), SWT.NONE);
|
||||
// Disconnection should work.
|
||||
view.setManager(null, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
mock-maker-inline
|
||||
79
gclc-test/pom.xml
Normal file
79
gclc-test/pom.xml
Normal file
@@ -0,0 +1,79 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.bigeon.config</groupId>
|
||||
<artifactId>ebigeon-config</artifactId>
|
||||
<version>1.8.21</version>
|
||||
</parent>
|
||||
<groupId>net.bigeon.gclc</groupId>
|
||||
<artifactId>test</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>GCLC Tests</name>
|
||||
<description>Test utilities for GCLC applications and extensions</description>
|
||||
<inceptionYear>2018</inceptionYear>
|
||||
<organization>
|
||||
<name>Bigeon</name>
|
||||
<url>https://bigeon.net</url>
|
||||
</organization>
|
||||
<licenses>
|
||||
<license>
|
||||
<distribution>manual</distribution>
|
||||
<name>CeCILL 2.1</name>
|
||||
<url>https://cecill.info/licences/Licence_CeCILL_V2.1-en.html</url>
|
||||
</license>
|
||||
</licenses>
|
||||
<developers>
|
||||
<developer>
|
||||
<email>emmanuel@bigeon.fr</email>
|
||||
<name>Emmanuel Bigeon</name>
|
||||
<url>bigeon.net</url>
|
||||
<roles>
|
||||
<role>PM</role>
|
||||
</roles>
|
||||
</developer>
|
||||
</developers>
|
||||
<scm>
|
||||
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
|
||||
<tag>HEAD</tag>
|
||||
</scm>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<license.licenseName>cecill_2.1</license.licenseName>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.bigeon</groupId>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>2.0.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.bigeon.test</groupId>
|
||||
<artifactId>junitmt</artifactId>
|
||||
<version>1.0.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.10.3</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||
<artifactId>dsm-maven-plugin</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
</project>
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.test.junitmt.FunctionalTestRunnable;
|
||||
import net.bigeon.test.junitmt.TestFunction;
|
||||
import net.bigeon.test.junitmt.ThreadTest;
|
||||
|
||||
/** @author Emmanuel Bigeon */
|
||||
public final class InputContract {
|
||||
|
||||
public void testInputContract(final Supplier<ConsoleInput> inputs)
|
||||
throws IOException, InterruptedException {
|
||||
// Test close contract
|
||||
final ConsoleInput input = inputs.get();
|
||||
assertFalse("An input should not initially be closed", input.isClosed());
|
||||
input.close();
|
||||
assertTrue("An input should close", input.isClosed());
|
||||
input.close();
|
||||
assertTrue("A closed input should stay closed", input.isClosed());
|
||||
try {
|
||||
input.prompt();
|
||||
fail("Closed input prompting should fail");
|
||||
} catch (final IOException e) {
|
||||
// ok
|
||||
}
|
||||
|
||||
// Test interruption contract
|
||||
final ConsoleInput input2 = inputs.get();
|
||||
final FunctionalTestRunnable prompting = new FunctionalTestRunnable(
|
||||
new TestFunction() {
|
||||
|
||||
@Override
|
||||
public void apply() throws Exception {
|
||||
try {
|
||||
input2.prompt();
|
||||
fail("Interrupted prompt should throw INterruptedIOException");
|
||||
} catch (final InterruptedIOException e) {
|
||||
// ok
|
||||
}
|
||||
}
|
||||
});
|
||||
final Thread th = new Thread(prompting);
|
||||
th.start();
|
||||
// while (!input2.isPrompting()) {
|
||||
th.join(200);
|
||||
//
|
||||
// }
|
||||
input2.interruptPrompt();
|
||||
ThreadTest.assertRuns(th, prompting);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.bigeon.config</groupId>
|
||||
@@ -9,7 +7,7 @@
|
||||
</parent>
|
||||
<groupId>net.bigeon.gclc</groupId>
|
||||
<artifactId>system</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
<version>0.0.3-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>GCLC system command</name>
|
||||
<description>Provide an exec command to execute system commands</description>
|
||||
@@ -45,25 +43,37 @@
|
||||
<copyright.email>emmanuel@bigeon.fr</copyright.email>
|
||||
<license.licenseName>cecill_2.1</license.licenseName>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.bigeon</groupId>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>2.0.9</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.bigeon</groupId>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>2.0.11</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>2.23.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
</plugins>
|
||||
</build>
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.10.3</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||
<artifactId>dsm-maven-plugin</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting><build>
|
||||
<plugins>
|
||||
</plugins>
|
||||
</build>
|
||||
</reporting>
|
||||
</project>
|
||||
|
||||
@@ -39,7 +39,6 @@ package net.bigeon.gclc.system;
|
||||
*/
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
@@ -92,15 +91,17 @@ public class ExecSystemCommand extends Command {
|
||||
}
|
||||
// Stream forwarding to the application's console
|
||||
// This is started in a parallel thread.
|
||||
final InputStream is = proc.getInputStream();
|
||||
final Thread th = new Thread(new ForwardingRunnable(out, is));
|
||||
th.start();
|
||||
final Thread th = ForwardingRunnable.connect(proc.getInputStream(), out,
|
||||
"Command output");
|
||||
// Set the empty prompt for the processes promptings.
|
||||
in.setPrompt(""); //$NON-NLS-1$
|
||||
// Forward console input to the process.
|
||||
// Forward console input to the process, whether the process is waiting or not
|
||||
// for input.
|
||||
final OutputStream os = proc.getOutputStream();
|
||||
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) {
|
||||
while (th.isAlive()) {
|
||||
final String user = in.prompt();
|
||||
// Avoid interruption being sent to process.
|
||||
if (!user.isEmpty()) {
|
||||
writer.write(user + EOL);
|
||||
}
|
||||
@@ -125,7 +126,7 @@ public class ExecSystemCommand extends Command {
|
||||
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$
|
||||
+ " As an example if you give \"cat\", \"/etc/hostname\" as arguments, on a linux" //$NON-NLS-1$
|
||||
+ System.lineSeparator() + "system, you would get the computer name." + //$NON-NLS-1$
|
||||
System.lineSeparator();
|
||||
}
|
||||
@@ -136,5 +137,4 @@ public class ExecSystemCommand extends Command {
|
||||
protected String usagePattern() {
|
||||
return " CMD <system command>"; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -44,6 +44,12 @@ import net.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/** A class forwarding the content of a stream into a console output.
|
||||
* <p>
|
||||
* This class is useful for processes that run in a different environment than
|
||||
* the application but return an output. Such processes include command
|
||||
* processes and socket communications for example.
|
||||
* <p>
|
||||
* In the current library is is used to retrieve a system command output.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class ForwardingRunnable implements Runnable {
|
||||
@@ -57,6 +63,7 @@ public final class ForwardingRunnable implements Runnable {
|
||||
private final ConsoleOutput out;
|
||||
/** The input stream. */
|
||||
private final InputStream is;
|
||||
private Exception error;
|
||||
|
||||
/** Create the runnable.
|
||||
*
|
||||
@@ -72,31 +79,32 @@ public final class ForwardingRunnable implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
readToEnd(out, is);
|
||||
is.close();
|
||||
readToEnd();
|
||||
} catch (final CommandRunException e) {
|
||||
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$
|
||||
LOGGER.log(Level.WARNING, "Manager was closed in the meantime...", e); //$NON-NLS-1$
|
||||
setError(e);
|
||||
}
|
||||
}
|
||||
|
||||
/** Read the input until its end.
|
||||
* <p>
|
||||
* This method actually perform a loop on the input stream to read the next
|
||||
* character and then print int on the screen.
|
||||
* <p>
|
||||
* When the stream returns a negative value, it will exit its loop and return.
|
||||
*
|
||||
* @param is the input stream
|
||||
* @throws CommandRunException if the manager was closed while writing the
|
||||
* stream */
|
||||
private void readToEnd(final ConsoleOutput out, final InputStream is)
|
||||
throws CommandRunException {
|
||||
private void readToEnd() throws CommandRunException {
|
||||
int c;
|
||||
try {
|
||||
while ((c = is.read()) >= 0) {
|
||||
// Print the chaeacter in the output
|
||||
// Print the character in the output
|
||||
printCharacter(out, (char) c);
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOGGER.log(Level.INFO, "input stream reading failed", e); //$NON-NLS-1$
|
||||
setError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,4 +122,44 @@ public final class ForwardingRunnable implements Runnable {
|
||||
MANAGER_WAS_CLOSED, e); // $NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
/** Tool method to connect an input stream to a console output.
|
||||
*
|
||||
* @param stream the stream
|
||||
* @param output the output
|
||||
* @param name the identification of the thread
|
||||
* @return the thread the forwarding runnable runs into. */
|
||||
public static Thread connect(final InputStream stream, final ConsoleOutput output,
|
||||
final String name) {
|
||||
final Thread th = new Thread(new ForwardingRunnable(output, stream), name);
|
||||
th.start();
|
||||
return th;
|
||||
}
|
||||
|
||||
/** Tool method to connect an input stream to a console output.
|
||||
*
|
||||
* @param stream the stream
|
||||
* @param output the output
|
||||
* @return the thread the forwarding runnable runs into. */
|
||||
public static Thread connect(final InputStream stream, final ConsoleOutput output) {
|
||||
return connect(stream, output, "Forwarding");
|
||||
}
|
||||
|
||||
/** Get the error in this runnable execution if any.
|
||||
* <p>
|
||||
* In the current implementation, this error can be an {@link IOException} if
|
||||
* the input stream failed or a {@link CommandRunException} if the output was
|
||||
* closed.
|
||||
*
|
||||
* @return the error */
|
||||
public synchronized Exception getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
/** Set the error that caused this runnable to stop executing.
|
||||
*
|
||||
* @param error the error to set */
|
||||
private synchronized void setError(final Exception error) {
|
||||
this.error = error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,8 +33,9 @@ package net.bigeon.gclc.system;
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -46,29 +47,35 @@ import net.bigeon.gclc.utils.SinkOutput;
|
||||
|
||||
public class ExecSystemCommandTest {
|
||||
|
||||
/** Test the help methods of the command. */
|
||||
@Test
|
||||
public void testExecSystemCommand() {
|
||||
ExecSystemCommand cmd = new ExecSystemCommand("test");
|
||||
final ExecSystemCommand cmd = new ExecSystemCommand("test");
|
||||
assertEquals("Name should be preserved", "test", cmd.getCommandName());
|
||||
assertNotNull("tip should be defined", cmd.tip());
|
||||
assertNotNull("usage should be defined", cmd.usagePattern());
|
||||
assertNotNull("usage should be defined", cmd.usageDetail());
|
||||
}
|
||||
|
||||
/** Test the execution of the command.
|
||||
*
|
||||
* @throws CommandRunException if the command fails */
|
||||
@Test
|
||||
public void testExecute() throws CommandRunException {
|
||||
ConsoleOutput out = SinkOutput.INSTANCE;
|
||||
ConsoleInput in = EmptyInput.INSTANCE;
|
||||
final ConsoleOutput out = SinkOutput.INSTANCE;
|
||||
final ConsoleInput in = EmptyInput.INSTANCE;
|
||||
|
||||
ExecSystemCommand cmd = new ExecSystemCommand();
|
||||
final ExecSystemCommand cmd = new ExecSystemCommand();
|
||||
if (System.getProperty("os.name").contains("indows")) {
|
||||
cmd.execute(out, in, "cmd", "/C", "dir");
|
||||
try {
|
||||
cmd.execute(out, in, "inexistent");
|
||||
fail("Able to execute inexistent command in system");
|
||||
} catch (CommandRunException e) {
|
||||
// ok
|
||||
}
|
||||
} else if (System.getProperty("os.name").contains("inux")) {
|
||||
cmd.execute(out, in, "ls");
|
||||
}
|
||||
try {
|
||||
cmd.execute(out, in, "inexistent");
|
||||
fail("Able to execute inexistent command in system");
|
||||
} catch (final CommandRunException e) {
|
||||
// ok
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.system;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.utils.SinkOutput;
|
||||
import net.bigeon.gclc.utils.StreamConsoleOutput;
|
||||
|
||||
/** @author Emmanuel Bigeon */
|
||||
public class ForwardingRunnableTest {
|
||||
|
||||
public void testClosedOutput() {
|
||||
final StreamConsoleOutput out = new StreamConsoleOutput();
|
||||
out.close();
|
||||
final InputStream is = new ByteArrayInputStream(new byte[] { 1, 2, 3 });
|
||||
final ForwardingRunnable runnable = new ForwardingRunnable(out, is);
|
||||
// Runnable should close immediatly.
|
||||
runnable.run();
|
||||
|
||||
assertTrue("Error should be a CommandRunException",
|
||||
runnable.getError() instanceof CommandRunException);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIOInInput() throws IOException {
|
||||
final InputStream is = Mockito.mock(InputStream.class);
|
||||
Mockito.when(is.read()).thenThrow(new IOException());
|
||||
final ForwardingRunnable runnable = new ForwardingRunnable(SinkOutput.INSTANCE,
|
||||
is);
|
||||
// Runnable should close immediatly.
|
||||
runnable.run();
|
||||
assertTrue("Error should be an IO", runnable.getError() instanceof IOException);
|
||||
}
|
||||
}
|
||||
42
gclc/pom.xml
42
gclc/pom.xml
@@ -3,12 +3,12 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>net.bigeon.config</groupId>
|
||||
<artifactId>ebigeon-config</artifactId>
|
||||
<version>1.8.21</version>
|
||||
<artifactId>ebigeon-public-conf</artifactId>
|
||||
<version>1.0.10</version>
|
||||
</parent>
|
||||
<groupId>net.bigeon</groupId>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>2.0.10</version>
|
||||
<version>2.1.2</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>Generic Command Ligne console</name>
|
||||
<description>A generic framework for console applications, with customized command input and output streams.</description>
|
||||
@@ -37,7 +37,7 @@
|
||||
</developers>
|
||||
<scm>
|
||||
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
|
||||
<tag>gclc-2.0.10</tag>
|
||||
<tag>gclc-2.1.2</tag>
|
||||
</scm>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
@@ -47,26 +47,12 @@
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<groupId>net.bigeon.test</groupId>
|
||||
<artifactId>junitmt</artifactId>
|
||||
<version>1.0.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||
<artifactId>dsm-maven-plugin</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>jdepend-maven-plugin</artifactId>
|
||||
<version>2.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -81,4 +67,18 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.github.sevntu-checkstyle</groupId>
|
||||
<artifactId>dsm-maven-plugin</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>jdepend-maven-plugin</artifactId>
|
||||
<version>2.0</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
</project>
|
||||
|
||||
@@ -95,8 +95,8 @@ import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
* A typical use case is the following:
|
||||
*
|
||||
* <pre>
|
||||
* {@link ConsoleOutput} out = new {@link net.bigeon.gclc.utils.StreamConsoleOutput StreamConsoleOutput}();
|
||||
* {@link ConsoleInput} in = new {@link net.bigeon.gclc.utils.StreamConsoleInput StreamConsoleInput}();
|
||||
* {@link ConsoleOutput} out = new {@link net.bigeon.gclc.manager.StreamConsoleOutput StreamConsoleOutput}();
|
||||
* {@link ConsoleInput} in = new {@link net.bigeon.gclc.manager.StreamConsoleInput StreamConsoleInput}();
|
||||
* {@link ConsoleApplication} app = new {@link ConsoleApplication}(out, in, "welcome", "see you latter")};
|
||||
* app.{@link ConsoleApplication#add(ICommand) add}("my_command", new {@link ICommand MyCommand()});
|
||||
* app.{@link ConsoleApplication#start() start()};
|
||||
|
||||
@@ -164,7 +164,7 @@ public final class GCLCConstants {
|
||||
}
|
||||
if (startIndex < cmd.length()) {
|
||||
final String arg = cmd.substring(startIndex, cmd.length());
|
||||
args.add(arg);
|
||||
args.add(removeEscaped(arg));
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
@@ -75,6 +75,9 @@ public abstract class Command implements ICommand {
|
||||
* @param name the command name */
|
||||
public Command(final String name) {
|
||||
super();
|
||||
if (name == null) {
|
||||
throw new IllegalArgumentException("The command name is mandatory");
|
||||
}
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
@@ -116,8 +116,7 @@ public abstract class ParametrizedCommand extends Command {
|
||||
* @param flag the boolean flag
|
||||
* @throws InvalidParameterException if the parameter is already defined as a
|
||||
* string parameter */
|
||||
protected final void addBooleanParameter(final String flag)
|
||||
throws InvalidParameterException {
|
||||
protected final void addBooleanParameter(final String flag) {
|
||||
data.addBooleanParameter(flag);
|
||||
}
|
||||
|
||||
@@ -127,8 +126,7 @@ public abstract class ParametrizedCommand extends Command {
|
||||
* @param needed if the parameter's absence should cause an exception
|
||||
* @throws InvalidParameterException if the parameter is already defined as a
|
||||
* boolean parameter */
|
||||
protected final void addStringParameter(final String flag, final boolean needed)
|
||||
throws InvalidParameterException {
|
||||
protected final void addStringParameter(final String flag, final boolean needed) {
|
||||
data.addStringParameter(flag, needed);
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import net.bigeon.gclc.exception.CommandParsingException;
|
||||
import net.bigeon.gclc.exception.InvalidParameterException;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.utils.EmptyInput;
|
||||
import net.bigeon.gclc.manager.EmptyInput;
|
||||
|
||||
/** An object to handle standardized command parameters.
|
||||
*
|
||||
@@ -120,7 +120,7 @@ public final class ParametrizedCommandData {
|
||||
* @param flag the boolean flag
|
||||
* @throws InvalidParameterException if the parameter is already defined as a
|
||||
* string parameter */
|
||||
public void addBooleanParameter(final String flag) throws InvalidParameterException {
|
||||
public void addBooleanParameter(final String flag) {
|
||||
if (params.containsKey(flag) && stringParams.containsKey(flag)) {
|
||||
throw new InvalidParameterException("Parameter is already defined as string"); //$NON-NLS-1$
|
||||
}
|
||||
@@ -134,8 +134,7 @@ public final class ParametrizedCommandData {
|
||||
* @param needed if the parameter's absence should cause an exception
|
||||
* @throws InvalidParameterException if the parameter is already defined as a
|
||||
* boolean parameter */
|
||||
public void addStringParameter(final String flag, final boolean needed)
|
||||
throws InvalidParameterException {
|
||||
public void addStringParameter(final String flag, final boolean needed) {
|
||||
if (params.containsKey(flag)) {
|
||||
checkParam(flag, needed);
|
||||
return;
|
||||
@@ -149,9 +148,8 @@ public final class ParametrizedCommandData {
|
||||
* @param param the string parameter
|
||||
* @param needed if the parameter is needed
|
||||
* @throws InvalidParameterException if the new definition is invalid */
|
||||
private void checkParam(final String param, final boolean needed)
|
||||
throws InvalidParameterException {
|
||||
Boolean val = stringParams.get(param);
|
||||
private void checkParam(final String param, final boolean needed) {
|
||||
final Boolean val = stringParams.get(param);
|
||||
if (val != null) {
|
||||
final Boolean need = Boolean.valueOf(needed || val.booleanValue());
|
||||
stringParams.put(param, need);
|
||||
@@ -217,7 +215,7 @@ public final class ParametrizedCommandData {
|
||||
* @param param the parameter name
|
||||
* @return if the parameter is needed */
|
||||
public boolean isNeeded(final String param) {
|
||||
Boolean val = params.get(param);
|
||||
final Boolean val = params.get(param);
|
||||
return val != null && val.booleanValue();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,100 +1,133 @@
|
||||
/*
|
||||
* ralph.runner, Runs RALPH (belongs to IREQ) in sequence for several days
|
||||
* Copyright (C) 2016-2018 E. Bigeon
|
||||
* mailto:emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.command.base;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Arrays;
|
||||
|
||||
import net.bigeon.gclc.command.Command;
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/** A command printing test to the console.
|
||||
* <p>
|
||||
* This command mimics the effect of the echo command in linux system. The
|
||||
* difference is this command accepts java like formatting.
|
||||
*
|
||||
* <pre>
|
||||
* echo.execute(out, in, "My Formatted {0}, with {1}", "message", "short arguments");
|
||||
* </pre>
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public class EchoCommand extends Command {
|
||||
|
||||
/** Create the command.
|
||||
*
|
||||
* @param name the command name */
|
||||
public EchoCommand(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[]) */
|
||||
@Override
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
try {
|
||||
if (args.length == 1) {
|
||||
out.print(args[0]);
|
||||
}
|
||||
if (args.length > 1) {
|
||||
out.print(MessageFormat.format(args[0],
|
||||
(Object[]) Arrays.copyOfRange(args, 1, args.length)));
|
||||
}
|
||||
out.println();
|
||||
} catch (final IOException e) {
|
||||
throw new CommandRunException("Printing failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||
@Override
|
||||
public String tip() {
|
||||
return "Print the message";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
return "A formatted message can be used";
|
||||
}
|
||||
|
||||
}
|
||||
/*
|
||||
* ralph.runner, Runs RALPH (belongs to IREQ) in sequence for several days
|
||||
* Copyright (C) 2016-2018 E. Bigeon
|
||||
* mailto:emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package net.bigeon.gclc.command.base;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* Generic Command Ligne console
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
import java.io.IOException;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Arrays;
|
||||
|
||||
import net.bigeon.gclc.command.Command;
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/** A command printing test to the console.
|
||||
* <p>
|
||||
* This command mimics the effect of the echo command in linux system. The
|
||||
* difference is this command accepts java like formatting.
|
||||
*
|
||||
* <pre>
|
||||
* echo.execute(out, in, "My Formatted {0}, with {1}", "message", "short arguments");
|
||||
* </pre>
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public class EchoCommand extends Command {
|
||||
|
||||
/** Create the command.
|
||||
*
|
||||
* @param name the command name */
|
||||
public EchoCommand(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[]) */
|
||||
@Override
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
try {
|
||||
if (args.length == 1) {
|
||||
out.print(args[0]);
|
||||
}
|
||||
if (args.length > 1) {
|
||||
out.print(MessageFormat.format(args[0],
|
||||
(Object[]) Arrays.copyOfRange(args, 1, args.length)));
|
||||
}
|
||||
out.println();
|
||||
} catch (final IOException e) {
|
||||
throw new CommandRunException("Printing failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||
@Override
|
||||
public String tip() {
|
||||
return "Print the message";
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.Command#usageDetail() */
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
return "A formatted message can be used";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ public final class ScriptExecution extends Command {
|
||||
try (InputStreamReader fReader = new InputStreamReader(
|
||||
Files.newInputStream(Paths.get(scriptFile)), charset);
|
||||
BufferedReader reader = new BufferedReader(fReader)) {
|
||||
String[] emptyArray = new String[0];
|
||||
final String[] emptyArray = new String[0];
|
||||
while ((cmd = reader.readLine()) != null) {
|
||||
lineNo++;
|
||||
final String cmdLine = readCommandLine(cmd, params);
|
||||
@@ -237,10 +237,16 @@ public final class ScriptExecution extends Command {
|
||||
* @return the exception to actually throw */
|
||||
private static CommandRunException manageRunException(final CommandRunException e,
|
||||
final int lineNo) {
|
||||
final StringBuilder builder = new StringBuilder(e.getLocalizedMessage());
|
||||
int index = builder.indexOf("\n");
|
||||
while (index > 0) {
|
||||
builder.insert(index + 1, " ");
|
||||
index = builder.indexOf("\n", index + 1);
|
||||
}
|
||||
return new CommandRunException(CommandRunExceptionType.EXECUTION,
|
||||
MessageFormat.format(
|
||||
"The script could not complete due to command failure at line {0} ({1})", //$NON-NLS-1$
|
||||
Integer.valueOf(lineNo), e.getLocalizedMessage()),
|
||||
"The script could not complete due to command failure at line {0}\n {1})", //$NON-NLS-1$
|
||||
Integer.valueOf(lineNo + 1), builder),
|
||||
e);
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ package net.bigeon.gclc.exception;
|
||||
/** An exception raised during command parsing.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public class CommandParsingException extends Exception {
|
||||
public final class CommandParsingException extends Exception {
|
||||
|
||||
/** svuid. */
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -75,7 +75,7 @@ package net.bigeon.gclc.exception;
|
||||
* the command is already used.
|
||||
*
|
||||
* @author Emmanuel BIGEON */
|
||||
public class InvalidCommandName extends Exception {
|
||||
public final class InvalidCommandName extends Exception {
|
||||
|
||||
/** the SVUID. */
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -75,7 +75,7 @@ package net.bigeon.gclc.exception;
|
||||
* parameter definition.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public class InvalidParameterException extends Exception {
|
||||
public final class InvalidParameterException extends RuntimeException {
|
||||
|
||||
/** the SVUID. */
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -73,8 +73,7 @@ package net.bigeon.gclc.manager;
|
||||
*/
|
||||
import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
|
||||
import net.bigeon.gclc.tools.StringProvider;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** A console application input.
|
||||
*
|
||||
@@ -90,13 +89,12 @@ public interface ConsoleInput extends AutoCloseable {
|
||||
/** Get the prompt string.
|
||||
*
|
||||
* @return the prompt prefix */
|
||||
StringProvider getPrompt();
|
||||
Supplier<String> getPrompt();
|
||||
|
||||
/** Indicate to the input that is should interrompt the prompting, if possible.
|
||||
* <p>
|
||||
* The pending {@link #prompt()} or {@link #prompt(String)} operations should
|
||||
* return immediatly. However the returned value can be anything (from the
|
||||
* partial prompt content to an empty string or even a null pointer). */
|
||||
* return immediately by throwing an InterruptedIOException. */
|
||||
void interruptPrompt();
|
||||
|
||||
/** Test if the input is closed.
|
||||
@@ -148,5 +146,5 @@ public interface ConsoleInput extends AutoCloseable {
|
||||
/** Set the prompting string.
|
||||
*
|
||||
* @param string the prompt string */
|
||||
void setPrompt(StringProvider string);
|
||||
void setPrompt(Supplier<String> string);
|
||||
}
|
||||
|
||||
@@ -36,53 +36,16 @@
|
||||
* gclc:net.bigeon.gclc.manager.EmptyInput.java
|
||||
* Created on: Nov 13, 2017
|
||||
*/
|
||||
package net.bigeon.gclc.utils;
|
||||
package net.bigeon.gclc.manager;
|
||||
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* Generic Command Ligne console
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import net.bigeon.gclc.tools.ConstantString;
|
||||
import net.bigeon.gclc.tools.StringProvider;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** A console input that return empty to all prompting.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class EmptyInput implements ConsoleInput {
|
||||
|
||||
private static final ConstantString CONSTANT_STRING = new ConstantString("");
|
||||
private static final Supplier<String> CONSTANT_STRING = () -> "";
|
||||
/** The empty prompter. */
|
||||
public static final ConsoleInput INSTANCE = new EmptyInput();
|
||||
|
||||
@@ -101,8 +64,8 @@ public final class EmptyInput implements ConsoleInput {
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.manager.ConsoleInput#getPrompt() */
|
||||
@Override
|
||||
public StringProvider getPrompt() {
|
||||
return CONSTANT_STRING; //$NON-NLS-1$
|
||||
public Supplier<String> getPrompt() {
|
||||
return CONSTANT_STRING;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -155,7 +118,7 @@ public final class EmptyInput implements ConsoleInput {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPrompt(StringProvider string) {
|
||||
public void setPrompt(final Supplier<String> string) {
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@
|
||||
* gclc-test:net.bigeon.gclc.test.TestConsoleManager.java
|
||||
* Created on: Nov 18, 2016
|
||||
*/
|
||||
package net.bigeon.gclc.utils;
|
||||
package net.bigeon.gclc.manager;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
@@ -76,9 +76,9 @@ import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.tools.StringProvider;
|
||||
import net.bigeon.gclc.utils.WritingRunnable;
|
||||
|
||||
/** This console input allows to enter commands and retrieve the output as an
|
||||
* input.
|
||||
@@ -120,7 +120,7 @@ public final class PipedConsoleInput implements ConsoleInput {
|
||||
}
|
||||
|
||||
@Override
|
||||
public StringProvider getPrompt() {
|
||||
public Supplier<String> getPrompt() {
|
||||
return innerManager.getPrompt();
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ public final class PipedConsoleInput implements ConsoleInput {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPrompt(StringProvider string) {
|
||||
public void setPrompt(final Supplier<String> string) {
|
||||
innerManager.setPrompt(string);
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
* gclc-test:net.bigeon.gclc.test.TestConsoleManager.java
|
||||
* Created on: Nov 18, 2016
|
||||
*/
|
||||
package net.bigeon.gclc.utils;
|
||||
package net.bigeon.gclc.manager;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
@@ -79,7 +79,7 @@ import java.io.PipedOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
import net.bigeon.gclc.utils.ReadingRunnable;
|
||||
|
||||
/** This console output allows to retrieve the output as an input.
|
||||
* <p>
|
||||
@@ -36,9 +36,7 @@
|
||||
* gclc:net.bigeon.gclc.manager.SinkOutput.java
|
||||
* Created on: Nov 13, 2017
|
||||
*/
|
||||
package net.bigeon.gclc.utils;
|
||||
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
package net.bigeon.gclc.manager;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
@@ -36,7 +36,7 @@
|
||||
* gclc:net.bigeon.gclc.system.SystemConsoleManager.java
|
||||
* Created on: Dec 19, 2014
|
||||
*/
|
||||
package net.bigeon.gclc.utils;
|
||||
package net.bigeon.gclc.manager;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
@@ -77,10 +77,9 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.tools.ConstantString;
|
||||
import net.bigeon.gclc.tools.StringProvider;
|
||||
import net.bigeon.gclc.utils.ReadingRunnable;
|
||||
|
||||
/** A console using the input stream and print stream.
|
||||
* <p>
|
||||
@@ -90,10 +89,10 @@ import net.bigeon.gclc.tools.StringProvider;
|
||||
public final class StreamConsoleInput implements ConsoleInput {
|
||||
|
||||
/** The default prompt. */
|
||||
public static final StringProvider DEFAULT_PROMPT = new ConstantString("> "); //$NON-NLS-1$
|
||||
public static final Supplier<String> DEFAULT_PROMPT = () -> ("> "); //$NON-NLS-1$
|
||||
|
||||
/** The command prompt. It can be changed. */
|
||||
private StringProvider prompt = DEFAULT_PROMPT;
|
||||
private Supplier<String> prompt = DEFAULT_PROMPT;
|
||||
|
||||
/** The print stream. */
|
||||
private final PrintStream out;
|
||||
@@ -152,7 +151,7 @@ public final class StreamConsoleInput implements ConsoleInput {
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.manager.ConsoleInput#getPrompt() */
|
||||
@Override
|
||||
public StringProvider getPrompt() {
|
||||
public Supplier<String> getPrompt() {
|
||||
return prompt;
|
||||
}
|
||||
|
||||
@@ -175,14 +174,14 @@ public final class StreamConsoleInput implements ConsoleInput {
|
||||
* @see net.bigeon.gclc.ConsoleManager#prompt() */
|
||||
@Override
|
||||
public String prompt() throws IOException {
|
||||
return prompt(prompt.apply());
|
||||
return prompt(prompt.get());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.manager.ConsoleManager#prompt(long) */
|
||||
@Override
|
||||
public String prompt(final long timeout) throws IOException {
|
||||
return prompt(prompt.apply(), timeout);
|
||||
return prompt(prompt.get(), timeout);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -213,11 +212,11 @@ public final class StreamConsoleInput implements ConsoleInput {
|
||||
* @see net.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */
|
||||
@Override
|
||||
public void setPrompt(final String prompt) {
|
||||
this.prompt = new ConstantString(prompt);
|
||||
this.prompt = () -> (prompt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPrompt(StringProvider string) {
|
||||
public void setPrompt(final Supplier<String> string) {
|
||||
prompt = string;
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@
|
||||
* gclc:net.bigeon.gclc.system.SystemConsoleManager.java
|
||||
* Created on: Dec 19, 2014
|
||||
*/
|
||||
package net.bigeon.gclc.utils;
|
||||
package net.bigeon.gclc.manager;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
@@ -74,8 +74,6 @@ package net.bigeon.gclc.utils;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
|
||||
/** A console using the input stream and print stream.
|
||||
* <p>
|
||||
* The default constructor will use the system standart input and output.
|
||||
@@ -36,7 +36,7 @@
|
||||
* gclc:net.bigeon.gclc.tools.AOutputForwardRunnable.java
|
||||
* Created on: Dec 1, 2016
|
||||
*/
|
||||
package net.bigeon.gclc.utils;
|
||||
package net.bigeon.gclc.manager.forwarding;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
@@ -75,6 +75,8 @@ import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
|
||||
/** An incomplete implematation used to forward messages from a piped console.
|
||||
* <p>
|
||||
* This forwarding can be interrupted without closing the piped manager.
|
||||
@@ -88,9 +90,9 @@ public abstract class AOutputForwardRunnable implements Runnable {
|
||||
/** The default timeout (one tenth of second). */
|
||||
private static final long DEFAULT_TIMEOUT = 100;
|
||||
/** The manager. */
|
||||
private final PipedConsoleOutput manager;
|
||||
protected final PipedConsoleOutput manager;
|
||||
/** The timeout. */
|
||||
private final long timeout;
|
||||
protected final long timeout;
|
||||
|
||||
/** Create a forwarding runnable.
|
||||
*
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
/** This package contains the {@link java.lang.Runnable} implementations used to
|
||||
* forward messages to or from console output or input.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
package net.bigeon.gclc.manager.forwarding;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* Generic Command Ligne console
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
@@ -34,6 +34,9 @@
|
||||
*/
|
||||
package net.bigeon.gclc.tools;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* Generic Command Ligne console
|
||||
@@ -68,23 +71,47 @@ package net.bigeon.gclc.tools;
|
||||
* #L%
|
||||
*/
|
||||
|
||||
/** A {@link StringProvider} that hold a constant string.
|
||||
/** A supplier of string that hold a constant string.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public class ConstantString implements StringProvider {
|
||||
* @author Emmanuel Bigeon
|
||||
* @deprecated since 2.1.2, use a lambda expression instead */
|
||||
@Deprecated
|
||||
public class ConstantString implements Supplier<String> {
|
||||
private final String string;
|
||||
|
||||
/** Create a provider for a string.
|
||||
*
|
||||
* @param string the string */
|
||||
public ConstantString(String string) {
|
||||
public ConstantString(final String string) {
|
||||
this.string = string;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see net.bigeon.gclc.tools.StringProvider#apply() */
|
||||
@Override
|
||||
public String apply() {
|
||||
public String get() {
|
||||
return string;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode() */
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(string);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#equals(java.lang.Object) */
|
||||
@Override
|
||||
public boolean equals(final Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final ConstantString other = (ConstantString) obj;
|
||||
return Objects.equals(string, other.string);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* Copyright Bigeon Emmanuel (2014)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a generic framework for console applications.
|
||||
*
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
*/
|
||||
package net.bigeon.gclc.tools;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* Generic Command Ligne console
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 bigeon.fr
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
/** A string providing object.
|
||||
* <p>
|
||||
* Implementations of this interface will provide a string, this internal state
|
||||
* of the object may be so that successive calls to the apply method return
|
||||
* different results.
|
||||
*
|
||||
* @author Emmanuel */
|
||||
public interface StringProvider {
|
||||
/** Provide a string.
|
||||
*
|
||||
* @return the string */
|
||||
String apply();
|
||||
}
|
||||
@@ -111,6 +111,7 @@ public final class ReadingRunnable implements Runnable {
|
||||
private final Map<String, Object> messageBlocker = new ConcurrentHashMap<>();
|
||||
/** The lock. */
|
||||
private final Object messageBlockerLock = new Object();
|
||||
private boolean interrupting = false;
|
||||
/** Create a reading runnable.
|
||||
*
|
||||
* @param reader the input to read from */
|
||||
@@ -145,6 +146,9 @@ public final class ReadingRunnable implements Runnable {
|
||||
* @throws IOException if the pipe is closed */
|
||||
public String getMessage() throws IOException {
|
||||
synchronized (lock) {
|
||||
if (!messages.isEmpty()) {
|
||||
return pollMessage();
|
||||
}
|
||||
if (!running) {
|
||||
throw new IOException(CLOSED_PIPE);
|
||||
}
|
||||
@@ -152,8 +156,7 @@ public final class ReadingRunnable implements Runnable {
|
||||
waitMessage(TIMEOUT);
|
||||
LOGGER.log(Level.FINEST, "Polled: {0}", messages.peek()); //$NON-NLS-1$
|
||||
waiting = false;
|
||||
notifyMessage(messages.peek());
|
||||
return messages.poll();
|
||||
return pollMessage();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,6 +167,9 @@ public final class ReadingRunnable implements Runnable {
|
||||
* @throws IOException if the input was closed */
|
||||
public String getNextMessage(final long timeout) throws IOException {
|
||||
synchronized (lock) {
|
||||
if (!messages.isEmpty()) {
|
||||
return pollMessage();
|
||||
}
|
||||
if (!running) {
|
||||
throw new IOException(CLOSED_PIPE);
|
||||
}
|
||||
@@ -173,11 +179,24 @@ public final class ReadingRunnable implements Runnable {
|
||||
if (messages.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return messages.poll();
|
||||
return pollMessage();
|
||||
}
|
||||
}
|
||||
|
||||
private String pollMessage() throws InterruptedIOException {
|
||||
final String msg = messages.poll();
|
||||
if (msg.isEmpty() && interrupting) {
|
||||
throw new InterruptedIOException();
|
||||
}
|
||||
notifyMessage(msg);
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void waitForDelivery(String message) throws InterruptedException {
|
||||
/** Wait for a message to be delivered.
|
||||
*
|
||||
* @param message the message
|
||||
* @throws InterruptedException if the wait was interrupted */
|
||||
public void waitForDelivery(final String message) throws InterruptedException {
|
||||
Object mLock;
|
||||
synchronized (messageBlockerLock) {
|
||||
if (!messageBlocker.containsKey(message)) {
|
||||
@@ -196,10 +215,11 @@ public final class ReadingRunnable implements Runnable {
|
||||
* @throws IOException if the pipe is closed */
|
||||
public boolean hasMessage() throws IOException {
|
||||
synchronized (lock) {
|
||||
if (!running) {
|
||||
final boolean empty = messages.isEmpty();
|
||||
if (!running && empty) {
|
||||
throw new IOException(CLOSED_PIPE);
|
||||
}
|
||||
return !messages.isEmpty();
|
||||
return !empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,7 +227,8 @@ public final class ReadingRunnable implements Runnable {
|
||||
public void interrupt() {
|
||||
synchronized (lock) {
|
||||
if (waiting) {
|
||||
messages.offer(""); //$NON-NLS-1$
|
||||
messages.offer("");
|
||||
interrupting = true;
|
||||
lock.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,8 +75,8 @@ import java.io.IOException;
|
||||
|
||||
import net.bigeon.gclc.command.ICommand;
|
||||
import net.bigeon.gclc.exception.InvalidCommandName;
|
||||
import net.bigeon.gclc.utils.PipedConsoleInput;
|
||||
import net.bigeon.gclc.utils.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleInput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
||||
@@ -95,8 +95,8 @@ import net.bigeon.gclc.exception.InvalidCommandName;
|
||||
import net.bigeon.gclc.i18n.Messages;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
import net.bigeon.gclc.utils.PipedConsoleInput;
|
||||
import net.bigeon.gclc.utils.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleInput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
|
||||
/** Test class for ConsoleApplication
|
||||
*
|
||||
|
||||
@@ -82,7 +82,7 @@ import org.junit.Test;
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
import net.bigeon.gclc.utils.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
||||
@@ -83,7 +83,7 @@ import net.bigeon.gclc.command.base.MockCommand;
|
||||
import net.bigeon.gclc.exception.CommandRunException;
|
||||
import net.bigeon.gclc.manager.ConsoleInput;
|
||||
import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
import net.bigeon.gclc.utils.PipedConsoleOutput;
|
||||
import net.bigeon.gclc.manager.PipedConsoleOutput;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
||||
@@ -1,5 +1,39 @@
|
||||
package net.bigeon.gclc.command;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* Generic Command Ligne console
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
* that may mean that it is complicated to manipulate, and that also
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user