You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ja...@apache.org on 2014/11/03 14:15:30 UTC

svn commit: r1636346 - in /ofbiz/trunk/framework: base/config/ base/src/org/ofbiz/base/util/ service/config/ service/src/org/ofbiz/service/engine/ webapp/src/org/ofbiz/webapp/event/

Author: jacopoc
Date: Mon Nov  3 13:15:29 2014
New Revision: 1636346

URL: http://svn.apache.org/r1636346
Log:
Cleaned up and inproved the code that integrates Groovy in the OFBiz framework:
* Groovy services and Groovy events now use the same util methods from GroovyUtil to parse/execute the scripts
* the script base class is now specified in the base/config/groovy.parameter file rather than in the serviceengine.xml
* the same util methods (DSL) are now available to all Groovy scripts (executed as services, events or widget actions)
* removed a series of unused methods in the GroovyUtil class


Added:
    ofbiz/trunk/framework/base/config/groovy.properties   (with props)
Modified:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java
    ofbiz/trunk/framework/service/config/serviceengine.xml
    ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java
    ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/GroovyEventHandler.java

Added: ofbiz/trunk/framework/base/config/groovy.properties
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/config/groovy.properties?rev=1636346&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/config/groovy.properties (added)
+++ ofbiz/trunk/framework/base/config/groovy.properties Mon Nov  3 13:15:29 2014
@@ -0,0 +1,21 @@
+###############################################################################
+# 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.
+###############################################################################
+
+# All the Groovy scripts in OFBiz will extend this class, making the DSL methods defined in it available to client code
+scriptBaseClass=org.ofbiz.service.engine.GroovyBaseScript
\ No newline at end of file

Propchange: ofbiz/trunk/framework/base/config/groovy.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/base/config/groovy.properties
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: ofbiz/trunk/framework/base/config/groovy.properties
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java?rev=1636346&r1=1636345&r2=1636346&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java Mon Nov  3 13:15:29 2014
@@ -32,6 +32,7 @@ import java.util.Map;
 import javax.script.ScriptContext;
 
 import org.codehaus.groovy.control.CompilationFailedException;
+import org.codehaus.groovy.control.CompilerConfiguration;
 import org.codehaus.groovy.runtime.InvokerHelper;
 import org.ofbiz.base.location.FlexibleLocation;
 import org.ofbiz.base.util.cache.UtilCache;
@@ -46,6 +47,18 @@ public class GroovyUtil {
 
     private static final UtilCache<String, Class<?>> parsedScripts = UtilCache.createUtilCache("script.GroovyLocationParsedCache", 0, 0, false);
 
+    private static final GroovyClassLoader groovyScriptClassLoader;
+    static {
+        GroovyClassLoader groovyClassLoader = null;
+        String scriptBaseClass = UtilProperties.getPropertyValue("groovy.properties", "scriptBaseClass");
+        if (!scriptBaseClass.isEmpty()) {
+            CompilerConfiguration conf = new CompilerConfiguration();
+            conf.setScriptBaseClass(scriptBaseClass);
+            groovyClassLoader = new GroovyClassLoader(GroovyUtil.class.getClassLoader(), conf);
+        }
+        groovyScriptClassLoader = groovyClassLoader;
+    }
+
     /**
      * Evaluate a Groovy condition or expression
      * @param expression The expression to evaluate
@@ -110,9 +123,6 @@ public class GroovyUtil {
     }
 
     public static Class<?> getScriptClassFromLocation(String location) throws GeneralException {
-        return getScriptClassFromLocation(location, null);
-    }
-    public static Class<?> getScriptClassFromLocation(String location, GroovyClassLoader groovyClassLoader) throws GeneralException {
         try {
             Class<?> scriptClass = parsedScripts.get(location);
             if (scriptClass == null) {
@@ -120,8 +130,8 @@ public class GroovyUtil {
                 if (scriptUrl == null) {
                     throw new GeneralException("Script not found at location [" + location + "]");
                 }
-                if (groovyClassLoader != null) {
-                    scriptClass = parseClass(scriptUrl.openStream(), location, groovyClassLoader);
+                if (groovyScriptClassLoader != null) {
+                    scriptClass = parseClass(scriptUrl.openStream(), location, groovyScriptClassLoader);
                 } else {
                     scriptClass = parseClass(scriptUrl.openStream(), location);
                 }
@@ -156,16 +166,8 @@ public class GroovyUtil {
         return new GroovyClassLoader().parseClass(text);
     }
 
-    public static Class<?> parseClass(String text, String location) {
-        return new GroovyClassLoader().parseClass(text, location);
-    }
-
     public static Object runScriptAtLocation(String location, String methodName, Map<String, Object> context) throws GeneralException {
-        return runScriptAtLocation(location, methodName, context, null);
-    }
-
-    public static Object runScriptAtLocation(String location, String methodName, Map<String, Object> context, GroovyClassLoader groovyClassLoader) throws GeneralException {
-        Script script = InvokerHelper.createScript(getScriptClassFromLocation(location, groovyClassLoader), getBinding(context));
+        Script script = InvokerHelper.createScript(getScriptClassFromLocation(location), getBinding(context));
         Object result = null;
         if (UtilValidate.isEmpty(methodName)) {
             result = script.run();
@@ -175,30 +177,5 @@ public class GroovyUtil {
         return result;
     }
 
-    public static Object runScriptFromClasspath(String script, Map<String,Object> context) throws GeneralException {
-        try {
-            Class<?> scriptClass = parsedScripts.get(script);
-            if (scriptClass == null) {
-                scriptClass = loadClass(script);
-                Class<?> cachedScriptClass = parsedScripts.putIfAbsent(script, scriptClass);
-                if (cachedScriptClass == null) { // putIfAbsent returns null if the class is added
-                    if (Debug.verboseOn()) {
-                        Debug.logVerbose("Cached Groovy script at: " + script, module);
-                    }
-                } else {
-                    // the newly parsed script is discarded and the one found in the cache (that has been created by a concurrent thread in the meantime) is used
-                    scriptClass = cachedScriptClass;
-                }
-            }
-            return InvokerHelper.createScript(scriptClass, getBinding(context)).run();
-        } catch (CompilationFailedException e) {
-            String errMsg = "Error loading Groovy script [" + script + "]: " + e.toString();
-            throw new GeneralException(errMsg, e);
-        } catch (ClassNotFoundException e) {
-            String errMsg = "Error loading Groovy script [" + script + "]: " + e.toString();
-            throw new GeneralException(errMsg, e);
-        }
-    }
-
     private GroovyUtil() {}
 }

Modified: ofbiz/trunk/framework/service/config/serviceengine.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/config/serviceengine.xml?rev=1636346&r1=1636345&r2=1636346&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/config/serviceengine.xml (original)
+++ ofbiz/trunk/framework/service/config/serviceengine.xml Mon Nov  3 13:15:29 2014
@@ -47,9 +47,7 @@ under the License.
         <engine name="script" class="org.ofbiz.service.engine.ScriptEngine"/>
         <!-- Engines that can be replaced by the generic script engine -->
         <engine name="bsh" class="org.ofbiz.service.engine.BeanShellEngine"/>
-        <engine name="groovy" class="org.ofbiz.service.engine.GroovyEngine">
-            <parameter name="scriptBaseClass" value="org.ofbiz.service.engine.GroovyBaseScript"/>
-        </engine>
+        <engine name="groovy" class="org.ofbiz.service.engine.GroovyEngine"/>
         <engine name="jacl" class="org.ofbiz.service.engine.BSFEngine"/>
         <engine name="javascript" class="org.ofbiz.service.engine.ScriptEngine"/>
         <engine name="jpython" class="org.ofbiz.service.engine.BSFEngine"/>

Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java?rev=1636346&r1=1636345&r2=1636346&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java Mon Nov  3 13:15:29 2014
@@ -19,7 +19,6 @@
 package org.ofbiz.service.engine;
 
 import static org.ofbiz.base.util.UtilGenerics.cast;
-import groovy.lang.GroovyClassLoader;
 import groovy.lang.Script;
 
 import java.util.Collections;
@@ -30,10 +29,7 @@ import java.util.Set;
 
 import javax.script.ScriptContext;
 
-import org.codehaus.groovy.control.CompilerConfiguration;
 import org.codehaus.groovy.runtime.InvokerHelper;
-import org.ofbiz.base.config.GenericConfigException;
-import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.GeneralException;
 import org.ofbiz.base.util.GroovyUtil;
 import org.ofbiz.base.util.ScriptHelper;
@@ -44,7 +40,6 @@ import org.ofbiz.service.GenericServiceE
 import org.ofbiz.service.ModelService;
 import org.ofbiz.service.ServiceDispatcher;
 import org.ofbiz.service.ServiceUtil;
-import org.ofbiz.service.config.ServiceConfigUtil;
 
 /**
  * Groovy Script Service Engine
@@ -55,8 +50,6 @@ public final class GroovyEngine extends 
     protected static final Object[] EMPTY_ARGS = {};
     private static final Set<String> protectedKeys = createProtectedKeys();
 
-    GroovyClassLoader groovyClassLoader;
-
     private static Set<String> createProtectedKeys() {
         Set<String> newSet = new HashSet<String>();
         /* Commenting out for now because some scripts write to the parameters Map - which should not be allowed.
@@ -70,16 +63,6 @@ public final class GroovyEngine extends 
 
     public GroovyEngine(ServiceDispatcher dispatcher) {
         super(dispatcher);
-        try {
-            String scriptBaseClass = ServiceConfigUtil.getEngineParameter("groovy", "scriptBaseClass");
-            if (scriptBaseClass != null) {
-                CompilerConfiguration conf = new CompilerConfiguration();
-                conf.setScriptBaseClass(scriptBaseClass);
-                groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader(), conf);
-            }
-        } catch (GenericConfigException gce) {
-            Debug.logWarning(gce, "Error retrieving the configuration for the groovy service engine: ", module);
-        }
     }
 
     /**
@@ -119,7 +102,7 @@ public final class GroovyEngine extends 
             if (scriptHelper != null) {
                 gContext.put(ScriptUtil.SCRIPT_HELPER_KEY, scriptHelper);
             }
-            Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(this.getLocation(modelService), groovyClassLoader), GroovyUtil.getBinding(gContext));
+            Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(this.getLocation(modelService)), GroovyUtil.getBinding(gContext));
             Object resultObj = null;
             if (UtilValidate.isEmpty(modelService.invoke)) {
                 resultObj = script.run();

Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/GroovyEventHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/GroovyEventHandler.java?rev=1636346&r1=1636345&r2=1636346&view=diff
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/GroovyEventHandler.java (original)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/GroovyEventHandler.java Mon Nov  3 13:15:29 2014
@@ -18,7 +18,6 @@
  */
 package org.ofbiz.webapp.event;
 
-import groovy.lang.GroovyClassLoader;
 import groovy.lang.Script;
 
 import java.util.Collections;
@@ -33,9 +32,7 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 
-import org.codehaus.groovy.control.CompilerConfiguration;
 import org.codehaus.groovy.runtime.InvokerHelper;
-import org.ofbiz.base.config.GenericConfigException;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.GroovyUtil;
 import org.ofbiz.base.util.ScriptHelper;
@@ -43,7 +40,6 @@ import org.ofbiz.base.util.ScriptUtil;
 import org.ofbiz.base.util.UtilHttp;
 import org.ofbiz.base.util.UtilMisc;
 import org.ofbiz.base.util.UtilValidate;
-import org.ofbiz.service.config.ServiceConfigUtil;
 import org.ofbiz.webapp.control.ConfigXMLReader.Event;
 import org.ofbiz.webapp.control.ConfigXMLReader.RequestMap;
 
@@ -70,20 +66,7 @@ public class GroovyEventHandler implemen
         return Collections.unmodifiableSet(newSet);
     }
 
-    private GroovyClassLoader groovyClassLoader;
-
     public void init(ServletContext context) throws EventHandlerException {
-        try {
-            // TODO: the name of the script base class is currently retrieved from the Groovy service engine configuration
-            String scriptBaseClass = ServiceConfigUtil.getEngineParameter("groovy", "scriptBaseClass");
-            if (scriptBaseClass != null) {
-                CompilerConfiguration conf = new CompilerConfiguration();
-                conf.setScriptBaseClass(scriptBaseClass);
-                groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader(), conf);
-            }
-        } catch (GenericConfigException gce) {
-            Debug.logWarning(gce, "Error retrieving the configuration for the groovy service engine: ", module);
-        }
     }
 
     public String invoke(Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException {
@@ -107,7 +90,7 @@ public class GroovyEventHandler implemen
                 if (scriptHelper != null) {
                     context.put(ScriptUtil.SCRIPT_HELPER_KEY, scriptHelper);
                 }
-                Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(event.path, groovyClassLoader), GroovyUtil.getBinding(context));
+                Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(event.path), GroovyUtil.getBinding(context));
                 if (UtilValidate.isEmpty(event.invoke)) {
                     result = script.run();
                 } else {