|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.sun.enterprise.admin.cli.CLICommand
@Contract @Scoped(value=org.jvnet.hk2.component.PerLookup.class) public abstract class CLICommand
Base class for a CLI command. An instance of a subclass of this class is created using the getCommand method with the name of the command and the information about its environment.
A command is executed with a list of arguments using the execute method. The implementation of the execute method in this class saves the arguments in the protected argv field, then calls the following protected methods in order: prepare, parse, validate, and executeCommand. A subclass must implement the prepare method to initialize the metadata that specified the valid options for the command, and the executeCommand method to actually perform the command. The parse and validate method may also be overridden if needed. Or, the subclass may override the execute method and provide the complete implementation for the command, including option parsing.
| Field Summary | |
|---|---|
protected String[] |
argv
The command line arguments for this execution. |
protected CommandModel |
commandModel
The metadata describing the command's options and operands. |
static int |
CONNECTION_ERROR
|
protected Environment |
env
The environment for the command. |
static int |
ERROR
|
static int |
INVALID_COMMAND_ERROR
|
protected static Logger |
logger
|
protected StringBuilder |
metadataErrors
|
protected String |
name
The name of the command. |
protected List<String> |
operands
The operands parsed from the command line. |
protected ParameterMap |
options
The options parsed from the command line. |
protected Map<String,String> |
passwords
The passwords read from the password file. |
protected ProgramOptions |
programOpts
The program options for the command. |
static int |
SUCCESS
|
static int |
WARNING
|
| Constructor Summary | |
|---|---|
protected |
CLICommand()
Constructor used by subclasses when instantiated by HK2. |
protected |
CLICommand(String name,
ProgramOptions programOpts,
Environment env)
Constructor used by subclasses to save the name, program options, and environment information into corresponding protected fields. |
| Method Summary | |
|---|---|
protected boolean |
checkHelp()
Check if the current request is a help request, either because --help was specified as a program option or a command option. |
int |
execute(String... argv)
Execute this command with the given arguemnts. |
protected abstract int |
executeCommand()
Execute the command using the options in options and the operands in operands. |
protected boolean |
getBooleanOption(String name)
Get a boolean option value, that might come from the command line or from the environment. |
String |
getBriefCommandUsage()
|
static CLICommand |
getCommand(org.jvnet.hk2.component.Habitat habitat,
String name)
Get a CLICommand object representing the named command. |
String |
getCommandUsage()
Get the usage text for the command. |
BufferedReader |
getManPage()
Return a BufferedReader for the man page for this command, or null if not found. |
String |
getName()
Return the name of this command. |
protected CommandModel.ParamModel |
getOperandModel()
Get the ParamModel that corresponds to the operand (primary parameter). |
protected String |
getOption(String name)
Get an option value, that might come from the command line or from the environment. |
protected String |
getPassword(CommandModel.ParamModel opt,
String defaultPassword,
boolean create)
Get a password for the given option. |
ProgramOptions |
getProgramOptions()
Returns the program options associated with this command. |
protected Map<String,String> |
getSystemProperties()
Return all the system properties and properties set in asenv.conf. |
protected String |
getSystemProperty(String name)
Return the named system property, or property set in asenv.conf. |
String |
getUsage()
Get the usage text for the subcommand. |
protected void |
initializeLogger()
Initialize the state of the logger based on any program options. |
protected void |
initializePasswords()
Initialize the passwords field based on the password file specified in the program options, and initialize the program option's password if available in the password file. |
protected void |
inject()
Inject this instance with the final values of all the command parameters. |
protected static boolean |
ok(String s)
|
protected void |
parse()
The parse method sets the options and operands fields based on the content of the command line arguments. |
void |
postConstruct()
Initialize the logger after being instantiated by HK2. |
protected void |
prepare()
The prepare method must ensure that the commandModel field is set. |
protected void |
prevalidate()
The prevalidate method supplies missing options from the environment. |
protected void |
printExceptionStackTrace(Throwable e)
Prints the exception message with level as FINER. |
protected void |
processProgramOptions()
If the program options haven't already been set, parse them on the command line and remove them from the command line. |
static String |
quote(String value)
Quote a value, if the value contains any special characters. |
protected String |
readPassword(String prompt)
Display the given prompt and read a password without echoing it. |
String |
toString()
|
protected Collection<CommandModel.ParamModel> |
usageOptions()
Subclasses can override this method to supply additional or different options that should be part of the usage text. |
protected void |
validate()
The validate method can be used by a subclass to validate that the type and quantity of parameters and operands matches the requirements for this command. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final int ERROR
public static final int CONNECTION_ERROR
public static final int INVALID_COMMAND_ERROR
public static final int SUCCESS
public static final int WARNING
protected static final Logger logger
protected String name
protected ProgramOptions programOpts
protected Environment env
protected String[] argv
protected CommandModel commandModel
protected StringBuilder metadataErrors
protected ParameterMap options
protected List<String> operands
protected Map<String,String> passwords
| Constructor Detail |
|---|
protected CLICommand()
protected CLICommand(String name,
ProgramOptions programOpts,
Environment env)
| Method Detail |
|---|
public static CLICommand getCommand(org.jvnet.hk2.component.Habitat habitat,
String name)
throws CommandException
CommandExceptionpublic void postConstruct()
postConstruct in interface org.glassfish.hk2.PostConstruct
public int execute(String... argv)
throws CommandException
CommandException - if execution of the command fails
CommandValidationException - if there's something wrong
with the options or argumentspublic String getName()
public ProgramOptions getProgramOptions()
public BufferedReader getManPage()
public String getUsage()
protected Collection<CommandModel.ParamModel> usageOptions()
public String getCommandUsage()
public String getBriefCommandUsage()
public String toString()
toString in class Objectpublic static String quote(String value)
value - value to be quoted
protected void processProgramOptions()
throws CommandException
CommandExceptionprotected void initializeLogger()
protected void initializePasswords()
throws CommandException
CommandException
protected void prepare()
throws CommandException
CommandException
protected void parse()
throws CommandException
CommandException - if execution of the command fails
CommandValidationException - if there's something wrong
with the options or arguments
protected boolean checkHelp()
throws CommandException
CommandException
protected void prevalidate()
throws CommandException
CommandException - if execution of the command fails
CommandValidationException - if there's something wrong
with the options or arguments
protected void inject()
throws CommandException
CommandException - if execution of the command fails
CommandValidationException - if there's something wrong
with the options or arguments
protected void validate()
throws CommandException
CommandException - if execution of the command fails
CommandValidationException - if there's something wrong
with the options or arguments
protected abstract int executeCommand()
throws CommandException
CommandException - if execution of the command fails
CommandValidationException - if there's something wrong
with the options or arguments
protected String getPassword(CommandModel.ParamModel opt,
String defaultPassword,
boolean create)
throws CommandValidationException
CommandValidationExceptionprotected String readPassword(String prompt)
protected CommandModel.ParamModel getOperandModel()
protected String getOption(String name)
protected boolean getBooleanOption(String name)
protected String getSystemProperty(String name)
protected Map<String,String> getSystemProperties()
protected void printExceptionStackTrace(Throwable e)
e - the exception object to printprotected static boolean ok(String s)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||