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/09 17:34:19 UTC
svn commit: r1298908 - in /ofbiz/trunk/framework:
base/src/org/ofbiz/base/util/ service/src/org/ofbiz/service/engine/
webapp/src/org/ofbiz/webapp/event/
Author: jacopoc
Date: Fri Mar 9 16:34:18 2012
New Revision: 1298908
URL: http://svn.apache.org/viewvc?rev=1298908&view=rev
Log:
Enhanced Groovy Event Handler to support the execution of a method (as an event) inside of a groovy source file (script class): the method can be specified in the "invoke" attribute.
Similarly to Groovy services, the Groovy Events Handler sets the base script class containing helper methods; these helper methods have been enhanced in order to be used by groovy method executed as services or events in a transparent way.
Modified:
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/GroovyUtil.java
ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy
ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java
ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/GroovyEventHandler.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=1298908&r1=1298907&r2=1298908&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 Fri Mar 9 16:34:18 2012
@@ -148,7 +148,11 @@ public class GroovyUtil {
}
public static Object runScriptAtLocation(String location, Map<String, Object> context) throws GeneralException {
- return InvokerHelper.createScript(getScriptClassFromLocation(location), getBinding(context)).run();
+ return runScriptAtLocation(location, context, null);
+ }
+
+ public static Object runScriptAtLocation(String location, Map<String, Object> context, GroovyClassLoader groovyClassLoader) throws GeneralException {
+ return InvokerHelper.createScript(getScriptClassFromLocation(location, groovyClassLoader), getBinding(context)).run();
}
public static Object runScriptFromClasspath(String script, Map<String,Object> context) throws GeneralException {
Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy?rev=1298908&r1=1298907&r2=1298908&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyBaseScript.groovy Fri Mar 9 16:34:18 2012
@@ -53,12 +53,18 @@ abstract class GroovyBaseScript extends
return genericValues;
}
- Map success(String message) {
+ def success(String message) {
// TODO: implement some clever i18n mechanism based on the userLogin and locale in the binding
- if (message) {
- return ServiceUtil.returnSuccess(message);
+ if (this.binding.getVariable('request')) {
+ // the script is invoked as an "event"
+ return 'success';
} else {
- return ServiceUtil.returnSuccess();
+ // the script is invoked as a "service"
+ if (message) {
+ return ServiceUtil.returnSuccess(message);
+ } else {
+ return ServiceUtil.returnSuccess();
+ }
}
}
Map failure(String message) {
@@ -69,12 +75,20 @@ abstract class GroovyBaseScript extends
return ServiceUtil.returnFailure();
}
}
- Map error(String message) {
+ def error(String message) {
// TODO: implement some clever i18n mechanism based on the userLogin and locale in the binding
- if (message) {
- return ServiceUtil.returnError(message);
+ if (this.binding.getVariable('request')) {
+ // the script is invoked as an "event"
+ if (message) {
+ this.binding.getVariable('request').setAttribute("_ERROR_MESSAGE_", message)
+ }
+ return 'error';
} else {
- return ServiceUtil.returnError();
+ if (message) {
+ return ServiceUtil.returnError(message);
+ } else {
+ return ServiceUtil.returnError();
+ }
}
}
def logInfo(String message) {
Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java?rev=1298908&r1=1298907&r2=1298908&view=diff
==============================================================================
--- ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java (original)
+++ ofbiz/trunk/framework/service/src/org/ofbiz/service/engine/GroovyEngine.java Fri Mar 9 16:34:18 2012
@@ -34,6 +34,7 @@ import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
import org.ofbiz.base.util.GroovyUtil;
import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.service.*;
import org.ofbiz.service.config.ServiceConfigUtil;
@@ -102,6 +103,8 @@ public final class GroovyEngine extends
} else if (context.get("result") != null && context.get("result") instanceof Map<?, ?>) {
return cast(context.get("result"));
}
+ } catch (GenericEntityException gee) {
+ return ServiceUtil.returnError(gee.getMessage());
} catch (ExecutionServiceException ese) {
return ServiceUtil.returnError(ese.getMessage());
} catch (GeneralException e) {
Modified: ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/GroovyEventHandler.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/GroovyEventHandler.java?rev=1298908&r1=1298907&r2=1298908&view=diff
==============================================================================
--- ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/GroovyEventHandler.java (original)
+++ ofbiz/trunk/framework/webapp/src/org/ofbiz/webapp/event/GroovyEventHandler.java Fri Mar 9 16:34:18 2012
@@ -25,19 +25,42 @@ import javax.servlet.http.HttpServletReq
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
+import groovy.lang.GroovyClassLoader;
+import groovy.lang.Script;
+import org.codehaus.groovy.control.CompilerConfiguration;
+import org.codehaus.groovy.runtime.InvokerHelper;
import javolution.util.FastMap;
+import org.ofbiz.base.config.GenericConfigException;
+import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GroovyUtil;
import org.ofbiz.base.util.UtilHttp;
import org.ofbiz.base.util.UtilMisc;
+import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.entity.GenericEntityException;
+import org.ofbiz.service.ExecutionServiceException;
+import org.ofbiz.service.config.ServiceConfigUtil;
import org.ofbiz.webapp.control.ConfigXMLReader.Event;
import org.ofbiz.webapp.control.ConfigXMLReader.RequestMap;
public class GroovyEventHandler implements EventHandler {
public static final String module = GroovyEventHandler.class.getName();
+ protected static final Object[] EMPTY_ARGS = {};
+ private GroovyClassLoader groovyClassLoader;
public void init(ServletContext context) throws EventHandlerException {
+ try {
+ // TODO: the name of the script base class is currently retrieved from the Groovy service engine configuration
+ String scriptBaseClass = ServiceConfigUtil.getEngineParameter("groovy", "scriptBaseClass");
+ if (scriptBaseClass != null) {
+ CompilerConfiguration conf = new CompilerConfiguration();
+ conf.setScriptBaseClass(scriptBaseClass);
+ groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader(), conf);
+ }
+ } catch (GenericConfigException gce) {
+ Debug.logWarning(gce, "Error retrieving the configuration for the groovy service engine: ", module);
+ }
}
public String invoke(Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException {
@@ -56,7 +79,19 @@ public class GroovyEventHandler implemen
groovyContext.put("userLogin", session.getAttribute("userLogin"));
groovyContext.put("parameters", UtilHttp.getCombinedMap(request, UtilMisc.toSet("delegator", "dispatcher", "security", "locale", "timeZone", "userLogin")));
- Object result = GroovyUtil.runScriptAtLocation(event.path + event.invoke, groovyContext);
+ Object result = null;
+ try {
+ Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(event.path, groovyClassLoader), GroovyUtil.getBinding(groovyContext));
+ if (UtilValidate.isEmpty(event.invoke)) {
+ result = script.run();
+ } else {
+ result = script.invokeMethod(event.invoke, EMPTY_ARGS);
+ }
+ } catch (GenericEntityException gee) {
+ return "error";
+ } catch (ExecutionServiceException ese) {
+ return "error";
+ }
// check the result
if (result != null && !(result instanceof String)) {
throw new EventHandlerException("Event did not return a String result, it returned a " + result.getClass().getName());