|
Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java Thu Apr 19 15:09:03 2012 @@ -58,14 +58,14 @@ import org.ofbiz.minilang.method.callops import org.ofbiz.minilang.method.callops.SetServiceFields; import org.ofbiz.minilang.method.conditional.MasterIf; import org.ofbiz.minilang.method.conditional.While; -import org.ofbiz.minilang.method.entityops.GetRelated; -import org.ofbiz.minilang.method.entityops.GetRelatedOne; import org.ofbiz.minilang.method.entityops.EntityAnd; import org.ofbiz.minilang.method.entityops.EntityCondition; import org.ofbiz.minilang.method.entityops.EntityCount; import org.ofbiz.minilang.method.entityops.EntityOne; import org.ofbiz.minilang.method.entityops.FindByAnd; import org.ofbiz.minilang.method.entityops.FindByPrimaryKey; +import org.ofbiz.minilang.method.entityops.GetRelated; +import org.ofbiz.minilang.method.entityops.GetRelatedOne; import org.ofbiz.minilang.method.entityops.MakeValue; import org.ofbiz.minilang.method.envops.Iterate; import org.ofbiz.minilang.method.envops.IterateMap; @@ -87,9 +87,16 @@ import org.w3c.dom.Element; * SimpleMethod Mini Language Core Object */ public class SimpleMethod { - private static final Map<String, MethodOperation.Factory<MethodOperation>> methodOperationFactories; + + public static final String module = SimpleMethod.class.getName(); + public static final String err_resource = "MiniLangErrorUiLabels"; // never read locally: private static final Method simpleMethodExecMethod; private static final Method methodOperationExecMethod; + private static final Map<String, MethodOperation.Factory<MethodOperation>> methodOperationFactories; + protected static UtilCache<String, Map<String, SimpleMethod>> simpleMethodsDirectCache = UtilCache.createUtilCache("minilang.SimpleMethodsDirect", 0, 0); + protected static UtilCache<String, Map<String, SimpleMethod>> simpleMethodsResourceCache = UtilCache.createUtilCache("minilang.SimpleMethodsResource", 0, 0); + protected static UtilCache<URL, Map<String, SimpleMethod>> simpleMethodsURLCache = UtilCache.createUtilCache("minilang.SimpleMethodsURL", 0, 0); + static { Map<String, MethodOperation.Factory<MethodOperation>> mapFactories = new HashMap<String, MethodOperation.Factory<MethodOperation>>(); Iterator<MethodOperation.Factory<MethodOperation>> it = UtilGenerics.cast(ServiceLoader.load(MethodOperation.Factory.class, SimpleMethod.class.getClassLoader()).iterator()); @@ -99,72 +106,221 @@ public class SimpleMethod { } methodOperationFactories = Collections.unmodifiableMap(mapFactories); try { - // never read locally: simpleMethodExecMethod = SimpleMethod.class.getDeclaredMethod("exec", MethodContext.class); methodOperationExecMethod = MethodOperation.class.getDeclaredMethod("exec", MethodContext.class); } catch (NoSuchMethodException e) { throw UtilMisc.initCause(new InternalError(e.getMessage()), e); } } - public static final String module = SimpleMethod.class.getName(); - public static final String err_resource = "MiniLangErrorUiLabels"; - - protected static UtilCache<String, Map<String, SimpleMethod>> simpleMethodsDirectCache = UtilCache.createUtilCache("minilang.SimpleMethodsDirect", 0, 0); - protected static UtilCache<String, Map<String, SimpleMethod>> simpleMethodsResourceCache = UtilCache.createUtilCache("minilang.SimpleMethodsResource", 0, 0); - protected static UtilCache<URL, Map<String, SimpleMethod>> simpleMethodsURLCache = UtilCache.createUtilCache("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)); + protected static SimpleMethod compileSimpleMethod(Element simpleMethodElement, Map<String, SimpleMethod> simpleMethods, String location) { + return new SimpleMethod(simpleMethodElement, simpleMethods, location); } - // ----- Service Context Invokers ----- - - public static Map<String, Object> runSimpleService(String xmlResource, String methodName, DispatchContext ctx, Map<String, ? extends Object> context) throws MiniLangException { - MethodContext methodContext = new MethodContext(ctx, context, null); - runSimpleMethod(xmlResource, methodName, methodContext); - return methodContext.getResults(); + protected static void findEntityNamesUsed(List<MethodOperation> methodOperations, Set<String> allEntityNames, Set<String> simpleMethodsVisited) throws MiniLangException { + for (MethodOperation methodOperation : methodOperations) { + if (methodOperation instanceof FindByPrimaryKey) { + String entName = ((FindByPrimaryKey) methodOperation).getEntityName(); + if (UtilValidate.isNotEmpty(entName)) + allEntityNames.add(entName); + } else if (methodOperation instanceof FindByAnd) { + String entName = ((FindByAnd) methodOperation).getEntityName(); + if (UtilValidate.isNotEmpty(entName)) + allEntityNames.add(entName); + } else if (methodOperation instanceof EntityOne) { + String entName = ((EntityOne) methodOperation).getEntityName(); + if (UtilValidate.isNotEmpty(entName)) + allEntityNames.add(entName); + } else if (methodOperation instanceof EntityAnd) { + String entName = ((EntityAnd) methodOperation).getEntityName(); + if (UtilValidate.isNotEmpty(entName)) + allEntityNames.add(entName); + } else if (methodOperation instanceof EntityCondition) { + String entName = ((EntityCondition) methodOperation).getEntityName(); + if (UtilValidate.isNotEmpty(entName)) + allEntityNames.add(entName); + } else if (methodOperation instanceof EntityCount) { + String entName = ((EntityCount) methodOperation).getEntityName(); + if (UtilValidate.isNotEmpty(entName)) + allEntityNames.add(entName); + } else if (methodOperation instanceof MakeValue) { + String entName = ((MakeValue) methodOperation).getEntityName(); + if (UtilValidate.isNotEmpty(entName)) + allEntityNames.add(entName); + } else if (methodOperation instanceof GetRelated) { + String relationName = ((GetRelated) methodOperation).getRelationName(); + if (UtilValidate.isNotEmpty(relationName)) + allEntityNames.add(relationName); + } else if (methodOperation instanceof GetRelatedOne) { + String relationName = ((GetRelatedOne) methodOperation).getRelationName(); + if (UtilValidate.isNotEmpty(relationName)) + allEntityNames.add(relationName); + } else if (methodOperation instanceof CallSimpleMethod) { + CallSimpleMethod csm = (CallSimpleMethod) methodOperation; + try { + SimpleMethod calledMethod = csm.getSimpleMethodToCall(null); + if (calledMethod == null) { + Debug.logWarning("Could not find simple-method [" + csm.getMethodName() + "] in [" + csm.getXmlResource() + "] from the SimpleMethod [" + csm.getSimpleMethod().getMethodName() + "] in [" + csm.getSimpleMethod().getFromLocation() + "]", module); + } else { + if (!simpleMethodsVisited.contains(calledMethod.getLocationAndName())) { + simpleMethodsVisited.add(calledMethod.getLocationAndName()); + findEntityNamesUsed(calledMethod.methodOperations, allEntityNames, simpleMethodsVisited); + } + } + } catch (MiniLangException e) { + Debug.logWarning("Error getting simple-method info in the [" + csm.getSimpleMethod().getMethodName() + "] in [" + csm.getSimpleMethod().getFromLocation() + "]: " + e.toString(), module); + } + } else if (methodOperation instanceof Iterate) { + findEntityNamesUsed(((Iterate) methodOperation).getSubOps(), allEntityNames, simpleMethodsVisited); + } else if (methodOperation instanceof IterateMap) { + findEntityNamesUsed(((IterateMap) methodOperation).getSubOps(), allEntityNames, simpleMethodsVisited); + } else if (methodOperation instanceof Loop) { + findEntityNamesUsed(((Loop) methodOperation).getSubOps(), allEntityNames, simpleMethodsVisited); + } else if (methodOperation instanceof MasterIf) { + findEntityNamesUsed(((MasterIf) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); + } else if (methodOperation instanceof While) { + findEntityNamesUsed(((While) methodOperation).getThenSubOps(), allEntityNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfValidateMethod) { + findEntityNamesUsed(((IfValidateMethod) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfInstanceOf) { + findEntityNamesUsed(((IfInstanceOf) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfCompare) { + findEntityNamesUsed(((IfCompare) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfCompareField) { + findEntityNamesUsed(((IfCompareField) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfRegexp) { + findEntityNamesUsed(((IfRegexp) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfEmpty) { + findEntityNamesUsed(((IfEmpty) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfNotEmpty) { + findEntityNamesUsed(((IfNotEmpty) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfHasPermission) { + findEntityNamesUsed(((IfHasPermission) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); + } + } } - public static Map<String, Object> runSimpleService(String xmlResource, String methodName, DispatchContext ctx, Map<String, ? extends Object> context, ClassLoader loader) throws MiniLangException { - MethodContext methodContext = new MethodContext(ctx, context, loader); - runSimpleMethod(xmlResource, methodName, methodContext); - return methodContext.getResults(); + protected static void findServiceNamesCalled(List<MethodOperation> methodOperations, Set<String> allServiceNames, Set<String> simpleMethodsVisited) throws MiniLangException { + for (MethodOperation methodOperation : methodOperations) { + if (methodOperation instanceof CallService) { + String svcName = ((CallService) methodOperation).getServiceName(); + if (UtilValidate.isNotEmpty(svcName)) + allServiceNames.add(svcName); + } else if (methodOperation instanceof CallServiceAsynch) { + String svcName = ((CallServiceAsynch) methodOperation).getServiceName(); + if (UtilValidate.isNotEmpty(svcName)) + allServiceNames.add(svcName); + } else if (methodOperation instanceof SetServiceFields) { + String svcName = ((SetServiceFields) methodOperation).getServiceName(); + if (UtilValidate.isNotEmpty(svcName)) + allServiceNames.add(svcName); + } else if (methodOperation instanceof CallSimpleMethod) { + CallSimpleMethod csm = (CallSimpleMethod) methodOperation; + try { + SimpleMethod calledMethod = csm.getSimpleMethodToCall(methodOperations.getClass().getClassLoader()); + if (calledMethod == null) { + Debug.logWarning("Could not find simple-method [" + csm.getMethodName() + "] in [" + csm.getXmlResource() + "] from the SimpleMethod [" + csm.getSimpleMethod().getMethodName() + "] in [" + csm.getSimpleMethod().getFromLocation() + "]", module); + } else { + if (!simpleMethodsVisited.contains(calledMethod.getLocationAndName())) { + simpleMethodsVisited.add(calledMethod.getLocationAndName()); + findServiceNamesCalled(calledMethod.methodOperations, allServiceNames, simpleMethodsVisited); + } + } + } catch (MiniLangException e) { + Debug.logWarning("Error getting simple-method info in the [" + csm.getSimpleMethod().getMethodName() + "] in [" + csm.getSimpleMethod().getFromLocation() + "]: " + e.toString(), module); + } + } else if (methodOperation instanceof Iterate) { + findServiceNamesCalled(((Iterate) methodOperation).getSubOps(), allServiceNames, simpleMethodsVisited); + } else if (methodOperation instanceof IterateMap) { + findServiceNamesCalled(((IterateMap) methodOperation).getSubOps(), allServiceNames, simpleMethodsVisited); + } else if (methodOperation instanceof Loop) { + findServiceNamesCalled(((Loop) methodOperation).getSubOps(), allServiceNames, simpleMethodsVisited); + } else if (methodOperation instanceof MasterIf) { + findServiceNamesCalled(((MasterIf) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); + } else if (methodOperation instanceof While) { + findServiceNamesCalled(((While) methodOperation).getThenSubOps(), allServiceNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfValidateMethod) { + findServiceNamesCalled(((IfValidateMethod) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfInstanceOf) { + findServiceNamesCalled(((IfInstanceOf) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfCompare) { + findServiceNamesCalled(((IfCompare) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfCompareField) { + findServiceNamesCalled(((IfCompareField) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfRegexp) { + findServiceNamesCalled(((IfRegexp) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfEmpty) { + findServiceNamesCalled(((IfEmpty) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfNotEmpty) { + findServiceNamesCalled(((IfNotEmpty) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); + } else if (methodOperation instanceof IfHasPermission) { + findServiceNamesCalled(((IfHasPermission) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); + } + } } - public static Map<String, Object> runSimpleService(URL xmlURL, String methodName, DispatchContext ctx, Map<String, ? extends Object> context, ClassLoader loader) throws MiniLangException { - MethodContext methodContext = new MethodContext(ctx, context, loader); - runSimpleMethod(xmlURL, methodName, methodContext); - return methodContext.getResults(); + protected static Map<String, SimpleMethod> getAllDirectSimpleMethods(String name, String content, String fromLocation) throws MiniLangException { + if (UtilValidate.isEmpty(fromLocation)) { + fromLocation = "<location not known>"; + } + Map<String, SimpleMethod> simpleMethods = FastMap.newInstance(); + Document document = null; + try { + if (content != null) { + document = UtilXml.readXmlDocument(content, true, 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(); + for (Element simpleMethodElement : UtilXml.childElementList(rootElement, "simple-method")) { + SimpleMethod simpleMethod = compileSimpleMethod(simpleMethodElement, simpleMethods, fromLocation); + simpleMethods.put(simpleMethod.getMethodName(), simpleMethod); + } + return simpleMethods; } - // ----- General Method Invokers ----- - - public static String runSimpleMethod(String xmlResource, String methodName, MethodContext methodContext) throws MiniLangException { - Map<String, SimpleMethod> simpleMethods = getSimpleMethods(xmlResource, methodContext.getLoader()); - SimpleMethod simpleMethod = simpleMethods.get(methodName); - if (simpleMethod == null) { - throw new MiniLangException("Could not find SimpleMethod " + methodName + " in XML document in resource: " + xmlResource); + protected static Map<String, SimpleMethod> getAllSimpleMethods(URL xmlURL) throws MiniLangException { + Map<String, SimpleMethod> simpleMethods = FastMap.newInstance(); + Document document = null; + try { + document = UtilXml.readXmlDocument(xmlURL, true, 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); } - return simpleMethod.exec(methodContext); + if (document == null) { + throw new MiniLangException("Could not find SimpleMethod XML document: " + xmlURL.toString()); + } + Element rootElement = document.getDocumentElement(); + for (Element simpleMethodElement : UtilXml.childElementList(rootElement, "simple-method")) { + SimpleMethod simpleMethod = compileSimpleMethod(simpleMethodElement, simpleMethods, xmlURL.toString()); + simpleMethods.put(simpleMethod.getMethodName(), simpleMethod); + } + return simpleMethods; } - public static String runSimpleMethod(URL xmlURL, String methodName, MethodContext methodContext) throws MiniLangException { - Map<String, SimpleMethod> simpleMethods = getSimpleMethods(xmlURL); - SimpleMethod simpleMethod = simpleMethods.get(methodName); - if (simpleMethod == null) { - throw new MiniLangException("Could not find SimpleMethod " + methodName + " in XML document from URL: " + xmlURL.toString()); + public static Map<String, SimpleMethod> getDirectSimpleMethods(String name, String content, String fromLocation) throws MiniLangException { + Map<String, SimpleMethod> simpleMethods = simpleMethodsDirectCache.get(name); + if (simpleMethods == null) { + synchronized (SimpleMethod.class) { + simpleMethods = simpleMethodsDirectCache.get(name); + if (simpleMethods == null) { + simpleMethods = getAllDirectSimpleMethods(name, content, fromLocation); + simpleMethodsDirectCache.put(name, simpleMethods); + } + } } - return simpleMethod.exec(methodContext); + return simpleMethods; } public static Map<String, SimpleMethod> getSimpleMethods(String xmlResource, ClassLoader loader) throws MiniLangException { @@ -173,34 +329,42 @@ public class SimpleMethod { synchronized (SimpleMethod.class) { simpleMethods = simpleMethodsResourceCache.get(xmlResource); if (simpleMethods == null) { - //URL xmlURL = UtilURL.fromResource(xmlResource, loader); + // URL xmlURL = UtilURL.fromResource(xmlResource, loader); URL xmlURL = null; try { xmlURL = FlexibleLocation.resolveLocation(xmlResource, loader); } catch (MalformedURLException e) { throw new MiniLangException("Could not find SimpleMethod XML document in resource: " + xmlResource + "; error was: " + e.toString(), e); } - 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<String, SimpleMethod> getSimpleMethods(URL xmlURL) throws MiniLangException { + Map<String, SimpleMethod> simpleMethods = simpleMethodsURLCache.get(xmlURL); + if (simpleMethods == null) { + synchronized (SimpleMethod.class) { + simpleMethods = simpleMethodsURLCache.get(xmlURL); + if (simpleMethods == null) { + simpleMethods = getAllSimpleMethods(xmlURL); + simpleMethodsURLCache.put(xmlURL, simpleMethods); + } + } + } return simpleMethods; } public static List<SimpleMethod> getSimpleMethodsList(String xmlResource, ClassLoader loader) throws MiniLangException { List<SimpleMethod> simpleMethods = FastList.newInstance(); - // Let the standard Map returning method take care of caching and compilation Map<String, SimpleMethod> simpleMethodMap = SimpleMethod.getSimpleMethods(xmlResource, loader); - // Load and traverse the document again to get a correctly ordered list of methods URL xmlURL = null; try { @@ -208,7 +372,6 @@ public class SimpleMethod { } catch (MalformedURLException e) { throw new MiniLangException("Could not find SimpleMethod XML document in resource: " + xmlResource + "; error was: " + e.toString(), e); } - // read in the file Document document = null; try { document = UtilXml.readXmlDocument(xmlURL, true, true); @@ -219,449 +382,166 @@ public class SimpleMethod { } 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(); - for (Element simpleMethodElement: UtilXml.childElementList(rootElement, "simple-method")) { + for (Element simpleMethodElement : UtilXml.childElementList(rootElement, "simple-method")) { simpleMethods.add(simpleMethodMap.get(simpleMethodElement.getAttribute("method-name"))); } - return simpleMethods; } - public static Map<String, SimpleMethod> getSimpleMethods(URL xmlURL) throws MiniLangException { - Map<String, SimpleMethod> simpleMethods = simpleMethodsURLCache.get(xmlURL); - - if (simpleMethods == null) { - synchronized (SimpleMethod.class) { - simpleMethods = simpleMethodsURLCache.get(xmlURL); - if (simpleMethods == null) { - simpleMethods = getAllSimpleMethods(xmlURL); - - // put it in the cache - simpleMethodsURLCache.put(xmlURL, simpleMethods); + public static void readOperations(Element simpleMethodElement, List<MethodOperation> methodOperations, SimpleMethod simpleMethod) { + List<? extends Element> operationElements = UtilXml.childElementList(simpleMethodElement); + if (UtilValidate.isNotEmpty(operationElements)) { + for (Element curOperElem : operationElements) { + String nodeName = curOperElem.getNodeName(); + MethodOperation methodOp = null; + MethodOperation.Factory<MethodOperation> factory = methodOperationFactories.get(nodeName); + if (factory != null) { + methodOp = factory.createMethodOperation(curOperElem, simpleMethod); + } else if ("else".equals(nodeName)) { + // don't add anything, but don't complain either, this one is handled in the individual operations + } else { + Debug.logWarning("Operation element \"" + nodeName + "\" no recognized", module); } + if (methodOp == null) + continue; + methodOperations.add(methodOp); + DeprecatedOperation depOp = methodOp.getClass().getAnnotation(DeprecatedOperation.class); + if (depOp != null) + Debug.logInfo("The " + nodeName + " operation has been deprecated in favor of the " + depOp.value() + " operation; found use of this in [" + simpleMethod.getShortDescription() + "]: " + methodOp.rawString(), module); } } - - return simpleMethods; } - protected static Map<String, SimpleMethod> getAllSimpleMethods(URL xmlURL) throws MiniLangException { - Map<String, SimpleMethod> simpleMethods = FastMap.newInstance(); - - // read in the file - Document document = null; - try { - document = UtilXml.readXmlDocument(xmlURL, true, 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(); - for (Element simpleMethodElement: UtilXml.childElementList(rootElement, "simple-method")) { - SimpleMethod simpleMethod = compileSimpleMethod(simpleMethodElement, simpleMethods, xmlURL.toString()); - simpleMethods.put(simpleMethod.getMethodName(), simpleMethod); - } - - return simpleMethods; + public static String runSimpleEvent(String xmlResource, String methodName, HttpServletRequest request, HttpServletResponse response) throws MiniLangException { + return runSimpleMethod(xmlResource, methodName, new MethodContext(request, response, null)); } - protected static SimpleMethod compileSimpleMethod(Element simpleMethodElement, Map<String, SimpleMethod> simpleMethods, String location) { - return new SimpleMethod(simpleMethodElement, simpleMethods, location); + 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 Map<String, SimpleMethod> getDirectSimpleMethods(String name, String content, String fromLocation) throws MiniLangException { - Map<String, SimpleMethod> simpleMethods = simpleMethodsDirectCache.get(name); - - if (simpleMethods == null) { - synchronized (SimpleMethod.class) { - simpleMethods = simpleMethodsDirectCache.get(name); - if (simpleMethods == null) { - simpleMethods = getAllDirectSimpleMethods(name, content, fromLocation); - - // put it in the cache - simpleMethodsDirectCache.put(name, simpleMethods); - } - } - } - - return simpleMethods; + 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)); } - protected static Map<String, SimpleMethod> getAllDirectSimpleMethods(String name, String content, String fromLocation) throws MiniLangException { - if (UtilValidate.isEmpty(fromLocation)) { - fromLocation = "<location not known>"; - } - - Map<String, SimpleMethod> simpleMethods = FastMap.newInstance(); - - // read in the file - Document document = null; - - try { - if (content != null) { - document = UtilXml.readXmlDocument(content, true, 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(); - for (Element simpleMethodElement: UtilXml.childElementList(rootElement, "simple-method")) { - SimpleMethod simpleMethod = compileSimpleMethod(simpleMethodElement, simpleMethods, fromLocation); - simpleMethods.put(simpleMethod.getMethodName(), simpleMethod); + public static String runSimpleMethod(String xmlResource, String methodName, MethodContext methodContext) throws MiniLangException { + Map<String, SimpleMethod> simpleMethods = getSimpleMethods(xmlResource, methodContext.getLoader()); + SimpleMethod simpleMethod = simpleMethods.get(methodName); + if (simpleMethod == null) { + throw new MiniLangException("Could not find SimpleMethod " + methodName + " in XML document in resource: " + xmlResource); } - - return simpleMethods; - } - - // Member fields begin here... - protected List<MethodOperation> methodOperations = FastList.newInstance(); - protected Map<String, SimpleMethod> parentSimpleMethodsMap; - protected String fromLocation; - protected String methodName; - protected String shortDescription; - protected String defaultErrorCode; - protected String defaultSuccessCode; - - protected String parameterMapName; - - // event fields - protected String eventRequestName; - protected String eventSessionName; - protected String eventResponseName; - protected String eventResponseCodeName; - protected String eventErrorMessageName; - protected String eventErrorMessageListName; - protected String eventEventMessageName; - protected String eventEventMessageListName; - - // 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; - protected String delegatorName; - protected String securityName; - protected String dispatcherName; - protected String userLoginName; - - public SimpleMethod(Element simpleMethodElement, Map<String, SimpleMethod> parentSimpleMethodsMap, String fromLocation) { - this.parentSimpleMethodsMap = parentSimpleMethodsMap; - this.fromLocation = fromLocation; - this.methodName = simpleMethodElement.getAttribute("method-name"); - this.shortDescription = simpleMethodElement.getAttribute("short-description"); - - defaultErrorCode = UtilXml.elementAttribute(simpleMethodElement, "default-error-code", "error"); - defaultSuccessCode = UtilXml.elementAttribute(simpleMethodElement, "default-success-code", "success"); - - parameterMapName = UtilXml.elementAttribute(simpleMethodElement, "parameter-map-name", "parameters"); - - eventRequestName = UtilXml.elementAttribute(simpleMethodElement, "event-request-object-name", "request"); - eventSessionName = UtilXml.elementAttribute(simpleMethodElement, "event-session-object-name", "session"); - eventResponseName = UtilXml.elementAttribute(simpleMethodElement, "event-response-object-name", "response"); - eventResponseCodeName = UtilXml.elementAttribute(simpleMethodElement, "event-response-code-name", "_response_code_"); - eventErrorMessageName = UtilXml.elementAttribute(simpleMethodElement, "event-error-message-name", "_error_message_"); - eventErrorMessageListName = UtilXml.elementAttribute(simpleMethodElement, "event-error-message-list-name", "_error_message_list_"); - eventEventMessageName = UtilXml.elementAttribute(simpleMethodElement, "event-event-message-name", "_event_message_"); - eventEventMessageListName = UtilXml.elementAttribute(simpleMethodElement, "event-event-message-list-name", "_event_message_list_"); - - serviceResponseMessageName = UtilXml.elementAttribute(simpleMethodElement, "service-response-message-name", "responseMessage"); - serviceErrorMessageName = UtilXml.elementAttribute(simpleMethodElement, "service-error-message-name", "errorMessage"); - serviceErrorMessageListName = UtilXml.elementAttribute(simpleMethodElement, "service-error-message-list-name", "errorMessageList"); - serviceErrorMessageMapName = UtilXml.elementAttribute(simpleMethodElement, "service-error-message-map-name", "errorMessageMap"); - - serviceSuccessMessageName = UtilXml.elementAttribute(simpleMethodElement, "service-success-message-name", "successMessage"); - serviceSuccessMessageListName = UtilXml.elementAttribute(simpleMethodElement, "service-success-message-list-name", "successMessageList"); - - loginRequired = !"false".equals(simpleMethodElement.getAttribute("login-required")); - useTransaction = !"false".equals(simpleMethodElement.getAttribute("use-transaction")); - - localeName = UtilXml.elementAttribute(simpleMethodElement, "locale-name", "locale"); - delegatorName = UtilXml.elementAttribute(simpleMethodElement, "delegator-name", "delegator"); - securityName = UtilXml.elementAttribute(simpleMethodElement, "security-name", "security"); - dispatcherName = UtilXml.elementAttribute(simpleMethodElement, "dispatcher-name", "dispatcher"); - userLoginName = UtilXml.elementAttribute(simpleMethodElement, "user-login-name", "userLogin"); - - readOperations(simpleMethodElement, this.methodOperations, this); - } - - public String getFromLocation() { - return this.fromLocation; - } - public String getMethodName() { - return this.methodName; - } - public String getLocationAndName() { - return this.fromLocation + "#" + this.methodName; - } - - public SimpleMethod getSimpleMethodInSameFile(String simpleMethodName) { - if (parentSimpleMethodsMap == null) return null; - return parentSimpleMethodsMap.get(simpleMethodName); - } - - public String getShortDescription() { - return this.shortDescription + " [" + this.fromLocation + "#" + this.methodName + "]"; - } - - public String getDefaultErrorCode() { - return this.defaultErrorCode; - } - - public String getDefaultSuccessCode() { - return this.defaultSuccessCode; - } - - public String getParameterMapName() { - return this.parameterMapName; - } - - // event fields - public String getEventRequestName() { - return this.eventRequestName; - } - - public String getEventSessionName() { - return this.eventSessionName; - } - - public String getEventResponseCodeName() { - return this.eventResponseCodeName; - } - - public String getEventErrorMessageName() { - return this.eventErrorMessageName; - } - public String getEventErrorMessageListName() { - return this.eventErrorMessageListName; - } - - public String getEventEventMessageName() { - return this.eventEventMessageName; - } - public String getEventEventMessageListName() { - return this.eventEventMessageListName; - } - - // service fields - public String getServiceResponseMessageName() { - return this.serviceResponseMessageName; - } - - public String getServiceErrorMessageName() { - return this.serviceErrorMessageName; - } - - public String getServiceErrorMessageListName() { - return this.serviceErrorMessageListName; - } - - public String getServiceErrorMessageMapName() { - return this.serviceErrorMessageMapName; - } - - public String getServiceSuccessMessageName() { - return this.serviceSuccessMessageName; - } - - public String getServiceSuccessMessageListName() { - return this.serviceSuccessMessageListName; - } - - public boolean getLoginRequired() { - return this.loginRequired; - } - - public boolean getUseTransaction() { - return this.useTransaction; - } - - public String getDelegatorEnvName() { - return this.delegatorName; - } - - public String getSecurityEnvName() { - return this.securityName; - } - - public String getDispatcherEnvName() { - return this.dispatcherName; - } - - public String getUserLoginEnvName() { - return this.userLoginName; - } - - public Set<String> getAllServiceNamesCalled() throws MiniLangException { - Set<String> allServiceNames = FastSet.newInstance(); - Set<String> simpleMethodsVisited = FastSet.newInstance(); - findServiceNamesCalled(this.methodOperations, allServiceNames, simpleMethodsVisited); - return allServiceNames; + return simpleMethod.exec(methodContext); } - protected static void findServiceNamesCalled(List<MethodOperation> methodOperations, Set<String> allServiceNames, Set<String> simpleMethodsVisited) throws MiniLangException { - for (MethodOperation methodOperation: methodOperations) { - if (methodOperation instanceof CallService) { - String svcName = ((CallService) methodOperation).getServiceName(); - if (UtilValidate.isNotEmpty(svcName)) allServiceNames.add(svcName); - } else if (methodOperation instanceof CallServiceAsynch) { - String svcName = ((CallServiceAsynch) methodOperation).getServiceName(); - if (UtilValidate.isNotEmpty(svcName)) allServiceNames.add(svcName); - } else if (methodOperation instanceof SetServiceFields) { - String svcName = ((SetServiceFields) methodOperation).getServiceName(); - if (UtilValidate.isNotEmpty(svcName)) allServiceNames.add(svcName); - } else if (methodOperation instanceof CallSimpleMethod) { - CallSimpleMethod csm = (CallSimpleMethod) methodOperation; - try { - SimpleMethod calledMethod = csm.getSimpleMethodToCall(methodOperations.getClass().getClassLoader()); - if (calledMethod == null) { - Debug.logWarning("Could not find simple-method [" + csm.getMethodName() + "] in [" + csm.getXmlResource() + "] from the SimpleMethod [" + csm.getSimpleMethod().getMethodName() + "] in [" + csm.getSimpleMethod().getFromLocation() + "]", module); - } else { - if (!simpleMethodsVisited.contains(calledMethod.getLocationAndName())) { - simpleMethodsVisited.add(calledMethod.getLocationAndName()); - findServiceNamesCalled(calledMethod.methodOperations, allServiceNames, simpleMethodsVisited); - } - } - } catch (MiniLangException e) { - Debug.logWarning("Error getting simple-method info in the [" + csm.getSimpleMethod().getMethodName() + "] in [" + csm.getSimpleMethod().getFromLocation() + "]: " + e.toString(), module); - } - } else if (methodOperation instanceof Iterate) { - findServiceNamesCalled(((Iterate) methodOperation).getSubOps(), allServiceNames, simpleMethodsVisited); - } else if (methodOperation instanceof IterateMap) { - findServiceNamesCalled(((IterateMap) methodOperation).getSubOps(), allServiceNames, simpleMethodsVisited); - } else if (methodOperation instanceof Loop) { - findServiceNamesCalled(((Loop) methodOperation).getSubOps(), allServiceNames, simpleMethodsVisited); - } else if (methodOperation instanceof MasterIf) { - findServiceNamesCalled(((MasterIf) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); - } else if (methodOperation instanceof While) { - findServiceNamesCalled(((While) methodOperation).getThenSubOps(), allServiceNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfValidateMethod) { - findServiceNamesCalled(((IfValidateMethod) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfInstanceOf) { - findServiceNamesCalled(((IfInstanceOf) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfCompare) { - findServiceNamesCalled(((IfCompare) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfCompareField) { - findServiceNamesCalled(((IfCompareField) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfRegexp) { - findServiceNamesCalled(((IfRegexp) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfEmpty) { - findServiceNamesCalled(((IfEmpty) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfNotEmpty) { - findServiceNamesCalled(((IfNotEmpty) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfHasPermission) { - findServiceNamesCalled(((IfHasPermission) methodOperation).getAllSubOps(), allServiceNames, simpleMethodsVisited); - } + public static String runSimpleMethod(URL xmlURL, String methodName, MethodContext methodContext) throws MiniLangException { + Map<String, SimpleMethod> simpleMethods = getSimpleMethods(xmlURL); + 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 Set<String> getAllEntityNamesUsed() throws MiniLangException { - Set<String> allEntityNames = FastSet.newInstance(); - Set<String> simpleMethodsVisited = FastSet.newInstance(); - findEntityNamesUsed(this.methodOperations, allEntityNames, simpleMethodsVisited); - return allEntityNames; + public static Map<String, Object> runSimpleService(String xmlResource, String methodName, DispatchContext ctx, Map<String, ? extends Object> context) throws MiniLangException { + MethodContext methodContext = new MethodContext(ctx, context, null); + runSimpleMethod(xmlResource, methodName, methodContext); + return methodContext.getResults(); } - protected static void findEntityNamesUsed(List<MethodOperation> methodOperations, Set<String> allEntityNames, Set<String> simpleMethodsVisited) throws MiniLangException { - for (MethodOperation methodOperation: methodOperations) { - if (methodOperation instanceof FindByPrimaryKey) { - String entName = ((FindByPrimaryKey) methodOperation).getEntityName(); - if (UtilValidate.isNotEmpty(entName)) allEntityNames.add(entName); - } else if (methodOperation instanceof FindByAnd) { - String entName = ((FindByAnd) methodOperation).getEntityName(); - if (UtilValidate.isNotEmpty(entName)) allEntityNames.add(entName); - } else if (methodOperation instanceof EntityOne) { - String entName = ((EntityOne) methodOperation).getEntityName(); - if (UtilValidate.isNotEmpty(entName)) allEntityNames.add(entName); - } else if (methodOperation instanceof EntityAnd) { - String entName = ((EntityAnd) methodOperation).getEntityName(); - if (UtilValidate.isNotEmpty(entName)) allEntityNames.add(entName); - } else if (methodOperation instanceof EntityCondition) { - String entName = ((EntityCondition) methodOperation).getEntityName(); - if (UtilValidate.isNotEmpty(entName)) allEntityNames.add(entName); - } else if (methodOperation instanceof EntityCount) { - String entName = ((EntityCount) methodOperation).getEntityName(); - if (UtilValidate.isNotEmpty(entName)) allEntityNames.add(entName); - } else if (methodOperation instanceof MakeValue) { - String entName = ((MakeValue) methodOperation).getEntityName(); - if (UtilValidate.isNotEmpty(entName)) allEntityNames.add(entName); - } else if (methodOperation instanceof GetRelated) { - String relationName = ((GetRelated) methodOperation).getRelationName(); - if (UtilValidate.isNotEmpty(relationName)) allEntityNames.add(relationName); - } else if (methodOperation instanceof GetRelatedOne) { - String relationName = ((GetRelatedOne) methodOperation).getRelationName(); - if (UtilValidate.isNotEmpty(relationName)) allEntityNames.add(relationName); - } else if (methodOperation instanceof CallSimpleMethod) { - CallSimpleMethod csm = (CallSimpleMethod) methodOperation; - try { - SimpleMethod calledMethod = csm.getSimpleMethodToCall(null); - if (calledMethod == null) { - Debug.logWarning("Could not find simple-method [" + csm.getMethodName() + "] in [" + csm.getXmlResource() + "] from the SimpleMethod [" + csm.getSimpleMethod().getMethodName() + "] in [" + csm.getSimpleMethod().getFromLocation() + "]", module); - } else { - if (!simpleMethodsVisited.contains(calledMethod.getLocationAndName())) { - simpleMethodsVisited.add(calledMethod.getLocationAndName()); - findEntityNamesUsed(calledMethod.methodOperations, allEntityNames, simpleMethodsVisited); - } - } - } catch (MiniLangException e) { - Debug.logWarning("Error getting simple-method info in the [" + csm.getSimpleMethod().getMethodName() + "] in [" + csm.getSimpleMethod().getFromLocation() + "]: " + e.toString(), module); - } - } else if (methodOperation instanceof Iterate) { - findEntityNamesUsed(((Iterate) methodOperation).getSubOps(), allEntityNames, simpleMethodsVisited); - } else if (methodOperation instanceof IterateMap) { - findEntityNamesUsed(((IterateMap) methodOperation).getSubOps(), allEntityNames, simpleMethodsVisited); - } else if (methodOperation instanceof Loop) { - findEntityNamesUsed(((Loop) methodOperation).getSubOps(), allEntityNames, simpleMethodsVisited); - } else if (methodOperation instanceof MasterIf) { - findEntityNamesUsed(((MasterIf) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); - } else if (methodOperation instanceof While) { - findEntityNamesUsed(((While) methodOperation).getThenSubOps(), allEntityNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfValidateMethod) { - findEntityNamesUsed(((IfValidateMethod) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfInstanceOf) { - findEntityNamesUsed(((IfInstanceOf) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfCompare) { - findEntityNamesUsed(((IfCompare) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfCompareField) { - findEntityNamesUsed(((IfCompareField) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfRegexp) { - findEntityNamesUsed(((IfRegexp) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfEmpty) { - findEntityNamesUsed(((IfEmpty) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfNotEmpty) { - findEntityNamesUsed(((IfNotEmpty) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); - } else if (methodOperation instanceof IfHasPermission) { - findEntityNamesUsed(((IfHasPermission) methodOperation).getAllSubOps(), allEntityNames, simpleMethodsVisited); + public static Map<String, Object> runSimpleService(String xmlResource, String methodName, DispatchContext ctx, Map<String, ? extends Object> context, ClassLoader loader) throws MiniLangException { + MethodContext methodContext = new MethodContext(ctx, context, loader); + runSimpleMethod(xmlResource, methodName, methodContext); + return methodContext.getResults(); + } + + public static Map<String, Object> runSimpleService(URL xmlURL, String methodName, DispatchContext ctx, Map<String, ? extends Object> context, ClassLoader loader) throws MiniLangException { + MethodContext methodContext = new MethodContext(ctx, context, loader); + runSimpleMethod(xmlURL, methodName, methodContext); + return methodContext.getResults(); + } + + /** + * Execs the given operations returning true if all return true, or returning false and stopping if any return false. + */ + public static boolean runSubOps(List<MethodOperation> methodOperations, MethodContext methodContext) { + for (MethodOperation methodOperation : methodOperations) { + try { + if (!methodOperation.exec(methodContext)) { + return false; + } + } catch (Throwable t) { + String errMsg = "Error in simple-method operation [" + methodOperation.rawString() + "]: " + t.toString(); + Debug.logError(t, errMsg, module); + throw new RuntimeException(errMsg); } } + return true; + } + + protected String defaultErrorCode; + protected String defaultSuccessCode; + protected String delegatorName; + protected String dispatcherName; + protected String eventErrorMessageListName; + protected String eventErrorMessageName; + protected String eventEventMessageListName; + protected String eventEventMessageName; + protected String eventRequestName; + protected String eventResponseCodeName; + protected String eventResponseName; + protected String eventSessionName; + protected String fromLocation; + protected String localeName; + protected boolean loginRequired = true; + protected String methodName; + protected List<MethodOperation> methodOperations = FastList.newInstance(); + protected String parameterMapName; + protected Map<String, SimpleMethod> parentSimpleMethodsMap; + protected String securityName; + protected String serviceErrorMessageListName; + protected String serviceErrorMessageMapName; + protected String serviceErrorMessageName; + protected String serviceResponseMessageName; + protected String serviceSuccessMessageListName; + protected String serviceSuccessMessageName; + protected String shortDescription; + protected String userLoginName; + protected boolean useTransaction = true; + + public SimpleMethod(Element simpleMethodElement, Map<String, SimpleMethod> parentSimpleMethodsMap, String fromLocation) { + this.parentSimpleMethodsMap = parentSimpleMethodsMap; + this.fromLocation = fromLocation; + this.methodName = simpleMethodElement.getAttribute("method-name"); + this.shortDescription = simpleMethodElement.getAttribute("short-description"); + defaultErrorCode = UtilXml.elementAttribute(simpleMethodElement, "default-error-code", "error"); + defaultSuccessCode = UtilXml.elementAttribute(simpleMethodElement, "default-success-code", "success"); + parameterMapName = UtilXml.elementAttribute(simpleMethodElement, "parameter-map-name", "parameters"); + eventRequestName = UtilXml.elementAttribute(simpleMethodElement, "event-request-object-name", "request"); + eventSessionName = UtilXml.elementAttribute(simpleMethodElement, "event-session-object-name", "session"); + eventResponseName = UtilXml.elementAttribute(simpleMethodElement, "event-response-object-name", "response"); + eventResponseCodeName = UtilXml.elementAttribute(simpleMethodElement, "event-response-code-name", "_response_code_"); + eventErrorMessageName = UtilXml.elementAttribute(simpleMethodElement, "event-error-message-name", "_error_message_"); + eventErrorMessageListName = UtilXml.elementAttribute(simpleMethodElement, "event-error-message-list-name", "_error_message_list_"); + eventEventMessageName = UtilXml.elementAttribute(simpleMethodElement, "event-event-message-name", "_event_message_"); + eventEventMessageListName = UtilXml.elementAttribute(simpleMethodElement, "event-event-message-list-name", "_event_message_list_"); + serviceResponseMessageName = UtilXml.elementAttribute(simpleMethodElement, "service-response-message-name", "responseMessage"); + serviceErrorMessageName = UtilXml.elementAttribute(simpleMethodElement, "service-error-message-name", "errorMessage"); + serviceErrorMessageListName = UtilXml.elementAttribute(simpleMethodElement, "service-error-message-list-name", "errorMessageList"); + serviceErrorMessageMapName = UtilXml.elementAttribute(simpleMethodElement, "service-error-message-map-name", "errorMessageMap"); + serviceSuccessMessageName = UtilXml.elementAttribute(simpleMethodElement, "service-success-message-name", "successMessage"); + serviceSuccessMessageListName = UtilXml.elementAttribute(simpleMethodElement, "service-success-message-list-name", "successMessageList"); + loginRequired = !"false".equals(simpleMethodElement.getAttribute("login-required")); + useTransaction = !"false".equals(simpleMethodElement.getAttribute("use-transaction")); + localeName = UtilXml.elementAttribute(simpleMethodElement, "locale-name", "locale"); + delegatorName = UtilXml.elementAttribute(simpleMethodElement, "delegator-name", "delegator"); + securityName = UtilXml.elementAttribute(simpleMethodElement, "security-name", "security"); + dispatcherName = UtilXml.elementAttribute(simpleMethodElement, "dispatcher-name", "dispatcher"); + userLoginName = UtilXml.elementAttribute(simpleMethodElement, "user-login-name", "userLogin"); + readOperations(simpleMethodElement, this.methodOperations, this); } /** Execute the Simple Method operations */ @@ -669,36 +549,30 @@ public class SimpleMethod { // always put the null field object in as "null" methodContext.putEnv("null", GenericEntity.NULL_FIELD); methodContext.putEnv("nullField", GenericEntity.NULL_FIELD); - methodContext.putEnv(delegatorName, methodContext.getDelegator()); methodContext.putEnv(securityName, methodContext.getSecurity()); methodContext.putEnv(dispatcherName, methodContext.getDispatcher()); methodContext.putEnv(localeName, methodContext.getLocale()); methodContext.putEnv(parameterMapName, methodContext.getParameters()); - if (methodContext.getMethodType() == MethodContext.EVENT) { methodContext.putEnv(eventRequestName, methodContext.getRequest()); methodContext.putEnv(eventSessionName, methodContext.getRequest().getSession()); methodContext.putEnv(eventResponseName, methodContext.getResponse()); } - methodContext.putEnv("methodName", this.getMethodName()); methodContext.putEnv("methodShortDescription", this.getShortDescription()); - - GenericValue userLogin = methodContext.getUserLogin(); Locale locale = methodContext.getLocale(); - if (userLogin != null) { methodContext.putEnv(userLoginName, userLogin); } if (loginRequired) { if (userLogin == null) { - Map<String, Object> messageMap = UtilMisc.<String, Object>toMap("shortDescription", shortDescription); + Map<String, Object> messageMap = UtilMisc.<String, Object> toMap("shortDescription", shortDescription); String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.must_logged_process", messageMap, locale) + "."; if (methodContext.getMethodType() == MethodContext.EVENT) { - methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errMsg); + methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errMsg); return defaultErrorCode; } else if (methodContext.getMethodType() == MethodContext.SERVICE) { methodContext.putResult(ModelService.ERROR_MESSAGE, errMsg); @@ -707,10 +581,8 @@ public class SimpleMethod { } } } - // if using transaction, try to start here boolean beganTransaction = false; - if (useTransaction) { try { beganTransaction = TransactionUtil.begin(); @@ -728,10 +600,8 @@ public class SimpleMethod { } } } - // declare errorMsg here just in case transaction ops fail String errorMsg = ""; - boolean finished = false; try { finished = runSubOps(methodOperations, methodContext); @@ -742,30 +612,25 @@ public class SimpleMethod { finished = false; errorMsg += errMsg + "<br/>"; } - String returnValue = null; String response = null; StringBuilder summaryErrorStringBuffer = new StringBuilder(); if (methodContext.getMethodType() == MethodContext.EVENT) { boolean forceError = false; - String tempErrorMsg = (String) methodContext.getEnv(eventErrorMessageName); if (errorMsg.length() > 0 || UtilValidate.isNotEmpty(tempErrorMsg)) { errorMsg += tempErrorMsg; methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errorMsg); forceError = true; - summaryErrorStringBuffer.append(errorMsg); } List<Object> tempErrorMsgList = UtilGenerics.checkList(methodContext.getEnv(eventErrorMessageListName)); if (UtilValidate.isNotEmpty(tempErrorMsgList)) { methodContext.getRequest().setAttribute("_ERROR_MESSAGE_LIST_", tempErrorMsgList); forceError = true; - summaryErrorStringBuffer.append("; "); summaryErrorStringBuffer.append(tempErrorMsgList.toString()); } - String eventMsg = (String) methodContext.getEnv(eventEventMessageName); if (UtilValidate.isNotEmpty(eventMsg)) { methodContext.getRequest().setAttribute("_EVENT_MESSAGE_", eventMsg); @@ -774,11 +639,10 @@ public class SimpleMethod { if (UtilValidate.isNotEmpty(eventMsgList)) { methodContext.getRequest().setAttribute("_EVENT_MESSAGE_LIST_", eventMsgList); } - response = (String) methodContext.getEnv(eventResponseCodeName); if (UtilValidate.isEmpty(response)) { if (forceError) { - //override response code, always use error code + // override response code, always use error code Debug.logInfo("No response code string found, but error messages found so assuming error; returning code [" + defaultErrorCode + "]", module); response = defaultErrorCode; } else { @@ -791,48 +655,39 @@ public class SimpleMethod { returnValue = response; } else if (methodContext.getMethodType() == MethodContext.SERVICE) { boolean forceError = false; - String tempErrorMsg = (String) methodContext.getEnv(serviceErrorMessageName); if (errorMsg.length() > 0 || UtilValidate.isNotEmpty(tempErrorMsg)) { errorMsg += tempErrorMsg; methodContext.putResult(ModelService.ERROR_MESSAGE, errorMsg); forceError = true; - summaryErrorStringBuffer.append(errorMsg); } - List<Object> errorMsgList = UtilGenerics.checkList(methodContext.getEnv(serviceErrorMessageListName)); if (UtilValidate.isNotEmpty(errorMsgList)) { methodContext.putResult(ModelService.ERROR_MESSAGE_LIST, errorMsgList); forceError = true; - summaryErrorStringBuffer.append("; "); summaryErrorStringBuffer.append(errorMsgList.toString()); } - Map<String, Object> errorMsgMap = UtilGenerics.checkMap(methodContext.getEnv(serviceErrorMessageMapName)); if (UtilValidate.isNotEmpty(errorMsgMap)) { methodContext.putResult(ModelService.ERROR_MESSAGE_MAP, errorMsgMap); forceError = true; - summaryErrorStringBuffer.append("; "); summaryErrorStringBuffer.append(errorMsgMap.toString()); } - String successMsg = (String) methodContext.getEnv(serviceSuccessMessageName); if (UtilValidate.isNotEmpty(successMsg)) { methodContext.putResult(ModelService.SUCCESS_MESSAGE, successMsg); } - List<Object> successMsgList = UtilGenerics.checkList(methodContext.getEnv(serviceSuccessMessageListName)); if (UtilValidate.isNotEmpty(successMsgList)) { methodContext.putResult(ModelService.SUCCESS_MESSAGE_LIST, successMsgList); } - response = (String) methodContext.getEnv(serviceResponseMessageName); if (UtilValidate.isEmpty(response)) { if (forceError) { - //override response code, always use error code + // override response code, always use error code Debug.logVerbose("No response code string found, but error messages found so assuming error; returning code [" + defaultErrorCode + "]", module); response = defaultErrorCode; } else { @@ -846,13 +701,11 @@ public class SimpleMethod { response = defaultSuccessCode; returnValue = defaultSuccessCode; } - // decide whether or not to commit based on the response message, ie only rollback if error is returned and not finished boolean doCommit = true; if (!finished && defaultErrorCode.equals(response)) { doCommit = false; } - if (doCommit) { // commit here passing beganTransaction to perform it properly try { @@ -872,49 +725,131 @@ public class SimpleMethod { errorMsg += errMsg + "<br/>"; } } - return returnValue; } - public static void readOperations(Element simpleMethodElement, List<MethodOperation> methodOperations, SimpleMethod simpleMethod) { - List<? extends Element> operationElements = UtilXml.childElementList(simpleMethodElement); + public Set<String> getAllEntityNamesUsed() throws MiniLangException { + Set<String> allEntityNames = FastSet.newInstance(); + Set<String> simpleMethodsVisited = FastSet.newInstance(); + findEntityNamesUsed(this.methodOperations, allEntityNames, simpleMethodsVisited); + return allEntityNames; + } - if (UtilValidate.isNotEmpty(operationElements)) { - for (Element curOperElem: operationElements) { - String nodeName = curOperElem.getNodeName(); - MethodOperation methodOp = null; + public Set<String> getAllServiceNamesCalled() throws MiniLangException { + Set<String> allServiceNames = FastSet.newInstance(); + Set<String> simpleMethodsVisited = FastSet.newInstance(); + findServiceNamesCalled(this.methodOperations, allServiceNames, simpleMethodsVisited); + return allServiceNames; + } - MethodOperation.Factory<MethodOperation> factory = methodOperationFactories.get(nodeName); - if (factory != null) { - methodOp = factory.createMethodOperation(curOperElem, simpleMethod); - } else if ("else".equals(nodeName)) { - // don't add anything, but don't complain either, this one is handled in the individual operations - } else { - Debug.logWarning("Operation element \"" + nodeName + "\" no recognized", module); - } - if (methodOp == null) continue; - methodOperations.add(methodOp); - DeprecatedOperation depOp = methodOp.getClass().getAnnotation(DeprecatedOperation.class); - if (depOp != null) Debug.logInfo("The " + nodeName + " operation has been deprecated in favor of the " + depOp.value() + " operation; found use of this in [" + simpleMethod.getShortDescription() + "]: " + methodOp.rawString(), module); - } - } + public String getDefaultErrorCode() { + return this.defaultErrorCode; } - /** Execs the given operations returning true if all return true, or returning - * false and stopping if any return false. - */ - public static boolean runSubOps(List<MethodOperation> methodOperations, MethodContext methodContext) { - for (MethodOperation methodOperation: methodOperations) { - try { - if (!methodOperation.exec(methodContext)) { - return false; - } - } catch (Throwable t) { - String errMsg = "Error in simple-method operation [" + methodOperation.rawString() + "]: " + t.toString(); - Debug.logError(t, errMsg, module); - throw new RuntimeException(errMsg); - } - } - return true; + public String getDefaultSuccessCode() { + return this.defaultSuccessCode; + } + + public String getDelegatorEnvName() { + return this.delegatorName; + } + + public String getDispatcherEnvName() { + return this.dispatcherName; + } + + public String getEventErrorMessageListName() { + return this.eventErrorMessageListName; + } + + public String getEventErrorMessageName() { + return this.eventErrorMessageName; + } + + public String getEventEventMessageListName() { + return this.eventEventMessageListName; + } + + public String getEventEventMessageName() { + return this.eventEventMessageName; + } + + // event fields + public String getEventRequestName() { + return this.eventRequestName; + } + + public String getEventResponseCodeName() { + return this.eventResponseCodeName; + } + + public String getEventSessionName() { + return this.eventSessionName; + } + + public String getFromLocation() { + return this.fromLocation; + } + + public String getLocationAndName() { + return this.fromLocation + "#" + this.methodName; + } + + public boolean getLoginRequired() { + return this.loginRequired; + } + + public String getMethodName() { + return this.methodName; + } + + public String getParameterMapName() { + return this.parameterMapName; + } + + public String getSecurityEnvName() { + return this.securityName; + } + + public String getServiceErrorMessageListName() { + return this.serviceErrorMessageListName; + } + + public String getServiceErrorMessageMapName() { + return this.serviceErrorMessageMapName; + } + + public String getServiceErrorMessageName() { + return this.serviceErrorMessageName; + } + + public String getServiceResponseMessageName() { + return this.serviceResponseMessageName; + } + + public String getServiceSuccessMessageListName() { + return this.serviceSuccessMessageListName; + } + + public String getServiceSuccessMessageName() { + return this.serviceSuccessMessageName; + } + + public String getShortDescription() { + return this.shortDescription + " [" + this.fromLocation + "#" + this.methodName + "]"; + } + + public SimpleMethod getSimpleMethodInSameFile(String simpleMethodName) { + if (parentSimpleMethodsMap == null) + return null; + return parentSimpleMethodsMap.get(simpleMethodName); + } + + public String getUserLoginEnvName() { + return this.userLoginName; + } + + public boolean getUseTransaction() { + return this.useTransaction; } } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethodBsfEngine.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethodBsfEngine.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethodBsfEngine.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethodBsfEngine.java Thu Apr 19 15:09:03 2012 @@ -24,20 +24,20 @@ import java.util.Vector; import javolution.util.FastMap; -import org.ofbiz.base.util.Debug; -import org.ofbiz.minilang.method.MethodContext; - import org.apache.bsf.BSFDeclaredBean; import org.apache.bsf.BSFException; import org.apache.bsf.BSFManager; import org.apache.bsf.util.BSFEngineImpl; +import org.ofbiz.base.util.Debug; +import org.ofbiz.minilang.method.MethodContext; /** - * <P>This is the OFBiz MiniLang SimpleMethod adapter for IBM's Bean Scripting Famework. - * It is an implementation of the BSFEngine class, allowing BSF aware - * applications to use SimpleMethod as a scripting language. - * - * <P>There should only be ONE simple-method in the XML file and it will be run as an event. + * <P> + * This is the OFBiz MiniLang SimpleMethod adapter for IBM's Bean Scripting Famework. It is an implementation of the BSFEngine class, allowing BSF aware applications to use SimpleMethod as a scripting + * language. + * + * <P> + * There should only be ONE simple-method in the XML file and it will be run as an event. */ public class SimpleMethodBsfEngine extends BSFEngineImpl { @@ -45,53 +45,36 @@ public class SimpleMethodBsfEngine exten protected Map<String, Object> context = FastMap.newInstance(); + /** + * This is an implementation of the apply() method. It executes the funcBody text in an "anonymous" method call with arguments. + */ @SuppressWarnings("unchecked") @Override - public void initialize(BSFManager mgr, String lang, Vector declaredBeans) throws BSFException { - super.initialize(mgr, lang, declaredBeans); - - // declare the bsf manager for callbacks, etc. - context.put("bsf", mgr); - - for(int i=0; i<declaredBeans.size(); i++) { - BSFDeclaredBean bean = (BSFDeclaredBean)declaredBeans.get(i); - declareBean(bean); - } - } - - public void setDebug(boolean debug) { - //interpreter.DEBUG=debug; + public Object apply(String source, int lineNo, int columnNo, Object funcBody, Vector namesVec, Vector argsVec) throws BSFException { + // if (namesVec.size() != argsVec.size()) throw new BSFException("number of params/names mismatch"); + // if (!(funcBody instanceof String)) throw new BSFException("apply: function body must be a string"); + throw new BSFException("The apply method is not yet supported for simple-methods"); } /** - * Invoke method name on the specified scripted object. - * The object may be null to indicate the global namespace of the - * interpreter. - * @param object may be null for the global namespace. + * Invoke method name on the specified scripted object. The object may be null to indicate the global namespace of the interpreter. + * + * @param object + * may be null for the global namespace. */ public Object call(Object object, String name, Object[] args) throws BSFException { throw new BSFException("The call method is not yet supported for SimpleMethods"); } - - /** - * This is an implementation of the apply() method. - * It exectutes the funcBody text in an "anonymous" method call with - * arguments. - */ - @SuppressWarnings("unchecked") @Override - public Object apply(String source, int lineNo, int columnNo, Object funcBody, Vector namesVec, Vector argsVec) throws BSFException { - //if (namesVec.size() != argsVec.size()) throw new BSFException("number of params/names mismatch"); - //if (!(funcBody instanceof String)) throw new BSFException("apply: function body must be a string"); - - throw new BSFException("The apply method is not yet supported for simple-methods"); + public void declareBean(BSFDeclaredBean bean) throws BSFException { + context.put(bean.name, bean.bean); } public Object eval(String source, int lineNo, int columnNo, Object expr) throws BSFException { - if (!(expr instanceof String)) throw new BSFException("simple-method expression must be a string"); - - //right now only supports one method per file, so get all methods and just run the first... + if (!(expr instanceof String)) + throw new BSFException("simple-method expression must be a string"); + // right now only supports one method per file, so get all methods and just run the first... Map<String, SimpleMethod> simpleMethods = null; try { simpleMethods = SimpleMethod.getDirectSimpleMethods(source, (String) expr, "<bsf source>"); @@ -99,46 +82,52 @@ public class SimpleMethodBsfEngine exten throw new BSFException("Error loading/parsing simple-method XML source: " + e.getMessage()); } Set<String> smNames = simpleMethods.keySet(); - if (smNames.size() == 0) throw new BSFException("Did not find any simple-methods in the file"); - + if (smNames.size() == 0) + throw new BSFException("Did not find any simple-methods in the file"); String methodName = smNames.iterator().next(); - if (smNames.size() > 1) Debug.logWarning("Found more than one simple-method in the file, running the [" + methodName + "] method, you should remove all but one method from this file", module); - + if (smNames.size() > 1) + Debug.logWarning("Found more than one simple-method in the file, running the [" + methodName + "] method, you should remove all but one method from this file", module); SimpleMethod simpleMethod = simpleMethods.get(methodName); MethodContext methodContext = new MethodContext(context, null, MethodContext.EVENT); return simpleMethod.exec(methodContext); - //methodContext.getResults(); + // methodContext.getResults(); } - @Override public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException { eval(source, lineNo, columnNo, script); } + @SuppressWarnings("unchecked") + @Override + public void initialize(BSFManager mgr, String lang, Vector declaredBeans) throws BSFException { + super.initialize(mgr, lang, declaredBeans); + // declare the bsf manager for callbacks, etc. + context.put("bsf", mgr); + for (int i = 0; i < declaredBeans.size(); i++) { + BSFDeclaredBean bean = (BSFDeclaredBean) declaredBeans.get(i); + declareBean(bean); + } + } -/* - public void compileApply (String source, int lineNo, int columnNo, - Object funcBody, Vector paramNames, Vector arguments, CodeBuffer cb) - throws BSFException; - - public void compileExpr (String source, int lineNo, int columnNo, - Object expr, CodeBuffer cb) throws BSFException; + /* + * public void compileApply (String source, int lineNo, int columnNo, Object funcBody, Vector paramNames, Vector arguments, CodeBuffer cb) throws BSFException; + * + * public void compileExpr (String source, int lineNo, int columnNo, Object expr, CodeBuffer cb) throws BSFException; + * + * public void compileScript (String source, int lineNo, int columnNo, Object script, CodeBuffer cb) throws BSFException; + */ - public void compileScript (String source, int lineNo, int columnNo, - Object script, CodeBuffer cb) throws BSFException; - */ + public void setDebug(boolean debug) { + // interpreter.DEBUG=debug; + } @Override - public void declareBean(BSFDeclaredBean bean) throws BSFException { - context.put(bean.name, bean.bean); + public void terminate() { } @Override public void undeclareBean(BSFDeclaredBean bean) throws BSFException { context.remove(bean.name); } - - @Override - public void terminate() { } } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleServiceEngine.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleServiceEngine.java?rev=1327981&r1=1327980&r2=1327981&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleServiceEngine.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleServiceEngine.java Thu Apr 19 15:09:03 2012 @@ -36,16 +36,11 @@ public final class SimpleServiceEngine e super(dispatcher); } - /** Run the service synchronously and IGNORE the result - * @param context Map of name, value pairs composing the context - */ - @Override - public void runSyncIgnore(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException { - runSync(localName, modelService, context); - } - - /** Run the service synchronously and return the result - * @param context Map of name, value pairs composing the context + /** + * Run the service synchronously and return the result + * + * @param context + * Map of name, value pairs composing the context * @return Map of name, value pairs composing the result */ @Override @@ -56,29 +51,34 @@ public final class SimpleServiceEngine e return result; } + /** + * Run the service synchronously and IGNORE the result + * + * @param context + * Map of name, value pairs composing the context + */ + @Override + public void runSyncIgnore(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException { + runSync(localName, modelService, context); + } + // Invoke the simple method from a service context private Map<String, Object> serviceInvoker(String localName, ModelService modelService, Map<String, ? extends Object> context) throws GenericServiceException { // static java service methods should be: public Map methodName(DispatchContext dctx, Map context) DispatchContext dctx = dispatcher.getLocalContext(localName); - // check the package and method names if (modelService.location == null || modelService.invoke == null) throw new GenericServiceException("Cannot locate service to invoke (location or invoke name missing)"); - // get the classloader to use ClassLoader classLoader = null; - if (dctx != null) classLoader = dctx.getClassLoader(); - // if the classLoader is null, no big deal, SimpleMethod will use the // current thread's ClassLoader by default if null passed in - try { return SimpleMethod.runSimpleService(this.getLocation(modelService), modelService.invoke, dctx, context, classLoader); } catch (MiniLangException e) { - throw new GenericServiceException("Error running simple method [" + modelService.invoke + - "] in XML file [" + modelService.location + "]: ", e); + throw new GenericServiceException("Error running simple method [" + modelService.invoke + "] in XML file [" + modelService.location + "]: ", e); } } } |
| Free forum by Nabble | Edit this page |
