|
||||||||||
| 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 java.lang.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
|
private static org.jvnet.hk2.component.InjectionManager |
injectionMgr
|
static int |
INVALID_COMMAND_ERROR
|
protected static java.util.logging.Logger |
logger
|
protected java.lang.StringBuilder |
metadataErrors
|
protected java.lang.String |
name
The name of the command. |
protected java.util.List<java.lang.String> |
operands
The operands parsed from the command line. |
protected ParameterMap |
options
The options parsed from the command line. |
private static java.lang.String |
PACKAGE_NAME
|
protected java.util.Map<java.lang.String,java.lang.String> |
passwords
The passwords read from the password file. |
protected ProgramOptions |
programOpts
The program options for the command. |
private static LocalStringsImpl |
strings
|
static int |
SUCCESS
|
private static java.util.Map<java.lang.String,java.lang.String> |
systemProps
|
private static java.util.Set<java.lang.String> |
unsupported
|
private static java.lang.String |
UNSUPPORTED_CMD_FILE_NAME
|
static int |
WARNING
|
| Constructor Summary | |
|---|---|
protected |
CLICommand()
Constructor used by subclasses when instantiated by HK2. |
protected |
CLICommand(java.lang.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 programoption or a command option. |
private static void |
checkUnsupportedLegacyCommand(java.lang.String cmd)
If this is an unsupported command, throw an exception. |
private java.lang.String |
echoCommand()
Return a string representing the command line used with this command. |
int |
execute(java.lang.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. |
private static void |
file2Set(java.lang.String file,
java.util.Set<java.lang.String> set)
Read the named resource file and add the first token on each line to the set. |
private java.lang.String |
generateUsageText()
|
protected boolean |
getBooleanOption(java.lang.String name)
Get a boolean option value, that might come from the command line or from the environment. |
static CLICommand |
getCommand(org.jvnet.hk2.component.Habitat habitat,
java.lang.String name)
Get a CLICommand object representing the named command. |
java.io.BufferedReader |
getManPage()
Return a BufferedReader for the man page for this command, or null if not found. |
java.lang.String |
getName()
Return the name of this command. |
protected CommandModel.ParamModel |
getOperandModel()
Get the ParamModel that corresponds to the operand (primary parameter). |
protected java.lang.String |
getOption(java.lang.String name)
Get an option value, that might come from the command line or from the environment. |
protected java.lang.String |
getPassword(CommandModel.ParamModel opt,
java.lang.String defaultPassword,
boolean create)
Get a password for the given option. |
ProgramOptions |
getProgramOptions()
Returns the program options associated with this command. |
protected java.util.Map<java.lang.String,java.lang.String> |
getSystemProperties()
Return all the system properties and properties set in asenv.conf. |
protected java.lang.String |
getSystemProperty(java.lang.String name)
Return the named system property, or property set in asenv.conf. |
java.lang.String |
getUsage()
Get the usage text. |
private void |
initializeCommandPassword()
Initialize all the passwords required by the command. |
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. |
private static java.lang.String |
lc(java.lang.String s)
|
protected static boolean |
ok(java.lang.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(java.lang.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 java.lang.String |
quote(java.lang.String value)
Quote a value, if the value contains any special characters. |
protected java.lang.String |
readPassword(java.lang.String prompt)
Display the given prompt and read a password without echoing it. |
java.lang.String |
toString()
|
protected java.util.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
private static final java.util.Set<java.lang.String> unsupported
private static final java.lang.String UNSUPPORTED_CMD_FILE_NAME
private static final java.lang.String PACKAGE_NAME
private static final LocalStringsImpl strings
private static final java.util.Map<java.lang.String,java.lang.String> systemProps
protected static final java.util.logging.Logger logger
private static final org.jvnet.hk2.component.InjectionManager injectionMgr
protected java.lang.String name
protected ProgramOptions programOpts
protected Environment env
protected java.lang.String[] argv
protected CommandModel commandModel
protected java.lang.StringBuilder metadataErrors
protected ParameterMap options
protected java.util.List<java.lang.String> operands
protected java.util.Map<java.lang.String,java.lang.String> passwords
| Constructor Detail |
|---|
protected CLICommand()
protected CLICommand(java.lang.String name,
ProgramOptions programOpts,
Environment env)
| Method Detail |
|---|
public static CLICommand getCommand(org.jvnet.hk2.component.Habitat habitat,
java.lang.String name)
throws CommandException
CommandExceptionpublic void postConstruct()
postConstruct in interface org.jvnet.hk2.component.PostConstruct
public int execute(java.lang.String... argv)
throws CommandException
CommandException - if execution of the command fails
CommandValidationException - if there's something wrong
with the options or argumentspublic java.lang.String getName()
public ProgramOptions getProgramOptions()
public java.io.BufferedReader getManPage()
public java.lang.String getUsage()
private java.lang.String generateUsageText()
protected java.util.Collection<CommandModel.ParamModel> usageOptions()
public java.lang.String toString()
toString in class java.lang.Objectprivate java.lang.String echoCommand()
public static java.lang.String quote(java.lang.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
private void initializeCommandPassword()
throws CommandException
CommandException
protected java.lang.String getPassword(CommandModel.ParamModel opt,
java.lang.String defaultPassword,
boolean create)
throws CommandValidationException
CommandValidationExceptionprotected java.lang.String readPassword(java.lang.String prompt)
protected CommandModel.ParamModel getOperandModel()
protected java.lang.String getOption(java.lang.String name)
protected boolean getBooleanOption(java.lang.String name)
protected java.lang.String getSystemProperty(java.lang.String name)
protected java.util.Map<java.lang.String,java.lang.String> getSystemProperties()
private static void checkUnsupportedLegacyCommand(java.lang.String cmd)
throws CommandException
CommandExceptionprotected void printExceptionStackTrace(java.lang.Throwable e)
e - the exception object to printprotected static boolean ok(java.lang.String s)
private static java.lang.String lc(java.lang.String s)
private static void file2Set(java.lang.String file,
java.util.Set<java.lang.String> set)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||