org.codehaus.plexus.util.cli

Class CommandLineUtils

public abstract class CommandLineUtils extends Object

Version: $Id: CommandLineUtils.java 8700 2010-04-24 12:03:28Z bentmann $

Author: Trygve Laugstøl

Nested Class Summary
static classCommandLineUtils.StringStreamConsumer
Method Summary
static voidaddShutdownHook()
static intexecuteCommandLine(Commandline cl, StreamConsumer systemOut, StreamConsumer systemErr)
static intexecuteCommandLine(Commandline cl, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds)
static intexecuteCommandLine(Commandline cl, InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr)
static intexecuteCommandLine(Commandline cl, InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds)
static PropertiesgetSystemEnvVars()
Gets the shell environment variables for this process.
static PropertiesgetSystemEnvVars(boolean caseSensitive)
Return the shell environment variables.
static booleanisAlive(long pid)
static booleanisAlive(Process p)
static voidkillProcess(long pid)
Kill a process launched by executeCommandLine methods.
static Stringquote(String argument)

Put quotes around the given String if necessary.

If the argument doesn't include spaces or quotes, return it as is.

static Stringquote(String argument, boolean wrapExistingQuotes)

Put quotes around the given String if necessary.

If the argument doesn't include spaces or quotes, return it as is.

static Stringquote(String argument, boolean escapeSingleQuotes, boolean escapeDoubleQuotes, boolean wrapExistingQuotes)
static voidremoveShutdownHook(boolean execute)
static StringtoString(String[] line)
static String[]translateCommandline(String toProcess)

Method Detail

addShutdownHook

public static void addShutdownHook()

executeCommandLine

public static int executeCommandLine(Commandline cl, StreamConsumer systemOut, StreamConsumer systemErr)

executeCommandLine

public static int executeCommandLine(Commandline cl, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds)

executeCommandLine

public static int executeCommandLine(Commandline cl, InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr)

executeCommandLine

public static int executeCommandLine(Commandline cl, InputStream systemIn, StreamConsumer systemOut, StreamConsumer systemErr, int timeoutInSeconds)

Parameters: cl The command line to execute systemIn The input to read from, must be thread safe systemOut A consumer that receives output, must be thread safe systemErr A consumer that receives system error stream output, must be thread safe timeoutInSeconds Positive integer to specify timeout, zero and negative integers for no timeout.

Returns: A return value, see Process#exitValue()

Throws: CommandLineException or CommandLineTimeOutException if time out occurs

getSystemEnvVars

public static Properties getSystemEnvVars()
Gets the shell environment variables for this process. Note that the returned mapping from variable names to values will always be case-sensitive regardless of the platform, i.e. getSystemEnvVars().get("path") and getSystemEnvVars().get("PATH") will in general return different values. However, on platforms with case-insensitive environment variables like Windows, all variable names will be normalized to upper case.

Returns: The shell environment variables, can be empty but never null.

Throws: IOException If the environment variables could not be queried from the shell.

See Also: System#getenv() System.getenv() API, new in JDK 5.0, to get the same result since 2.0.2 System#getenv() will be used if available in the current running jvm.

getSystemEnvVars

public static Properties getSystemEnvVars(boolean caseSensitive)
Return the shell environment variables. If caseSensitive == true, then envar keys will all be upper-case.

Parameters: caseSensitive Whether environment variable keys should be treated case-sensitively.

Returns: Properties object of (possibly modified) envar keys mapped to their values.

Throws: IOException

See Also: System#getenv() System.getenv() API, new in JDK 5.0, to get the same result since 2.0.2 System#getenv() will be used if available in the current running jvm.

isAlive

public static boolean isAlive(long pid)

isAlive

public static boolean isAlive(Process p)

killProcess

public static void killProcess(long pid)
Kill a process launched by executeCommandLine methods. Doesn't work correctly on windows, only the cmd process will be destroy but not the sub process (Bug ID 4770092)

Parameters: pid The pid of command return by Commandline.getPid()

quote

public static String quote(String argument)

Deprecated: Use (String, char, char[], char[], char, boolean), (String, char, char[], char, boolean), or StringUtils instead.

Put quotes around the given String if necessary.

If the argument doesn't include spaces or quotes, return it as is. If it contains double quotes, use single quotes - else surround the argument by double quotes.

Throws: CommandLineException if the argument contains both, single and double quotes.

quote

public static String quote(String argument, boolean wrapExistingQuotes)

Deprecated: Use (String, char, char[], char[], char, boolean), (String, char, char[], char, boolean), or StringUtils instead.

Put quotes around the given String if necessary.

If the argument doesn't include spaces or quotes, return it as is. If it contains double quotes, use single quotes - else surround the argument by double quotes.

Throws: CommandLineException if the argument contains both, single and double quotes.

quote

public static String quote(String argument, boolean escapeSingleQuotes, boolean escapeDoubleQuotes, boolean wrapExistingQuotes)

Deprecated: Use (String, char, char[], char[], char, boolean), (String, char, char[], char, boolean), or StringUtils instead.

removeShutdownHook

public static void removeShutdownHook(boolean execute)

toString

public static String toString(String[] line)

translateCommandline

public static String[] translateCommandline(String toProcess)
Copyright © 2001-2010 Codehaus. All Rights Reserved.