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:
-
CmsShellCommands
CmsRequestContext
CmsObject
-
Field Summary
Modifier and TypeFieldDescriptionprotected CmsObject
The OpenCms context object.protected PrintStream
Stream to write the error messages output to.protected int
The code which the process should exit with in case of errors; -1 means exit is not called.protected PrintStream
Stream to write the regular output messages to.static final String
Prefix for "additional" parameter.static final String
Prefix for "base" parameter.static final String
Prefix for "servletMapping" parameter.static final String
Prefix for errorCode parameter.static final String
Command line parameter to prevent disabling of JLAN.static final String
Prefix for "script" parameter.static final String
Prefix for "servletMapping" parameter.static final ThreadLocal<ArrayList<CmsShell>>
Thread local which stores the currently active shell instance. -
Constructor Summary
ConstructorDescriptionCmsShell
(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 TypeMethodDescriptionvoid
execute
(InputStream inputStream) Executes the commands from the given input stream in this shell.void
Executes the commands from the given reader in this shell.void
Executes the commands from the given string in this shell.void
executeCommand
(String command, List<String> parameters) Executes a shell command with a list of parameters.void
exit()
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.int
Gets 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 CmsShell
Gets the top of thread-local shell stack, or null if it is empty.boolean
hasEcho()
Returns true if echo mode is on.boolean
Checks whether a report error occurred during execution of the last command.protected void
Shows the signature of all methods containing the given search String.protected CmsUserSettings
Initializes the internalCmsWorkplaceSettings
that contain (amongst other information) important information additional information about the current user (an instance ofCmsUserSettings
).void
initShell
(List<I_CmsShellCommands> additionalShellCommands, PrintStream out, PrintStream err) Initializes the CmsShell.boolean
Returns true if exit was called.boolean
Iftrue
this is an interactive session with a user sitting on a console.static boolean
Check if JLAN should be disabled.static void
Main program entry point when started via the command line.static void
popShell()
Removes top of thread-local shell stack.void
Prints the shell prompt.static void
Pushes shell instance on thread-local stack.protected void
setEcho
(boolean echo) Sets the echo status.void
setInteractive
(boolean interactive) Settrue
if this is an interactive session with a user sitting on a console.void
Sets the locale of the current user.protected void
Sets the current shell prompt.static void
If running in the context of a CmsShell, this method notifies the running shell instance that an error has occured in a report.void
start
(FileInputStream inputStream) Deprecated.boolean
validateUser
(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 (ornull
to use the default"/opencms/*"
)defaultWebAppName
- the name of the default web application (ornull
to 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 (ornull
to use the default"/opencms/*"
)defaultWebAppName
- the name of the default web application (ornull
to 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 (ornull
to use the default"/opencms/*"
)defaultWebAppName
- the name of the default web application (ornull
to 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
- iftrue
this 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
Iftrue
this is an interactive session with a user sitting on a console.- Returns:
true
if this is an interactive session with a user sitting on a console
-
printPrompt
Prints the shell prompt. -
setInteractive
Settrue
if 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
- iftrue
this 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:
true
if 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 internalCmsWorkplaceSettings
that contain (amongst other information) important information additional information about the current user (an instance ofCmsUserSettings
).This step is performed within the
CmsShell
constructor directly after switching to run-level 2 and obtaining theCmsObject
for 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:
$u
the current user name
$s
the current site root
$p
the current project name- Parameters:
prompt
- the prompt to set
-
execute(InputStream)
instead