Package org.apache.bookkeeper.util
Class Shell
java.lang.Object
org.apache.bookkeeper.util.Shell
- Direct Known Subclasses:
Shell.ShellCommandExecutor
A base class for running a Unix command.
Shell can be used to run unix commands like du or
df. It also offers facilities to gate commands by
time-intervals.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThis is an IOException with exit code added.static classA simple shell command executor. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.apache.commons.logging.Logprotected longstatic final booleanSet to true on Windows platforms. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringexecCommand(String... cmd) Static method to execute a shell command.static StringexecCommand(Map<String, String> env, String... cmd) Static method to execute a shell command.static StringexecCommand(Map<String, String> env, String[] cmd, long timeout) Static method to execute a shell command.protected abstract String[]Return an array containing the command name & its parameters.intGet the exit code.Get the current sub-process executing the given command.booleanTo check if the passed script to shell command executor timed out or not.protected abstract voidparseExecResult(BufferedReader lines) Parse the execution result.protected voidrun()Check to see if a command needs to be executed and execute if needed.protected voidsetEnvironment(Map<String, String> env) Set the environment for the command.protected voidsetWorkingDirectory(File dir) Set the working directory.
-
Field Details
-
LOG
public static final org.apache.commons.logging.Log LOG -
timeOutInterval
protected long timeOutInterval -
WINDOWS
public static final boolean WINDOWSSet to true on Windows platforms.
-
-
Constructor Details
-
Shell
public Shell() -
Shell
public Shell(long interval) - Parameters:
interval- the minimum duration to wait before re-executing the command.
-
-
Method Details
-
setEnvironment
Set the environment for the command.- Parameters:
env- Mapping of environment variables
-
setWorkingDirectory
Set the working directory.- Parameters:
dir- The directory where the command would be executed
-
run
Check to see if a command needs to be executed and execute if needed.- Throws:
IOException
-
getExecString
Return an array containing the command name & its parameters. -
parseExecResult
Parse the execution result.- Throws:
IOException
-
getProcess
Get the current sub-process executing the given command.- Returns:
- process executing the command
-
getExitCode
public int getExitCode()Get the exit code.- Returns:
- the exit code of the process
-
isTimedOut
public boolean isTimedOut()To check if the passed script to shell command executor timed out or not.- Returns:
- if the script timed out.
-
execCommand
Static method to execute a shell command. Covers most of the simple cases without requiring the user to implement theShellinterface.- Parameters:
cmd- shell command to execute.- Returns:
- the output of the executed command.
- Throws:
IOException
-
execCommand
public static String execCommand(Map<String, String> env, String[] cmd, long timeout) throws IOExceptionStatic method to execute a shell command. Covers most of the simple cases without requiring the user to implement theShellinterface.- Parameters:
env- the map of environment key=valuecmd- shell command to execute.timeout- time in milliseconds after which script should be marked timeout- Returns:
- the output of the executed command.o
- Throws:
IOException
-
execCommand
Static method to execute a shell command. Covers most of the simple cases without requiring the user to implement theShellinterface.- Parameters:
env- the map of environment key=valuecmd- shell command to execute.- Returns:
- the output of the executed command.
- Throws:
IOException
-