svn commit: r1334427 - in /ofbiz/trunk/framework: minilang/src/org/ofbiz/minilang/ minilang/src/org/ofbiz/minilang/method/callops/ webapp/src/org/ofbiz/webapp/event/ webtools/src/org/ofbiz/webtools/artifactinfo/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1334427 - in /ofbiz/trunk/framework: minilang/src/org/ofbiz/minilang/ minilang/src/org/ofbiz/minilang/method/callops/ webapp/src/org/ofbiz/webapp/event/ webtools/src/org/ofbiz/webtools/artifactinfo/

adrianc
Author: adrianc
Date: Sat May  5 15:05:40 2012
New Revision: 1334427

URL: http://svn.apache.org/viewvc?rev=1334427&view=rev
Log:
More work on the Mini-language <simple-method> element: Simplified script caching. This prepares the way to have simple methods cached individually, instead of caching the entire file.

Modified:
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java
    ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallSimpleMethod.java
    ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/WfsEventHandler.java
    ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ServiceArtifactInfo.java

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=1334427&r1=1334426&r2=1334427&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java Sat May  5 15:05:40 2012
@@ -69,7 +69,6 @@ public final class SimpleMethod {
     private static final Map<String, MethodOperation.Factory<MethodOperation>> methodOperationFactories;
     private static final UtilCache<String, Map<String, SimpleMethod>> simpleMethodsDirectCache = UtilCache.createUtilCache("minilang.SimpleMethodsDirect", 0, 0);
     private static final UtilCache<String, Map<String, SimpleMethod>> simpleMethodsResourceCache = UtilCache.createUtilCache("minilang.SimpleMethodsResource", 0, 0);
-    private static final 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>>();
@@ -95,7 +94,7 @@ public final class SimpleMethod {
 
     private static void compileAllSimpleMethods(Element rootElement, Map<String, SimpleMethod> simpleMethods, String location) throws MiniLangException {
         for (Element simpleMethodElement : UtilXml.childElementList(rootElement, "simple-method")) {
-            SimpleMethod simpleMethod = compileSimpleMethod(simpleMethodElement, simpleMethods, location);
+            SimpleMethod simpleMethod = new SimpleMethod(simpleMethodElement, location);
             if (simpleMethods.containsKey(simpleMethod.getMethodName())) {
                 MiniLangValidate.handleError("Duplicate method name found", simpleMethod, simpleMethodElement);
             }
@@ -103,10 +102,6 @@ public final class SimpleMethod {
         }
     }
 
-    private static SimpleMethod compileSimpleMethod(Element simpleMethodElement, Map<String, SimpleMethod> simpleMethods, String location) throws MiniLangException {
-        return new SimpleMethod(simpleMethodElement, simpleMethods, location);
-    }
-
     private static Map<String, SimpleMethod> getAllDirectSimpleMethods(String name, String content, String fromLocation) throws MiniLangException {
         if (UtilValidate.isEmpty(fromLocation)) {
             fromLocation = "<location not known>";
@@ -148,33 +143,40 @@ public final class SimpleMethod {
         return simpleMethods;
     }
 
-    public static Map<String, SimpleMethod> getSimpleMethods(String xmlResource, ClassLoader loader) throws MiniLangException {
+    public static SimpleMethod getSimpleMethod(String xmlResource, String methodName, ClassLoader loader) throws MiniLangException {
+        Assert.notNull("methodName", methodName);
+        Map<String, SimpleMethod> simpleMethods = getSimpleMethods(xmlResource, loader);
+        return simpleMethods.get(methodName);
+    }
+
+    public static SimpleMethod getSimpleMethod(URL xmlUrl, String methodName) throws MiniLangException {
+        Assert.notNull("methodName", methodName);
+        Map<String, SimpleMethod> simpleMethods = getSimpleMethods(xmlUrl);
+        return simpleMethods.get(methodName);
+    }
+
+    private static Map<String, SimpleMethod> getSimpleMethods(String xmlResource, ClassLoader loader) throws MiniLangException {
         Assert.notNull("xmlResource", xmlResource);
-        Map<String, SimpleMethod> simpleMethods = simpleMethodsResourceCache.get(xmlResource);
-        if (simpleMethods == null) {
-            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);
-            simpleMethodsResourceCache.putIfAbsent(xmlResource, simpleMethods);
-            simpleMethods = simpleMethodsResourceCache.get(xmlResource);
+        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);
         }
-        return simpleMethods;
+        if (xmlURL == null) {
+            throw new MiniLangException("Could not find SimpleMethod XML document in resource: " + xmlResource);
+        }
+        return getSimpleMethods(xmlURL);
     }
 
-    public static Map<String, SimpleMethod> getSimpleMethods(URL xmlURL) throws MiniLangException {
+    private static Map<String, SimpleMethod> getSimpleMethods(URL xmlURL) throws MiniLangException {
         Assert.notNull("xmlURL", xmlURL);
-        Map<String, SimpleMethod> simpleMethods = simpleMethodsURLCache.get(xmlURL);
+        String cacheKey = xmlURL.toString();
+        Map<String, SimpleMethod> simpleMethods = simpleMethodsResourceCache.get(cacheKey);
         if (simpleMethods == null) {
             simpleMethods = getAllSimpleMethods(xmlURL);
-            simpleMethodsURLCache.putIfAbsent(xmlURL, simpleMethods);
-            simpleMethods = simpleMethodsURLCache.get(xmlURL);
+            simpleMethodsResourceCache.putIfAbsent(cacheKey, simpleMethods);
+            simpleMethods = simpleMethodsResourceCache.get(cacheKey);
         }
         return simpleMethods;
     }
@@ -183,7 +185,7 @@ public final class SimpleMethod {
         Assert.notNull("xmlResource", xmlResource);
         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);
+        Map<String, SimpleMethod> simpleMethodMap = getSimpleMethods(xmlResource, loader);
         // Load and traverse the document again to get a correctly ordered list of methods
         URL xmlURL = null;
         try {
@@ -247,9 +249,8 @@ public final class SimpleMethod {
     }
 
     public static String runSimpleMethod(String xmlResource, String methodName, MethodContext methodContext) throws MiniLangException {
-        Assert.notNull("methodName", methodName, "methodContext", methodContext);
-        Map<String, SimpleMethod> simpleMethods = getSimpleMethods(xmlResource, methodContext.getLoader());
-        SimpleMethod simpleMethod = simpleMethods.get(methodName);
+        Assert.notNull("methodContext", methodContext);
+        SimpleMethod simpleMethod = getSimpleMethod(xmlResource, methodName, methodContext.getLoader());
         if (simpleMethod == null) {
             throw new MiniLangException("Could not find SimpleMethod " + methodName + " in XML document in resource: " + xmlResource);
         }
@@ -257,9 +258,7 @@ public final class SimpleMethod {
     }
 
     public static String runSimpleMethod(URL xmlURL, String methodName, MethodContext methodContext) throws MiniLangException {
-        Assert.notNull("methodName", methodName, "methodContext", methodContext);
-        Map<String, SimpleMethod> simpleMethods = getSimpleMethods(xmlURL);
-        SimpleMethod simpleMethod = simpleMethods.get(methodName);
+        SimpleMethod simpleMethod = getSimpleMethod(xmlURL, methodName);
         if (simpleMethod == null) {
             throw new MiniLangException("Could not find SimpleMethod " + methodName + " in XML document from URL: " + xmlURL.toString());
         }
@@ -312,7 +311,6 @@ public final class SimpleMethod {
     private final boolean loginRequired;
     private final String methodName;
     private final List<MethodOperation> methodOperations;
-    private final Map<String, SimpleMethod> parentSimpleMethodsMap;
     private final String serviceErrorMessageListName;
     private final String serviceErrorMessageMapName;
     private final String serviceErrorMessageName;
@@ -322,7 +320,7 @@ public final class SimpleMethod {
     private final String shortDescription;
     private final boolean useTransaction;
 
-    public SimpleMethod(Element simpleMethodElement, Map<String, SimpleMethod> parentSimpleMethodsMap, String fromLocation) throws MiniLangException {
+    public SimpleMethod(Element simpleMethodElement, String fromLocation) throws MiniLangException {
         if (MiniLangValidate.validationOn()) {
             String locationMsg = " File = ".concat(fromLocation);
             if (simpleMethodElement.getAttribute("method-name").isEmpty()) {
@@ -330,7 +328,7 @@ public final class SimpleMethod {
             }
             for (int i = 0; i < DEPRECATED_ATTRIBUTES.length; i++) {
                 if (!simpleMethodElement.getAttribute(DEPRECATED_ATTRIBUTES[i]).isEmpty()) {
-                    MiniLangValidate.handleError("Attribute \"" + DEPRECATED_ATTRIBUTES[i] + "\" is deprecated (no replacement)", null, simpleMethodElement);
+                    MiniLangValidate.handleError("Attribute \"" + DEPRECATED_ATTRIBUTES[i] + "\" is deprecated (no replacement)." + locationMsg, null, simpleMethodElement);
                 }
             }
         }
@@ -338,7 +336,6 @@ public final class SimpleMethod {
         if (elementModified && MiniLangUtil.autoCorrectOn()) {
             MiniLangUtil.flagDocumentAsCorrected(simpleMethodElement);
         }
-        this.parentSimpleMethodsMap = parentSimpleMethodsMap;
         this.fromLocation = fromLocation;
         methodName = simpleMethodElement.getAttribute("method-name");
         shortDescription = simpleMethodElement.getAttribute("short-description");
@@ -663,12 +660,6 @@ public final class SimpleMethod {
         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 "userLogin";
     }

Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallSimpleMethod.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallSimpleMethod.java?rev=1334427&r1=1334426&r2=1334427&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallSimpleMethod.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallSimpleMethod.java Sat May  5 15:05:40 2012
@@ -18,12 +18,15 @@
  *******************************************************************************/
 package org.ofbiz.minilang.method.callops;
 
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
 import javolution.util.FastMap;
 
+import org.ofbiz.base.location.FlexibleLocation;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.UtilValidate;
 import org.ofbiz.base.util.UtilXml;
@@ -47,6 +50,7 @@ public final class CallSimpleMethod exte
 
     private final String methodName;
     private final String xmlResource;
+    private final URL xmlURL;
     private final String scope;
     private final List<ResultToField> resultToFieldList;
 
@@ -59,7 +63,18 @@ public final class CallSimpleMethod exte
             MiniLangValidate.childElements(simpleMethod, element, "result-to-field");
         }
         this.methodName = element.getAttribute("method-name");
-        this.xmlResource = element.getAttribute("xml-resource");
+        String xmlResourceAttribute = element.getAttribute("xml-resource");
+        if (xmlResourceAttribute.isEmpty()) {
+            xmlResourceAttribute = simpleMethod.getFromLocation();
+        }
+        this.xmlResource = xmlResourceAttribute;
+        URL xmlURL = null;
+        try {
+            xmlURL = FlexibleLocation.resolveLocation(this.xmlResource);
+        } catch (MalformedURLException e) {
+            MiniLangValidate.handleError("Could not find SimpleMethod XML document in resource: " + this.xmlResource + "; error was: " + e.toString(), simpleMethod, element);
+        }
+        this.xmlURL = xmlURL;
         this.scope = element.getAttribute("scope");
         List<? extends Element> resultToFieldElements = UtilXml.childElementList(element, "result-to-field");
         if (UtilValidate.isNotEmpty(resultToFieldElements)) {
@@ -81,13 +96,7 @@ public final class CallSimpleMethod exte
         if (UtilValidate.isEmpty(this.methodName)) {
             throw new MiniLangRuntimeException("method-name attribute is empty", this);
         }
-        SimpleMethod simpleMethodToCall = null;
-        if (UtilValidate.isEmpty(this.xmlResource)) {
-            simpleMethodToCall = this.simpleMethod.getSimpleMethodInSameFile(methodName);
-        } else {
-            Map<String, SimpleMethod> simpleMethods = SimpleMethod.getSimpleMethods(this.xmlResource, methodContext.getLoader());
-            simpleMethodToCall = simpleMethods.get(this.methodName);
-        }
+        SimpleMethod simpleMethodToCall = SimpleMethod.getSimpleMethod(this.xmlURL, this.methodName);
         if (simpleMethodToCall == null) {
             throw new MiniLangRuntimeException("Could not find <simple-method name=\"" + this.methodName + "\"> in XML document " + this.xmlResource, this);
         }
@@ -149,14 +158,7 @@ public final class CallSimpleMethod exte
     }
 
     public SimpleMethod getSimpleMethodToCall(ClassLoader loader) throws MiniLangException {
-        SimpleMethod simpleMethodToCall = null;
-        if (UtilValidate.isEmpty(xmlResource)) {
-            simpleMethodToCall = this.simpleMethod.getSimpleMethodInSameFile(methodName);
-        } else {
-            Map<String, SimpleMethod> simpleMethods = SimpleMethod.getSimpleMethods(xmlResource, loader);
-            simpleMethodToCall = simpleMethods.get(methodName);
-        }
-        return simpleMethodToCall;
+        return SimpleMethod.getSimpleMethod(xmlResource, methodName, loader);
     }
 
     public String getXmlResource() {

Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/WfsEventHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/WfsEventHandler.java?rev=1334427&r1=1334426&r2=1334427&view=diff
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/WfsEventHandler.java (original)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/WfsEventHandler.java Sat May  5 15:05:40 2012
@@ -109,7 +109,7 @@ public class WfsEventHandler implements
             // run simple method script to get a list of entities
             Document simpleDoc = UtilXml.readXmlDocument(xmlScript);
             Element simpleElem = simpleDoc.getDocumentElement();
-            SimpleMethod meth = new SimpleMethod(simpleElem, null, null);
+            SimpleMethod meth = new SimpleMethod(simpleElem, null);
             MethodContext methodContext = new MethodContext(request, response, null);
             meth.exec(methodContext); //Need to check return string
             List<GenericValue> entityList = UtilGenerics.cast(request.getAttribute("entityList"));

Modified: ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ServiceArtifactInfo.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ServiceArtifactInfo.java?rev=1334427&r1=1334426&r2=1334427&view=diff
==============================================================================
--- ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ServiceArtifactInfo.java (original)
+++ ofbiz/trunk/framework/webtools/src/org/ofbiz/webtools/artifactinfo/ServiceArtifactInfo.java Sat May  5 15:05:40 2012
@@ -84,8 +84,7 @@ public class ServiceArtifactInfo extends
             // we can do something with this!
             SimpleMethod simpleMethodToCall = null;
             try {
-                Map<String, SimpleMethod> simpleMethods = SimpleMethod.getSimpleMethods(this.modelService.location, null);
-                simpleMethodToCall = simpleMethods.get(this.modelService.invoke);
+                simpleMethodToCall = SimpleMethod.getSimpleMethod(this.modelService.location, this.modelService.invoke,null);
             } catch (MiniLangException e) {
                 Debug.logWarning("Error getting Simple-method [" + this.modelService.invoke + "] in [" + this.modelService.location + "] referenced in service [" + this.modelService.name + "]: " + e.toString(), module);
             }
@@ -145,8 +144,7 @@ public class ServiceArtifactInfo extends
             // we can do something with this!
             SimpleMethod simpleMethodToCall = null;
             try {
-                Map<String, SimpleMethod> simpleMethods = SimpleMethod.getSimpleMethods(this.modelService.location, null);
-                simpleMethodToCall = simpleMethods.get(this.modelService.invoke);
+                simpleMethodToCall = SimpleMethod.getSimpleMethod(this.modelService.location, this.modelService.invoke,null);
             } catch (MiniLangException e) {
                 Debug.logWarning("Error getting Simple-method [" + this.modelService.invoke + "] in [" + this.modelService.location + "] referenced in service [" + this.modelService.name + "]: " + e.toString(), module);
             }