Package com.sun.enterprise.admin.cli
Class CLICommand
- java.lang.Object
-
- com.sun.enterprise.admin.cli.CLICommand
-
- All Implemented Interfaces:
org.glassfish.hk2.api.PostConstruct
- Direct Known Subclasses:
ChangeMasterPasswordCommand
,CreateDomainCommand
,CreateServiceCommand
,DatabaseCommand
,DeleteServiceCommand
,ExportCommand
,HelpCommand
,InstallNodeDcomCommand
,InstallNodeSshCommand
,ListCommandsCommand
,LocalOSGiShellCommand
,LocalServerCommand
,LoginCommand
,MonitorCommand
,MultimodeCommand
,PrintCertificateCommand
,RemoteCLICommand
,RemoteCommand
,SetupLocalDcom
,SetupSshKey
,UninstallNodeDcomCommand
,UninstallNodeSshCommand
,UnsetCommand
,VersionCommand
@Contract @PerLookup public abstract class CLICommand extends Object implements org.glassfish.hk2.api.PostConstruct
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.
- Author:
- Bill Shannon
-
-
Field Summary
Fields Modifier and Type Field Description protected String[]
argv
The command line arguments for this execution.protected static String
ASADMIN
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 org.jline.reader.LineReader
lineReader
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
protected org.jline.terminal.Terminal
terminal
static int
WARNING
-
Constructor Summary
Constructors Modifier Constructor Description 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
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
buildLineReader()
protected void
buildTerminal()
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.protected void
closeTerminal()
int
execute(String... argv)
Execute this command with the given arguemnts.int
execute(org.jline.terminal.Terminal terminal, String... argv)
protected abstract int
executeCommand()
Execute the command using the options in options and the operands in operands.BufferedReader
expandManPage(Reader r)
Return a man page for this command that has the tokens substitutedprotected 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(CLIContainer cLIContainer, String name)
static CLICommand
getCommand(org.glassfish.hk2.api.ServiceLocator serviceLocator, String name)
Get a CLICommand object representing the named command.static String
getCommandScope()
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 List<String>
getOptions(String name)
Get option values, that might come from the command line or from the environment.protected char[]
getPassword(String paramname, String localizedPrompt, String localizedPromptConfirm, boolean create)
protected char[]
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()
Initialise the passwords field based on the password file specified in the program options, and initialise 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 org.jline.reader.LineReaderBuilder
newLineReaderBuilder()
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()
Initialise 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 char[]
readPassword(String prompt)
Display the given prompt and read a password without echoing it.static void
setCommandScope(String ctx)
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.
-
-
-
Field Detail
-
ERROR
public static final int ERROR
- See Also:
- Constant Field Values
-
CONNECTION_ERROR
public static final int CONNECTION_ERROR
- See Also:
- Constant Field Values
-
INVALID_COMMAND_ERROR
public static final int INVALID_COMMAND_ERROR
- See Also:
- Constant Field Values
-
SUCCESS
public static final int SUCCESS
- See Also:
- Constant Field Values
-
WARNING
public static final int WARNING
- See Also:
- Constant Field Values
-
terminal
protected org.jline.terminal.Terminal terminal
-
lineReader
protected org.jline.reader.LineReader lineReader
-
ASADMIN
protected static final String ASADMIN
- See Also:
- Constant Field Values
-
logger
protected static final Logger logger
-
name
protected String name
The name of the command. Initialized in the constructor.
-
programOpts
@Inject protected ProgramOptions programOpts
The program options for the command. Initialized in the constructor.
-
env
@Inject protected Environment env
The environment for the command. Initialized in the constructor.
-
argv
protected String[] argv
The command line arguments for this execution. Initialized in the execute method.
-
commandModel
protected CommandModel commandModel
The metadata describing the command's options and operands.
-
metadataErrors
protected StringBuilder metadataErrors
-
options
protected ParameterMap options
The options parsed from the command line. Initialized by the parse method. The keys are the parameter names from the command model, not the "forced to all lower case" names that are presented to the user.
-
operands
protected List<String> operands
The operands parsed from the command line. Initialized by the parse method.
-
-
Constructor Detail
-
CLICommand
protected CLICommand()
Constructor used by subclasses when instantiated by HK2. ProgramOptions and Environment are injected. name is set here.
-
CLICommand
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. Finally, this constructor calls the initializeLogger method.- Parameters:
name
-programOpts
-env
-
-
-
Method Detail
-
getCommand
public static CLICommand getCommand(org.glassfish.hk2.api.ServiceLocator serviceLocator, String name) throws CommandException
Get a CLICommand object representing the named command.- Parameters:
serviceLocator
-name
- The name of the command- Returns:
- Throws:
CommandException
-
getCommand
public static CLICommand getCommand(CLIContainer cLIContainer, String name) throws CommandException
- Throws:
CommandException
-
postConstruct
public void postConstruct()
Initialise the logger after being instantiated by HK2.- Specified by:
postConstruct
in interfaceorg.glassfish.hk2.api.PostConstruct
-
execute
public int execute(org.jline.terminal.Terminal terminal, String... argv) throws CommandException
- Throws:
CommandException
-
execute
public int execute(String... argv) throws CommandException
Execute this command with the given arguemnts. The implementation in this class saves the passed arguments in the argv field and calls the initializePasswords method. Then it calls the prepare, parse, and validate methods, finally returning the result of calling the executeCommand method. Note that argv[0] is the command name.- Parameters:
argv
- Arguments to execute command with- Returns:
- exit code of the command
- Throws:
CommandException
- if execution of the command failsCommandValidationException
- if there's something wrong with the options or arguments
-
getName
public String getName()
Return the name of this command.- Returns:
-
getCommandScope
public static String getCommandScope()
-
setCommandScope
public static void setCommandScope(String ctx)
-
getProgramOptions
public ProgramOptions getProgramOptions()
Returns the program options associated with this command.- Returns:
- the command's program options
-
getManPage
public BufferedReader getManPage()
Return a BufferedReader for the man page for this command, or null if not found.- Returns:
-
expandManPage
public BufferedReader expandManPage(Reader r)
Return a man page for this command that has the tokens substituted- Parameters:
r
-- Returns:
-
getUsage
public String getUsage()
Get the usage text for the subcommand. This method shows the details for the subcommand options but does not provide details about the command options.- Returns:
- usage text
-
usageOptions
protected Collection<CommandModel.ParamModel> usageOptions()
Subclasses can override this method to supply additional or different options that should be part of the usage text. Most commands will never need to do this, but the create-domain command uses it to include the --user option as a required option.- Returns:
-
getCommandUsage
public String getCommandUsage()
Get the usage text for the command. This usage text shows the details of the command options but does not show the details for the subcommand options. The subcommand argument is used to fill in the subcommand name in the usage text.- Returns:
- usage text for the command
-
getBriefCommandUsage
public String getBriefCommandUsage()
-
quote
public static String quote(String value)
Quote a value, if the value contains any special characters.- Parameters:
value
- value to be quoted- Returns:
- the possibly quoted value
-
processProgramOptions
protected void processProgramOptions() throws CommandException
If the program options haven't already been set, parse them on the command line and remove them from the command line. Subclasses should call this method in their prepare method after initializing commandOpts (so usage is available on failure) if they want to allow program options after the command name. Currently RemoteCommand does this, as well as the local commands that also need to talk to the server.- Throws:
CommandException
-
initializeLogger
protected void initializeLogger()
Initialize the state of the logger based on any program options.
-
initializePasswords
protected void initializePasswords() throws CommandException
Initialise the passwords field based on the password file specified in the program options, and initialise the program option's password if available in the password file.- Throws:
CommandException
-
prepare
protected void prepare() throws CommandException
The prepare method must ensure that the commandModel field is set.- Throws:
CommandException
-
parse
protected void parse() throws CommandException
The parse method sets the options and operands fields based on the content of the command line arguments. If the program options say this is a help request, we set options and operands as if "--help" had been specified.- Throws:
CommandException
- if execution of the command failsCommandValidationException
- if there's something wrong with the options or arguments
-
checkHelp
protected boolean checkHelp() throws CommandException
Check if the current request is a help request, either because --help was specified as a program option or a command option. If so, get the man page using the getManPage method, copy the content to System.out, and return true. Otherwise return false. Subclasses may override this method to perform a different check or to use a different method to display the man page. If this method returns true, the validate and executeCommand methods won't be called.- Returns:
- Throws:
CommandException
-
prevalidate
protected void prevalidate() throws CommandException
The prevalidate method supplies missing options from the environment. It also supplies passwords from the password file or prompts for them if interactive.- Throws:
CommandException
- if execution of the command failsCommandValidationException
- if there's something wrong with the options or arguments
-
inject
protected void inject() throws CommandException
Inject this instance with the final values of all the command parameters.- Throws:
CommandException
- if execution of the command failsCommandValidationException
- if there's something wrong with the options or arguments
-
validate
protected void validate() throws CommandException
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.- Throws:
CommandException
- if execution of the command failsCommandValidationException
- if there's something wrong with the options or arguments
-
executeCommand
protected abstract int executeCommand() throws CommandException
Execute the command using the options in options and the operands in operands.- Returns:
- the exit code
- Throws:
CommandException
- if execution of the command failsCommandValidationException
- if there's something wrong with the options or arguments
-
getPassword
protected char[] getPassword(String paramname, String localizedPrompt, String localizedPromptConfirm, boolean create) throws CommandValidationException
- Throws:
CommandValidationException
-
getPassword
protected char[] getPassword(CommandModel.ParamModel opt, String defaultPassword, boolean create) throws CommandValidationException
Get a password for the given option. First, look in the passwords map. If found, return it. If not found, and not required, return null; If not interactive, return null. Otherwise, prompt for the password. If create is true, prompt twice and compare the two values to make sure they're the same. If the password meets other validity criteria (i.e., length) returns the password. If defaultPassword is not null, "Enter" selects this default password, which is returned.- Parameters:
opt
-defaultPassword
-create
-- Returns:
- Throws:
CommandValidationException
-
readPassword
protected char[] readPassword(String prompt)
Display the given prompt and read a password without echoing it. Returns null if no console available.- Parameters:
prompt
-- Returns:
-
getOperandModel
protected CommandModel.ParamModel getOperandModel()
Get the ParamModel that corresponds to the operand (primary parameter). Return null if none.- Returns:
-
getOption
protected String getOption(String name)
Get an option value, that might come from the command line or from the environment. Return the default value for the option if not otherwise specified.- Parameters:
name
-- Returns:
-
getOptions
protected List<String> getOptions(String name)
Get option values, that might come from the command line or from the environment. Return the default value for the option if not otherwise specified. This method works with options for with multiple() is true.- Parameters:
name
-- Returns:
-
getBooleanOption
protected boolean getBooleanOption(String name)
Get a boolean option value, that might come from the command line or from the environment.- Parameters:
name
-- Returns:
-
getSystemProperty
protected String getSystemProperty(String name)
Return the named system property, or property set in asenv.conf.- Parameters:
name
-- Returns:
-
getSystemProperties
protected Map<String,String> getSystemProperties()
Return all the system properties and properties set in asenv.conf. The returned Map may not be modified.- Returns:
-
printExceptionStackTrace
protected void printExceptionStackTrace(Throwable e)
Prints the exception message with level as FINER.- Parameters:
e
- the exception object to print
-
ok
protected static boolean ok(String s)
-
buildTerminal
protected void buildTerminal()
-
buildLineReader
protected void buildLineReader()
-
newLineReaderBuilder
protected org.jline.reader.LineReaderBuilder newLineReaderBuilder()
-
closeTerminal
protected void closeTerminal()
-
-