cmsshell.java
来自「找了很久才找到到源代码」· Java 代码 · 共 824 行 · 第 1/3 页
JAVA
824 行
buf.append(")\n");
}
}
}
return buf.toString();
}
/**
* Returns the object to execute the methods on.<p>
*
* @return the object to execute the methods on
*/
protected Object getObject() {
return m_object;
}
/**
* Builds a method lookup String.<p>
*
* @param methodName the name of the method
* @param paramCount the parameter count of the method
* @return a method lookup String
*/
private String buildMethodLookup(String methodName, int paramCount) {
StringBuffer buf = new StringBuffer(32);
buf.append(methodName.toLowerCase());
buf.append(" [");
buf.append(paramCount);
buf.append("]");
return buf.toString();
}
/**
* Initilizes the map of accessible methods.<p>
*/
private void initShellMethods() {
Map result = new TreeMap();
Method[] methods = m_object.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
// only public methods directly declared in the base class can be used in the shell
if ((methods[i].getDeclaringClass() == m_object.getClass())
&& (methods[i].getModifiers() == Modifier.PUBLIC)) {
// check if the method signature only uses primitive data types
boolean onlyPrimitive = true;
Class[] clazz = methods[i].getParameterTypes();
for (int j = 0; j < clazz.length; j++) {
if (!CmsDataTypeUtil.isParseable(clazz[j])) {
// complex data type methods can not be called from the shell
onlyPrimitive = false;
break;
}
}
if (onlyPrimitive) {
// add this method to the set of methods that can be called from the shell
String lookup = buildMethodLookup(methods[i].getName(), methods[i].getParameterTypes().length);
List l;
if (result.containsKey(lookup)) {
l = (List)result.get(lookup);
} else {
l = new ArrayList(1);
}
l.add(methods[i]);
result.put(lookup, l);
}
}
}
m_methods = result;
}
}
/** Prefix for "base" parameter. */
public static final String SHELL_PARAM_BASE = "-base=";
/** Prefix for "servletMapping" parameter. */
public static final String SHELL_PARAM_DEFAULT_WEB_APP = "-defaultWebApp=";
/** Prefix for "script" parameter. */
public static final String SHELL_PARAM_SCRIPT = "-script=";
/** Prefix for "servletMapping" parameter. */
public static final String SHELL_PARAM_SERVLET_MAPPING = "-servletMapping=";
/** The OpenCms context object. */
protected CmsObject m_cms;
/** Additional shell commands object. */
private I_CmsShellCommands m_additionaShellCommands;
/** All shell callable objects. */
private List m_commandObjects;
/** If set to true, all commands are echoed. */
private boolean m_echo;
/** Indicates if the 'exit' command has been called. */
private boolean m_exitCalled;
/** The messages object. */
private CmsMessages m_messages;
/** The OpenCms system object. */
private OpenCmsCore m_opencms;
/** The shell prompt format. */
private String m_prompt;
/** The current users settings. */
private CmsUserSettings m_settings;
/** Internal shell command object. */
private I_CmsShellCommands m_shellCommands;
/**
* Creates a new CmsShell.<p>
*
* @param webInfPath the path to the 'WEB-INF' folder of the OpenCms installation
* @param servletMapping the mapping of the servlet (or <code>null</code> to use the default <code>"/opencms/*"</code>)
* @param defaultWebAppName the name of the default web application (or <code>null</code> to use the default <code>"ROOT"</code>)
* @param prompt the prompt format to set
* @param additionalShellCommands optional object for additional shell commands, or null
*/
public CmsShell(
String webInfPath,
String servletMapping,
String defaultWebAppName,
String prompt,
I_CmsShellCommands additionalShellCommands) {
setPrompt(prompt);
if (CmsStringUtil.isEmpty(servletMapping)) {
servletMapping = "/opencms/*";
}
if (CmsStringUtil.isEmpty(defaultWebAppName)) {
defaultWebAppName = "ROOT";
}
try {
// first initialize runlevel 1
m_opencms = OpenCmsCore.getInstance();
// Externalisation: get Locale: will be the System default since no CmsObject is up before
// runlevel 2
Locale locale = getLocale();
m_messages = Messages.get().getBundle(locale);
// search for the WEB-INF folder
if (CmsStringUtil.isEmpty(webInfPath)) {
System.out.println(m_messages.key(Messages.GUI_SHELL_NO_HOME_FOLDER_SPECIFIED_0));
System.out.println();
webInfPath = CmsFileUtil.searchWebInfFolder(System.getProperty("user.dir"));
if (CmsStringUtil.isEmpty(webInfPath)) {
System.err.println(m_messages.key(Messages.GUI_SHELL_HR_0));
System.err.println(m_messages.key(Messages.GUI_SHELL_NO_HOME_FOLDER_FOUND_0));
System.err.println();
System.err.println(m_messages.key(Messages.GUI_SHELL_START_DIR_LINE1_0));
System.err.println(m_messages.key(Messages.GUI_SHELL_START_DIR_LINE2_0));
System.err.println(m_messages.key(Messages.GUI_SHELL_HR_0));
return;
}
}
System.out.println(Messages.get().getBundle(locale).key(Messages.GUI_SHELL_WEB_INF_PATH_1, webInfPath));
// set the path to the WEB-INF folder (the 2nd and 3rd parameters are just reasonable dummies)
m_opencms.getSystemInfo().init(webInfPath, servletMapping, null, defaultWebAppName);
// now read the configuration properties
String propertyPath = m_opencms.getSystemInfo().getConfigurationFileRfsPath();
System.out.println(m_messages.key(Messages.GUI_SHELL_CONFIG_FILE_1, propertyPath));
System.out.println();
ExtendedProperties configuration = CmsPropertyUtils.loadProperties(propertyPath);
// now upgrade to runlevel 2
m_opencms = m_opencms.upgradeRunlevel(configuration);
// create a context object with 'Guest' permissions
m_cms = m_opencms.initCmsObject(m_opencms.getDefaultUsers().getUserGuest());
// initialize the settings of the user
m_settings = initSettings();
// initialize shell command object
m_shellCommands = new CmsShellCommands();
m_shellCommands.initShellCmsObject(m_cms, this);
// initialize additional shell command object
if (additionalShellCommands != null) {
m_additionaShellCommands = additionalShellCommands;
m_additionaShellCommands.initShellCmsObject(m_cms, null);
m_additionaShellCommands.shellStart();
} else {
m_shellCommands.shellStart();
}
m_commandObjects = new ArrayList();
if (m_additionaShellCommands != null) {
// get all shell callable methods from the the additionsl shell command object
m_commandObjects.add(new CmsCommandObject(m_additionaShellCommands));
}
// get all shell callable methods from the CmsShellCommands
m_commandObjects.add(new CmsCommandObject(m_shellCommands));
// get all shell callable methods from the CmsRequestContext
m_commandObjects.add(new CmsCommandObject(m_cms.getRequestContext()));
// get all shell callable methods from the CmsObject
m_commandObjects.add(new CmsCommandObject(m_cms));
} catch (Throwable t) {
t.printStackTrace(System.err);
}
}
/**
* Main program entry point when started via the command line.<p>
*
* @param args parameters passed to the application via the command line
*/
public static void main(String[] args) {
boolean wrongUsage = false;
String webInfPath = null;
String script = null;
String servletMapping = null;
String defaultWebApp = null;
if (args.length > 4) {
wrongUsage = true;
} else {
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (arg.startsWith(SHELL_PARAM_BASE)) {
webInfPath = arg.substring(SHELL_PARAM_BASE.length());
} else if (arg.startsWith(SHELL_PARAM_SCRIPT)) {
script = arg.substring(SHELL_PARAM_SCRIPT.length());
} else if (arg.startsWith(SHELL_PARAM_SERVLET_MAPPING)) {
servletMapping = arg.substring(SHELL_PARAM_SERVLET_MAPPING.length());
} else if (arg.startsWith(SHELL_PARAM_DEFAULT_WEB_APP)) {
defaultWebApp = arg.substring(SHELL_PARAM_DEFAULT_WEB_APP.length());
} else {
System.out.println(Messages.get().getBundle().key(Messages.GUI_SHELL_WRONG_USAGE_0));
wrongUsage = true;
}
}
}
if (wrongUsage) {
System.out.println(Messages.get().getBundle().key(Messages.GUI_SHELL_USAGE_1, CmsShell.class.getName()));
} else {
FileInputStream stream = null;
if (script != null) {
try {
stream = new FileInputStream(script);
} catch (IOException exc) {
System.out.println(Messages.get().getBundle().key(Messages.GUI_SHELL_ERR_SCRIPTFILE_1, script));
}
}
if (stream == null) {
// no script-file, use standard input stream
stream = new FileInputStream(FileDescriptor.in);
}
CmsShell shell = new CmsShell(
webInfPath,
servletMapping,
defaultWebApp,
"${user}@${project}:${siteroot}|${uri}>",
null);
shell.start(stream);
}
}
/**
* Exits this shell and destroys the OpenCms instance.<p>
*/
public void exit() {
if (m_exitCalled) {
return;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?