Package com.linkedin.alpini.io
Class ExternalCommand
- java.lang.Object
-
- com.linkedin.alpini.io.ExternalCommand
-
public class ExternalCommand extends java.lang.ObjectThis class encapsulates a javaProcessto handle properly output and error and preventing potential deadlocks. The idea is that the command is executed and you can get the error or output. Simple to use. Should not be used for big outputs because they are buffered internally.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringJDK_HOME_DIRstatic org.apache.logging.log4j.LoggerLOGstatic java.lang.StringMODULE
-
Constructor Summary
Constructors Constructor Description ExternalCommand(java.lang.ProcessBuilder processBuilder)Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ExternalCommandcreate(java.lang.String... commands)Creates an external process from the command.static ExternalCommandcreate(java.util.List<java.lang.String> commands)Creates an external process from the command.voiddestroy()static ExternalCommandexecute(java.io.File workingDirectory, java.lang.String command, java.lang.String... args)Executes the external command in the given working directory and waits for it to be finished.static ExternalCommandexecuteWithTimeout(java.io.File workingDirectory, java.lang.String command, long timeout, java.lang.String... args)Executes the external command in the given working directory and waits (until timeout is elapsed) for it to be finished.static ExternalCommandexecuteWithTimeoutWithEnv(java.io.File workingDirectory, java.lang.String command, long timeout, java.util.Map<java.lang.String,java.lang.String> env, java.lang.String... args)intexitValue()java.util.Map<java.lang.String,java.lang.String>getEnvironment()byte[]getError()byte[]getOutput()booleangetRedirectErrorStream()java.lang.StringgetStringError()Returns the error as a string.java.lang.StringgetStringError(java.lang.String encoding)Returns the error as a string.java.lang.StringgetStringError(java.nio.charset.Charset encoding)Returns the error as a string.java.lang.StringgetStringOutput()Returns the output as a string.java.lang.StringgetStringOutput(java.lang.String encoding)Returns the output as a string.java.lang.StringgetStringOutput(java.nio.charset.Charset encoding)Returns the output as a string.java.io.FilegetWorkingDirectory()booleanisTimedOut()voidsetRedirectErrorStream(boolean redirectErrorStream)voidsetTimedOut(boolean val)voidsetWorkingDirectory(java.io.File directory)voidstart()After creating the command, you have to start it...static ExternalCommandstart(java.lang.String... commands)Creates an external process from the command.intwaitFor()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.intwaitFor(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.intwaitFor(long timeout, java.util.concurrent.TimeUnit unit)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.
-
-
-
Method Detail
-
start
public void start() throws java.io.IOExceptionAfter creating the command, you have to start it...- Throws:
java.io.IOException
-
getEnvironment
public java.util.Map<java.lang.String,java.lang.String> getEnvironment()
- See Also:
ProcessBuilder
-
getWorkingDirectory
public java.io.File getWorkingDirectory()
- See Also:
ProcessBuilder
-
setWorkingDirectory
public void setWorkingDirectory(java.io.File directory)
- See Also:
ProcessBuilder
-
getRedirectErrorStream
public boolean getRedirectErrorStream()
- See Also:
ProcessBuilder
-
setRedirectErrorStream
public void setRedirectErrorStream(boolean redirectErrorStream)
- See Also:
ProcessBuilder
-
setTimedOut
public void setTimedOut(boolean val)
-
isTimedOut
public boolean isTimedOut()
-
getOutput
public byte[] getOutput() throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
getError
public byte[] getError() throws java.lang.InterruptedException- Throws:
java.lang.InterruptedException
-
getStringOutput
public java.lang.String getStringOutput(java.lang.String encoding) throws java.lang.InterruptedException, java.io.UnsupportedEncodingExceptionReturns the output as a string.- Parameters:
encoding-- Returns:
- encoded string
- Throws:
java.lang.InterruptedExceptionjava.io.UnsupportedEncodingException
-
getStringOutput
public java.lang.String getStringOutput(java.nio.charset.Charset encoding) throws java.lang.InterruptedExceptionReturns the output as a string.- Parameters:
encoding-- Returns:
- encoded string
- Throws:
java.lang.InterruptedExceptionjava.io.UnsupportedEncodingException
-
getStringOutput
public java.lang.String getStringOutput() throws java.lang.InterruptedExceptionReturns the output as a string. Uses encoding "UTF-8".- Returns:
- utf8 encoded string
- Throws:
java.lang.InterruptedException
-
getStringError
public java.lang.String getStringError(java.nio.charset.Charset encoding) throws java.lang.InterruptedExceptionReturns the error as a string.- Parameters:
encoding-- Returns:
- error as string
- Throws:
java.lang.InterruptedException
-
getStringError
public java.lang.String getStringError(java.lang.String encoding) throws java.lang.InterruptedException, java.io.UnsupportedEncodingExceptionReturns the error as a string.- Parameters:
encoding-- Returns:
- error as string
- Throws:
java.lang.InterruptedExceptionjava.io.UnsupportedEncodingException
-
getStringError
public java.lang.String getStringError() throws java.lang.InterruptedExceptionReturns the error as a string. Uses encoding "UTF-8".- Returns:
- error as string
- Throws:
java.lang.InterruptedException
-
waitFor
public int waitFor() throws java.lang.InterruptedExceptionProperly 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:
java.lang.InterruptedException
-
waitFor
public int waitFor(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException, java.io.IOExceptionProperly 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 aTimeoutException- Returns:
- the status code of the process.
- Throws:
java.util.concurrent.TimeoutExceptionjava.lang.InterruptedExceptionjava.io.IOException
-
waitFor
public int waitFor(long timeout) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException, java.io.IOExceptionProperly 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 aTimeoutException- Returns:
- the status code of the process.
- Throws:
java.util.concurrent.TimeoutExceptionjava.lang.InterruptedExceptionjava.io.IOException
-
exitValue
public int exitValue()
-
destroy
public void destroy()
-
create
public static ExternalCommand create(java.lang.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
-
create
public static ExternalCommand create(java.util.List<java.lang.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
-
start
@CheckReturnValue public static ExternalCommand start(java.lang.String... commands) throws java.io.IOException
Creates an external process from the command. The command is executed.- Parameters:
commands- the commands to execute- Returns:
- the process
- Throws:
java.io.IOException- if there is an error
-
execute
@CheckReturnValue public static ExternalCommand execute(java.io.File workingDirectory, java.lang.String command, java.lang.String... args) throws java.io.IOException, java.lang.InterruptedException
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 commandcommand- the command to execute (should be relative to the working directoryargs- the arguments to the command- Returns:
- the process
- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
executeWithTimeout
@CheckReturnValue public static ExternalCommand executeWithTimeout(java.io.File workingDirectory, java.lang.String command, long timeout, java.lang.String... args) throws java.io.IOException, java.lang.InterruptedException
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 commandcommand- the command to execute (should be relative to the working directorytimeout- 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 ignoredargs- the arguments to the command- Returns:
- the process
- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
executeWithTimeoutWithEnv
@CheckReturnValue public static ExternalCommand executeWithTimeoutWithEnv(java.io.File workingDirectory, java.lang.String command, long timeout, java.util.Map<java.lang.String,java.lang.String> env, java.lang.String... args) throws java.io.IOException, java.lang.InterruptedException
- Throws:
java.io.IOExceptionjava.lang.InterruptedException
-
-