Set socket closing by interruption trace as FINE level
Additional comments, extract exec command default name to constant Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
@@ -23,53 +23,63 @@ import fr.bigeon.gclc.manager.ConsoleManager;
|
||||
* @author Emmanuel Bigeon */
|
||||
public class ExecSystemCommand extends Command {
|
||||
|
||||
/** The command default name */
|
||||
private static final String COMMAND_DEFAULT_NAME = "exec"; //$NON-NLS-1$
|
||||
/** The end of line separator */
|
||||
private static final String EOL = System.lineSeparator();
|
||||
/** The class logger */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(ExecSystemCommand.class.getName());
|
||||
/** The manager for the application's user interface */
|
||||
private final ConsoleManager manager;
|
||||
|
||||
/** @param name
|
||||
* @param manager */
|
||||
/** @param name the name of the command (the input from the manager that
|
||||
* should trigger this command)
|
||||
* @param manager the console manager for input and outputs */
|
||||
public ExecSystemCommand(String name, ConsoleManager manager) {
|
||||
super(name);
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
/** @param name
|
||||
* @param manager */
|
||||
/** @param manager the console manager for input and outputs */
|
||||
public ExecSystemCommand(ConsoleManager manager) {
|
||||
super("exec");
|
||||
super(COMMAND_DEFAULT_NAME);
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */
|
||||
@SuppressWarnings("resource")
|
||||
@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);
|
||||
LOGGER.log(Level.SEVERE, "Unable to run process", e2); //$NON-NLS-1$
|
||||
return;
|
||||
}
|
||||
|
||||
final InputStream is = proc.getInputStream();
|
||||
final InputStream is = proc
|
||||
.getInputStream();
|
||||
Thread th = new Thread(new Runnable() {
|
||||
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
readToEnd(is);
|
||||
is.close();
|
||||
} catch (CommandRunException e) {
|
||||
LOGGER.log(Level.WARNING,
|
||||
"Manager was closed in the meantime...", e);
|
||||
"Manager was closed in the meantime...", e); //$NON-NLS-1$
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.WARNING, "Input stream was closed...", e); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
});
|
||||
th.start();
|
||||
manager.setPrompt("");
|
||||
manager.setPrompt(new String());
|
||||
final OutputStream os = proc.getOutputStream();
|
||||
try (BufferedWriter writer = new BufferedWriter(
|
||||
new OutputStreamWriter(os))) {
|
||||
@@ -80,18 +90,19 @@ public class ExecSystemCommand extends Command {
|
||||
} catch (IOException e) {
|
||||
throw new CommandRunException(
|
||||
CommandRunExceptionType.INTERACTION,
|
||||
"manager was closed", e, this);
|
||||
"manager was closed", e, this); //$NON-NLS-1$
|
||||
}
|
||||
writer.write(user + EOL);
|
||||
}
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
throw new CommandRunException(CommandRunExceptionType.INTERACTION,
|
||||
"manager was closed", e1, this); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
/** @param is the input stream
|
||||
* @throws CommandRunException */
|
||||
* @throws CommandRunException if the manager was closed while writing the
|
||||
* stream */
|
||||
protected void readToEnd(InputStream is) throws CommandRunException {
|
||||
int c;
|
||||
try {
|
||||
@@ -101,11 +112,11 @@ public class ExecSystemCommand extends Command {
|
||||
} catch (IOException e) {
|
||||
throw new CommandRunException(
|
||||
CommandRunExceptionType.INTERACTION,
|
||||
"manager was closed", e, this);
|
||||
"manager was closed", e, this); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.INFO, "input stream reading failed", e);
|
||||
LOGGER.log(Level.INFO, "input stream reading failed", e); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,12 +124,12 @@ public class ExecSystemCommand extends Command {
|
||||
* @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." +
|
||||
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" +
|
||||
" As an example if you give \"cat /etc/hostname\" as argument, on a linux" + //$NON-NLS-1$
|
||||
System.lineSeparator() +
|
||||
"system, you would get the computer name." +
|
||||
"system, you would get the computer name." + //$NON-NLS-1$
|
||||
System.lineSeparator();
|
||||
}
|
||||
|
||||
@@ -126,14 +137,14 @@ public class ExecSystemCommand extends Command {
|
||||
* @see fr.bigeon.gclc.command.Command#usagePattern() */
|
||||
@Override
|
||||
protected String usagePattern() {
|
||||
return " CMD <system command>";
|
||||
return " CMD <system command>"; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#tip() */
|
||||
@Override
|
||||
public String tip() {
|
||||
return "Execute a system command";
|
||||
return "Execute a system command"; //$NON-NLS-1$
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
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 );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user