Moving from bshInterpreter to GroovyShell

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/branches/20120209RemoveBsh@1243012 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java b/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java
index 13c3723..e482c1f 100644
--- a/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java
+++ b/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java
@@ -266,10 +266,7 @@
                 // append everything from the current index to the start of the expression
                 strElems.add(new ConstOffsetElem(chars, currentInd, (escapedExpression ? start -1 : start) - currentInd));
             }
-            if (expression.indexOf("bsh:", start + 2) == start + 2 && !escapedExpression) {
-                // checks to see if this starts with a "bsh:", if so treat the rest of the expression as a bsh scriptlet
-                strElems.add(new BshElem(chars, start, Math.min(end + 1, start + length) - start, start + 6, end - start - 6));
-            } else if (expression.indexOf("groovy:", start + 2) == start + 2 && !escapedExpression) {
+            if (expression.indexOf("groovy:", start + 2) == start + 2 && !escapedExpression) {
                 // checks to see if this starts with a "groovy:", if so treat the rest of the expression as a groovy scriptlet
                 strElems.add(new GroovyElem(chars, start, Math.min(end + 1, start + length) - start, start + 9, end - start - 9));
             } else {
@@ -488,35 +485,6 @@
         }
     }
 
-    /** An object that represents a <code>${bsh:}</code> expression. */
-    protected static class BshElem extends ArrayOffsetString {
-        private final int parseStart;
-        private final int parseLength;
-
-        protected BshElem(char[] chars, int offset, int length, int parseStart, int parseLength) {
-            super(chars, offset, length);
-            this.parseStart = parseStart;
-            this.parseLength = parseLength;
-        }
-
-        @Override
-        protected Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
-            try {
-                Object obj = BshUtil.eval(new String(this.chars, this.parseStart, this.parseLength), UtilMisc.makeMapWritable(context));
-                if (obj != null) {
-                    return obj;
-                } else {
-                    if (Debug.verboseOn()) {
-                        Debug.logVerbose("BSH scriptlet evaluated to null [" + this + "], got no return so inserting nothing.", module);
-                    }
-                }
-            } catch (EvalError e) {
-                Debug.logWarning(e, "Error evaluating BSH scriptlet [" + this + "], inserting nothing; error was: " + e, module);
-            }
-            return null;
-        }
-    }
-
     /** An object that represents a <code>String</code> constant portion of an expression. */
     protected static class ConstSimpleElem extends FlexibleStringExpander {
         protected ConstSimpleElem(char[] chars) {
diff --git a/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java b/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java
index fbc6d08..24e33a2 100644
--- a/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java
+++ b/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java
@@ -73,13 +73,7 @@
         }
 
         Map<String, Object> context = methodContext.getEnvMap();        
-        if (location.endsWith(".bsh")) {
-            try {
-                BshUtil.runBshAtLocation(location, context);
-            } catch (GeneralException e) {
-                messages.add("Error running BSH script at location [" + location + "]: " + e.getMessage());
-            }
-        } else if (location.endsWith(".groovy")) {
+        if (location.endsWith(".groovy")) {
             try {
                 groovy.lang.Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), GroovyUtil.getBinding(context));
                 if (UtilValidate.isEmpty(method)) {
diff --git a/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java b/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java
index d754192..68bbd0a 100644
--- a/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java
+++ b/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java
@@ -403,13 +403,7 @@
 
         @Override
         public void runAction(Map<String, Object> context) throws GeneralException {
-            if (location.endsWith(".bsh")) {
-                try {
-                    BshUtil.runBshAtLocation(location, context);
-                } catch (GeneralException e) {
-                    throw new GeneralException("Error running BSH script at location [" + location + "]", e);
-                }
-            } else if (location.endsWith(".groovy")) {
+            if (location.endsWith(".groovy")) {
                 try {
                     groovy.lang.Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), GroovyUtil.getBinding(context));
                     if (UtilValidate.isEmpty(method)) {
diff --git a/framework/widget/src/org/ofbiz/widget/form/ModelForm.java b/framework/widget/src/org/ofbiz/widget/form/ModelForm.java
index 0d85d54..0aea4da 100644
--- a/framework/widget/src/org/ofbiz/widget/form/ModelForm.java
+++ b/framework/widget/src/org/ofbiz/widget/form/ModelForm.java
@@ -30,19 +30,13 @@
 import java.util.TreeMap;
 import java.util.TreeSet;
 
+import groovy.lang.GroovyShell;
 import javolution.util.FastList;
 import javolution.util.FastMap;
 import javolution.util.FastSet;
 
-import org.ofbiz.base.util.BshUtil;
-import org.ofbiz.base.util.Debug;
-import org.ofbiz.base.util.GeneralException;
-import org.ofbiz.base.util.StringUtil;
-import org.ofbiz.base.util.UtilGenerics;
-import org.ofbiz.base.util.UtilMisc;
-import org.ofbiz.base.util.UtilProperties;
-import org.ofbiz.base.util.UtilValidate;
-import org.ofbiz.base.util.UtilXml;
+import org.codehaus.groovy.control.CompilationFailedException;
+import org.ofbiz.base.util.*;
 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
 import org.ofbiz.base.util.collections.MapStack;
 import org.ofbiz.base.util.string.FlexibleStringExpander;
@@ -60,9 +54,6 @@
 import org.ofbiz.widget.WidgetWorker;
 import org.w3c.dom.Element;
 
-import bsh.EvalError;
-import bsh.Interpreter;
-
 /**
  * Widget Library - Form model class
  */
@@ -1490,7 +1481,7 @@
                 }
 
                 // reset/remove the BshInterpreter now as well as later because chances are there is an interpreter at this level of the stack too
-                this.resetBshInterpreter(localContext);
+                this.resetGroovyShell(localContext);
                 localContext.push();
                 localContext.put("previousItem", previousItem);
                 previousItem = FastMap.newInstance();
@@ -1503,7 +1494,7 @@
                     localContext.put("formUniqueId", "_"+context.get("renderFormSeqNumber"));
                 }
 
-                this.resetBshInterpreter(localContext);
+                this.resetGroovyShell(localContext);
 
                 if (Debug.verboseOn()) Debug.logVerbose("In form got another row, context is: " + localContext, module);
 
@@ -2025,9 +2016,9 @@
 
         try {
             // use the same Interpreter (ie with the same context setup) for all evals
-            Interpreter bsh = this.getBshInterpreter(context);
+            GroovyShell shell = this.getGroovyShell(context);
             for (AltTarget altTarget: this.altTargets) {
-                Object retVal = bsh.eval(StringUtil.convertOperatorSubstitutions(altTarget.useWhen));
+                Object retVal = shell.evaluate(StringUtil.convertOperatorSubstitutions(altTarget.useWhen));
                 boolean condTrue = false;
                 // retVal should be a Boolean, if not something weird is up...
                 if (retVal instanceof Boolean) {
@@ -2042,7 +2033,7 @@
                     return altTarget.targetExdr.expandString(expanderContext);
                 }
             }
-        } catch (EvalError e) {
+        } catch (CompilationFailedException e) {
             String errmsg = "Error evaluating BeanShell target conditions on form " + this.name;
             Debug.logError(e, errmsg, module);
             throw new IllegalArgumentException(errmsg);
@@ -2098,17 +2089,17 @@
         return formLocation + "#" + name;
     }
 
-    public void resetBshInterpreter(Map<String, Object> context) {
-        context.remove("bshInterpreter");
+    public void resetGroovyShell(Map<String, Object> context) {
+        context.remove("groovyShell");
     }
 
-    public Interpreter getBshInterpreter(Map<String, Object> context) throws EvalError {
-        Interpreter bsh = (Interpreter) context.get("bshInterpreter");
-        if (bsh == null) {
-            bsh = BshUtil.makeInterpreter(context);
-            context.put("bshInterpreter", bsh);
+    public GroovyShell getGroovyShell(Map<String, Object> context) throws CompilationFailedException {
+        GroovyShell shell = (GroovyShell) context.get("groovyShell");
+        if (shell == null) {
+            shell = new GroovyShell(GroovyUtil.getBinding(context));
+            context.put("groovyShell", shell);
         }
-        return bsh;
+        return shell;
     }
 
     /**
@@ -2754,9 +2745,9 @@
         String styles = "";
         try {
             // use the same Interpreter (ie with the same context setup) for all evals
-            Interpreter bsh = this.getBshInterpreter(context);
+            GroovyShell shell = this.getGroovyShell(context);
             for (AltRowStyle altRowStyle : this.altRowStyles) {
-                Object retVal = bsh.eval(StringUtil.convertOperatorSubstitutions(altRowStyle.useWhen));
+                Object retVal = shell.evaluate(StringUtil.convertOperatorSubstitutions(altRowStyle.useWhen));
                 // retVal should be a Boolean, if not something weird is up...
                 if (retVal instanceof Boolean) {
                     Boolean boolVal = (Boolean) retVal;
@@ -2768,7 +2759,7 @@
                         "Return value from style condition eval was not a Boolean: " + retVal.getClass().getName() + " [" + retVal + "] of form " + this.name);
                 }
             }
-        } catch (EvalError e) {
+        } catch (CompilationFailedException e) {
             String errmsg = "Error evaluating BeanShell style conditions on form " + this.name;
             Debug.logError(e, errmsg, module);
             throw new IllegalArgumentException(errmsg);
diff --git a/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java b/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java
index 9892783..3cf660c 100644
--- a/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java
+++ b/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java
@@ -32,7 +32,6 @@
 import javolution.util.FastMap;
 
 import org.codehaus.groovy.runtime.InvokerHelper;
-import org.ofbiz.base.util.BshUtil;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.GeneralException;
 import org.ofbiz.base.util.GroovyUtil;
@@ -285,15 +284,7 @@
 
         @Override
         public void runAction(Map<String, Object> context) {
-            if (location.endsWith(".bsh")) {
-                try {
-                    BshUtil.runBshAtLocation(location, context);
-                } catch (GeneralException e) {
-                    String errMsg = "Error running BSH script at location [" + location + "]: " + e.toString();
-                    Debug.logError(e, errMsg, module);
-                    throw new IllegalArgumentException(errMsg);
-                }
-            } else if (location.endsWith(".groovy")) {
+            if (location.endsWith(".groovy")) {
                 try {
                     groovy.lang.Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), GroovyUtil.getBinding(context));
                     if (UtilValidate.isEmpty(method)) {
diff --git a/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java b/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java
index 85d39ba..4deccd8 100644
--- a/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java
+++ b/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java
@@ -32,24 +32,15 @@
 import java.util.StringTokenizer;
 import java.util.TimeZone;
 
+import groovy.lang.GroovyShell;
 import javolution.util.FastList;
 import javolution.util.FastMap;
 
+import org.codehaus.groovy.control.CompilationFailedException;
 import org.ofbiz.base.conversion.ConversionException;
 import org.ofbiz.base.conversion.DateTimeConverters;
 import org.ofbiz.base.conversion.DateTimeConverters.StringToTimestamp;
-import org.ofbiz.base.util.BshUtil;
-import org.ofbiz.base.util.Debug;
-import org.ofbiz.base.util.GeneralException;
-import org.ofbiz.base.util.ObjectType;
-import org.ofbiz.base.util.StringUtil;
-import org.ofbiz.base.util.UtilDateTime;
-import org.ofbiz.base.util.UtilFormatOut;
-import org.ofbiz.base.util.UtilGenerics;
-import org.ofbiz.base.util.UtilMisc;
-import org.ofbiz.base.util.UtilProperties;
-import org.ofbiz.base.util.UtilValidate;
-import org.ofbiz.base.util.UtilXml;
+import org.ofbiz.base.util.*;
 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
 import org.ofbiz.base.util.collections.MapStack;
 import org.ofbiz.base.util.string.FlexibleStringExpander;
@@ -71,9 +62,6 @@
 import org.ofbiz.widget.form.ModelForm.UpdateArea;
 import org.w3c.dom.Element;
 
-import bsh.EvalError;
-import bsh.Interpreter;
-
 /**
  * Widget Library - Form model class
  */
@@ -1014,8 +1002,8 @@
         if (UtilValidate.isEmpty(useWhenStr)) return true;
         
         try {
-            Interpreter bsh = this.modelForm.getBshInterpreter(context);
-            Object retVal = bsh.eval(StringUtil.convertOperatorSubstitutions(useWhenStr));
+            GroovyShell shell = this.modelForm.getGroovyShell(context);
+            Object retVal = shell.evaluate(StringUtil.convertOperatorSubstitutions(useWhenStr));
             boolean condTrue = false;
             // retVal should be a Boolean, if not something weird is up...
             if (retVal instanceof Boolean) {
@@ -1027,7 +1015,7 @@
             }
 
             return condTrue;
-        } catch (EvalError e) {
+        } catch (CompilationFailedException e) {
             String errMsg = "Error evaluating BeanShell use-when condition [" + useWhenStr + "] on the field "
                     + this.name + " of form " + this.modelForm.getName() + ": " + e.toString();
             Debug.logError(e, errMsg, module);
@@ -2508,13 +2496,13 @@
             String useWhen = this.getUseWhen(context);
             if (UtilValidate.isNotEmpty(useWhen)) {
                 try {
-                    Interpreter bsh = (Interpreter) context.get("bshInterpreter");
-                    if (bsh == null) {
-                        bsh = BshUtil.makeInterpreter(context);
-                        context.put("bshInterpreter", bsh);
+                    GroovyShell shell = (GroovyShell) context.get("groovyShell");
+                    if (shell == null) {
+                        shell = new GroovyShell(GroovyUtil.getBinding(context));
+                        context.put("groovyShell", shell);
                     }
 
-                    Object retVal = bsh.eval(StringUtil.convertOperatorSubstitutions(useWhen));
+                    Object retVal = shell.evaluate(StringUtil.convertOperatorSubstitutions(useWhen));
 
                     // retVal should be a Boolean, if not something weird is up...
                     if (retVal instanceof Boolean) {
@@ -2524,7 +2512,7 @@
                         throw new IllegalArgumentException(
                             "Return value from target condition eval was not a Boolean: " + retVal.getClass().getName() + " [" + retVal + "]");
                     }
-                } catch (EvalError e) {
+                } catch (CompilationFailedException e) {
                     String errmsg = "Error evaluating BeanShell target conditions";
                     Debug.logError(e, errmsg, module);
                     throw new IllegalArgumentException(errmsg);
diff --git a/framework/widget/src/org/ofbiz/widget/menu/ModelMenu.java b/framework/widget/src/org/ofbiz/widget/menu/ModelMenu.java
index 06cb925..38978a1 100644
--- a/framework/widget/src/org/ofbiz/widget/menu/ModelMenu.java
+++ b/framework/widget/src/org/ofbiz/widget/menu/ModelMenu.java
@@ -24,10 +24,9 @@
 import java.util.List;
 import java.util.Map;
 
-import org.ofbiz.base.util.BshUtil;
-import org.ofbiz.base.util.Debug;
-import org.ofbiz.base.util.UtilValidate;
-import org.ofbiz.base.util.UtilXml;
+import groovy.lang.GroovyShell;
+import org.codehaus.groovy.control.CompilationFailedException;
+import org.ofbiz.base.util.*;
 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
 import org.ofbiz.base.util.string.FlexibleStringExpander;
 import org.ofbiz.entity.Delegator;
@@ -35,9 +34,6 @@
 import org.ofbiz.widget.ModelWidget;
 import org.w3c.dom.Element;
 
-import bsh.EvalError;
-import bsh.Interpreter;
-
 /**
  * Widget Library - Menu model class
  */
@@ -446,13 +442,13 @@
         return menuLocation + "#" + name;
     }
 
-    public Interpreter getBshInterpreter(Map<String, Object> context) throws EvalError {
-        Interpreter bsh = (Interpreter) context.get("bshInterpreter");
-        if (bsh == null) {
-            bsh = BshUtil.makeInterpreter(context);
-            context.put("bshInterpreter", bsh);
+    public GroovyShell getGroovyShell(Map<String, Object> context) throws CompilationFailedException {
+        GroovyShell shell = (GroovyShell) context.get("groovyShell");
+        if (shell == null) {
+            shell = new GroovyShell(GroovyUtil.getBinding(context));
+            context.put("groovyShell", shell);
         }
-        return bsh;
+        return shell;
     }
 
     /**
diff --git a/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java b/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java
index a624c37..54ca6ab 100644
--- a/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java
+++ b/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java
@@ -364,15 +364,7 @@
 
         @Override
         public void runAction(Map<String, Object> context) {
-            if (location.endsWith(".bsh")) {
-                try {
-                    BshUtil.runBshAtLocation(location, context);
-                } catch (GeneralException e) {
-                    String errMsg = "Error running BSH script at location [" + location + "]: " + e.toString();
-                    Debug.logError(e, errMsg, module);
-                    throw new IllegalArgumentException(errMsg);
-                }
-            } else if (location.endsWith(".groovy")) {
+            if (location.endsWith(".groovy")) {
                 try {
                     groovy.lang.Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), GroovyUtil.getBinding(context));
                     if (UtilValidate.isEmpty(method)) {
diff --git a/framework/widget/src/org/ofbiz/widget/tree/ModelTreeAction.java b/framework/widget/src/org/ofbiz/widget/tree/ModelTreeAction.java
index 4f67c1f..54e15ee 100644
--- a/framework/widget/src/org/ofbiz/widget/tree/ModelTreeAction.java
+++ b/framework/widget/src/org/ofbiz/widget/tree/ModelTreeAction.java
@@ -28,14 +28,8 @@
 import javolution.util.FastList;
 import javolution.util.FastMap;
 
-import org.ofbiz.base.util.BshUtil;
-import org.ofbiz.base.util.Debug;
-import org.ofbiz.base.util.GeneralException;
-import org.ofbiz.base.util.ObjectType;
-import org.ofbiz.base.util.UtilFormatOut;
-import org.ofbiz.base.util.UtilGenerics;
-import org.ofbiz.base.util.UtilValidate;
-import org.ofbiz.base.util.UtilXml;
+import org.codehaus.groovy.runtime.InvokerHelper;
+import org.ofbiz.base.util.*;
 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
 import org.ofbiz.base.util.string.FlexibleStringExpander;
 import org.ofbiz.entity.finder.ByAndFinder;
@@ -45,6 +39,7 @@
 import org.ofbiz.entity.util.EntityListIterator;
 import org.ofbiz.service.GenericServiceException;
 import org.ofbiz.service.ModelService;
+import org.ofbiz.widget.WidgetWorker;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
@@ -173,24 +168,35 @@
     }
 
     public static class Script extends ModelTreeAction {
+        protected static final Object[] EMPTY_ARGS = {};
         protected String location;
+        protected String method;
 
         public Script(ModelTree.ModelNode modelNode, Element scriptElement) {
             super (modelNode, scriptElement);
-            this.location = scriptElement.getAttribute("location");
+            String scriptLocation = scriptElement.getAttribute("location");
+            this.location = WidgetWorker.getScriptLocation(scriptLocation);
+            this.method = WidgetWorker.getScriptMethodName(scriptLocation);
         }
 
         public Script(ModelTree.ModelNode.ModelSubNode modelSubNode, Element scriptElement) {
             super (modelSubNode, scriptElement);
-            this.location = scriptElement.getAttribute("location");
+            String scriptLocation = scriptElement.getAttribute("location");
+            this.location = WidgetWorker.getScriptLocation(scriptLocation);
+            this.method = WidgetWorker.getScriptMethodName(scriptLocation);
         }
 
         @Override
         public void runAction(Map<String, Object> context) {
-            if (location.endsWith(".bsh")) {
+            if (location.endsWith(".groovy")) {
                 try {
                     context.put("_LIST_ITERATOR_", null);
-                    BshUtil.runBshAtLocation(location, context);
+                    groovy.lang.Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), GroovyUtil.getBinding(context));
+                    if (UtilValidate.isEmpty(method)) {
+                        script.run();
+                    } else {
+                        script.invokeMethod(method, EMPTY_ARGS);
+                    }
                     Object obj = context.get("_LIST_ITERATOR_");
                     if (this.modelSubNode != null) {
                         if (obj != null && (obj instanceof EntityListIterator || obj instanceof ListIterator<?>)) {
@@ -204,7 +210,7 @@
                         }
                     }
                 } catch (GeneralException e) {
-                    String errMsg = "Error running BSH script at location [" + location + "]: " + e.toString();
+                    String errMsg = "Error running Groovy script at location [" + location + "]: " + e.toString();
                     Debug.logError(e, errMsg, module);
                     throw new IllegalArgumentException(errMsg);
                 }