svn commit: r660228 - in /ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util: Debug.java GroovyUtil.java UtilURL.java

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

svn commit: r660228 - in /ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util: Debug.java GroovyUtil.java UtilURL.java

jonesde
Author: jonesde
Date: Mon May 26 09:52:54 2008
New Revision: 660228

URL: http://svn.apache.org/viewvc?rev=660228&view=rev
Log:
Fixed issue with groovy where if Script was cached the context was ignored; note that I could not find a thread safe way to run a parsed script, so this versio only caches the script text; need to find a way to parse just the script and run from the cache; the performance in this isn't terrible, but isn't good either

Modified:
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/Debug.java
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilURL.java

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/Debug.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/Debug.java?rev=660228&r1=660227&r2=660228&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/Debug.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/Debug.java Mon May 26 09:52:54 2008
@@ -58,7 +58,7 @@
 
     public static final String[] levels = {"Always", "Verbose", "Timing", "Info", "Important", "Warning", "Error", "Fatal", "Notify"};
     public static final String[] levelProps = {"", "print.verbose", "print.timing", "print.info", "print.important", "print.warning", "print.error", "print.fatal", "print.notify"};
-    public static final Level[] levelObjs = {Level.INFO, Level.DEBUG, Level.DEBUG, Level.INFO, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL, NotifyLevel.NOTIFY};
+    public static final Level[] levelObjs = {Level.INFO, Level.DEBUG, Level.INFO, Level.INFO, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL, NotifyLevel.NOTIFY};
 
     protected static Map<String, Integer> levelStringMap = new HashMap<String, Integer>();
     

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java?rev=660228&r1=660227&r2=660228&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java Mon May 26 09:52:54 2008
@@ -1,6 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
 package org.ofbiz.base.util;
 
-
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -15,24 +32,23 @@
 import groovy.lang.Script;
 import org.codehaus.groovy.control.CompilationFailedException;
 
-
 /**
  * GroovyUtil - Groovy Utilities
  *
- * @version $Rev$
  */
 public class GroovyUtil {
 
     public static final String module = GroovyUtil.class.getName();
 
-    public static UtilCache<String, Script> parsedScripts = new UtilCache<String, Script>("script.GroovyLocationParsedCache", 0, 0, false);
-
-
-    private static GroovyShell getShell(Map context) {
+    //public static UtilCache<String, Script> parsedScripts = new UtilCache<String, Script>("script.GroovyLocationParsedCache", 0, 0, false);
+    public static UtilCache<String, String> sourceScripts = new UtilCache<String, String>("script.GroovyLocationSourceCache", 0, 0, false);
+    
+    public static GroovyShell emptyGroovyShell = new GroovyShell();
 
+    private static Binding getBinding(Map<String, Object> context) {
         Binding binding = new Binding();
         if (context != null) {
-            Set keySet = context.keySet();
+            Set<String> keySet = context.keySet();
             for (Object key : keySet) {
                 binding.setVariable((String) key, context.get(key));
             }
@@ -41,32 +57,51 @@
             binding.setVariable("context", context);
         }
 
-        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-        return new GroovyShell(classLoader, binding);
+        return binding;
     }
 
-    public static Object runScriptAtLocation(String location, Map context) throws GeneralException {
-
+    public static Object runScriptAtLocation(String location, Map<String, Object> context) throws GeneralException {
         try {
-
+         /* NOTE DEJ20080526: this approach uses a pre-parsed script but it is not thread safe
+         *
+         * the groovy Script object contains both the parsed script AND the context, which is weird when trying to run a cached Script
+         * there is no available clone() method on the Script object, so we can't clone and set the context/binding to get around thread-safe issues
             Script script = parsedScripts.get(location);
             if (script == null) {
-
                 URL scriptUrl = FlexibleLocation.resolveLocation(location);
                 if (scriptUrl == null) {
                     throw new GeneralException("Script not found at location [" + location + "]");
                 }
                 
-                GroovyShell shell = getShell(context);
-                script = shell.parse(scriptUrl.openStream(), scriptUrl.getFile());
-                if (Debug.verboseOn()) {
-                    Debug.logVerbose("Caching Groovy script at: " + location, module);
-                }
+                script = emptyGroovyShell.parse(scriptUrl.openStream(), scriptUrl.getFile());
+                if (Debug.verboseOn()) Debug.logVerbose("Caching Groovy script at: " + location, module);
                 parsedScripts.put(location, script);
             }
             
+            script.setBinding(getBinding(context));
             return script.run();
+            */
 
+            String scriptString = sourceScripts.get(location);
+            if (scriptString == null) {
+                URL scriptUrl = FlexibleLocation.resolveLocation(location);
+                if (scriptUrl == null) {
+                    throw new GeneralException("Script not found at location [" + location + "]");
+                }
+                
+                scriptString = UtilURL.readUrlText(scriptUrl);
+                
+                if (Debug.verboseOn()) Debug.logVerbose("Caching Groovy script source at: " + location, module);
+                sourceScripts.put(location, scriptString);
+            }
+
+            long startTime = System.currentTimeMillis();
+            Script script = emptyGroovyShell.parse(scriptString, location);
+            script.setBinding(getBinding(context));
+            Object scriptResult = script.run();
+            if (Debug.timingOn()) Debug.logTiming("Parsed and ran groovy script in [" + (System.currentTimeMillis() - startTime) + "]ms at: " + location, module);
+            
+            return scriptResult;
         } catch (MalformedURLException e) {
             String errMsg = "Error loading Groovy script at [" + location + "]: " + e.toString();
             throw new GeneralException(errMsg, e);
@@ -78,5 +113,4 @@
             throw new GeneralException(errMsg, e);
         }
     }
-
 }

Modified: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilURL.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilURL.java?rev=660228&r1=660227&r2=660228&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilURL.java (original)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/UtilURL.java Mon May 26 09:52:54 2008
@@ -18,7 +18,12 @@
  *******************************************************************************/
 package org.ofbiz.base.util;
 
+import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.net.MalformedURLException;
 import java.net.URL;
 
@@ -127,4 +132,33 @@
         }
         return path;
     }
+    
+    public static String readUrlText(URL url) throws IOException {
+     InputStream stream = url.openStream();
+    
+        StringBuffer buf = new StringBuffer();
+        BufferedReader in = null;
+        try {
+            in = new BufferedReader(new InputStreamReader(stream));
+
+            String str;
+            while ((str = in.readLine()) != null) {
+                buf.append(str);
+                buf.append(System.getProperty("line.separator"));
+            }
+        } catch (IOException e) {
+            Debug.logError(e, "Error reading text from URL [" + url + "]: " + e.toString(), module);
+            throw e;
+        } finally {
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e) {
+                    Debug.logError(e, "Error closing after reading text from URL [" + url + "]: " + e.toString(), module);
+                }
+            }
+        }
+        
+        return buf.toString();
+    }
 }