Made a code review on CmdExec (exception management, readiness…).
This commit is contained in:
parent
cc33285f5f
commit
ebf0585a83
5 changed files with 707 additions and 600 deletions
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright (C) 2005-2010, 2013, 2015 Christian Pierre MOMON
|
||||
* Copyright (C) 2005-2010, 2013, 2015-2016 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
|
@ -18,9 +18,12 @@
|
|||
*/
|
||||
package fr.devinsy.util.cmdexec;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import fr.devinsy.util.cmdexec.StreamGobbler.StreamWay;
|
||||
import fr.devinsy.util.strings.StringListUtils;
|
||||
|
||||
/**
|
||||
|
@ -31,430 +34,468 @@ import fr.devinsy.util.strings.StringListUtils;
|
|||
*/
|
||||
public class CmdExec
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(CmdExec.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(CmdExec.class);
|
||||
|
||||
protected int exitValue;
|
||||
protected String out;
|
||||
protected String err;
|
||||
private int exitValue;
|
||||
private String out;
|
||||
private String err;
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String command)
|
||||
{
|
||||
run(command, StreamGobbler.NONE, StreamGobbler.NONE);
|
||||
}
|
||||
public CmdExec(final String command)
|
||||
{
|
||||
run(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String... command)
|
||||
{
|
||||
run(command, StreamGobbler.NONE, StreamGobbler.NONE);
|
||||
}
|
||||
public CmdExec(final String... command)
|
||||
{
|
||||
run(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String command, final int STDOUT, final int STDERR)
|
||||
{
|
||||
run(command, STDOUT, STDERR);
|
||||
}
|
||||
public CmdExec(final String command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
run(command, outputGobbler, errorGobbler);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
run(command, outputGobbler, errorGobbler);
|
||||
}
|
||||
public CmdExec(final String command, final StreamGobbler.StreamWay stdout, final StreamGobbler.StreamWay stderr)
|
||||
{
|
||||
run(command, stdout, stderr);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String[] command, final int STDOUT, final int STDERR)
|
||||
{
|
||||
run(command, STDOUT, STDERR);
|
||||
}
|
||||
public CmdExec(final String[] command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
run(command, outputGobbler, errorGobbler);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String[] command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
run(command, outputGobbler, errorGobbler);
|
||||
}
|
||||
public CmdExec(final String[] command, final StreamGobbler.StreamWay stdout, final StreamGobbler.StreamWay stderr)
|
||||
{
|
||||
run(command, stdout, stderr);
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getErrStream()
|
||||
{
|
||||
String result;
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getErrStream()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.err;
|
||||
result = this.err;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getExitValue()
|
||||
{
|
||||
int result;
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public int getExitValue()
|
||||
{
|
||||
int result;
|
||||
|
||||
result = this.exitValue;
|
||||
result = this.exitValue;
|
||||
|
||||
return (result);
|
||||
}
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getOutStream()
|
||||
{
|
||||
String result;
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getOutStream()
|
||||
{
|
||||
String result;
|
||||
|
||||
result = this.out;
|
||||
result = this.out;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
* @param command
|
||||
* : not a shell command, it must be a executable program.
|
||||
* @param outputGobbler
|
||||
* @param errorGobbler
|
||||
* @return
|
||||
*/
|
||||
public int run(final String command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
int result;
|
||||
|
||||
logger.info("CmdExec(commande) = [" + command + "]");
|
||||
|
||||
String[] commands = command.split("[ \t\n\r\f]");
|
||||
|
||||
result = run(commands, outputGobbler, errorGobbler);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public int run(final String command, final int STDOUT, final int STDERR)
|
||||
{
|
||||
int result;
|
||||
public int run(final String command, final StreamWay stdout, final StreamWay stderr)
|
||||
{
|
||||
int result;
|
||||
|
||||
result = run(command, new StreamGobbler("OUTPUT", STDOUT), new StreamGobbler("ERROR", STDERR));
|
||||
result = run(command, new StreamGobbler("OUTPUT", stdout), new StreamGobbler("ERROR", stderr));
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param command
|
||||
* : not a shell command, it must be a executable program.
|
||||
* @param outputGobbler
|
||||
* @param errorGobbler
|
||||
* @return
|
||||
*/
|
||||
public int run(final String command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
int result;
|
||||
/**
|
||||
* Note: this code is inspired by an article of Michael C. Daconta published
|
||||
* in JavaWorld Dec 29, 2000 (http://www.javaworld.com/article/2071275
|
||||
* /core-java/when-runtime-exec---won -t.html?page=2).
|
||||
*
|
||||
* @param command
|
||||
* not a shell command, it must be a executable program.
|
||||
* @param outputGobbler
|
||||
* @param errorGobbler
|
||||
* @return
|
||||
*/
|
||||
public int run(final String[] command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
int result;
|
||||
|
||||
logger.info("CmdExec(commande) = [" + command + "]");
|
||||
logger.info("CmdExec(commande[]) = [" + StringListUtils.toStringSeparatedBy(command, " ") + "]");
|
||||
logger.info("CmdExec(commande[]) = [" + StringListUtils.toStringWithBrackets(command) + "]");
|
||||
|
||||
String[] commands = command.split("[ \t\n\r\f]");
|
||||
try
|
||||
{
|
||||
Runtime rt = Runtime.getRuntime();
|
||||
|
||||
result = run(commands, outputGobbler, errorGobbler);
|
||||
Process process = rt.exec(command);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
// Set a collector for error message.
|
||||
errorGobbler.setInputStream(process.getErrorStream());
|
||||
|
||||
//
|
||||
public int run(final String[] command, final int STDOUT, final int STDERR)
|
||||
{
|
||||
int result;
|
||||
// Set a collector for output message.
|
||||
outputGobbler.setInputStream(process.getInputStream());
|
||||
|
||||
result = run(command, new StreamGobbler("OUTPUT", STDOUT), new StreamGobbler("ERROR", STDERR));
|
||||
// Collect messages.
|
||||
errorGobbler.start();
|
||||
outputGobbler.start();
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
// Wait and manage the exit value.
|
||||
this.exitValue = process.waitFor();
|
||||
logger.info("ExitValue: {}", this.exitValue);
|
||||
|
||||
/**
|
||||
* Note: this code is inspired by an article of Michael C. Daconta published
|
||||
* in JavaWorld Dec 29, 2000 (http://www.javaworld.com/article/2071275
|
||||
* /core-java/when-runtime-exec---won -t.html?page=2).
|
||||
*
|
||||
* @param command
|
||||
* not a shell command, it must be a executable program.
|
||||
* @param outputGobbler
|
||||
* @param errorGobbler
|
||||
* @return
|
||||
*/
|
||||
public int run(final String[] command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
this.exitValue = 0;
|
||||
// Sometimes, process ends before Gobblers read its outpout, so we
|
||||
// must wait them.
|
||||
while ((!outputGobbler.isOver()) || (!errorGobbler.isOver()))
|
||||
{
|
||||
Thread.sleep(2);
|
||||
}
|
||||
|
||||
logger.info("CmdExec(commande[]) = [" + StringListUtils.toString(command) + "]");
|
||||
logger.info("CmdExec(commande[]) = [" + StringListUtils.toStringWithBrackets(command) + "]");
|
||||
// Store messages.
|
||||
this.out = outputGobbler.getStream();
|
||||
this.err = errorGobbler.getStream();
|
||||
|
||||
try
|
||||
{
|
||||
Runtime rt = Runtime.getRuntime();
|
||||
this.exitValue = 0;
|
||||
result = this.exitValue;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
this.err = exception.getMessage();
|
||||
this.exitValue = -77;
|
||||
result = this.exitValue;
|
||||
logger.error(exception.getMessage(), exception);
|
||||
}
|
||||
|
||||
Process proc = rt.exec(command);
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
// Set a collector for error message.
|
||||
errorGobbler.setInputStream(proc.getErrorStream());
|
||||
/**
|
||||
*
|
||||
* @param command
|
||||
* @param stdout
|
||||
* @param stderr
|
||||
* @return
|
||||
*/
|
||||
public int run(final String[] command, final StreamGobbler.StreamWay stdout, final StreamGobbler.StreamWay stderr)
|
||||
{
|
||||
int result;
|
||||
|
||||
// Set a collector for output message.
|
||||
outputGobbler.setInputStream(proc.getInputStream());
|
||||
result = run(command, new StreamGobbler("OUTPUT", stdout), new StreamGobbler("ERROR", stderr));
|
||||
|
||||
// Collect messages.
|
||||
errorGobbler.start();
|
||||
outputGobbler.start();
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
// Wait and manage the exit value.
|
||||
this.exitValue = proc.waitFor();
|
||||
logger.info("ExitValue: " + this.exitValue);
|
||||
/**
|
||||
*
|
||||
* @param commands
|
||||
* @return
|
||||
* @throws CmdExecException
|
||||
*/
|
||||
public static String multirun(final String... commands) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
// Sometimes, process ends before Gobblers read its outpout, so we
|
||||
// must wait them.
|
||||
while ((!outputGobbler.isOver()) || (!errorGobbler.isOver()))
|
||||
{
|
||||
Thread.sleep(2);
|
||||
}
|
||||
ArrayList<String> stdouts = new ArrayList<String>(commands.length);
|
||||
|
||||
// Store messsages.
|
||||
this.out = outputGobbler.getStream();
|
||||
this.err = errorGobbler.getStream();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
this.err = exception.getMessage();
|
||||
this.exitValue = -77;
|
||||
exception.printStackTrace();
|
||||
}
|
||||
boolean ended = false;
|
||||
int commandCounter = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (commandCounter < commands.length)
|
||||
{
|
||||
String command = commands[commandCounter];
|
||||
|
||||
//
|
||||
return (this.exitValue);
|
||||
}
|
||||
if ((command == null) || (command.length() == 0))
|
||||
{
|
||||
result = null;
|
||||
commandCounter += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
stdouts.add(CmdExec.run(command));
|
||||
commandCounter += 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ended = true;
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
static public String multirun(final String... commands) throws Exception
|
||||
{
|
||||
String result;
|
||||
//
|
||||
int resultLength = 0;
|
||||
for (String stdout : stdouts)
|
||||
{
|
||||
resultLength += stdout.length();
|
||||
}
|
||||
StringBuffer buffer = new StringBuffer(resultLength);
|
||||
for (String stdout : stdouts)
|
||||
{
|
||||
buffer.append(stdout);
|
||||
}
|
||||
result = buffer.toString();
|
||||
|
||||
result = "";
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
boolean ended = false;
|
||||
int commandCounter = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (commandCounter < commands.length)
|
||||
{
|
||||
String command = commands[commandCounter];
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* @throws CmdExecException
|
||||
*/
|
||||
public static String run(final String command) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
if ((command == null) || (command.length() == 0))
|
||||
{
|
||||
result = null;
|
||||
commandCounter += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
result += CmdExec.run(command);
|
||||
commandCounter += 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ended = true;
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
result = CmdExec.run(command.split("[ \t\n\r\f]"));
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
*
|
||||
*/
|
||||
static public String run(final String command) throws Exception
|
||||
{
|
||||
String result;
|
||||
/**
|
||||
* @throws CmdExecException
|
||||
* @throws Exception
|
||||
*
|
||||
*/
|
||||
public static String run(final String... command) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = CmdExec.run(command.split("[ \t\n\r\f]"));
|
||||
if ((command == null) || (command.length == 0))
|
||||
{
|
||||
throw new IllegalArgumentException("Empty command");
|
||||
}
|
||||
else
|
||||
{
|
||||
CmdExec cmd = new CmdExec(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
if (cmd.getExitValue() == 0)
|
||||
{
|
||||
result = cmd.getOutStream();
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.error("Command=\"" + StringListUtils.toStringWithBrackets(command));
|
||||
logger.error("Command=\"[" + StringListUtils.toString(command) + "]\n out => [" + cmd.getOutStream() + "]\n " + "err => (" + cmd.getErrStream().length() + ")[" + cmd.getErrStream()
|
||||
+ "]");
|
||||
throw new CmdExecException(cmd.getErrStream());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
static public String run(final String... command) throws Exception
|
||||
{
|
||||
String result;
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
if ((command == null) || (command.length == 0))
|
||||
{
|
||||
throw new Exception("Empty command");
|
||||
}
|
||||
else
|
||||
{
|
||||
CmdExec cmd = new CmdExec(command, StreamGobbler.BUFFER, StreamGobbler.BUFFER);
|
||||
/**
|
||||
* Examples:
|
||||
*
|
||||
* setfacl("sudo", "setfacl", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
* setfacl("sudo", "setfacl", "-R", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
*
|
||||
* @throws CmdExecException
|
||||
*/
|
||||
public static String run(final String program1, final String program2, final String[] args, final int min, final int max) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
if (cmd.getExitValue() == 0)
|
||||
{
|
||||
result = cmd.getOutStream();
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.error("Command=\"" + StringListUtils.toStringWithBrackets(command));
|
||||
logger.error("Command=\"[" + StringListUtils.toString(command) + "]\n out => [" + cmd.getOutStream() + "]\n " + "err => (" + cmd.getErrStream().length() + ")[" + cmd.getErrStream()
|
||||
+ "]");
|
||||
throw new Exception(cmd.getErrStream());
|
||||
}
|
||||
}
|
||||
//
|
||||
boolean nullArg = false;
|
||||
boolean ended = false;
|
||||
int argumentIndex = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (argumentIndex >= args.length)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (args[argumentIndex] == null)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
argumentIndex += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
//
|
||||
if (program1 == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Null program parameter 1 detected: [" + program1 + "].");
|
||||
}
|
||||
else if (program2 == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Null program parameter 2 detected: [" + program2 + "].");
|
||||
}
|
||||
else if (nullArg)
|
||||
{
|
||||
throw new IllegalArgumentException("Null parameter detected in position " + argumentIndex + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else if ((args.length < min) || (args.length > max))
|
||||
{
|
||||
throw new IllegalArgumentException("Bad number of parameters: " + args.length + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
String[] command = new String[args.length + 2];
|
||||
command[0] = program1;
|
||||
command[1] = program2;
|
||||
for (argumentIndex = 0; argumentIndex < args.length; argumentIndex++)
|
||||
{
|
||||
command[argumentIndex + 2] = args[argumentIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* Examples: setfacl("sudo", "setfacl", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
* setfacl("sudo", "setfacl", "-R", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
*/
|
||||
static public String run(final String program1, final String program2, final String[] args, final int min, final int max) throws Exception
|
||||
{
|
||||
String result;
|
||||
result = CmdExec.run(command);
|
||||
}
|
||||
|
||||
//
|
||||
boolean nullArg = false;
|
||||
boolean ended = false;
|
||||
int nArg = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (nArg >= args.length)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (args[nArg] == null)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
nArg += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
//
|
||||
if (program1 == null)
|
||||
{
|
||||
throw new Exception("Null program parameter 1 detected: [" + program1 + "].");
|
||||
}
|
||||
else if (program2 == null)
|
||||
{
|
||||
throw new Exception("Null program parameter 2 detected: [" + program2 + "].");
|
||||
}
|
||||
else if (nullArg)
|
||||
{
|
||||
throw new Exception("Null parameter detected in position " + nArg + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else if ((args.length < min) || (args.length > max))
|
||||
{
|
||||
throw new Exception("Bad number of parameters: " + args.length + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
String[] command = new String[args.length + 2];
|
||||
command[0] = program1;
|
||||
command[1] = program2;
|
||||
for (nArg = 0; nArg < args.length; nArg++)
|
||||
{
|
||||
command[nArg + 2] = args[nArg];
|
||||
}
|
||||
/**
|
||||
* Examples:
|
||||
*
|
||||
* setfacl("setfacl", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
*
|
||||
* setfacl("setfacl", "-R", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
*
|
||||
* @throws CmdExecException
|
||||
*/
|
||||
public static String run(final String program, final String[] args, final int min, final int max) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = CmdExec.run(command);
|
||||
}
|
||||
//
|
||||
boolean nullArg = false;
|
||||
boolean ended = false;
|
||||
int argumentCounter = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (argumentCounter >= args.length)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (args[argumentCounter] == null)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
argumentCounter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
//
|
||||
if (program == null)
|
||||
{
|
||||
throw new IllegalArgumentException("Null program parameter detected: [" + program + "].");
|
||||
}
|
||||
else if (nullArg)
|
||||
{
|
||||
throw new IllegalArgumentException("Null parameter detected in position " + argumentCounter + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else if ((args.length < min) || (args.length > max))
|
||||
{
|
||||
throw new IllegalArgumentException("Bad number of parameters: " + args.length + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
String[] command = new String[args.length + 1];
|
||||
command[0] = program;
|
||||
for (argumentCounter = 0; argumentCounter < args.length; argumentCounter++)
|
||||
{
|
||||
command[argumentCounter + 1] = args[argumentCounter];
|
||||
}
|
||||
|
||||
/**
|
||||
* Examples: setfacl("setfacl", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
* setfacl("setfacl", "-R", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
*/
|
||||
static public String run(final String program, final String[] args, final int min, final int max) throws Exception
|
||||
{
|
||||
String result;
|
||||
result = CmdExec.run(command);
|
||||
}
|
||||
|
||||
//
|
||||
boolean nullArg = false;
|
||||
boolean ended = false;
|
||||
int argumentCounter = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (argumentCounter >= args.length)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (args[argumentCounter] == null)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
argumentCounter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
if (program == null)
|
||||
{
|
||||
throw new Exception("Null program parameter detected: [" + program + "].");
|
||||
}
|
||||
else if (nullArg)
|
||||
{
|
||||
throw new Exception("Null parameter detected in position " + argumentCounter + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else if ((args.length < min) || (args.length > max))
|
||||
{
|
||||
throw new Exception("Bad number of parameters: " + args.length + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
String[] command = new String[args.length + 1];
|
||||
command[0] = program;
|
||||
for (argumentCounter = 0; argumentCounter < args.length; argumentCounter++)
|
||||
{
|
||||
command[argumentCounter + 1] = args[argumentCounter];
|
||||
}
|
||||
|
||||
result = CmdExec.run(command);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
}
|
||||
|
|
62
src/fr/devinsy/util/cmdexec/CmdExecException.java
Normal file
62
src/fr/devinsy/util/cmdexec/CmdExecException.java
Normal file
|
@ -0,0 +1,62 @@
|
|||
/**
|
||||
* Copyright (C) 2016 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.util.cmdexec;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
|
||||
*
|
||||
*/
|
||||
public class CmdExecException extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = 3264886426311529668L;
|
||||
|
||||
public CmdExecException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param message
|
||||
*/
|
||||
public CmdExecException(final String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param message
|
||||
* @param cause
|
||||
*/
|
||||
public CmdExecException(final String message, final Throwable cause)
|
||||
{
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param cause
|
||||
*/
|
||||
public CmdExecException(final Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Copyright (C) 2005-2008, 2010, 2013 Christian Pierre MOMON
|
||||
* Copyright (C) 2005-2008, 2010, 2013, 2016 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
|
@ -32,160 +32,165 @@ import org.slf4j.LoggerFactory;
|
|||
*/
|
||||
public class StreamGobbler extends Thread
|
||||
{
|
||||
private static final Logger logger = LoggerFactory.getLogger(CmdExec.class);
|
||||
public enum StreamWay
|
||||
{
|
||||
NONE,
|
||||
PRINT,
|
||||
BUFFER
|
||||
}
|
||||
|
||||
public static final int NONE = 0;
|
||||
public static final int PRINT = 1;
|
||||
public static final int BUFFER = 2;
|
||||
private static final Logger logger = LoggerFactory.getLogger(CmdExec.class);
|
||||
|
||||
protected InputStream is;
|
||||
protected String type;
|
||||
protected int streamWay;
|
||||
protected StringBuffer stream;
|
||||
private InputStream is;
|
||||
private String name;
|
||||
private StreamWay streamWay;
|
||||
private StringBuffer stream;
|
||||
|
||||
// Important if the caller wants have complete stream in case of very short
|
||||
// command.
|
||||
protected boolean isOverStatus;
|
||||
// Important if the caller wants have complete stream in case of very short
|
||||
// command.
|
||||
private boolean isOverStatus;
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
*/
|
||||
StreamGobbler()
|
||||
{
|
||||
this.type = "";
|
||||
this.streamWay = NONE;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
StreamGobbler()
|
||||
{
|
||||
this.is = null;
|
||||
this.name = "";
|
||||
this.streamWay = StreamWay.NONE;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param is
|
||||
* @param type
|
||||
*/
|
||||
StreamGobbler(final InputStream is, final String type)
|
||||
{
|
||||
this.is = is;
|
||||
this.type = type;
|
||||
this.streamWay = NONE;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param is
|
||||
* @param name
|
||||
*/
|
||||
StreamGobbler(final InputStream is, final String name)
|
||||
{
|
||||
this.is = is;
|
||||
this.name = name;
|
||||
this.streamWay = StreamWay.NONE;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param is
|
||||
* @param type
|
||||
* @param streamWay
|
||||
*/
|
||||
StreamGobbler(final InputStream is, final String type, final int streamWay)
|
||||
{
|
||||
this.is = is;
|
||||
this.type = type;
|
||||
this.streamWay = streamWay;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param is
|
||||
* @param name
|
||||
* @param streamWay
|
||||
*/
|
||||
StreamGobbler(final InputStream is, final String name, final StreamWay streamWay)
|
||||
{
|
||||
this.is = is;
|
||||
this.name = name;
|
||||
this.streamWay = streamWay;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param type
|
||||
* @param streamWay
|
||||
*/
|
||||
StreamGobbler(final String type, final int streamWay)
|
||||
{
|
||||
this.type = type;
|
||||
this.streamWay = streamWay;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param type
|
||||
* @param streamWay
|
||||
*/
|
||||
StreamGobbler(final String type, final StreamWay streamWay)
|
||||
{
|
||||
this.name = type;
|
||||
this.streamWay = streamWay;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getStream()
|
||||
{
|
||||
String result;
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getStream()
|
||||
{
|
||||
String result;
|
||||
|
||||
if (this.stream != null)
|
||||
{
|
||||
result = this.stream.toString();
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
if (this.stream != null)
|
||||
{
|
||||
result = this.stream.toString();
|
||||
}
|
||||
else
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isOver()
|
||||
{
|
||||
boolean result;
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isOver()
|
||||
{
|
||||
boolean result;
|
||||
|
||||
result = this.isOverStatus;
|
||||
result = this.isOverStatus;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
InputStreamReader isr = new InputStreamReader(this.is);
|
||||
BufferedReader buffer = new BufferedReader(isr);
|
||||
String line = null;
|
||||
if (this.streamWay == NONE)
|
||||
{
|
||||
while ((line = buffer.readLine()) != null)
|
||||
{
|
||||
;
|
||||
}
|
||||
}
|
||||
else if (this.streamWay == PRINT)
|
||||
{
|
||||
while ((line = buffer.readLine()) != null)
|
||||
{
|
||||
System.out.println(this.type + ">" + line);
|
||||
}
|
||||
}
|
||||
else if (this.streamWay == BUFFER)
|
||||
{
|
||||
while ((line = buffer.readLine()) != null)
|
||||
{
|
||||
this.stream.append(line + "\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.warn("unknow way for stream");
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
{
|
||||
ioe.printStackTrace();
|
||||
}
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
InputStreamReader isr = new InputStreamReader(this.is);
|
||||
BufferedReader buffer = new BufferedReader(isr);
|
||||
String line = null;
|
||||
switch (this.streamWay)
|
||||
{
|
||||
case NONE:
|
||||
while ((line = buffer.readLine()) != null)
|
||||
{
|
||||
;
|
||||
}
|
||||
break;
|
||||
|
||||
this.isOverStatus = true;
|
||||
}
|
||||
case PRINT:
|
||||
while ((line = buffer.readLine()) != null)
|
||||
{
|
||||
System.out.println(this.name + ">" + line);
|
||||
}
|
||||
break;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param is
|
||||
*/
|
||||
public void setInputStream(final InputStream is)
|
||||
{
|
||||
this.is = is;
|
||||
}
|
||||
case BUFFER:
|
||||
while ((line = buffer.readLine()) != null)
|
||||
{
|
||||
this.stream.append(line + "\n");
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
logger.warn("unknow way for stream");
|
||||
}
|
||||
}
|
||||
catch (IOException exception)
|
||||
{
|
||||
logger.error(exception.getMessage(), exception);
|
||||
}
|
||||
|
||||
this.isOverStatus = true;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param is
|
||||
*/
|
||||
public void setInputStream(final InputStream is)
|
||||
{
|
||||
this.is = is;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,109 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2013 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
import org.apache.log4j.ConsoleAppender;
|
||||
import org.apache.log4j.PatternLayout;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import fr.devinsy.util.cmdexec.CmdExec;
|
||||
import fr.devinsy.util.cmdexec.StreamGobbler;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class CmdExecSandbox
|
||||
{
|
||||
static private final Logger logger;
|
||||
|
||||
static
|
||||
{
|
||||
// Initialize logger.
|
||||
org.apache.log4j.BasicConfigurator.configure();
|
||||
org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO);
|
||||
|
||||
logger = LoggerFactory.getLogger(CmdExecSandbox.class);
|
||||
logger.info("Enter");
|
||||
|
||||
//
|
||||
logger.info("Set the log file format...");
|
||||
org.apache.log4j.Logger defaultLogger = org.apache.log4j.Logger.getRootLogger();
|
||||
defaultLogger.removeAllAppenders();
|
||||
defaultLogger.addAppender(new ConsoleAppender(new PatternLayout("%d{ISO8601} - dutils [%-5p] %34.34c.%-25M - %m%n")));
|
||||
|
||||
logger.info("... done.");
|
||||
|
||||
logger.debug("Exit");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String check(final String title, final StringBuffer source, final String model)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (source.indexOf(model) == -1)
|
||||
{
|
||||
result = String.format("%-40s -> KO <-", title) + "\nGet:\n" + source + "\nWaiting:\n" + model;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
result = String.format("%-40s [ OK ] ", title);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void main(final String[] args)
|
||||
{
|
||||
System.out.println("Automatic test action for CmdExec!");
|
||||
|
||||
test1();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void test1()
|
||||
{
|
||||
try
|
||||
{
|
||||
System.out.println("Launch ...");
|
||||
|
||||
// String command = "/bin/sort -r /etc/passwd";
|
||||
String[] command = { "/bin/sort", "-r", "/etc/passwd" };
|
||||
|
||||
CmdExec cmd = new CmdExec(command, StreamGobbler.BUFFER, StreamGobbler.BUFFER);
|
||||
System.out.println("exitVal=[" + cmd.getExitValue() + "]");
|
||||
System.out.println("out=[" + cmd.getOutStream() + "]");
|
||||
System.out.println("err=[" + cmd.getErrStream() + "]");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
logger.info("ERRRO=" + exception);
|
||||
}
|
||||
}
|
||||
}
|
108
test/fr/devinsy/util/cmdexec/CmdExecSandbox.java
Normal file
108
test/fr/devinsy/util/cmdexec/CmdExecSandbox.java
Normal file
|
@ -0,0 +1,108 @@
|
|||
package fr.devinsy.util.cmdexec;
|
||||
|
||||
/**
|
||||
* Copyright (C) 2013 Christian Pierre MOMON
|
||||
*
|
||||
* This file is part of Devinsy-utils.
|
||||
*
|
||||
* Devinsy-utils is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Devinsy-utils is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Devinsy-utils. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
import org.apache.log4j.ConsoleAppender;
|
||||
import org.apache.log4j.PatternLayout;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class CmdExecSandbox
|
||||
{
|
||||
static private final Logger logger;
|
||||
|
||||
static
|
||||
{
|
||||
// Initialize logger.
|
||||
org.apache.log4j.BasicConfigurator.configure();
|
||||
org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.INFO);
|
||||
|
||||
logger = LoggerFactory.getLogger(CmdExecSandbox.class);
|
||||
logger.info("Enter");
|
||||
|
||||
//
|
||||
logger.info("Set the log file format…");
|
||||
org.apache.log4j.Logger defaultLogger = org.apache.log4j.Logger.getRootLogger();
|
||||
defaultLogger.removeAllAppenders();
|
||||
defaultLogger.addAppender(new ConsoleAppender(new PatternLayout("%d{ISO8601} - dutils [%-5p] %34.34c.%-25M - %m%n")));
|
||||
|
||||
logger.info("… done.");
|
||||
|
||||
logger.debug("Exit");
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static String check(final String title, final StringBuffer source, final String model)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (source.indexOf(model) == -1)
|
||||
{
|
||||
result = String.format("%-40s -> KO <-", title) + "\nGet:\n" + source + "\nWaiting:\n" + model;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
result = String.format("%-40s [ OK ] ", title);
|
||||
}
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void main(final String[] args)
|
||||
{
|
||||
System.out.println("Automatic test action for CmdExec!");
|
||||
|
||||
test1();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public static void test1()
|
||||
{
|
||||
try
|
||||
{
|
||||
System.out.println("Launch…s");
|
||||
|
||||
// String command = "/bin/sort -r /etc/passwd";
|
||||
String[] command = { "/usr/bin/sort", "-r", "/etc/passwd" };
|
||||
|
||||
CmdExec cmd = new CmdExec(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
|
||||
System.out.println("exitVal=[" + cmd.getExitValue() + "]");
|
||||
System.out.println("out=[" + cmd.getOutStream() + "]");
|
||||
System.out.println("err=[" + cmd.getErrStream() + "]");
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
exception.printStackTrace();
|
||||
logger.info("ERRRO=" + exception);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue