@InterfaceAudience.Private @InterfaceStability.Evolving public abstract class Command extends Configured
Modifier and Type | Field and Description |
---|---|
protected String[] |
args |
static String |
DESCRIPTION
the command's long description
|
PrintStream |
err
allows stderr to be captured if necessary
|
protected ArrayList<Exception> |
exceptions |
protected int |
exitCode |
protected String |
name |
static String |
NAME
default name of the command
|
protected int |
numErrors |
PrintStream |
out
allows stdout to be captured if necessary
|
protected boolean |
recursive |
static String |
USAGE
the command's usage switches and arguments format
|
Modifier | Constructor and Description |
---|---|
protected |
Command()
Constructor
|
protected |
Command(Configuration conf)
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
displayError(Exception e)
Display an exception prefaced with the command name.
|
void |
displayError(String message)
Display an error string prefaced with the command name.
|
void |
displayWarning(String message)
Display an warning string prefaced with the command name.
|
protected int |
exitCodeForError()
The exit code to be returned if any errors occur during execution.
|
protected List<PathData> |
expandArgument(String arg)
Expand the given argument into a list of
PathData objects. |
protected LinkedList<PathData> |
expandArguments(LinkedList<String> args)
Expands a list of arguments into
PathData objects. |
abstract String |
getCommandName() |
protected int |
getDepth() |
String |
getDescription()
The long usage suitable for help output
|
String |
getName()
The name of the command.
|
String |
getReplacementCommand()
The replacement for a deprecated command
|
String |
getUsage()
The short usage suitable for the synopsis
|
boolean |
isDeprecated()
Is the command deprecated?
|
protected boolean |
isRecursive() |
protected void |
postProcessPath(PathData item)
Hook for commands to implement an operation to be applied on each
path for the command after being processed successfully
|
protected void |
processArgument(PathData item)
Processes a
PathData item, calling
processPathArgument(PathData) or
processNonexistentPath(PathData) on each item. |
protected void |
processArguments(LinkedList<PathData> args)
Processes the command's list of expanded arguments.
|
protected void |
processNonexistentPath(PathData item)
Provides a hook for handling paths that don't exist.
|
protected void |
processOptions(LinkedList<String> args)
Must be implemented by commands to process the command line flags and
check the bounds of the remaining arguments.
|
protected void |
processPath(PathData item)
Hook for commands to implement an operation to be applied on each
path for the command.
|
protected void |
processPathArgument(PathData item)
This is the last chance to modify an argument before going into the
(possibly) recursive
processPaths(PathData, PathData...)
-> processPath(PathData) loop. |
protected void |
processPaths(PathData parent,
PathData... items)
Iterates over the given expanded paths and invokes
processPath(PathData) on each element. |
protected void |
processRawArguments(LinkedList<String> args)
Allows commands that don't use paths to handle the raw arguments.
|
protected void |
recursePath(PathData item)
Gets the directory listing for a path and invokes
processPaths(PathData, PathData...) |
protected abstract void |
run(Path path)
Execute the command on the input path
|
int |
run(String... argv)
Invokes the command handler.
|
int |
runAll()
For each source path, execute the command
|
void |
setName(String name)
Define the name of the command.
|
protected void |
setRecursive(boolean flag) |
getConf, setConf
public static String NAME
public static String USAGE
public static String DESCRIPTION
protected String[] args
protected String name
protected int exitCode
protected int numErrors
protected boolean recursive
public PrintStream out
public PrintStream err
protected Command()
protected Command(Configuration conf)
public abstract String getCommandName()
protected void setRecursive(boolean flag)
protected boolean isRecursive()
protected int getDepth()
protected abstract void run(Path path) throws IOException
path
- the input pathIOException
- if any error occurspublic int runAll()
public int run(String... argv)
run |->Most commands will chose to implement justprocessOptions(LinkedList)
\->processRawArguments(LinkedList)
|->expandArguments(LinkedList)
| \->expandArgument(String)
* \->processArguments(LinkedList)
|->processArgument(PathData)
* | |->processPathArgument(PathData)
| \->processPaths(PathData, PathData...)
| \->processPath(PathData)
* \->processNonexistentPath(PathData)
processOptions(LinkedList)
and processPath(PathData)
argv
- the list of command line argumentsIllegalArgumentException
- if called with invalid argumentsprotected int exitCodeForError()
protected void processOptions(LinkedList<String> args) throws IOException
args
- the command line argumentsIOException
protected void processRawArguments(LinkedList<String> args) throws IOException
expandArguments(LinkedList)
and pass the resulting list to
processArguments(LinkedList)
args
- the list of argument stringsIOException
protected LinkedList<PathData> expandArguments(LinkedList<String> args) throws IOException
PathData
objects. The default
behavior is to call expandArgument(String)
on each element
which by default globs the argument. The loop catches IOExceptions,
increments the error count, and displays the exception.args
- strings to expand into PathData
objectsPathData
objects the argumentsIOException
- if anything goes wrong...protected List<PathData> expandArgument(String arg) throws IOException
PathData
objects.
The default behavior is to expand globs. Commands may override to
perform other expansions on an argument.arg
- string pattern to expandPathData
objectsIOException
- if anything goes wrong...protected void processArguments(LinkedList<PathData> args) throws IOException
processArgument(PathData)
will be invoked with each item
in the list. The loop catches IOExceptions, increments the error
count, and displays the exception.args
- a list of PathData
to processIOException
- if anything goes wrong...protected void processArgument(PathData item) throws IOException
PathData
item, calling
processPathArgument(PathData)
or
processNonexistentPath(PathData)
on each item.item
- PathData
item to processIOException
- if anything goes wrong...protected void processPathArgument(PathData item) throws IOException
processPaths(PathData, PathData...)
-> processPath(PathData)
loop. Ex. ls and du use this to
expand out directories.item
- a PathData
representing a path which existsIOException
- if anything goes wrong...protected void processNonexistentPath(PathData item) throws IOException
item
- the PathData
that doesn't existFileNotFoundException
- if arg is a path and it doesn't existIOException
- if anything else goes wrong...protected void processPaths(PathData parent, PathData... items) throws IOException
processPath(PathData)
on each element. If "recursive" is true,
will do a post-visit DFS on directories.parent
- if called via a recurse, will be the parent dir, else nullitems
- a list of PathData
objects to processIOException
- if anything goes wrong...protected void processPath(PathData item) throws IOException
item
- a PathData
objectRuntimeException
- if invoked but not implementedIOException
- if anything else goes wrong in the user-implementationprotected void postProcessPath(PathData item) throws IOException
item
- a PathData
objectIOException
- if anything goes wrong...protected void recursePath(PathData item) throws IOException
processPaths(PathData, PathData...)
item
- PathData
for directory to recurse intoIOException
- if anything goes wrong...public void displayError(Exception e)
e
- exception to displaypublic void displayError(String message)
message
- error message to displaypublic void displayWarning(String message)
message
- warning message to displaypublic String getName()
public void setName(String name)
name
- as invokedpublic String getUsage()
public String getDescription()
public final boolean isDeprecated()
public String getReplacementCommand()
Copyright © 2013 Apache Software Foundation. All rights reserved.