Package org.apache.bookkeeper.util
Class Shell
- java.lang.Object
-
- org.apache.bookkeeper.util.Shell
-
- Direct Known Subclasses:
Shell.ShellCommandExecutor
public abstract class Shell extends java.lang.Object
A base class for running a Unix command.Shell
can be used to run unix commands likedu
ordf
. It also offers facilities to gate commands by time-intervals.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Shell.ExitCodeException
This is an IOException with exit code added.static class
Shell.ShellCommandExecutor
A simple shell command executor.
-
Field Summary
Fields Modifier and Type Field Description static org.apache.commons.logging.Log
LOG
protected long
timeOutInterval
static boolean
WINDOWS
Set to true on Windows platforms.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static java.lang.String
execCommand(java.lang.String... cmd)
Static method to execute a shell command.static java.lang.String
execCommand(java.util.Map<java.lang.String,java.lang.String> env, java.lang.String... cmd)
Static method to execute a shell command.static java.lang.String
execCommand(java.util.Map<java.lang.String,java.lang.String> env, java.lang.String[] cmd, long timeout)
Static method to execute a shell command.protected abstract java.lang.String[]
getExecString()
Return an array containing the command name & its parameters.int
getExitCode()
Get the exit code.java.lang.Process
getProcess()
Get the current sub-process executing the given command.boolean
isTimedOut()
To check if the passed script to shell command executor timed out or not.protected abstract void
parseExecResult(java.io.BufferedReader lines)
Parse the execution result.protected void
run()
Check to see if a command needs to be executed and execute if needed.protected void
setEnvironment(java.util.Map<java.lang.String,java.lang.String> env)
Set the environment for the command.protected void
setWorkingDirectory(java.io.File dir)
Set the working directory.
-
-
-
Method Detail
-
setEnvironment
protected void setEnvironment(java.util.Map<java.lang.String,java.lang.String> env)
Set the environment for the command.- Parameters:
env
- Mapping of environment variables
-
setWorkingDirectory
protected void setWorkingDirectory(java.io.File dir)
Set the working directory.- Parameters:
dir
- The directory where the command would be executed
-
run
protected void run() throws java.io.IOException
Check to see if a command needs to be executed and execute if needed.- Throws:
java.io.IOException
-
getExecString
protected abstract java.lang.String[] getExecString()
Return an array containing the command name & its parameters.
-
parseExecResult
protected abstract void parseExecResult(java.io.BufferedReader lines) throws java.io.IOException
Parse the execution result.- Throws:
java.io.IOException
-
getProcess
public java.lang.Process 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
public static java.lang.String execCommand(java.lang.String... cmd) throws java.io.IOException
Static method to execute a shell command. Covers most of the simple cases without requiring the user to implement theShell
interface.- Parameters:
cmd
- shell command to execute.- Returns:
- the output of the executed command.
- Throws:
java.io.IOException
-
execCommand
public static java.lang.String execCommand(java.util.Map<java.lang.String,java.lang.String> env, java.lang.String[] cmd, long timeout) throws java.io.IOException
Static method to execute a shell command. Covers most of the simple cases without requiring the user to implement theShell
interface.- 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:
java.io.IOException
-
execCommand
public static java.lang.String execCommand(java.util.Map<java.lang.String,java.lang.String> env, java.lang.String... cmd) throws java.io.IOException
Static method to execute a shell command. Covers most of the simple cases without requiring the user to implement theShell
interface.- Parameters:
env
- the map of environment key=valuecmd
- shell command to execute.- Returns:
- the output of the executed command.
- Throws:
java.io.IOException
-
-