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.
|
* This file is part of Devinsy-utils.
|
||||||
*
|
*
|
||||||
|
@ -18,9 +18,12 @@
|
||||||
*/
|
*/
|
||||||
package fr.devinsy.util.cmdexec;
|
package fr.devinsy.util.cmdexec;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import fr.devinsy.util.cmdexec.StreamGobbler.StreamWay;
|
||||||
import fr.devinsy.util.strings.StringListUtils;
|
import fr.devinsy.util.strings.StringListUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,430 +34,468 @@ import fr.devinsy.util.strings.StringListUtils;
|
||||||
*/
|
*/
|
||||||
public class CmdExec
|
public class CmdExec
|
||||||
{
|
{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(CmdExec.class);
|
private static final Logger logger = LoggerFactory.getLogger(CmdExec.class);
|
||||||
|
|
||||||
protected int exitValue;
|
private int exitValue;
|
||||||
protected String out;
|
private String out;
|
||||||
protected String err;
|
private String err;
|
||||||
|
|
||||||
// ////////////////////////////////////////////////////////////////////
|
// ////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// ////////////////////////////////////////////////////////////////////
|
// ////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public CmdExec(final String command)
|
public CmdExec(final String command)
|
||||||
{
|
{
|
||||||
run(command, StreamGobbler.NONE, StreamGobbler.NONE);
|
run(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public CmdExec(final String... command)
|
public CmdExec(final String... command)
|
||||||
{
|
{
|
||||||
run(command, StreamGobbler.NONE, StreamGobbler.NONE);
|
run(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public CmdExec(final String command, final int STDOUT, final int STDERR)
|
public CmdExec(final String command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||||
{
|
{
|
||||||
run(command, STDOUT, STDERR);
|
run(command, outputGobbler, errorGobbler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public CmdExec(final String command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
public CmdExec(final String command, final StreamGobbler.StreamWay stdout, final StreamGobbler.StreamWay stderr)
|
||||||
{
|
{
|
||||||
run(command, outputGobbler, errorGobbler);
|
run(command, stdout, stderr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public CmdExec(final String[] command, final int STDOUT, final int STDERR)
|
public CmdExec(final String[] command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||||
{
|
{
|
||||||
run(command, STDOUT, STDERR);
|
run(command, outputGobbler, errorGobbler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public CmdExec(final String[] command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
public CmdExec(final String[] command, final StreamGobbler.StreamWay stdout, final StreamGobbler.StreamWay stderr)
|
||||||
{
|
{
|
||||||
run(command, outputGobbler, errorGobbler);
|
run(command, stdout, stderr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ////////////////////////////////////////////////////////////////////
|
// ////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
// ////////////////////////////////////////////////////////////////////
|
// ////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String getErrStream()
|
public String getErrStream()
|
||||||
{
|
{
|
||||||
String result;
|
String result;
|
||||||
|
|
||||||
result = this.err;
|
result = this.err;
|
||||||
|
|
||||||
//
|
//
|
||||||
return (result);
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public int getExitValue()
|
public int getExitValue()
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
result = this.exitValue;
|
result = this.exitValue;
|
||||||
|
|
||||||
return (result);
|
//
|
||||||
}
|
return (result);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String getOutStream()
|
public String getOutStream()
|
||||||
{
|
{
|
||||||
String result;
|
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)
|
public int run(final String command, final StreamWay stdout, final StreamWay stderr)
|
||||||
{
|
{
|
||||||
int result;
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Note: this code is inspired by an article of Michael C. Daconta published
|
||||||
* @param command
|
* in JavaWorld Dec 29, 2000 (http://www.javaworld.com/article/2071275
|
||||||
* : not a shell command, it must be a executable program.
|
* /core-java/when-runtime-exec---won -t.html?page=2).
|
||||||
* @param outputGobbler
|
*
|
||||||
* @param errorGobbler
|
* @param command
|
||||||
* @return
|
* not a shell command, it must be a executable program.
|
||||||
*/
|
* @param outputGobbler
|
||||||
public int run(final String command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
* @param errorGobbler
|
||||||
{
|
* @return
|
||||||
int result;
|
*/
|
||||||
|
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);
|
||||||
|
|
||||||
//
|
// Set a collector for error message.
|
||||||
return (result);
|
errorGobbler.setInputStream(process.getErrorStream());
|
||||||
}
|
|
||||||
|
|
||||||
//
|
// Set a collector for output message.
|
||||||
public int run(final String[] command, final int STDOUT, final int STDERR)
|
outputGobbler.setInputStream(process.getInputStream());
|
||||||
{
|
|
||||||
int result;
|
|
||||||
|
|
||||||
result = run(command, new StreamGobbler("OUTPUT", STDOUT), new StreamGobbler("ERROR", STDERR));
|
// Collect messages.
|
||||||
|
errorGobbler.start();
|
||||||
|
outputGobbler.start();
|
||||||
|
|
||||||
//
|
// Wait and manage the exit value.
|
||||||
return (result);
|
this.exitValue = process.waitFor();
|
||||||
}
|
logger.info("ExitValue: {}", this.exitValue);
|
||||||
|
|
||||||
/**
|
// Sometimes, process ends before Gobblers read its outpout, so we
|
||||||
* Note: this code is inspired by an article of Michael C. Daconta published
|
// must wait them.
|
||||||
* in JavaWorld Dec 29, 2000 (http://www.javaworld.com/article/2071275
|
while ((!outputGobbler.isOver()) || (!errorGobbler.isOver()))
|
||||||
* /core-java/when-runtime-exec---won -t.html?page=2).
|
{
|
||||||
*
|
Thread.sleep(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;
|
|
||||||
|
|
||||||
logger.info("CmdExec(commande[]) = [" + StringListUtils.toString(command) + "]");
|
// Store messages.
|
||||||
logger.info("CmdExec(commande[]) = [" + StringListUtils.toStringWithBrackets(command) + "]");
|
this.out = outputGobbler.getStream();
|
||||||
|
this.err = errorGobbler.getStream();
|
||||||
|
|
||||||
try
|
this.exitValue = 0;
|
||||||
{
|
result = this.exitValue;
|
||||||
Runtime rt = Runtime.getRuntime();
|
}
|
||||||
|
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.
|
result = run(command, new StreamGobbler("OUTPUT", stdout), new StreamGobbler("ERROR", stderr));
|
||||||
outputGobbler.setInputStream(proc.getInputStream());
|
|
||||||
|
|
||||||
// Collect messages.
|
//
|
||||||
errorGobbler.start();
|
return (result);
|
||||||
outputGobbler.start();
|
}
|
||||||
|
|
||||||
// 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
|
ArrayList<String> stdouts = new ArrayList<String>(commands.length);
|
||||||
// must wait them.
|
|
||||||
while ((!outputGobbler.isOver()) || (!errorGobbler.isOver()))
|
|
||||||
{
|
|
||||||
Thread.sleep(2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store messsages.
|
boolean ended = false;
|
||||||
this.out = outputGobbler.getStream();
|
int commandCounter = 0;
|
||||||
this.err = errorGobbler.getStream();
|
while (!ended)
|
||||||
}
|
{
|
||||||
catch (Exception exception)
|
if (commandCounter < commands.length)
|
||||||
{
|
{
|
||||||
this.err = exception.getMessage();
|
String command = commands[commandCounter];
|
||||||
this.exitValue = -77;
|
|
||||||
exception.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
if ((command == null) || (command.length() == 0))
|
||||||
return (this.exitValue);
|
{
|
||||||
}
|
result = null;
|
||||||
|
commandCounter += 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stdouts.add(CmdExec.run(command));
|
||||||
|
commandCounter += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ended = true;
|
||||||
|
result = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
//
|
||||||
*
|
int resultLength = 0;
|
||||||
*/
|
for (String stdout : stdouts)
|
||||||
static public String multirun(final String... commands) throws Exception
|
{
|
||||||
{
|
resultLength += stdout.length();
|
||||||
String result;
|
}
|
||||||
|
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)
|
* @throws CmdExecException
|
||||||
{
|
*/
|
||||||
String command = commands[commandCounter];
|
public static String run(final String command) throws CmdExecException
|
||||||
|
{
|
||||||
|
String result;
|
||||||
|
|
||||||
if ((command == null) || (command.length() == 0))
|
result = CmdExec.run(command.split("[ \t\n\r\f]"));
|
||||||
{
|
|
||||||
result = null;
|
|
||||||
commandCounter += 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result += CmdExec.run(command);
|
|
||||||
commandCounter += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ended = true;
|
|
||||||
result = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
//
|
||||||
return (result);
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ////////////////////////////////////////////////////////////////////
|
/**
|
||||||
//
|
* @throws CmdExecException
|
||||||
// ////////////////////////////////////////////////////////////////////
|
* @throws Exception
|
||||||
/**
|
*
|
||||||
*
|
*/
|
||||||
*/
|
public static String run(final String... command) throws CmdExecException
|
||||||
static public String run(final String command) throws Exception
|
{
|
||||||
{
|
String result;
|
||||||
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);
|
||||||
|
|
||||||
//
|
if (cmd.getExitValue() == 0)
|
||||||
return (result);
|
{
|
||||||
}
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
//
|
||||||
*
|
return (result);
|
||||||
*/
|
}
|
||||||
static public String run(final String... command) throws Exception
|
|
||||||
{
|
|
||||||
String result;
|
|
||||||
|
|
||||||
if ((command == null) || (command.length == 0))
|
/**
|
||||||
{
|
* Examples:
|
||||||
throw new Exception("Empty command");
|
*
|
||||||
}
|
* setfacl("sudo", "setfacl", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||||
else
|
* setfacl("sudo", "setfacl", "-R", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||||
{
|
*
|
||||||
CmdExec cmd = new CmdExec(command, StreamGobbler.BUFFER, StreamGobbler.BUFFER);
|
* @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)
|
//
|
||||||
{
|
boolean nullArg = false;
|
||||||
result = cmd.getOutStream();
|
boolean ended = false;
|
||||||
}
|
int argumentIndex = 0;
|
||||||
else
|
while (!ended)
|
||||||
{
|
{
|
||||||
logger.error("Command=\"" + StringListUtils.toStringWithBrackets(command));
|
if (argumentIndex >= args.length)
|
||||||
logger.error("Command=\"[" + StringListUtils.toString(command) + "]\n out => [" + cmd.getOutStream() + "]\n " + "err => (" + cmd.getErrStream().length() + ")[" + cmd.getErrStream()
|
{
|
||||||
+ "]");
|
ended = true;
|
||||||
throw new Exception(cmd.getErrStream());
|
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];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
result = CmdExec.run(command);
|
||||||
* 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;
|
|
||||||
|
|
||||||
//
|
//
|
||||||
boolean nullArg = false;
|
return (result);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
/**
|
||||||
if (program1 == null)
|
* Examples:
|
||||||
{
|
*
|
||||||
throw new Exception("Null program parameter 1 detected: [" + program1 + "].");
|
* setfacl("setfacl", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||||
}
|
*
|
||||||
else if (program2 == null)
|
* setfacl("setfacl", "-R", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||||
{
|
*
|
||||||
throw new Exception("Null program parameter 2 detected: [" + program2 + "].");
|
* @throws CmdExecException
|
||||||
}
|
*/
|
||||||
else if (nullArg)
|
public static String run(final String program, final String[] args, final int min, final int max) throws CmdExecException
|
||||||
{
|
{
|
||||||
throw new Exception("Null parameter detected in position " + nArg + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
String result;
|
||||||
}
|
|
||||||
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];
|
|
||||||
}
|
|
||||||
|
|
||||||
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];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
result = CmdExec.run(command);
|
||||||
* 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;
|
|
||||||
|
|
||||||
//
|
//
|
||||||
boolean nullArg = false;
|
return (result);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
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.
|
* This file is part of Devinsy-utils.
|
||||||
*
|
*
|
||||||
|
@ -32,160 +32,165 @@ import org.slf4j.LoggerFactory;
|
||||||
*/
|
*/
|
||||||
public class StreamGobbler extends Thread
|
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;
|
private static final Logger logger = LoggerFactory.getLogger(CmdExec.class);
|
||||||
public static final int PRINT = 1;
|
|
||||||
public static final int BUFFER = 2;
|
|
||||||
|
|
||||||
protected InputStream is;
|
private InputStream is;
|
||||||
protected String type;
|
private String name;
|
||||||
protected int streamWay;
|
private StreamWay streamWay;
|
||||||
protected StringBuffer stream;
|
private StringBuffer stream;
|
||||||
|
|
||||||
// Important if the caller wants have complete stream in case of very short
|
// Important if the caller wants have complete stream in case of very short
|
||||||
// command.
|
// command.
|
||||||
protected boolean isOverStatus;
|
private boolean isOverStatus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
StreamGobbler()
|
StreamGobbler()
|
||||||
{
|
{
|
||||||
this.type = "";
|
this.is = null;
|
||||||
this.streamWay = NONE;
|
this.name = "";
|
||||||
this.stream = new StringBuffer();
|
this.streamWay = StreamWay.NONE;
|
||||||
this.isOverStatus = false;
|
this.stream = new StringBuffer();
|
||||||
}
|
this.isOverStatus = false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param is
|
* @param is
|
||||||
* @param type
|
* @param name
|
||||||
*/
|
*/
|
||||||
StreamGobbler(final InputStream is, final String type)
|
StreamGobbler(final InputStream is, final String name)
|
||||||
{
|
{
|
||||||
this.is = is;
|
this.is = is;
|
||||||
this.type = type;
|
this.name = name;
|
||||||
this.streamWay = NONE;
|
this.streamWay = StreamWay.NONE;
|
||||||
this.stream = new StringBuffer();
|
this.stream = new StringBuffer();
|
||||||
this.isOverStatus = false;
|
this.isOverStatus = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param is
|
* @param is
|
||||||
* @param type
|
* @param name
|
||||||
* @param streamWay
|
* @param streamWay
|
||||||
*/
|
*/
|
||||||
StreamGobbler(final InputStream is, final String type, final int streamWay)
|
StreamGobbler(final InputStream is, final String name, final StreamWay streamWay)
|
||||||
{
|
{
|
||||||
this.is = is;
|
this.is = is;
|
||||||
this.type = type;
|
this.name = name;
|
||||||
this.streamWay = streamWay;
|
this.streamWay = streamWay;
|
||||||
this.stream = new StringBuffer();
|
this.stream = new StringBuffer();
|
||||||
this.isOverStatus = false;
|
this.isOverStatus = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param type
|
* @param type
|
||||||
* @param streamWay
|
* @param streamWay
|
||||||
*/
|
*/
|
||||||
StreamGobbler(final String type, final int streamWay)
|
StreamGobbler(final String type, final StreamWay streamWay)
|
||||||
{
|
{
|
||||||
this.type = type;
|
this.name = type;
|
||||||
this.streamWay = streamWay;
|
this.streamWay = streamWay;
|
||||||
this.stream = new StringBuffer();
|
this.stream = new StringBuffer();
|
||||||
this.isOverStatus = false;
|
this.isOverStatus = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String getStream()
|
public String getStream()
|
||||||
{
|
{
|
||||||
String result;
|
String result;
|
||||||
|
|
||||||
if (this.stream != null)
|
if (this.stream != null)
|
||||||
{
|
{
|
||||||
result = this.stream.toString();
|
result = this.stream.toString();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result = null;
|
result = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
return (result);
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean isOver()
|
public boolean isOver()
|
||||||
{
|
{
|
||||||
boolean result;
|
boolean result;
|
||||||
|
|
||||||
result = this.isOverStatus;
|
result = this.isOverStatus;
|
||||||
|
|
||||||
//
|
//
|
||||||
return (result);
|
return (result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
InputStreamReader isr = new InputStreamReader(this.is);
|
InputStreamReader isr = new InputStreamReader(this.is);
|
||||||
BufferedReader buffer = new BufferedReader(isr);
|
BufferedReader buffer = new BufferedReader(isr);
|
||||||
String line = null;
|
String line = null;
|
||||||
if (this.streamWay == NONE)
|
switch (this.streamWay)
|
||||||
{
|
{
|
||||||
while ((line = buffer.readLine()) != null)
|
case NONE:
|
||||||
{
|
while ((line = buffer.readLine()) != null)
|
||||||
;
|
{
|
||||||
}
|
;
|
||||||
}
|
}
|
||||||
else if (this.streamWay == PRINT)
|
break;
|
||||||
{
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.isOverStatus = true;
|
case PRINT:
|
||||||
}
|
while ((line = buffer.readLine()) != null)
|
||||||
|
{
|
||||||
|
System.out.println(this.name + ">" + line);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
/**
|
case BUFFER:
|
||||||
*
|
while ((line = buffer.readLine()) != null)
|
||||||
* @param is
|
{
|
||||||
*/
|
this.stream.append(line + "\n");
|
||||||
public void setInputStream(final InputStream is)
|
}
|
||||||
{
|
break;
|
||||||
this.is = is;
|
|
||||||
}
|
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