New project for the system command execution command in gclc

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
2016-08-05 09:54:35 -04:00
parent 7289fc12ad
commit ecc10994ca
4 changed files with 222 additions and 0 deletions

View File

@@ -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;
/** <p>
* 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 <system command>";
}
/* (non-Javadoc)
* @see fr.bigeon.gclc.command.ICommand#tip() */
@Override
public String tip() {
return "Execute a system command";
}
}

View File

@@ -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 );
}
}