svn commit: r769508 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java

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

svn commit: r769508 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java

jaz-3
Author: jaz
Date: Tue Apr 28 19:41:21 2009
New Revision: 769508

URL: http://svn.apache.org/viewvc?rev=769508&view=rev
Log:
added method to run script from classpath; added annotations to suppress warnings

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

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=769508&r1=769507&r2=769508&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 Tue Apr 28 19:41:21 2009
@@ -30,10 +30,8 @@
 import groovy.lang.Binding;
 import groovy.lang.GroovyClassLoader;
 import groovy.lang.GroovyShell;
-import groovy.lang.Script;
 import org.codehaus.groovy.control.CompilationFailedException;
 import org.codehaus.groovy.runtime.InvokerHelper;
-import bsh.EvalError;
 
 /**
  * GroovyUtil - Groovy Utilities
@@ -43,6 +41,7 @@
 
     public static final String module = GroovyUtil.class.getName();
 
+    @SuppressWarnings("unchecked")
     public static UtilCache<String, Class> parsedScripts = new UtilCache<String, Class>("script.GroovyLocationParsedCache", 0, 0, false);
 
     public static GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
@@ -69,6 +68,7 @@
      * @return Object The result of the evaluation
      * @throws CompilationFailedException
      */
+    @SuppressWarnings("unchecked")
     public static Object eval(String expression, Map<String, Object> context) throws CompilationFailedException {
         Object o;
         if (expression == null || expression.equals("")) {
@@ -100,6 +100,27 @@
         return o;
     }
 
+    @SuppressWarnings("unchecked")
+    public static Object runScriptFromClasspath(String script, Map<String,Object> context) throws GeneralException {
+        try {
+            Class scriptClass = parsedScripts.get(script);
+            if (scriptClass == null) {
+                scriptClass = groovyClassLoader.loadClass(script);
+                if (Debug.verboseOn()) Debug.logVerbose("Caching Groovy script: " + script, module);
+                parsedScripts.put(script, scriptClass);
+            }
+            
+            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);
+        }
+    }
+    
+    @SuppressWarnings("unchecked")
     public static Object runScriptAtLocation(String location, Map<String, Object> context) throws GeneralException {
         try {
             Class scriptClass = parsedScripts.get(location);