cmsshell.java
来自「找了很久才找到到源代码」· Java 代码 · 共 824 行 · 第 1/3 页
JAVA
824 行
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/CmsShell.java,v $
* Date : $Date: 2007-08-13 16:29:59 $
* Version: $Revision: 1.50 $
*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.main;
import org.opencms.db.CmsUserSettings;
import org.opencms.file.CmsObject;
import org.opencms.i18n.CmsLocaleManager;
import org.opencms.i18n.CmsMessages;
import org.opencms.util.CmsDataTypeUtil;
import org.opencms.util.CmsFileUtil;
import org.opencms.util.CmsPropertyUtils;
import org.opencms.util.CmsStringUtil;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import org.apache.commons.collections.ExtendedProperties;
/**
* A commad line interface to access OpenCms functions which
* is used for the initial setup and also can be used to directly access the OpenCms
* repository without the Workplace.<p>
*
* The CmsShell has direct access to all methods in the "command objects".
* Currently the following classes are used as command objects:
* <code>{@link org.opencms.main.CmsShellCommands}</code>,
* <code>{@link org.opencms.file.CmsRequestContext}</code> and
* <code>{@link org.opencms.file.CmsObject}</code>.<p>
*
* Only public methods in the command objects that use supported data types
* as parameters can be called from the shell. Supported data types are:
* <code>String, {@link org.opencms.util.CmsUUID}, boolean, int, long, double, float</code>.<p>
*
* If a method name is ambiguous, i.e. the method name with the same numer of parameter exist
* in more then one of the command objects, the method is only executed on the first matching method object.<p>
*
* @author Alexander Kandzior
*
* @version $Revision: 1.50 $
*
* @since 6.0.0
*
* @see org.opencms.main.CmsShellCommands
* @see org.opencms.file.CmsRequestContext
* @see org.opencms.file.CmsObject
*/
public class CmsShell {
/**
* Command object class.<p>
*/
private class CmsCommandObject {
/** The list of methods. */
private Map m_methods;
/** The object to execute the methods on. */
private Object m_object;
/**
* Creates a new command object.<p>
*
* @param object the object to execute the methods on
*/
protected CmsCommandObject(Object object) {
m_object = object;
initShellMethods();
}
/**
* Tries to execute a method for the provided parameters on this command object.<p>
*
* If methods with the same name and number of parameters exist in this command object,
* the given parameters are tried to be converted from String to matching types.<p>
*
* @param command the command entered by the user in the shell
* @param parameters the parameters entered by the user in the shell
* @return true if a method was executed, false otherwise
*/
protected boolean executeMethod(String command, List parameters) {
// TODO: ensure that publishmanager, oumanager and rolemanager methods are accessible
int todo;
// build the method lookup
String lookup = buildMethodLookup(command, parameters.size());
// try to look up the methods of this command object
List possibleMethods = (List)m_methods.get(lookup);
if (possibleMethods == null) {
return false;
}
// a match for the mehod name was found, now try to figure out if the parameters are ok
Method onlyStringMethod = null;
Method foundMethod = null;
Object[] params = null;
Iterator i;
// first check if there is one method with only has String parameters, make this the fallback
i = possibleMethods.iterator();
while (i.hasNext()) {
Method method = (Method)i.next();
Class[] clazz = method.getParameterTypes();
boolean onlyString = true;
for (int j = 0; j < clazz.length; j++) {
if (!(clazz[j].equals(String.class))) {
onlyString = false;
break;
}
}
if (onlyString) {
onlyStringMethod = method;
break;
}
}
// now check a method matches the provided parameters
// if so, use this method, else continue searching
i = possibleMethods.iterator();
while (i.hasNext()) {
Method method = (Method)i.next();
if (method == onlyStringMethod) {
// skip the String only signature because this would always match
continue;
}
// now try to convert the parameters to the required types
Class[] clazz = method.getParameterTypes();
Object[] converted = new Object[clazz.length];
boolean match = true;
for (int j = 0; j < clazz.length; j++) {
String value = (String)parameters.get(j);
try {
converted[j] = CmsDataTypeUtil.parse(value, clazz[j]);
} catch (Throwable t) {
match = false;
break;
}
}
if (match) {
// we found a matching method signature
params = converted;
foundMethod = method;
break;
}
}
if ((foundMethod == null) && (onlyStringMethod != null)) {
// no match found but String only signature available, use this
params = parameters.toArray();
foundMethod = onlyStringMethod;
}
if ((params == null) || (foundMethod == null)) {
// no match found at all
return false;
}
// now try to invoke the method
try {
Object result = foundMethod.invoke(m_object, params);
if (result != null) {
if (result instanceof Collection) {
Collection c = (Collection)result;
System.out.println(c.getClass().getName() + " (size: " + c.size() + ")");
int count = 0;
if (result instanceof Map) {
Map m = (Map)result;
Iterator j = m.entrySet().iterator();
while (j.hasNext()) {
Map.Entry entry = (Map.Entry)j.next();
System.out.println(count++ + ": " + entry.getKey() + "= " + entry.getValue());
}
} else {
Iterator j = c.iterator();
while (j.hasNext()) {
System.out.println(count++ + ": " + j.next());
}
}
} else {
System.out.println(result.toString());
}
}
} catch (InvocationTargetException ite) {
System.out.println(Messages.get().getBundle(getLocale()).key(
Messages.GUI_SHELL_EXEC_METHOD_1,
new Object[] {foundMethod.getName()}));
ite.getTargetException().printStackTrace(System.out);
} catch (Throwable t) {
System.out.println(Messages.get().getBundle(getLocale()).key(
Messages.GUI_SHELL_EXEC_METHOD_1,
new Object[] {foundMethod.getName()}));
t.printStackTrace(System.out);
}
return true;
}
/**
* Returns a signature overview of all methods containing the given search String.<p>
*
* If no method name matches the given search String, the empty String is returned.<p>
*
* @param searchString the String to search for, if null all methods are shown
* @return a signature overview of all methods containing the given search String
*/
protected String getMethodHelp(String searchString) {
StringBuffer buf = new StringBuffer(512);
Iterator i = m_methods.keySet().iterator();
while (i.hasNext()) {
List l = (List)m_methods.get(i.next());
Iterator j = l.iterator();
while (j.hasNext()) {
Method method = (Method)j.next();
if ((searchString == null)
|| (method.getName().toLowerCase().indexOf(searchString.toLowerCase()) > -1)) {
buf.append("* ");
buf.append(method.getName());
buf.append("(");
Class[] params = method.getParameterTypes();
for (int k = 0; k < params.length; k++) {
String par = params[k].getName();
par = par.substring(par.lastIndexOf('.') + 1);
if (k != 0) {
buf.append(", ");
}
buf.append(par);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?