diff --git a/gclc.system/src/main/java/net/bigeon/gclc/system/ExecSystemCommand.java b/gclc.system/src/main/java/net/bigeon/gclc/system/ExecSystemCommand.java index 34a71ad..4e1f675 100644 --- a/gclc.system/src/main/java/net/bigeon/gclc/system/ExecSystemCommand.java +++ b/gclc.system/src/main/java/net/bigeon/gclc/system/ExecSystemCommand.java @@ -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,9 +91,8 @@ 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, whether the process is waiting or not @@ -103,9 +101,7 @@ public class ExecSystemCommand extends Command { try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) { while (th.isAlive()) { final String user = in.prompt(); - if (!user.isEmpty()) { - writer.write(user + EOL); - } + writer.write(user + EOL); } } catch (final IOException e1) { throw new CommandRunException(CommandRunExceptionType.INTERACTION, @@ -127,7 +123,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(); } @@ -138,5 +134,4 @@ public class ExecSystemCommand extends Command { protected String usagePattern() { return " CMD "; //$NON-NLS-1$ } - } diff --git a/gclc.system/src/main/java/net/bigeon/gclc/system/ForwardingRunnable.java b/gclc.system/src/main/java/net/bigeon/gclc/system/ForwardingRunnable.java index 504a525..1f4700e 100644 --- a/gclc.system/src/main/java/net/bigeon/gclc/system/ForwardingRunnable.java +++ b/gclc.system/src/main/java/net/bigeon/gclc/system/ForwardingRunnable.java @@ -44,6 +44,10 @@ import net.bigeon.gclc.exception.CommandRunExceptionType; import net.bigeon.gclc.manager.ConsoleOutput; /** A class forwarding the content of a stream into a console output. + *

+ * 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. * * @author Emmanuel Bigeon */ public final class ForwardingRunnable implements Runnable { @@ -75,8 +79,7 @@ public final class ForwardingRunnable implements Runnable { readToEnd(out, is); is.close(); } catch (final CommandRunException e) { - LOGGER.log(Level.WARNING, - "Manager was closed in the meantime...", e); //$NON-NLS-1$ + LOGGER.log(Level.WARNING, "Manager was closed in the meantime...", e); //$NON-NLS-1$ } catch (final IOException e) { LOGGER.log(Level.WARNING, "Input stream was closed...", e); //$NON-NLS-1$ } @@ -114,4 +117,26 @@ 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"); + } }