From ecc10994ca33ad06a13aca790aafd87bf2b878e8 Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Fri, 5 Aug 2016 09:54:35 -0400 Subject: [PATCH] New project for the system command execution command in gclc Signed-off-by: Emmanuel Bigeon --- gclc.system/.gitignore | 4 + gclc.system/pom.xml | 41 ++++++ .../bigeon/gclc/system/ExecSystemCommand.java | 139 ++++++++++++++++++ .../java/net/bigeon/gclc/system/AppTest.java | 38 +++++ 4 files changed, 222 insertions(+) create mode 100644 gclc.system/.gitignore create mode 100644 gclc.system/pom.xml create mode 100644 gclc.system/src/main/java/net/bigeon/gclc/system/ExecSystemCommand.java create mode 100644 gclc.system/src/test/java/net/bigeon/gclc/system/AppTest.java diff --git a/gclc.system/.gitignore b/gclc.system/.gitignore new file mode 100644 index 0000000..d98981c --- /dev/null +++ b/gclc.system/.gitignore @@ -0,0 +1,4 @@ +/target/ +/.classpath +/.project +/.settings/ diff --git a/gclc.system/pom.xml b/gclc.system/pom.xml new file mode 100644 index 0000000..c3b3e56 --- /dev/null +++ b/gclc.system/pom.xml @@ -0,0 +1,41 @@ + + 4.0.0 + net.bigeon + gclc.system + 0.0.1-SNAPSHOT + jar + http://www.bigeon.net + + UTF-8 + + + + + + + + junit + junit + 4.11 + test + + + fr.bigeon + gclc + 1.2.5 + + + GCLC system command + Provide an exec command to execute system commands + 2016 + + scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git + HEAD + + + fr.bigeon + ebigeon-config + 1.7.0 + + 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 new file mode 100644 index 0000000..81a86c6 --- /dev/null +++ b/gclc.system/src/main/java/net/bigeon/gclc/system/ExecSystemCommand.java @@ -0,0 +1,139 @@ +/** + * gclc.system:net.bigeon.gclc.system.ExecSystemCommand.java + * Created on: Jun 20, 2016 + */ +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; +import java.util.logging.Level; +import java.util.logging.Logger; + +import fr.bigeon.gclc.command.Command; +import fr.bigeon.gclc.exception.CommandRunException; +import fr.bigeon.gclc.exception.CommandRunExceptionType; +import fr.bigeon.gclc.manager.ConsoleManager; + +/**

+ * TODO + * + * @author Emmanuel Bigeon */ +public class ExecSystemCommand extends Command { + + private static final String EOL = System.lineSeparator(); + /** The class logger */ + private static final Logger LOGGER = Logger + .getLogger(ExecSystemCommand.class.getName()); + private final ConsoleManager manager; + + /** @param name + * @param manager */ + public ExecSystemCommand(String name, ConsoleManager manager) { + super(name); + this.manager = manager; + } + + /** @param name + * @param manager */ + public ExecSystemCommand(ConsoleManager manager) { + super("exec"); + this.manager = manager; + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */ + @Override + public void execute(String... args) throws CommandRunException { + Process proc; + try { + proc = Runtime.getRuntime().exec(args); + } catch (IOException e2) { + LOGGER.log(Level.SEVERE, "Unable to run process", e2); + return; + } + + final InputStream is = proc.getInputStream(); + Thread th = new Thread(new Runnable() { + + @Override + public void run() { + try { + readToEnd(is); + } catch (CommandRunException e) { + LOGGER.log(Level.WARNING, + "Manager was closed in the meantime...", e); + } + } + }); + th.start(); + manager.setPrompt(""); + final OutputStream os = proc.getOutputStream(); + try (BufferedWriter writer = new BufferedWriter( + new OutputStreamWriter(os))) { + while (th.isAlive()) { + String user; + try { + user = manager.prompt(); + } catch (IOException e) { + throw new CommandRunException( + CommandRunExceptionType.INTERACTION, + "manager was closed", e, this); + } + writer.write(user + EOL); + } + } catch (IOException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + + /** @param is the input stream + * @throws CommandRunException */ + protected void readToEnd(InputStream is) throws CommandRunException { + int c; + try { + while ((c = is.read()) != -1) { + try { + manager.print(Character.valueOf((char) c).toString()); + } catch (IOException e) { + throw new CommandRunException( + CommandRunExceptionType.INTERACTION, + "manager was closed", e, this); + } + } + } catch (IOException e) { + LOGGER.log(Level.INFO, "input stream reading failed", e); + } + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.Command#usageDetail() */ + @Override + protected String usageDetail() { + return " The system command is a system dependend command like sh on linux or" + + System.lineSeparator() + "powershell on windows." + + System.lineSeparator() + System.lineSeparator() + + " As an example if you give \"cat /etc/hostname\" as argument, on a linux" + + System.lineSeparator() + + "system, you would get the computer name." + + System.lineSeparator(); + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.Command#usagePattern() */ + @Override + protected String usagePattern() { + return " CMD "; + } + + /* (non-Javadoc) + * @see fr.bigeon.gclc.command.ICommand#tip() */ + @Override + public String tip() { + return "Execute a system command"; + } + +} diff --git a/gclc.system/src/test/java/net/bigeon/gclc/system/AppTest.java b/gclc.system/src/test/java/net/bigeon/gclc/system/AppTest.java new file mode 100644 index 0000000..543473d --- /dev/null +++ b/gclc.system/src/test/java/net/bigeon/gclc/system/AppTest.java @@ -0,0 +1,38 @@ +package net.bigeon.gclc.system; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +}