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;
|
||||
|
||||
/**
|
||||
|
@ -33,9 +36,9 @@ public class CmdExec
|
|||
{
|
||||
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;
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
|
@ -46,7 +49,7 @@ public class CmdExec
|
|||
*/
|
||||
public CmdExec(final String command)
|
||||
{
|
||||
run(command, StreamGobbler.NONE, StreamGobbler.NONE);
|
||||
run(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -54,15 +57,7 @@ public class CmdExec
|
|||
*/
|
||||
public CmdExec(final String... command)
|
||||
{
|
||||
run(command, StreamGobbler.NONE, StreamGobbler.NONE);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String command, final int STDOUT, final int STDERR)
|
||||
{
|
||||
run(command, STDOUT, STDERR);
|
||||
run(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -76,9 +71,9 @@ public class CmdExec
|
|||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String[] command, final int STDOUT, final int STDERR)
|
||||
public CmdExec(final String command, final StreamGobbler.StreamWay stdout, final StreamGobbler.StreamWay stderr)
|
||||
{
|
||||
run(command, STDOUT, STDERR);
|
||||
run(command, stdout, stderr);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -89,6 +84,14 @@ public class CmdExec
|
|||
run(command, outputGobbler, errorGobbler);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public CmdExec(final String[] command, final StreamGobbler.StreamWay stdout, final StreamGobbler.StreamWay stderr)
|
||||
{
|
||||
run(command, stdout, stderr);
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
|
@ -117,6 +120,7 @@ public class CmdExec
|
|||
|
||||
result = this.exitValue;
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
|
@ -134,19 +138,6 @@ public class CmdExec
|
|||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public int run(final String command, final int STDOUT, final int STDERR)
|
||||
{
|
||||
int result;
|
||||
|
||||
result = run(command, new StreamGobbler("OUTPUT", STDOUT), new StreamGobbler("ERROR", STDERR));
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param command
|
||||
|
@ -169,12 +160,14 @@ public class CmdExec
|
|||
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;
|
||||
|
||||
result = run(command, new StreamGobbler("OUTPUT", STDOUT), new StreamGobbler("ERROR", STDERR));
|
||||
result = run(command, new StreamGobbler("OUTPUT", stdout), new StreamGobbler("ERROR", stderr));
|
||||
|
||||
//
|
||||
return (result);
|
||||
|
@ -193,30 +186,30 @@ public class CmdExec
|
|||
*/
|
||||
public int run(final String[] command, final StreamGobbler outputGobbler, final StreamGobbler errorGobbler)
|
||||
{
|
||||
this.exitValue = 0;
|
||||
int result;
|
||||
|
||||
logger.info("CmdExec(commande[]) = [" + StringListUtils.toString(command) + "]");
|
||||
logger.info("CmdExec(commande[]) = [" + StringListUtils.toStringSeparatedBy(command, " ") + "]");
|
||||
logger.info("CmdExec(commande[]) = [" + StringListUtils.toStringWithBrackets(command) + "]");
|
||||
|
||||
try
|
||||
{
|
||||
Runtime rt = Runtime.getRuntime();
|
||||
|
||||
Process proc = rt.exec(command);
|
||||
Process process = rt.exec(command);
|
||||
|
||||
// Set a collector for error message.
|
||||
errorGobbler.setInputStream(proc.getErrorStream());
|
||||
errorGobbler.setInputStream(process.getErrorStream());
|
||||
|
||||
// Set a collector for output message.
|
||||
outputGobbler.setInputStream(proc.getInputStream());
|
||||
outputGobbler.setInputStream(process.getInputStream());
|
||||
|
||||
// Collect messages.
|
||||
errorGobbler.start();
|
||||
outputGobbler.start();
|
||||
|
||||
// Wait and manage the exit value.
|
||||
this.exitValue = proc.waitFor();
|
||||
logger.info("ExitValue: " + this.exitValue);
|
||||
this.exitValue = process.waitFor();
|
||||
logger.info("ExitValue: {}", this.exitValue);
|
||||
|
||||
// Sometimes, process ends before Gobblers read its outpout, so we
|
||||
// must wait them.
|
||||
|
@ -225,29 +218,53 @@ public class CmdExec
|
|||
Thread.sleep(2);
|
||||
}
|
||||
|
||||
// Store messsages.
|
||||
// Store messages.
|
||||
this.out = outputGobbler.getStream();
|
||||
this.err = errorGobbler.getStream();
|
||||
|
||||
this.exitValue = 0;
|
||||
result = this.exitValue;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
this.err = exception.getMessage();
|
||||
this.exitValue = -77;
|
||||
exception.printStackTrace();
|
||||
result = this.exitValue;
|
||||
logger.error(exception.getMessage(), exception);
|
||||
}
|
||||
|
||||
//
|
||||
return (this.exitValue);
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param command
|
||||
* @param stdout
|
||||
* @param stderr
|
||||
* @return
|
||||
*/
|
||||
static public String multirun(final String... commands) throws Exception
|
||||
public int run(final String[] command, final StreamGobbler.StreamWay stdout, final StreamGobbler.StreamWay stderr)
|
||||
{
|
||||
int result;
|
||||
|
||||
result = run(command, new StreamGobbler("OUTPUT", stdout), new StreamGobbler("ERROR", stderr));
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param commands
|
||||
* @return
|
||||
* @throws CmdExecException
|
||||
*/
|
||||
public static String multirun(final String... commands) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
result = "";
|
||||
ArrayList<String> stdouts = new ArrayList<String>(commands.length);
|
||||
|
||||
boolean ended = false;
|
||||
int commandCounter = 0;
|
||||
|
@ -264,7 +281,7 @@ public class CmdExec
|
|||
}
|
||||
else
|
||||
{
|
||||
result += CmdExec.run(command);
|
||||
stdouts.add(CmdExec.run(command));
|
||||
commandCounter += 1;
|
||||
}
|
||||
}
|
||||
|
@ -275,6 +292,19 @@ public class CmdExec
|
|||
}
|
||||
}
|
||||
|
||||
//
|
||||
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();
|
||||
|
||||
//
|
||||
return (result);
|
||||
}
|
||||
|
@ -283,9 +313,9 @@ public class CmdExec
|
|||
//
|
||||
// ////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
*
|
||||
* @throws CmdExecException
|
||||
*/
|
||||
static public String run(final String command) throws Exception
|
||||
public static String run(final String command) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
|
@ -296,19 +326,21 @@ public class CmdExec
|
|||
}
|
||||
|
||||
/**
|
||||
* @throws CmdExecException
|
||||
* @throws Exception
|
||||
*
|
||||
*/
|
||||
static public String run(final String... command) throws Exception
|
||||
public static String run(final String... command) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
if ((command == null) || (command.length == 0))
|
||||
{
|
||||
throw new Exception("Empty command");
|
||||
throw new IllegalArgumentException("Empty command");
|
||||
}
|
||||
else
|
||||
{
|
||||
CmdExec cmd = new CmdExec(command, StreamGobbler.BUFFER, StreamGobbler.BUFFER);
|
||||
CmdExec cmd = new CmdExec(command, StreamGobbler.StreamWay.BUFFER, StreamGobbler.StreamWay.BUFFER);
|
||||
|
||||
if (cmd.getExitValue() == 0)
|
||||
{
|
||||
|
@ -319,7 +351,7 @@ public class CmdExec
|
|||
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());
|
||||
throw new CmdExecException(cmd.getErrStream());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -328,34 +360,38 @@ public class CmdExec
|
|||
}
|
||||
|
||||
/**
|
||||
* Examples: setfacl("sudo", "setfacl", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
* Examples:
|
||||
*
|
||||
* setfacl("sudo", "setfacl", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
* setfacl("sudo", "setfacl", "-R", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
*
|
||||
* @throws CmdExecException
|
||||
*/
|
||||
static public String run(final String program1, final String program2, final String[] args, final int min, final int max) throws Exception
|
||||
public static String run(final String program1, final String program2, final String[] args, final int min, final int max) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
//
|
||||
boolean nullArg = false;
|
||||
boolean ended = false;
|
||||
int nArg = 0;
|
||||
int argumentIndex = 0;
|
||||
while (!ended)
|
||||
{
|
||||
if (nArg >= args.length)
|
||||
if (argumentIndex >= args.length)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (args[nArg] == null)
|
||||
if (args[argumentIndex] == null)
|
||||
{
|
||||
ended = true;
|
||||
nullArg = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
nArg += 1;
|
||||
argumentIndex += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -363,19 +399,19 @@ public class CmdExec
|
|||
//
|
||||
if (program1 == null)
|
||||
{
|
||||
throw new Exception("Null program parameter 1 detected: [" + program1 + "].");
|
||||
throw new IllegalArgumentException("Null program parameter 1 detected: [" + program1 + "].");
|
||||
}
|
||||
else if (program2 == null)
|
||||
{
|
||||
throw new Exception("Null program parameter 2 detected: [" + program2 + "].");
|
||||
throw new IllegalArgumentException("Null program parameter 2 detected: [" + program2 + "].");
|
||||
}
|
||||
else if (nullArg)
|
||||
{
|
||||
throw new Exception("Null parameter detected in position " + nArg + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
throw new IllegalArgumentException("Null parameter detected in position " + argumentIndex + " 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) + ".");
|
||||
throw new IllegalArgumentException("Bad number of parameters: " + args.length + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -383,9 +419,9 @@ public class CmdExec
|
|||
String[] command = new String[args.length + 2];
|
||||
command[0] = program1;
|
||||
command[1] = program2;
|
||||
for (nArg = 0; nArg < args.length; nArg++)
|
||||
for (argumentIndex = 0; argumentIndex < args.length; argumentIndex++)
|
||||
{
|
||||
command[nArg + 2] = args[nArg];
|
||||
command[argumentIndex + 2] = args[argumentIndex];
|
||||
}
|
||||
|
||||
result = CmdExec.run(command);
|
||||
|
@ -396,10 +432,15 @@ public class CmdExec
|
|||
}
|
||||
|
||||
/**
|
||||
* Examples: setfacl("setfacl", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
* Examples:
|
||||
*
|
||||
* setfacl("setfacl", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
*
|
||||
* setfacl("setfacl", "-R", "-m", "g:cpm:rwX", "/tmp/toto");
|
||||
*
|
||||
* @throws CmdExecException
|
||||
*/
|
||||
static public String run(final String program, final String[] args, final int min, final int max) throws Exception
|
||||
public static String run(final String program, final String[] args, final int min, final int max) throws CmdExecException
|
||||
{
|
||||
String result;
|
||||
|
||||
|
@ -431,15 +472,15 @@ public class CmdExec
|
|||
//
|
||||
if (program == null)
|
||||
{
|
||||
throw new Exception("Null program parameter detected: [" + program + "].");
|
||||
throw new IllegalArgumentException("Null program parameter detected: [" + program + "].");
|
||||
}
|
||||
else if (nullArg)
|
||||
{
|
||||
throw new Exception("Null parameter detected in position " + argumentCounter + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
throw new IllegalArgumentException("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) + ".");
|
||||
throw new IllegalArgumentException("Bad number of parameters: " + args.length + " for " + StringListUtils.toStringWithBrackets(args) + ".");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
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,28 +32,32 @@ import org.slf4j.LoggerFactory;
|
|||
*/
|
||||
public class StreamGobbler extends Thread
|
||||
{
|
||||
public enum StreamWay
|
||||
{
|
||||
NONE,
|
||||
PRINT,
|
||||
BUFFER
|
||||
}
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CmdExec.class);
|
||||
|
||||
public static final int NONE = 0;
|
||||
public static final int PRINT = 1;
|
||||
public static final int BUFFER = 2;
|
||||
|
||||
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;
|
||||
private boolean isOverStatus;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
StreamGobbler()
|
||||
{
|
||||
this.type = "";
|
||||
this.streamWay = NONE;
|
||||
this.is = null;
|
||||
this.name = "";
|
||||
this.streamWay = StreamWay.NONE;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
|
@ -61,13 +65,13 @@ public class StreamGobbler extends Thread
|
|||
/**
|
||||
*
|
||||
* @param is
|
||||
* @param type
|
||||
* @param name
|
||||
*/
|
||||
StreamGobbler(final InputStream is, final String type)
|
||||
StreamGobbler(final InputStream is, final String name)
|
||||
{
|
||||
this.is = is;
|
||||
this.type = type;
|
||||
this.streamWay = NONE;
|
||||
this.name = name;
|
||||
this.streamWay = StreamWay.NONE;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
}
|
||||
|
@ -75,13 +79,13 @@ public class StreamGobbler extends Thread
|
|||
/**
|
||||
*
|
||||
* @param is
|
||||
* @param type
|
||||
* @param name
|
||||
* @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.type = type;
|
||||
this.name = name;
|
||||
this.streamWay = streamWay;
|
||||
this.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
|
@ -92,9 +96,9 @@ public class StreamGobbler extends Thread
|
|||
* @param type
|
||||
* @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.stream = new StringBuffer();
|
||||
this.isOverStatus = false;
|
||||
|
@ -146,35 +150,36 @@ public class StreamGobbler extends Thread
|
|||
InputStreamReader isr = new InputStreamReader(this.is);
|
||||
BufferedReader buffer = new BufferedReader(isr);
|
||||
String line = null;
|
||||
if (this.streamWay == NONE)
|
||||
switch (this.streamWay)
|
||||
{
|
||||
case NONE:
|
||||
while ((line = buffer.readLine()) != null)
|
||||
{
|
||||
;
|
||||
}
|
||||
}
|
||||
else if (this.streamWay == PRINT)
|
||||
{
|
||||
break;
|
||||
|
||||
case PRINT:
|
||||
while ((line = buffer.readLine()) != null)
|
||||
{
|
||||
System.out.println(this.type + ">" + line);
|
||||
System.out.println(this.name + ">" + line);
|
||||
}
|
||||
}
|
||||
else if (this.streamWay == BUFFER)
|
||||
{
|
||||
break;
|
||||
|
||||
case BUFFER:
|
||||
while ((line = buffer.readLine()) != null)
|
||||
{
|
||||
this.stream.append(line + "\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
|
||||
default:
|
||||
logger.warn("unknow way for stream");
|
||||
}
|
||||
}
|
||||
catch (IOException ioe)
|
||||
catch (IOException exception)
|
||||
{
|
||||
ioe.printStackTrace();
|
||||
logger.error(exception.getMessage(), exception);
|
||||
}
|
||||
|
||||
this.isOverStatus = true;
|
||||
|
|
|
@ -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