@@ -11,17 +11,17 @@ package net.bigeon.gclc.system;
|
||||
* Copyright (C) 2016 - 2018 Bigeon
|
||||
* %%
|
||||
* This software is governed by the CeCILL license under French law and
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* abiding by the rules of distribution of free software. You can use,
|
||||
* modify and/ or redistribute the software under the terms of the CeCILL
|
||||
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* "http://www.cecill.info".
|
||||
*
|
||||
* As a counterpart to the access to the source code and rights to copy,
|
||||
* modify and redistribute granted by the license, users are provided only
|
||||
* with a limited warranty and the software's author, the holder of the
|
||||
* economic rights, and the successive licensors have only limited
|
||||
* liability.
|
||||
*
|
||||
* liability.
|
||||
*
|
||||
* In this respect, the user's attention is drawn to the risks associated
|
||||
* with loading, using, modifying and/or developing or reproducing the
|
||||
* software by the user in light of its specific status of free software,
|
||||
@@ -29,10 +29,10 @@ package net.bigeon.gclc.system;
|
||||
* therefore means that it is reserved for developers and experienced
|
||||
* professionals having in-depth computer knowledge. Users are therefore
|
||||
* encouraged to load and test the software's suitability as regards their
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* requirements in conditions enabling the security of their systems and/or
|
||||
* data to be ensured and, more generally, to use and operate it in the
|
||||
* same conditions as regards security.
|
||||
*
|
||||
* The fact that you are presently reading this means that you have had
|
||||
* knowledge of the CeCILL license and that you accept its terms.
|
||||
* #L%
|
||||
@@ -57,11 +57,11 @@ import net.bigeon.gclc.manager.ConsoleOutput;
|
||||
public class ExecSystemCommand extends Command {
|
||||
|
||||
/** The command default name */
|
||||
private static final String COMMAND_DEFAULT_NAME = "exec"; //$NON-NLS-1$
|
||||
private static final String COMMAND_DEFAULT_NAME = "exec"; //$NON-NLS-1$
|
||||
/** The end of line separator */
|
||||
private static final String EOL = System.lineSeparator();
|
||||
private static final String EOL = System.lineSeparator();
|
||||
/** The class logger */
|
||||
private static final Logger LOGGER = Logger
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(ExecSystemCommand.class.getName());
|
||||
|
||||
/***/
|
||||
@@ -69,19 +69,18 @@ public class ExecSystemCommand extends Command {
|
||||
super(COMMAND_DEFAULT_NAME);
|
||||
}
|
||||
|
||||
/** @param name the name of the command (the input from the manager that
|
||||
* should trigger this command) */
|
||||
/** @param name the name of the command (the input from the manager that should
|
||||
* trigger this command) */
|
||||
public ExecSystemCommand(final String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.command.ICommand#execute(fr.bigeon.gclc.manager.
|
||||
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput,
|
||||
* java.lang.String[]) */
|
||||
* ConsoleOutput, fr.bigeon.gclc.manager.ConsoleInput, java.lang.String[]) */
|
||||
@Override
|
||||
public void execute(final ConsoleOutput out, final ConsoleInput in,
|
||||
final String... args) throws CommandRunException {
|
||||
final String... args) throws CommandRunException {
|
||||
Process proc;
|
||||
try {
|
||||
proc = Runtime.getRuntime().exec(args);
|
||||
@@ -90,8 +89,7 @@ public class ExecSystemCommand extends Command {
|
||||
return;
|
||||
}
|
||||
|
||||
final InputStream is = proc
|
||||
.getInputStream();
|
||||
final InputStream is = proc.getInputStream();
|
||||
final Thread th = new Thread(new Runnable() {
|
||||
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@@ -101,8 +99,7 @@ public class ExecSystemCommand extends Command {
|
||||
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$
|
||||
}
|
||||
@@ -111,15 +108,13 @@ public class ExecSystemCommand extends Command {
|
||||
th.start();
|
||||
in.setPrompt(""); //$NON-NLS-1$
|
||||
final OutputStream os = proc.getOutputStream();
|
||||
try (BufferedWriter writer = new BufferedWriter(
|
||||
new OutputStreamWriter(os))) {
|
||||
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) {
|
||||
while (th.isAlive()) {
|
||||
String user;
|
||||
try {
|
||||
user = in.prompt();
|
||||
} catch (final IOException e) {
|
||||
throw new CommandRunException(
|
||||
CommandRunExceptionType.INTERACTION,
|
||||
throw new CommandRunException(CommandRunExceptionType.INTERACTION,
|
||||
"manager was closed", e); //$NON-NLS-1$
|
||||
}
|
||||
writer.write(user + EOL);
|
||||
@@ -132,17 +127,16 @@ public class ExecSystemCommand extends Command {
|
||||
|
||||
/** @param is the input stream
|
||||
* @throws CommandRunException if the manager was closed while writing the
|
||||
* stream */
|
||||
protected void readToEnd(final ConsoleOutput out,
|
||||
final InputStream is) throws CommandRunException {
|
||||
* stream */
|
||||
protected void readToEnd(final ConsoleOutput out, final InputStream is)
|
||||
throws CommandRunException {
|
||||
int c;
|
||||
try {
|
||||
while ((c = is.read()) != -1) {
|
||||
try {
|
||||
out.print(Character.valueOf((char) c).toString());
|
||||
} catch (final IOException e) {
|
||||
throw new CommandRunException(
|
||||
CommandRunExceptionType.INTERACTION,
|
||||
throw new CommandRunException(CommandRunExceptionType.INTERACTION,
|
||||
"manager was closed", e); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
@@ -163,12 +157,11 @@ public class ExecSystemCommand extends Command {
|
||||
@Override
|
||||
protected String usageDetail() {
|
||||
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$
|
||||
System.lineSeparator() +
|
||||
"system, you would get the computer name." + //$NON-NLS-1$
|
||||
System.lineSeparator();
|
||||
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$
|
||||
+ System.lineSeparator() + "system, you would get the computer name." + //$NON-NLS-1$
|
||||
System.lineSeparator();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
||||
Reference in New Issue
Block a user