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 2012/03/04 08:24:48 UTC
svn commit: r1296762 - in /ofbiz/trunk/framework:
base/src/org/ofbiz/base/util/ base/src/org/ofbiz/base/util/string/
minilang/src/org/ofbiz/minilang/method/callops/
minilang/src/org/ofbiz/minilang/method/envops/ widget/src/org/ofbiz/widget/
widget/src/...
Author: jacopoc
Date: Sun Mar 4 07:24:47 2012
New Revision: 1296762
URL: http://svn.apache.org/viewvc?rev=1296762&view=rev
Log:
First pass in the direction of the implementation of a more flexible mechanism to plugin different scripting technologies and in the same time being able to get rid of a lot of framework related custom code to support them.
In particular these chenges should make it easier to implement JSR-223 (even if I think that we should still treat Groovy in a special way).
Added:
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java
Modified:
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java
ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java
ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/SetOperation.java
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/screen/ModelScreenAction.java
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java?rev=1296762&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ScriptUtil.java Sun Mar 4 07:24:47 2012
@@ -0,0 +1,99 @@
+/*******************************************************************************
+ * 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.
+ *******************************************************************************/
+package org.ofbiz.base.util;
+
+import org.codehaus.groovy.runtime.InvokerHelper;
+
+import java.util.Map;
+
+public class ScriptUtil {
+ public static final String module = ScriptUtil.class.getName();
+ protected static final Object[] EMPTY_ARGS = {};
+
+ public static Class<?> parseScript(String language, String script) {
+ Class<?> scriptClass = null;
+ if ("groovy".equals(language)) {
+ scriptClass = GroovyUtil.parseClass(script);
+ }
+ return scriptClass;
+ }
+ /* TODO: the "evaluate" and "executeScript" method method could be enhanced to implement JSR-223 using code like:
+ import javax.script.ScriptEngineManager;
+ import javax.script.ScriptEngine;
+ ...
+ ScriptEngineManager manager = new ScriptEngineManager();
+ ScriptEngine scriptEngine = manager.getEngineByExtension(location.substring(location.indexOf(".") + 1));
+ ...
+ Object result = scriptEngine.eval(scriptFileReader, scriptContext);
+
+ However it may make more sense to keep a custom way to load and execute Groovy scripts and implement JSR-223
+ for the other scripting languages: in this way the OFBiz framework will support any script language with JSR-223
+ but will still have specialized support for Groovy (where we could/should inject OFBiz specific utility methods
+ and create a security sandbox for Groovy dynamic code).
+ */
+ public static Object evaluate(String language, String script, Class<?> scriptClass, Map inputMap) throws Exception {
+ /*
+ TODO: for JSR-223 we could use:
+ ScriptEngine scriptEngine = manager.getEngineByName(location);
+ */
+ Object result = null;
+ if ("groovy".equals(language)) {
+ if (scriptClass == null) {
+ scriptClass = ScriptUtil.parseScript(language, script);
+ }
+ if (scriptClass != null) {
+ result = InvokerHelper.createScript(scriptClass, GroovyUtil.getBinding(inputMap)).run();
+ }
+ } else if ("bsh".equals(language)) {
+ result = BshUtil.eval(script, UtilMisc.makeMapWritable(inputMap));
+ }
+ return result;
+ }
+
+ public static void executeScript(String location, String method, Map<String, Object> context) {
+ /*
+ TODO: for JSR-223 we could use:
+ ScriptEngine scriptEngine = manager.getEngineByExtension(location.substring(location.indexOf(".") + 1));
+ */
+ 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")) {
+ try {
+ groovy.lang.Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), GroovyUtil.getBinding(context));
+ if (UtilValidate.isEmpty(method)) {
+ script.run();
+ } else {
+ script.invokeMethod(method, EMPTY_ARGS);
+ }
+ } catch (GeneralException e) {
+ String errMsg = "Error running Groovy script at location [" + location + "]: " + e.toString();
+ Debug.logError(e, errMsg, module);
+ throw new IllegalArgumentException(errMsg);
+ }
+ } else {
+ throw new IllegalArgumentException("The script type is not yet support for location:" + location);
+ }
+ }
+}
Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java?rev=1296762&r1=1296761&r2=1296762&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/string/FlexibleStringExpander.java Sun Mar 4 07:24:47 2012
@@ -29,19 +29,8 @@ import javax.el.PropertyNotFoundExceptio
import org.ofbiz.base.lang.IsEmpty;
import org.ofbiz.base.lang.SourceMonitored;
-import org.ofbiz.base.util.BshUtil;
-import org.ofbiz.base.util.Debug;
-import org.ofbiz.base.util.GroovyUtil;
-import org.ofbiz.base.util.ObjectType;
-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.UtilValidate;
+import org.ofbiz.base.util.*;
import org.ofbiz.base.util.cache.UtilCache;
-import org.codehaus.groovy.runtime.InvokerHelper;
-
-import bsh.EvalError;
/** Expands String values that contain Unified Expression Language (JSR 245)
* syntax. This class also supports the execution of bsh scripts by using the
@@ -268,10 +257,10 @@ public abstract class FlexibleStringExpa
}
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));
+ strElems.add(new ScriptElem(chars, start, Math.min(end + 1, start + length) - start, start + 6, end - start - 6));
} else 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));
+ strElems.add(new ScriptElem(chars, start, Math.min(end + 1, start + length) - start, start + 9, end - start - 9));
} else {
// Scan for matching closing bracket
int ptr = expression.indexOf(openBracket, start + 2);
@@ -488,35 +477,6 @@ public abstract class FlexibleStringExpa
}
}
- /** 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) {
@@ -613,29 +573,36 @@ public abstract class FlexibleStringExpa
}
}
- /** An object that represents a <code>${groovy:}</code> expression. */
- protected static class GroovyElem extends ArrayOffsetString {
+ /** An object that represents a <code>${[groovy|bsh]:}</code> expression. */
+ protected static class ScriptElem extends ArrayOffsetString {
+ private final String language;
+ private final int parseStart;
+ private final int parseLength;
+ private final String script;
protected final Class<?> parsedScript;
- protected GroovyElem(char[] chars, int offset, int length, int parseStart, int parseLength) {
+ protected ScriptElem(char[] chars, int offset, int length, int parseStart, int parseLength) {
super(chars, offset, length);
- this.parsedScript = GroovyUtil.parseClass(new String(chars, parseStart, parseLength));
+ this.language = new String(this.chars, offset + 2, parseStart - offset - 3);
+ this.parseStart = parseStart;
+ this.parseLength = parseLength;
+ this.script = new String(this.chars, this.parseStart, this.parseLength);
+ this.parsedScript = ScriptUtil.parseScript(this.language, this.script);
}
@Override
protected Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
try {
- Object obj = InvokerHelper.createScript(this.parsedScript, GroovyUtil.getBinding(context)).run();
+ Object obj = ScriptUtil.evaluate(this.language, this.script, this.parsedScript, context);
if (obj != null) {
return obj;
} else {
if (Debug.verboseOn()) {
- Debug.logVerbose("Groovy scriptlet evaluated to null [" + this + "], got no return so inserting nothing.", module);
+ Debug.logVerbose("Scriptlet evaluated to null [" + this + "].", module);
}
}
} catch (Exception e) {
- // handle other things, like the groovy.lang.MissingPropertyException
- Debug.logWarning(e, "Error evaluating Groovy scriptlet [" + this + "], inserting nothing; error was: " + e, module);
+ Debug.logWarning(e, "Error evaluating scriptlet [" + this + "]; error was: " + e, module);
}
return null;
}
Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java?rev=1296762&r1=1296761&r2=1296762&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/callops/CallScript.java Sun Mar 4 07:24:47 2012
@@ -23,11 +23,7 @@ import java.util.Map;
import javolution.util.FastList;
-import org.codehaus.groovy.runtime.InvokerHelper;
-import org.ofbiz.base.util.BshUtil;
-import org.ofbiz.base.util.GeneralException;
-import org.ofbiz.base.util.GroovyUtil;
-import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.*;
import org.ofbiz.minilang.MiniLangException;
import org.ofbiz.minilang.SimpleMethod;
import org.ofbiz.minilang.method.ContextAccessor;
@@ -47,8 +43,7 @@ public class CallScript extends MethodOp
}
public static final String module = CallScript.class.getName();
- protected static final Object[] EMPTY_ARGS = {};
-
+
private ContextAccessor<List<Object>> errorListAcsr;
private String location;
private String method;
@@ -73,31 +68,14 @@ public class CallScript extends MethodOp
}
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")) {
- try {
- groovy.lang.Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), GroovyUtil.getBinding(context));
- if (UtilValidate.isEmpty(method)) {
- script.run();
- } else {
- script.invokeMethod(method, EMPTY_ARGS);
- }
- } catch (GeneralException e) {
- messages.add("Error running Groovy script at location [" + location + "]: " + e.getMessage());
- }
- } else if (location.endsWith(".xml")) {
+ if (location.endsWith(".xml")) {
try {
SimpleMethod.runSimpleMethod(location, method, methodContext);
} catch (MiniLangException e) {
messages.add("Error running simple method at location [" + location + "]: " + e.getMessage());
}
} else {
- messages.add("Unsupported script type [" + location + "]");
+ ScriptUtil.executeScript(this.location, this.method, context);
}
// update the method environment
Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/SetOperation.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/SetOperation.java?rev=1296762&r1=1296761&r2=1296762&view=diff
==============================================================================
--- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/SetOperation.java (original)
+++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/envops/SetOperation.java Sun Mar 4 07:24:47 2012
@@ -21,12 +21,7 @@ package org.ofbiz.minilang.method.envops
import javolution.util.FastList;
import javolution.util.FastMap;
-import org.codehaus.groovy.runtime.InvokerHelper;
-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.UtilValidate;
+import org.ofbiz.base.util.*;
import org.ofbiz.base.util.string.FlexibleStringExpander;
import org.ofbiz.minilang.SimpleMethod;
import org.ofbiz.minilang.method.ContextAccessor;
@@ -56,14 +51,14 @@ public class SetOperation extends Method
protected String type;
protected boolean setIfNull; // default to false
protected boolean setIfEmpty; // default to true
- protected Class<?> parsedGroovyScript = null;
+ protected Class<?> parsedScript = null;
public SetOperation(Element element, SimpleMethod simpleMethod) {
super(element, simpleMethod);
this.field = new ContextAccessor<Object>(element.getAttribute("field"));
String fromFieldStr = element.getAttribute("from-field");
if (fromFieldStr != null && fromFieldStr.startsWith("groovy:")) {
- this.parsedGroovyScript = GroovyUtil.parseClass(fromFieldStr.replace("groovy:", ""));
+ this.parsedScript = ScriptUtil.parseScript("groovy", fromFieldStr.replace("groovy:", ""));
}
this.fromField = new ContextAccessor<Object>(fromFieldStr);
this.valueExdr = FlexibleStringExpander.getInstance(element.getAttribute("value"));
@@ -82,8 +77,12 @@ public class SetOperation extends Method
@Override
public boolean exec(MethodContext methodContext) {
Object newValue = null;
- if (this.parsedGroovyScript != null) {
- newValue = InvokerHelper.createScript(this.parsedGroovyScript, GroovyUtil.getBinding(methodContext.getEnvMap())).run();
+ if (this.parsedScript != null) {
+ try {
+ newValue = ScriptUtil.evaluate("groovy", null, this.parsedScript, methodContext.getEnvMap());
+ } catch (Exception exc) {
+ Debug.logWarning(exc, "Error evaluating scriptlet [" + this + "]; error was: " + exc, module);
+ }
} else if (!this.fromField.isEmpty()) {
newValue = this.fromField.get(methodContext);
if (Debug.verboseOn()) Debug.logVerbose("In screen getting value for field from [" + this.fromField.toString() + "]: " + newValue, module);
Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java?rev=1296762&r1=1296761&r2=1296762&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java (original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/ModelWidgetAction.java Sun Mar 4 07:24:47 2012
@@ -34,12 +34,10 @@ import javolution.util.FastList;
import javolution.util.FastMap;
import org.w3c.dom.Element;
-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;
import org.ofbiz.base.util.ObjectType;
+import org.ofbiz.base.util.ScriptUtil;
import org.ofbiz.base.util.StringUtil;
import org.ofbiz.base.util.UtilGenerics;
import org.ofbiz.base.util.UtilProperties;
@@ -390,7 +388,6 @@ public abstract class ModelWidgetAction
}
public static class Script extends ModelWidgetAction {
- protected static final Object[] EMPTY_ARGS = {};
protected String location;
protected String method;
@@ -403,24 +400,7 @@ public abstract class ModelWidgetAction
@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")) {
- try {
- groovy.lang.Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), GroovyUtil.getBinding(context));
- if (UtilValidate.isEmpty(method)) {
- script.run();
- } else {
- script.invokeMethod(method, EMPTY_ARGS);
- }
- } catch (GeneralException e) {
- throw new GeneralException("Error running Groovy script at location [" + location + "]", e);
- }
- } else if (location.endsWith(".xml")) {
+ if (location.endsWith(".xml")) {
Map<String, Object> localContext = FastMap.newInstance();
localContext.putAll(context);
DispatchContext ctx = WidgetWorker.getDispatcher(context).getDispatchContext();
@@ -432,7 +412,7 @@ public abstract class ModelWidgetAction
throw new GeneralException("Error running simple method at location [" + location + "]", e);
}
} else {
- throw new GeneralException("For widget script actions the script type is not yet supported for location: [" + location + "]");
+ ScriptUtil.executeScript(this.location, this.method, context);
}
}
}
Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java?rev=1296762&r1=1296761&r2=1296762&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java (original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java Sun Mar 4 07:24:47 2012
@@ -31,12 +31,10 @@ import java.util.regex.PatternSyntaxExce
import javolution.util.FastList;
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;
import org.ofbiz.base.util.ObjectType;
+import org.ofbiz.base.util.ScriptUtil;
import org.ofbiz.base.util.UtilGenerics;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.base.util.UtilValidate;
@@ -274,7 +272,6 @@ public abstract class ModelFormAction {
}
public static class Script extends ModelFormAction {
- protected static final Object[] EMPTY_ARGS = {};
protected String location;
protected String method;
@@ -287,28 +284,7 @@ public abstract class ModelFormAction {
@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")) {
- try {
- groovy.lang.Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), GroovyUtil.getBinding(context));
- if (UtilValidate.isEmpty(method)) {
- script.run();
- } else {
- script.invokeMethod(method, EMPTY_ARGS);
- }
- } catch (GeneralException e) {
- String errMsg = "Error running Groovy script at location [" + location + "]: " + e.toString();
- Debug.logError(e, errMsg, module);
- throw new IllegalArgumentException(errMsg);
- }
- } else if (location.endsWith(".xml")) {
+ if (location.endsWith(".xml")) {
Map<String, Object> localContext = FastMap.newInstance();
localContext.putAll(context);
DispatchContext ctx = this.modelForm.dispatchContext;
@@ -320,7 +296,7 @@ public abstract class ModelFormAction {
throw new IllegalArgumentException("Error running simple method at location [" + location + "]", e);
}
} else {
- throw new IllegalArgumentException("For screen script actions the script type is not yet support for location:" + location);
+ ScriptUtil.executeScript(this.location, this.method, context);
}
}
}
Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java?rev=1296762&r1=1296761&r2=1296762&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java (original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/menu/ModelMenuAction.java Sun Mar 4 07:24:47 2012
@@ -31,13 +31,10 @@ import javax.servlet.http.HttpSession;
import javolution.util.FastList;
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;
import org.ofbiz.base.util.ObjectType;
+import org.ofbiz.base.util.ScriptUtil;
import org.ofbiz.base.util.UtilFormatOut;
import org.ofbiz.base.util.UtilGenerics;
import org.ofbiz.base.util.UtilProperties;
@@ -351,7 +348,6 @@ public abstract class ModelMenuAction {
}
public static class Script extends ModelMenuAction {
- protected static final Object[] EMPTY_ARGS = {};
protected String location;
protected String method;
@@ -364,30 +360,7 @@ public abstract class ModelMenuAction {
@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")) {
- try {
- groovy.lang.Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), GroovyUtil.getBinding(context));
- if (UtilValidate.isEmpty(method)) {
- script.run();
- } else {
- script.invokeMethod(method, EMPTY_ARGS);
- }
- } catch (GeneralException e) {
- String errMsg = "Error running Groovy script at location [" + location + "]: " + e.toString();
- Debug.logError(e, errMsg, module);
- throw new IllegalArgumentException(errMsg);
- }
- } else {
- throw new IllegalArgumentException("For screen script actions the script type is not yet support for location:" + location);
- }
+ ScriptUtil.executeScript(this.location, this.method, context);
}
}
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=1296762&r1=1296761&r2=1296762&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 Mar 4 07:24:47 2012
@@ -33,13 +33,10 @@ import javax.servlet.http.HttpSession;
import javolution.util.FastList;
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;
import org.ofbiz.base.util.ObjectType;
+import org.ofbiz.base.util.ScriptUtil;
import org.ofbiz.base.util.StringUtil;
import org.ofbiz.base.util.UtilGenerics;
import org.ofbiz.base.util.UtilProperties;
@@ -403,7 +400,6 @@ public abstract class ModelScreenAction
@Deprecated
public static class Script extends ModelScreenAction {
- protected static final Object[] EMPTY_ARGS = {};
protected String location;
protected String method;
@@ -416,24 +412,7 @@ public abstract class ModelScreenAction
@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")) {
- try {
- groovy.lang.Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(location), GroovyUtil.getBinding(context));
- if (UtilValidate.isEmpty(method)) {
- script.run();
- } else {
- script.invokeMethod(method, EMPTY_ARGS);
- }
- } catch (GeneralException e) {
- throw new GeneralException("Error running Groovy script at location [" + location + "]", e);
- }
- } else if (location.endsWith(".xml")) {
+ if (location.endsWith(".xml")) {
Map<String, Object> localContext = FastMap.newInstance();
localContext.putAll(context);
DispatchContext ctx = this.modelScreen.getDispatcher(context).getDispatchContext();
@@ -445,7 +424,7 @@ public abstract class ModelScreenAction
throw new GeneralException("Error running simple method at location [" + location + "]", e);
}
} else {
- throw new GeneralException("For screen script actions the script type is not yet supported for location: [" + location + "]");
+ ScriptUtil.executeScript(this.location, this.method, context);
}
}
}