📄 simplemethod.java
字号:
/*
* $Id: SimpleMethod.java,v 1.5 2003/11/23 11:57:09 jonesde Exp $
*
* Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ofbiz.minilang;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilCache;
import org.ofbiz.base.util.UtilURL;
import org.ofbiz.base.util.UtilXml;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.transaction.GenericTransactionException;
import org.ofbiz.entity.transaction.TransactionUtil;
import org.ofbiz.minilang.method.MethodContext;
import org.ofbiz.minilang.method.MethodOperation;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ModelService;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* SimpleMethod Mini Language Core Object
*
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @version $Revision: 1.5 $
* @since 2.0
*/
public class SimpleMethod {
public static final String module = SimpleMethod.class.getName();
protected static UtilCache simpleMethodsDirectCache = new UtilCache("minilang.SimpleMethodsDirect", 0, 0);
protected static UtilCache simpleMethodsResourceCache = new UtilCache("minilang.SimpleMethodsResource", 0, 0);
protected static UtilCache simpleMethodsURLCache = new UtilCache("minilang.SimpleMethodsURL", 0, 0);
// ----- Event Context Invokers -----
public static String runSimpleEvent(String xmlResource, String methodName, HttpServletRequest request, HttpServletResponse response) throws MiniLangException {
return runSimpleMethod(xmlResource, methodName, new MethodContext(request, response, null));
}
public static String runSimpleEvent(String xmlResource, String methodName, HttpServletRequest request, HttpServletResponse response, ClassLoader loader) throws MiniLangException {
return runSimpleMethod(xmlResource, methodName, new MethodContext(request, response, loader));
}
public static String runSimpleEvent(URL xmlURL, String methodName, HttpServletRequest request, HttpServletResponse response, ClassLoader loader) throws MiniLangException {
return runSimpleMethod(xmlURL, methodName, new MethodContext(request, response, loader));
}
// ----- Service Context Invokers -----
public static Map runSimpleService(String xmlResource, String methodName, DispatchContext ctx, Map context) throws MiniLangException {
MethodContext methodContext = new MethodContext(ctx, context, null);
runSimpleMethod(xmlResource, methodName, methodContext);
return methodContext.getResults();
}
public static Map runSimpleService(String xmlResource, String methodName, DispatchContext ctx, Map context, ClassLoader loader) throws MiniLangException {
MethodContext methodContext = new MethodContext(ctx, context, loader);
runSimpleMethod(xmlResource, methodName, methodContext);
return methodContext.getResults();
}
public static Map runSimpleService(URL xmlURL, String methodName, DispatchContext ctx, Map context, ClassLoader loader) throws MiniLangException {
MethodContext methodContext = new MethodContext(ctx, context, loader);
runSimpleMethod(xmlURL, methodName, methodContext);
return methodContext.getResults();
}
// ----- General Method Invokers -----
public static String runSimpleMethod(String xmlResource, String methodName, MethodContext methodContext) throws MiniLangException {
Map simpleMethods = getSimpleMethods(xmlResource, methodName, methodContext.getLoader());
SimpleMethod simpleMethod = (SimpleMethod) simpleMethods.get(methodName);
if (simpleMethod == null) {
throw new MiniLangException("Could not find SimpleMethod " + methodName + " in XML document in resource: " + xmlResource);
}
return simpleMethod.exec(methodContext);
}
public static String runSimpleMethod(URL xmlURL, String methodName, MethodContext methodContext) throws MiniLangException {
Map simpleMethods = getSimpleMethods(xmlURL, methodName);
SimpleMethod simpleMethod = (SimpleMethod) simpleMethods.get(methodName);
if (simpleMethod == null) {
throw new MiniLangException("Could not find SimpleMethod " + methodName + " in XML document from URL: " + xmlURL.toString());
}
return simpleMethod.exec(methodContext);
}
public static Map getSimpleMethods(String xmlResource, String methodName, ClassLoader loader) throws MiniLangException {
Map simpleMethods = (Map) simpleMethodsResourceCache.get(xmlResource);
if (simpleMethods == null) {
synchronized (SimpleMethod.class) {
simpleMethods = (Map) simpleMethodsResourceCache.get(xmlResource);
if (simpleMethods == null) {
URL xmlURL = UtilURL.fromResource(xmlResource, loader);
if (xmlURL == null) {
throw new MiniLangException("Could not find SimpleMethod XML document in resource: " + xmlResource);
}
simpleMethods = getAllSimpleMethods(xmlURL);
// put it in the cache
simpleMethodsResourceCache.put(xmlResource, simpleMethods);
}
}
}
return simpleMethods;
}
public static Map getSimpleMethods(URL xmlURL, String methodName) throws MiniLangException {
Map simpleMethods = (Map) simpleMethodsURLCache.get(xmlURL);
if (simpleMethods == null) {
synchronized (SimpleMethod.class) {
simpleMethods = (Map) simpleMethodsURLCache.get(xmlURL);
if (simpleMethods == null) {
simpleMethods = getAllSimpleMethods(xmlURL);
// put it in the cache
simpleMethodsURLCache.put(xmlURL, simpleMethods);
}
}
}
return simpleMethods;
}
protected static Map getAllSimpleMethods(URL xmlURL) throws MiniLangException {
Map simpleMethods = new HashMap();
// read in the file
Document document = null;
try {
document = UtilXml.readXmlDocument(xmlURL, true);
} catch (java.io.IOException e) {
throw new MiniLangException("Could not read XML file", e);
} catch (org.xml.sax.SAXException e) {
throw new MiniLangException("Could not parse XML file", e);
} catch (javax.xml.parsers.ParserConfigurationException e) {
throw new MiniLangException("XML parser not setup correctly", e);
}
if (document == null) {
throw new MiniLangException("Could not find SimpleMethod XML document: " + xmlURL.toString());
}
Element rootElement = document.getDocumentElement();
List simpleMethodElements = UtilXml.childElementList(rootElement, "simple-method");
Iterator simpleMethodIter = simpleMethodElements.iterator();
while (simpleMethodIter.hasNext()) {
Element simpleMethodElement = (Element) simpleMethodIter.next();
SimpleMethod simpleMethod = new SimpleMethod(simpleMethodElement, simpleMethods);
simpleMethods.put(simpleMethod.getMethodName(), simpleMethod);
}
return simpleMethods;
}
public static Map getDirectSimpleMethods(String name, String content) throws MiniLangException {
Map simpleMethods = (Map) simpleMethodsDirectCache.get(name);
if (simpleMethods == null) {
synchronized (SimpleMethod.class) {
simpleMethods = (Map) simpleMethodsDirectCache.get(name);
if (simpleMethods == null) {
simpleMethods = getAllDirectSimpleMethods(name, content);
// put it in the cache
simpleMethodsDirectCache.put(name, simpleMethods);
}
}
}
return simpleMethods;
}
protected static Map getAllDirectSimpleMethods(String name, String content) throws MiniLangException {
Map simpleMethods = new HashMap();
// read in the file
Document document = null;
try {
if (content != null) {
document = UtilXml.readXmlDocument(content, true);
}
} catch (java.io.IOException e) {
throw new MiniLangException("Could not read XML content", e);
} catch (org.xml.sax.SAXException e) {
throw new MiniLangException("Could not parse direct XML content", e);
} catch (javax.xml.parsers.ParserConfigurationException e) {
throw new MiniLangException("XML parser not setup correctly", e);
}
if (document == null) {
throw new MiniLangException("Could not load SimpleMethod XML document: " + name);
}
Element rootElement = document.getDocumentElement();
List simpleMethodElements = UtilXml.childElementList(rootElement, "simple-method");
Iterator simpleMethodIter = simpleMethodElements.iterator();
while (simpleMethodIter.hasNext()) {
Element simpleMethodElement = (Element) simpleMethodIter.next();
SimpleMethod simpleMethod = new SimpleMethod(simpleMethodElement, simpleMethods);
simpleMethods.put(simpleMethod.getMethodName(), simpleMethod);
}
return simpleMethods;
}
// Member fields begin here...
protected List methodOperations = new LinkedList();
protected Map parentSimpleMethodsMap;
protected String methodName;
protected String shortDescription;
protected String defaultErrorCode;
protected String defaultSuccessCode;
protected String parameterMapName;
// event fields
protected String eventRequestName;
protected String eventResponseName;
protected String eventResponseCodeName;
protected String eventErrorMessageName;
protected String eventEventMessageName;
// service fields
protected String serviceResponseMessageName;
protected String serviceErrorMessageName;
protected String serviceErrorMessageListName;
protected String serviceErrorMessageMapName;
protected String serviceSuccessMessageName;
protected String serviceSuccessMessageListName;
protected boolean loginRequired = true;
protected boolean useTransaction = true;
protected String localeName;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -