svn commit: r657645 - in /ofbiz/trunk: applications/product/webapp/catalog/WEB-INF/actions/product/ framework/base/config/ framework/base/lib/scripting/ framework/base/src/base/org/ofbiz/base/util/ framework/widget/src/org/ofbiz/widget/screen/

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

svn commit: r657645 - in /ofbiz/trunk: applications/product/webapp/catalog/WEB-INF/actions/product/ framework/base/config/ framework/base/lib/scripting/ framework/base/src/base/org/ofbiz/base/util/ framework/widget/src/org/ofbiz/widget/screen/

jonesde
Author: jonesde
Date: Sun May 18 14:57:07 2008
New Revision: 657645

URL: http://svn.apache.org/viewvc?rev=657645&view=rev
Log:
Added groovy support, implemented by Joe Eckard in jira #OFBIZ-1779; includes an example EditProductFeatures.groovy file showing somatures.groovy file showing some nice groovy syntax features, especially for Maps and Lists, though it isn't on by default yet in this commit, ie bsh still used

Added:
    ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/actions/product/EditProductFeatures.groovy
    ofbiz/trunk/framework/base/lib/scripting/groovy-1.5.6.jar   (with props)
    ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java   (with props)
Modified:
    ofbiz/trunk/framework/base/config/cache.properties
    ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ModelScreenAction.java

Added: ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/actions/product/EditProductFeatures.groovy
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/actions/product/EditProductFeatures.groovy?rev=657645&view=auto
==============================================================================
--- ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/actions/product/EditProductFeatures.groovy (added)
+++ ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/actions/product/EditProductFeatures.groovy Sun May 18 14:57:07 2008
@@ -0,0 +1,49 @@
+/*
+ * 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.
+ */
+
+import org.ofbiz.entity.*;
+
+
+productFeatureAndAppls = delegator.findByAnd('ProductFeatureAndAppl',
+        ['productId' : productId],
+        ['sequenceNum', 'productFeatureApplTypeId', 'productFeatureTypeId', 'description']);
+if (productFeatureAndAppls != null) {
+    context.put('productFeatureAndAppls', productFeatureAndAppls);
+}
+
+productFeatureCategories = delegator.findAll('ProductFeatureCategory', ['description']);
+if (productFeatureCategories != null) {
+    context.put('productFeatureCategories', productFeatureCategories);
+}
+
+productFeatureApplTypes = delegator.findAll('ProductFeatureApplType', ['description']);
+if (productFeatureApplTypes != null) {
+    context.put('productFeatureApplTypes', productFeatureApplTypes);
+}
+
+productFeatureGroups = delegator.findAll('ProductFeatureGroup', ['description']);
+if (productFeatureGroups != null) {
+    context.put('productFeatureGroups', productFeatureGroups);
+}
+
+productFeatureTypes = delegator.findAll('ProductFeatureType', ['description']);
+if (productFeatureTypes != null) {
+    context.put('productFeatureTypes', productFeatureTypes);
+}
+

Modified: ofbiz/trunk/framework/base/config/cache.properties
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/config/cache.properties?rev=657645&r1=657644&r2=657645&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/config/cache.properties (original)
+++ ofbiz/trunk/framework/base/config/cache.properties Sun May 18 14:57:07 2008
@@ -71,6 +71,8 @@
 script.BshLocationParsedCache.expireTime=10000
 script.BshBsfParsedCache.expireTime=10000
 
+script.GroovyLocationParsedCache.expireTime=10000
+
 webapp.BsfEvents.expireTime=10000
 webapp.JasperReportsCompiled.expireTime=10000
 webapp.ControllerConfig.expireTime=10000

Added: ofbiz/trunk/framework/base/lib/scripting/groovy-1.5.6.jar
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/lib/scripting/groovy-1.5.6.jar?rev=657645&view=auto
==============================================================================
Binary file - no diff available.

Propchange: ofbiz/trunk/framework/base/lib/scripting/groovy-1.5.6.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: 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=657645&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java (added)
+++ ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java Sun May 18 14:57:07 2008
@@ -0,0 +1,78 @@
+package org.ofbiz.base.util;
+
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Map;
+import java.util.Set;
+
+import org.ofbiz.base.location.FlexibleLocation;
+import org.ofbiz.base.util.cache.UtilCache;
+
+import groovy.lang.Binding;
+import groovy.lang.GroovyShell;
+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) {
+
+        Binding binding = new Binding();
+        if (context != null) {
+            Set keySet = context.keySet();
+            for (Object key : keySet) {
+                binding.setVariable((String) key, context.get(key));
+            }
+
+            // include the context itself in for easier access in the scripts
+            binding.setVariable("context", context);
+        }
+
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        return new GroovyShell(classLoader, binding);
+    }
+
+    public static Object runScriptAtLocation(String location, Map context) throws GeneralException {
+
+        try {
+
+            Script script = parsedScripts.get(location);
+            if (script == null) {
+
+                URL scriptUrl = FlexibleLocation.resolveLocation(location);
+                GroovyShell shell = getShell(context);
+                script = shell.parse(scriptUrl.openStream(), scriptUrl.getFile());
+                if (Debug.verboseOn()) {
+                    Debug.logVerbose("Caching Groovy script at: " + location, module);
+                }
+                parsedScripts.put(location, script);
+            }
+            
+            return script.run();
+
+        } catch (MalformedURLException e) {
+            String errMsg = "Error loading Groovy script at [" + location + "]: " + e.toString();
+            throw new GeneralException(errMsg, e);
+        } catch (IOException e) {
+            String errMsg = "Error loading Groovy script at [" + location + "]: " + e.toString();
+            throw new GeneralException(errMsg, e);
+        } catch (CompilationFailedException e) {
+            String errMsg = "Error loading Groovy script at [" + location + "]: " + e.toString();
+            throw new GeneralException(errMsg, e);
+        }
+    }
+
+}

Propchange: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Author URL Id"

Propchange: ofbiz/trunk/framework/base/src/base/org/ofbiz/base/util/GroovyUtil.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ModelScreenAction.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ModelScreenAction.java?rev=657645&r1=657644&r2=657645&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ModelScreenAction.java (original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ModelScreenAction.java Sun May 18 14:57:07 2008
@@ -36,6 +36,7 @@
 import org.ofbiz.base.util.BshUtil;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.GeneralException;
+import org.ofbiz.base.util.GroovyUtil;
 import org.ofbiz.base.util.ObjectType;
 import org.ofbiz.base.util.StringUtil;
 import org.ofbiz.base.util.UtilMisc;
@@ -391,6 +392,12 @@
                 } catch (GeneralException e) {
                     throw new GeneralException("Error running BSH script at location [" + location + "]", e);
                 }
+            } else if (location.endsWith(".groovy")) {
+                try {
+                    GroovyUtil.runScriptAtLocation(location, context);
+                } catch (GeneralException e) {
+                    throw new GeneralException("Error running Groovy script at location [" + location + "]", e);
+                }
             } else if (location.contains(".xml#")) {
                 String xmlResource = ScreenFactory.getResourceNameFromCombined(location);
                 String methodName = ScreenFactory.getScreenNameFromCombined(location);