Permalink
Browse files

Moving from bshInterpreter to GroovyShell

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/branches/20120209RemoveBsh@1243012 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent c37b0c9 commit 6917866baf1ba7f948d2675f8800c08035b58f8b Erwan de Ferrieres committed Feb 11, 2012
@@ -266,10 +266,7 @@ private static FlexibleStringExpander parse(char[] chars, int offset, int length
// 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 @@ public String getOriginal() {
}
}
- /** 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) {
@@ -73,13 +73,7 @@ public boolean exec(MethodContext methodContext) {
}
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)) {
@@ -403,13 +403,7 @@ public Script(ModelWidget modelWidget, Element scriptElement) {
@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)) {
@@ -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 @@ public void renderItemRows(Appendable writer, Map<String, Object> context, FormS
}
// 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 @@ public void renderItemRows(Appendable writer, Map<String, Object> context, FormS
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 @@ public String getTarget(Map<String, Object> context, String targetType) {
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 @@ public String getTarget(Map<String, Object> context, String targetType) {
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 @@ public String getBoundaryCommentName() {
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 @@ public String getStyleAltRowStyle(Map<String, Object> context) {
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 @@ public String getStyleAltRowStyle(Map<String, Object> context) {
"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);
@@ -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 @@ public Script(ModelForm modelForm, Element scriptElement) {
@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)) {
@@ -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 @@ public boolean shouldUse(Map<String, Object> context) {
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 @@ public boolean shouldUse(Map<String, Object> context) {
}
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 @@ public boolean shouldUse(Map<String, Object> context) {
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 @@ public boolean shouldUse(Map<String, Object> context) {
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);
@@ -24,20 +24,16 @@
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;
import org.ofbiz.service.LocalDispatcher;
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 @@ public String getBoundaryCommentName() {
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;
}
/**
@@ -364,15 +364,7 @@ public Script(ModelMenu modelMenu, Element scriptElement) {
@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)) {
Oops, something went wrong.

0 comments on commit 6917866

Please sign in to comment.