Added tool methods for most common uses

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
Emmanuel Bigeon 2018-10-27 09:50:28 -04:00
parent cf29eb37cc
commit 55bb54ca43
2 changed files with 31 additions and 11 deletions

View File

@ -39,7 +39,6 @@ package net.bigeon.gclc.system;
*/ */
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
@ -92,9 +91,8 @@ public class ExecSystemCommand extends Command {
} }
// Stream forwarding to the application's console // Stream forwarding to the application's console
// This is started in a parallel thread. // This is started in a parallel thread.
final InputStream is = proc.getInputStream(); final Thread th = ForwardingRunnable.connect(proc.getInputStream(), out,
final Thread th = new Thread(new ForwardingRunnable(out, is)); "Command output");
th.start();
// Set the empty prompt for the processes promptings. // Set the empty prompt for the processes promptings.
in.setPrompt(""); //$NON-NLS-1$ in.setPrompt(""); //$NON-NLS-1$
// Forward console input to the process, whether the process is waiting or not // Forward console input to the process, whether the process is waiting or not
@ -103,10 +101,8 @@ public class ExecSystemCommand extends Command {
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) { try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) {
while (th.isAlive()) { while (th.isAlive()) {
final String user = in.prompt(); final String user = in.prompt();
if (!user.isEmpty()) {
writer.write(user + EOL); writer.write(user + EOL);
} }
}
} catch (final IOException e1) { } catch (final IOException e1) {
throw new CommandRunException(CommandRunExceptionType.INTERACTION, throw new CommandRunException(CommandRunExceptionType.INTERACTION,
MANAGER_WAS_CLOSED, e1); // $NON-NLS-1$ MANAGER_WAS_CLOSED, e1); // $NON-NLS-1$
@ -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$ return " The system command is a system dependend command like sh on linux or" + //$NON-NLS-1$
System.lineSeparator() + "powershell on windows." + //$NON-NLS-1$ System.lineSeparator() + "powershell on windows." + //$NON-NLS-1$
System.lineSeparator() + System.lineSeparator() System.lineSeparator() + System.lineSeparator()
+ " As an example if you give \"cat /etc/hostname\" as argument, on a linux" //$NON-NLS-1$ + " As an example if you give \"cat\", \"/etc/hostname\" as arguments, on a linux" //$NON-NLS-1$
+ System.lineSeparator() + "system, you would get the computer name." + //$NON-NLS-1$ + System.lineSeparator() + "system, you would get the computer name." + //$NON-NLS-1$
System.lineSeparator(); System.lineSeparator();
} }
@ -138,5 +134,4 @@ public class ExecSystemCommand extends Command {
protected String usagePattern() { protected String usagePattern() {
return " CMD <system command>"; //$NON-NLS-1$ return " CMD <system command>"; //$NON-NLS-1$
} }
} }

View File

@ -44,6 +44,10 @@ import net.bigeon.gclc.exception.CommandRunExceptionType;
import net.bigeon.gclc.manager.ConsoleOutput; import net.bigeon.gclc.manager.ConsoleOutput;
/** A class forwarding the content of a stream into a console output. /** 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.
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
public final class ForwardingRunnable implements Runnable { public final class ForwardingRunnable implements Runnable {
@ -75,8 +79,7 @@ public final class ForwardingRunnable implements Runnable {
readToEnd(out, is); readToEnd(out, is);
is.close(); is.close();
} catch (final CommandRunException e) { } catch (final CommandRunException e) {
LOGGER.log(Level.WARNING, LOGGER.log(Level.WARNING, "Manager was closed in the meantime...", e); //$NON-NLS-1$
"Manager was closed in the meantime...", e); //$NON-NLS-1$
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.log(Level.WARNING, "Input stream was closed...", e); //$NON-NLS-1$ LOGGER.log(Level.WARNING, "Input stream was closed...", e); //$NON-NLS-1$
} }
@ -114,4 +117,26 @@ public final class ForwardingRunnable implements Runnable {
MANAGER_WAS_CLOSED, e); // $NON-NLS-1$ 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");
}
} }