Class CmsShell
The CmsShell has direct access to all methods in the "command objects".
Currently, the following classes are used as command objects:
,
CmsShellCommands and
CmsRequestContext.CmsObject
It is also possible to add a custom command object when calling the script API,
like in CmsShell(String, String, String, String, List, PrintStream, PrintStream, boolean).
Only public methods in the command objects that use supported data types
as parameters can be called from the shell. Supported data types are:
String, .CmsUUID, boolean, int, long, double, float
If a method name is ambiguous, i.e. the method name with the same number of parameter exist in more then one of the command objects, the method is only executed on the first matching method object.
- Since:
- 6.0.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CmsObjectThe OpenCms context object.protected PrintStreamStream to write the error messages output to.protected intThe code which the process should exit with in case of errors; -1 means exit is not called.protected PrintStreamStream to write the regular output messages to.static final StringPrefix for "additional" parameter.static final StringPrefix for "base" parameter.static final StringPrefix for "servletMapping" parameter.static final StringPrefix for errorCode parameter.static final StringCommand line parameter to prevent disabling of JLAN.static final StringPrefix for "script" parameter.static final StringPrefix for "servletMapping" parameter.static final ThreadLocal<ArrayList<CmsShell>> Thread local which stores the currently active shell instance. -
Constructor Summary
ConstructorsConstructorDescriptionCmsShell(String webInfPath, String servletMapping, String defaultWebAppName, String prompt, List<I_CmsShellCommands> additionalShellCommands) Creates a new CmsShell using System.out and System.err for output of the messages.CmsShell(String webInfPath, String servletMapping, String defaultWebAppName, String prompt, List<I_CmsShellCommands> additionalShellCommands, PrintStream out, PrintStream err, boolean interactive) Creates a new CmsShell.CmsShell(String webInfPath, String servletMapping, String defaultWebAppName, String prompt, I_CmsShellCommands additionalShellCommands) Creates a new CmsShell using System.out and System.err for output of the messages.CmsShell(CmsObject cms, String prompt, List<I_CmsShellCommands> additionalShellCommands, PrintStream out, PrintStream err) Creates a new CmsShell. -
Method Summary
Modifier and TypeMethodDescriptionvoidexecute(InputStream inputStream) Executes the commands from the given input stream in this shell.voidExecutes the commands from the given reader in this shell.voidExecutes the commands from the given string in this shell.voidexecuteCommand(String command, List<String> parameters) Executes a shell command with a list of parameters.voidexit()Exits this shell and destroys the OpenCms instance.Gets the benchmark table for the shell, lazily initializing it if it didn't exist yet.getErr()Returns the stream this shell writes its error messages to.intGets the error code.Private internal helper for localization to the current user's locale within OpenCms.Returns the localized messages object for the current user.getOut()Returns the stream this shell writes its regular messages to.Gets the prompt.Obtain the additional settings related to the current user.static CmsShellGets the top of thread-local shell stack, or null if it is empty.booleanhasEcho()Returns true if echo mode is on.booleanChecks whether a report error occurred during execution of the last command.protected voidShows the signature of all methods containing the given search String.protected CmsUserSettingsInitializes the internalCmsWorkplaceSettingsthat contain (amongst other information) important information additional information about the current user (an instance ofCmsUserSettings).voidinitShell(List<I_CmsShellCommands> additionalShellCommands, PrintStream out, PrintStream err) Initializes the CmsShell.booleanReturns true if exit was called.booleanIftruethis is an interactive session with a user sitting on a console.static booleanCheck if JLAN should be disabled.static voidMain program entry point when started via the command line.static voidpopShell()Removes top of thread-local shell stack.voidPrints the shell prompt.static voidPushes shell instance on thread-local stack.protected voidsetEcho(boolean echo) Sets the echo status.voidsetInteractive(boolean interactive) Settrueif this is an interactive session with a user sitting on a console.voidSets the locale of the current user.protected voidSets the current shell prompt.static voidIf running in the context of a CmsShell, this method notifies the running shell instance that an error has occured in a report.voidstart(FileInputStream inputStream) Deprecated.booleanvalidateUser(String userName, String password, CmsRole requiredRole) Validates the given user and password and checks if the user has the requested role.
-
Field Details
-
SHELL_PARAM_ADDITIONAL_COMMANDS
Prefix for "additional" parameter.- See Also:
-
SHELL_PARAM_BASE
Prefix for "base" parameter.- See Also:
-
SHELL_PARAM_DEFAULT_WEB_APP
Prefix for "servletMapping" parameter.- See Also:
-
SHELL_PARAM_ERROR_CODE
Prefix for errorCode parameter.- See Also:
-
SHELL_PARAM_JLAN
Command line parameter to prevent disabling of JLAN.- See Also:
-
SHELL_PARAM_SCRIPT
Prefix for "script" parameter.- See Also:
-
SHELL_PARAM_SERVLET_MAPPING
Prefix for "servletMapping" parameter.- See Also:
-
SHELL_STACK
Thread local which stores the currently active shell instance.We need multiple ones because shell commands may cause another nested shell to be launched (e.g. for module import scripts).
-
m_cms
The OpenCms context object. -
m_err
Stream to write the error messages output to. -
m_errorCode
The code which the process should exit with in case of errors; -1 means exit is not called. -
m_out
Stream to write the regular output messages to.
-
-
Constructor Details
-
CmsShell
public CmsShell(CmsObject cms, String prompt, List<I_CmsShellCommands> additionalShellCommands, PrintStream out, PrintStream err) Creates a new CmsShell.- Parameters:
cms- the user context to run the shell fromprompt- the prompt format to setadditionalShellCommands- optional objects for additional shell commands, or nullout- stream to write the regular output messages toerr- stream to write the error messages output to
-
CmsShell
public CmsShell(String webInfPath, String servletMapping, String defaultWebAppName, String prompt, I_CmsShellCommands additionalShellCommands) Creates a new CmsShell using System.out and System.err for output of the messages.- Parameters:
webInfPath- the path to the 'WEB-INF' folder of the OpenCms installationservletMapping- the mapping of the servlet (ornullto use the default"/opencms/*")defaultWebAppName- the name of the default web application (ornullto use the default"ROOT")prompt- the prompt format to setadditionalShellCommands- optional object for additional shell commands, or null
-
CmsShell
public CmsShell(String webInfPath, String servletMapping, String defaultWebAppName, String prompt, List<I_CmsShellCommands> additionalShellCommands) Creates a new CmsShell using System.out and System.err for output of the messages.- Parameters:
webInfPath- the path to the 'WEB-INF' folder of the OpenCms installationservletMapping- the mapping of the servlet (ornullto use the default"/opencms/*")defaultWebAppName- the name of the default web application (ornullto use the default"ROOT")prompt- the prompt format to setadditionalShellCommands- optional objects for additional shell commands, or null
-
CmsShell
public CmsShell(String webInfPath, String servletMapping, String defaultWebAppName, String prompt, List<I_CmsShellCommands> additionalShellCommands, PrintStream out, PrintStream err, boolean interactive) Creates a new CmsShell.- Parameters:
webInfPath- the path to the 'WEB-INF' folder of the OpenCms installationservletMapping- the mapping of the servlet (ornullto use the default"/opencms/*")defaultWebAppName- the name of the default web application (ornullto use the default"ROOT")prompt- the prompt format to setadditionalShellCommands- optional list of objects for additional shell commands, or nullout- stream to write the regular output messages toerr- stream to write the error messages output tointeractive- iftruethis is an interactive session with a user sitting on a console
-
-
Method Details
-
getTopShell
Gets the top of thread-local shell stack, or null if it is empty.- Returns:
- the top of the shell stack
-
isJlanDisabled
Check if JLAN should be disabled.- Returns:
- true if JLAN should be disabled
-
main
Main program entry point when started via the command line.- Parameters:
args- parameters passed to the application via the command line
-
popShell
Removes top of thread-local shell stack. -
pushShell
Pushes shell instance on thread-local stack.- Parameters:
shell- the shell to push
-
setReportError
If running in the context of a CmsShell, this method notifies the running shell instance that an error has occured in a report. -
execute
Executes the commands from the given input stream in this shell.- Commands in the must be separated with a line break '\n'.
- Only one command per line is allowed.
- String parameters must be quoted like this:
'string value'.
- Parameters:
inputStream- the input stream from which the commands are read
-
execute
Executes the commands from the given reader in this shell.- Commands in the must be separated with a line break '\n'.
- Only one command per line is allowed.
- String parameters must be quoted like this:
'string value'.
- Parameters:
reader- the reader from which the commands are read
-
execute
Executes the commands from the given string in this shell.- Commands in the must be separated with a line break '\n'.
- Only one command per line is allowed.
- String parameters must be quoted like this:
'string value'.
- Parameters:
commands- the string from which the commands are read
-
executeCommand
Executes a shell command with a list of parameters.- Parameters:
command- the command to executeparameters- the list of parameters for the command
-
exit
Exits this shell and destroys the OpenCms instance. -
getBenchmarkTable
Gets the benchmark table for the shell, lazily initializing it if it didn't exist yet.- Returns:
- the benchmark table for the shell.
-
getErr
Returns the stream this shell writes its error messages to.- Returns:
- the stream this shell writes its error messages to
-
getErrorCode
Gets the error code.- Returns:
- the error code
-
getLocale
Private internal helper for localization to the current user's locale within OpenCms.- Returns:
- the current user's
Locale.
-
getMessages
Returns the localized messages object for the current user.- Returns:
- the localized messages object for the current user
-
getOut
Returns the stream this shell writes its regular messages to.- Returns:
- the stream this shell writes its regular messages to
-
getPrompt
Gets the prompt.- Returns:
- the prompt
-
getSettings
Obtain the additional settings related to the current user.- Returns:
- the additional settings related to the current user.
-
hasEcho
Returns true if echo mode is on.- Returns:
- true if echo mode is on
-
hasReportError
Checks whether a report error occurred during execution of the last command.- Returns:
- true if a report error occurred
-
initShell
public void initShell(List<I_CmsShellCommands> additionalShellCommands, PrintStream out, PrintStream err) Initializes the CmsShell.- Parameters:
additionalShellCommands- optional objects for additional shell commands, or nullout- stream to write the regular output messages toerr- stream to write the error messages output to
-
isExitCalled
Returns true if exit was called.- Returns:
- true if exit was called
-
isInteractive
Iftruethis is an interactive session with a user sitting on a console.- Returns:
trueif this is an interactive session with a user sitting on a console
-
printPrompt
Prints the shell prompt. -
setInteractive
Settrueif this is an interactive session with a user sitting on a console.This controls the output of the prompt and some other info that is valuable on the console, but not required in an automatic session.
- Parameters:
interactive- iftruethis is an interactive session with a user sitting on a console
-
setLocale
Sets the locale of the current user.- Parameters:
locale- the locale to set- Throws:
CmsException- in case the locale of the current user can not be stored
-
start
Deprecated.useexecute(InputStream)insteadReads the given stream and executes the commands in this shell.- Parameters:
inputStream- an input stream from which commands are read
-
validateUser
Validates the given user and password and checks if the user has the requested role.- Parameters:
userName- the user namepassword- the passwordrequiredRole- the required role- Returns:
trueif the user is valid
-
help
Shows the signature of all methods containing the given search String.- Parameters:
searchString- the String to search for in the methods, if null all methods are shown
-
initSettings
Initializes the internalCmsWorkplaceSettingsthat contain (amongst other information) important information additional information about the current user (an instance ofCmsUserSettings).This step is performed within the
CmsShellconstructor directly after switching to run-level 2 and obtaining theCmsObjectfor the guest user as well as when invoking the CmsShell commandlogin.- Returns:
- the user settings for the current user.
-
setEcho
Sets the echo status.- Parameters:
echo- the echo status to set
-
setPrompt
Sets the current shell prompt.To set the prompt, the following variables are available:
$uthe current user name
$sthe current site root
$pthe current project name- Parameters:
prompt- the prompt to set
-
execute(InputStream)instead