public class CmsShell extends java.lang.Object
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, I_CmsShellCommands, 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.
CmsShellCommands
,
CmsRequestContext
,
CmsObject
Modifier and Type | Field and Description |
---|---|
protected CmsObject |
m_cms
The OpenCms context object.
|
protected java.io.PrintStream |
m_err
Stream to write the error messages output to.
|
protected int |
m_errorCode
The code which the process should exit with in case of errors; -1 means exit is not called.
|
protected java.io.PrintStream |
m_out
Stream to write the regular output messages to.
|
static java.lang.String |
SHELL_PARAM_ADDITIONAL_COMMANDS
Prefix for "additional" parameter.
|
static java.lang.String |
SHELL_PARAM_BASE
Prefix for "base" parameter.
|
static java.lang.String |
SHELL_PARAM_DEFAULT_WEB_APP
Prefix for "servletMapping" parameter.
|
static java.lang.String |
SHELL_PARAM_ERROR_CODE
Prefix for errorCode parameter.
|
static java.lang.String |
SHELL_PARAM_JLAN
Command line parameter to prevent disabling of JLAN.
|
static java.lang.String |
SHELL_PARAM_SCRIPT
Prefix for "script" parameter.
|
static java.lang.String |
SHELL_PARAM_SERVLET_MAPPING
Prefix for "servletMapping" parameter.
|
static java.lang.ThreadLocal<java.util.ArrayList<CmsShell>> |
SHELL_STACK
Thread local which stores the currently active shell instance.
|
Constructor and Description |
---|
CmsShell(CmsObject cms,
java.lang.String prompt,
I_CmsShellCommands additionalShellCommands,
java.io.PrintStream out,
java.io.PrintStream err)
Creates a new CmsShell.
|
CmsShell(java.lang.String webInfPath,
java.lang.String servletMapping,
java.lang.String defaultWebAppName,
java.lang.String prompt,
I_CmsShellCommands additionalShellCommands)
Creates a new CmsShell using System.out and System.err for output of the messages.
|
CmsShell(java.lang.String webInfPath,
java.lang.String servletMapping,
java.lang.String defaultWebAppName,
java.lang.String prompt,
I_CmsShellCommands additionalShellCommands,
java.io.PrintStream out,
java.io.PrintStream err,
boolean interactive)
Creates a new CmsShell.
|
Modifier and Type | Method and Description |
---|---|
void |
execute(java.io.InputStream inputStream)
Executes the commands from the given input stream in this shell.
|
void |
execute(java.io.Reader reader)
Executes the commands from the given reader in this shell.
|
void |
execute(java.lang.String commands)
Executes the commands from the given string in this shell.
|
void |
executeCommand(java.lang.String command,
java.util.List<java.lang.String> parameters)
Executes a shell command with a list of parameters.
|
void |
exit()
Exits this shell and destroys the OpenCms instance.
|
java.io.PrintStream |
getErr()
Returns the stream this shell writes its error messages to.
|
int |
getErrorCode()
Gets the error code.
|
java.util.Locale |
getLocale()
Private internal helper for localization to the current user's locale
within OpenCms.
|
CmsMessages |
getMessages()
Returns the localized messages object for the current user.
|
java.io.PrintStream |
getOut()
Returns the stream this shell writes its regular messages to.
|
java.lang.String |
getPrompt()
Gets the prompt.
|
CmsUserSettings |
getSettings()
Obtain the additional settings related to the current user.
|
static CmsShell |
getTopShell()
Gets the top of thread-local shell stack, or null if it is empty.
|
boolean |
hasEcho()
Returns true if echo mode is on.
|
boolean |
hasReportError()
Checks whether a report error occurred during execution of the last command.
|
protected void |
help(java.lang.String searchString)
Shows the signature of all methods containing the given search String.
|
protected CmsUserSettings |
initSettings()
Initializes the internal
CmsWorkplaceSettings that contain (amongst other
information) important information additional information about the current user
(an instance of CmsUserSettings ). |
void |
initShell(I_CmsShellCommands additionalShellCommands,
java.io.PrintStream out,
java.io.PrintStream err)
Initializes the CmsShell.
|
boolean |
isExitCalled()
Returns true if exit was called.
|
boolean |
isInteractive()
If
true this is an interactive session with a user sitting on a console. |
static boolean |
isJlanDisabled()
Check if JLAN should be disabled.
|
static void |
main(java.lang.String[] args)
Main program entry point when started via the command line.
|
static void |
popShell()
Removes top of thread-local shell stack.
|
void |
printPrompt()
Prints the shell prompt.
|
static void |
pushShell(CmsShell shell)
Pushes shell instance on thread-local stack.
|
protected void |
setEcho(boolean echo)
Sets the echo status.
|
void |
setInteractive(boolean interactive)
Set
true if this is an interactive session with a user sitting on a console. |
void |
setLocale(java.util.Locale locale)
Sets the locale of the current user.
|
protected void |
setPrompt(java.lang.String prompt)
Sets the current shell prompt.
|
static void |
setReportError()
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(java.io.FileInputStream inputStream)
Deprecated.
use
execute(InputStream) instead |
boolean |
validateUser(java.lang.String userName,
java.lang.String password,
CmsRole requiredRole)
Validates the given user and password and checks if the user has the requested role.
|
public static final java.lang.String SHELL_PARAM_ADDITIONAL_COMMANDS
public static final java.lang.String SHELL_PARAM_BASE
public static final java.lang.String SHELL_PARAM_DEFAULT_WEB_APP
public static final java.lang.String SHELL_PARAM_ERROR_CODE
public static final java.lang.String SHELL_PARAM_JLAN
public static final java.lang.String SHELL_PARAM_SCRIPT
public static final java.lang.String SHELL_PARAM_SERVLET_MAPPING
public static final java.lang.ThreadLocal<java.util.ArrayList<CmsShell>> SHELL_STACK
We need multiple ones because shell commands may cause another nested shell to be launched (e.g. for module import scripts).
protected java.io.PrintStream m_err
protected int m_errorCode
protected java.io.PrintStream m_out
public CmsShell(CmsObject cms, java.lang.String prompt, I_CmsShellCommands additionalShellCommands, java.io.PrintStream out, java.io.PrintStream err)
cms
- the user context to run the shell fromprompt
- the prompt format to setadditionalShellCommands
- optional object for additional shell commands, or nullout
- stream to write the regular output messages toerr
- stream to write the error messages output topublic CmsShell(java.lang.String webInfPath, java.lang.String servletMapping, java.lang.String defaultWebAppName, java.lang.String prompt, I_CmsShellCommands additionalShellCommands)
webInfPath
- the path to the 'WEB-INF' folder of the OpenCms installationservletMapping
- the mapping of the servlet (or null
to use the default "/opencms/*"
)defaultWebAppName
- the name of the default web application (or null
to use the default "ROOT"
)prompt
- the prompt format to setadditionalShellCommands
- optional object for additional shell commands, or nullpublic CmsShell(java.lang.String webInfPath, java.lang.String servletMapping, java.lang.String defaultWebAppName, java.lang.String prompt, I_CmsShellCommands additionalShellCommands, java.io.PrintStream out, java.io.PrintStream err, boolean interactive)
webInfPath
- the path to the 'WEB-INF' folder of the OpenCms installationservletMapping
- the mapping of the servlet (or null
to use the default "/opencms/*"
)defaultWebAppName
- the name of the default web application (or null
to use the default "ROOT"
)prompt
- the prompt format to setadditionalShellCommands
- optional object for additional shell commands, or nullout
- stream to write the regular output messages toerr
- stream to write the error messages output tointeractive
- if true
this is an interactive session with a user sitting on a consolepublic static CmsShell getTopShell()
public static boolean isJlanDisabled()
public static void main(java.lang.String[] args)
args
- parameters passed to the application via the command linepublic static void popShell()
public static void pushShell(CmsShell shell)
shell
- the shell to pushpublic static void setReportError()
public void execute(java.io.InputStream inputStream)
'string value'
.
inputStream
- the input stream from which the commands are readpublic void execute(java.io.Reader reader)
'string value'
.
reader
- the reader from which the commands are readpublic void execute(java.lang.String commands)
'string value'
.
commands
- the string from which the commands are readpublic void executeCommand(java.lang.String command, java.util.List<java.lang.String> parameters)
command
- the command to executeparameters
- the list of parameters for the commandpublic void exit()
public java.io.PrintStream getErr()
public int getErrorCode()
public java.util.Locale getLocale()
Locale
.public CmsMessages getMessages()
public java.io.PrintStream getOut()
public java.lang.String getPrompt()
public CmsUserSettings getSettings()
public boolean hasEcho()
public boolean hasReportError()
public void initShell(I_CmsShellCommands additionalShellCommands, java.io.PrintStream out, java.io.PrintStream err)
additionalShellCommands
- optional object for additional shell commands, or nullout
- stream to write the regular output messages toerr
- stream to write the error messages output topublic boolean isExitCalled()
public boolean isInteractive()
true
this is an interactive session with a user sitting on a console.true
if this is an interactive session with a user sitting on a consolepublic void printPrompt()
public void setInteractive(boolean interactive)
true
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.
interactive
- if true
this is an interactive session with a user sitting on a consolepublic void setLocale(java.util.Locale locale) throws CmsException
locale
- the locale to setCmsException
- in case the locale of the current user can not be stored@Deprecated public void start(java.io.FileInputStream inputStream)
execute(InputStream)
insteadinputStream
- an input stream from which commands are readpublic boolean validateUser(java.lang.String userName, java.lang.String password, CmsRole requiredRole)
userName
- the user namepassword
- the passwordrequiredRole
- the required roletrue
if the user is validprotected void help(java.lang.String searchString)
searchString
- the String to search for in the methods, if null all methods are shownprotected CmsUserSettings initSettings()
CmsWorkplaceSettings
that contain (amongst other
information) important information additional information about the current user
(an instance of CmsUserSettings
).
This step is performed within the CmsShell
constructor directly after
switching to run-level 2 and obtaining the CmsObject
for the guest user as
well as when invoking the CmsShell command login
.
protected void setEcho(boolean echo)
echo
- the echo status to setprotected void setPrompt(java.lang.String 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
prompt
- the prompt to set