public class

ExternalCommand

extends Object
java.lang.Object
   ↳ org.apache.helix.ExternalCommand

Class Overview

Wrapper for running commands outside of the JVM

See Also
  • {@link Process}

Summary

Fields
public static final Logger LOG
public static final String MODULE
Public Constructors
ExternalCommand(ProcessBuilder processBuilder)
Initialize with a ProcessBuilder
Public Methods
static ExternalCommand create(List<String> commands)
Creates an external process from the command.
static ExternalCommand create(String... commands)
Creates an external process from the command.
void destroy()
static ExternalCommand execute(File workingDirectory, String command, String... args)
Executes the external command in the given working directory and waits for it to be finished.
static ExternalCommand executeWithTimeout(File workingDirectory, String command, long timeout, String... args)
Executes the external command in the given working directory and waits (until timeout is elapsed) for it to be finished.
int exitValue()
Map<StringString> getEnvironment()
byte[] getError()
Get the contents of the error stream after completion
byte[] getOutput()
Get the contents of the output stream after completion
boolean getRedirectErrorStream()
String getStringError(String encoding)
Returns the error as a string.
String getStringError()
Returns the error as a string.
String getStringOutput(String encoding)
Returns the output as a string.
String getStringOutput()
Returns the output as a string.
File getWorkingDirectory()
void setRedirectErrorStream(boolean redirectErrorStream)
void setWorkingDirectory(File directory)
void start()
After creating the command, you have to start it...
static ExternalCommand start(String... commands)
Creates an external process from the command.
int waitFor(long timeout)
Properly waits until everything is complete: joins on the thread that reads the output, joins on the thread that reads the error and finally wait for the process to be finished.
int waitFor()
Properly waits until everything is complete: joins on the thread that reads the output, joins on the thread that reads the error and finally wait for the process to be finished.
[Expand]
Inherited Methods
From class java.lang.Object

Fields

public static final Logger LOG

public static final String MODULE

Public Constructors

public ExternalCommand (ProcessBuilder processBuilder)

Initialize with a ProcessBuilder

Parameters
processBuilder initialized ProcessBuilder object

Public Methods

public static ExternalCommand create (List<String> commands)

Creates an external process from the command. It is not started and you have to call start on it!

Parameters
commands the command to execute
Returns
  • the process

public static ExternalCommand create (String... commands)

Creates an external process from the command. It is not started and you have to call start on it!

Parameters
commands the command to execute
Returns
  • the process

public void destroy ()

public static ExternalCommand execute (File workingDirectory, String command, String... args)

Executes the external command in the given working directory and waits for it to be finished.

Parameters
workingDirectory the root directory from where to run the command
command the command to execute (should be relative to the working directory
args the arguments to the command
Returns
  • the process

public static ExternalCommand executeWithTimeout (File workingDirectory, String command, long timeout, String... args)

Executes the external command in the given working directory and waits (until timeout is elapsed) for it to be finished.

Parameters
workingDirectory the root directory from where to run the command
command the command to execute (should be relative to the working directory
timeout the maximum amount of time to wait for this external command (in ms). If this value is less than or equal to 0, timeout is ignored
args the arguments to the command
Returns
  • the process

public int exitValue ()

Returns
  • the return code of the process
See Also
  • {@link Process#exitValue()}

public Map<StringString> getEnvironment ()

See Also
  • {@link ProcessBuilder#environment()}

public byte[] getError ()

Get the contents of the error stream after completion

Returns
  • bytes from the error stream
Throws
InterruptedException the process was interrupted before completion

public byte[] getOutput ()

Get the contents of the output stream after completion

Returns
  • bytes from the output stream
Throws
InterruptedException the process was interrupted before completion

public boolean getRedirectErrorStream ()

See Also
  • {@link ProcessBuilder#redirectErrorStream()}

public String getStringError (String encoding)

Returns the error as a string.

Parameters
encoding the encoding scheme, e.g. "UTF-8"
Returns
  • error as string
Throws
InterruptedException the process was interrupted before completion
UnsupportedEncodingException the encoding scheme is invalid

public String getStringError ()

Returns the error as a string. Uses encoding "UTF-8".

Returns
  • error as string
Throws
InterruptedException the process was interrupted before completion

public String getStringOutput (String encoding)

Returns the output as a string.

Parameters
encoding string encoding scheme, e.g. "UTF-8"
Returns
  • encoded string
Throws
InterruptedException the process was interrupted before completion
UnsupportedEncodingException the encoding scheme is invalid

public String getStringOutput ()

Returns the output as a string. Uses encoding "UTF-8".

Returns
  • utf8 encoded string
Throws
InterruptedException the process was interrupted before completion

public File getWorkingDirectory ()

See Also
  • {@link ProcessBuilder#directory()}

public void setRedirectErrorStream (boolean redirectErrorStream)

See Also
  • {@link ProcessBuilder#redirectErrorStream(boolean)}

public void setWorkingDirectory (File directory)

See Also
  • {@link ProcessBuilder#directory(File)}

public void start ()

After creating the command, you have to start it...

Throws
IOException

public static ExternalCommand start (String... commands)

Creates an external process from the command. The command is executed.

Parameters
commands the commands to execute
Returns
  • the process
Throws
IOException if there is an error

public int waitFor (long timeout)

Properly waits until everything is complete: joins on the thread that reads the output, joins on the thread that reads the error and finally wait for the process to be finished. If the process has not completed before the timeout, throws a TimeoutException

Returns
  • the status code of the process.
Throws
TimeoutException the process timed out
InterruptedException the process was interrupted before completion

public int waitFor ()

Properly waits until everything is complete: joins on the thread that reads the output, joins on the thread that reads the error and finally wait for the process to be finished.

Returns
  • the status code of the process.
Throws
InterruptedException the process was interrupted before completion