Author: jacopoc
Date: Thu Apr 12 13:42:57 2012
New Revision: 1325253
URL: http://svn.apache.org/viewvc?rev=1325253&view=rev
Log:
Modified Groovy service engine and Groovy event handler to act in the same way
of the "Script" (JSR 223) engine and handler: now they can execute the same
exact Grrovy services and events.
In order to use these engine and event rather than the one that uses JSR223:
* for services (service definition): set the engine to "groovy" rather than
"script"
* for events (event element in the controller): set the event type to
"groovy" rather than "script"
The main advantage of using them is that it is possible to attach a debugger
to the Groovy methods.
Modified:
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/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=1325253&r1=1325252&r2=1325253&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
Thu Apr 12 13:42:57 2012
@@ -23,21 +23,24 @@ import static org.ofbiz.base.util.UtilGe
import groovy.lang.GroovyClassLoader;
import groovy.lang.Script;
+import java.util.Collections;
+import java.util.HashSet;
import java.util.Map;
+import java.util.Set;
import javolution.util.FastMap;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.ofbiz.base.config.GenericConfigException;
-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.base.util.*;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.service.*;
import org.ofbiz.service.config.ServiceConfigUtil;
+import javax.script.ScriptContext;
+import javax.script.ScriptException;
+
/**
* Groovy Script Service Engine
*/
@@ -45,9 +48,21 @@ public final class GroovyEngine extends
public static final String module = GroovyEngine.class.getName();
protected static final Object[] EMPTY_ARGS = {};
+ private static final Set<String> protectedKeys = createProtectedKeys();
GroovyClassLoader groovyClassLoader;
+ private static Set<String> createProtectedKeys() {
+ Set<String> newSet = new HashSet<String>();
+ /* Commenting out for now because some scripts write to the parameters
Map - which should not be allowed.
+ newSet.add(ScriptUtil.PARAMETERS_KEY);
+ */
+ newSet.add("dctx");
+ newSet.add("dispatcher");
+ newSet.add("delegator");
+ return Collections.unmodifiableSet(newSet);
+ }
+
public GroovyEngine(ServiceDispatcher dispatcher) {
super(dispatcher);
try {
@@ -84,13 +99,18 @@ public final class GroovyEngine extends
}
Map<String, Object> params = FastMap.newInstance();
params.putAll(context);
- context.put("parameters", params);
+ context.put(ScriptUtil.PARAMETERS_KEY, params);
DispatchContext dctx = dispatcher.getLocalContext(localName);
context.put("dctx", dctx);
context.put("dispatcher", dctx.getDispatcher());
context.put("delegator", dispatcher.getDelegator());
try {
+ ScriptContext scriptContext =
ScriptUtil.createScriptContext(context, protectedKeys);
+ ScriptHelper scriptHelper =
(ScriptHelper)scriptContext.getAttribute(ScriptUtil.SCRIPT_HELPER_KEY);
+ if (scriptHelper != null) {
+ context.put(ScriptUtil.SCRIPT_HELPER_KEY, scriptHelper);
+ }
Script script =
InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(this.getLocation(modelService),
groovyClassLoader), GroovyUtil.getBinding(context));
Object resultObj = null;
if (UtilValidate.isEmpty(modelService.invoke)) {
@@ -98,18 +118,26 @@ public final class GroovyEngine extends
} else {
resultObj = script.invokeMethod(modelService.invoke,
EMPTY_ARGS);
}
+ /*
if (resultObj != null && resultObj instanceof Map<?, ?>) {
return cast(resultObj);
} 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) {
- throw new GenericServiceException(e);
+ */
+ if (resultObj == null) {
+ resultObj = scriptContext.getAttribute(ScriptUtil.RESULT_KEY);
+ }
+ if (resultObj != null && resultObj instanceof Map<?, ?>) {
+ return cast(resultObj);
+ }
+ Map<String, Object> result = ServiceUtil.returnSuccess();
+
result.putAll(modelService.makeValid(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE),
"OUT"));
+ return result;
+ } catch (GeneralException ge) {
+ throw new GenericServiceException(ge);
+ } catch (Exception e) {
+ return ServiceUtil.returnError(e.getMessage());
}
- return ServiceUtil.returnSuccess();
}
}
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=1325253&r1=1325252&r2=1325253&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
Thu Apr 12 13:42:57 2012
@@ -18,8 +18,9 @@
*/
package org.ofbiz.webapp.event;
-import java.util.Map;
+import java.util.*;
+import javax.script.ScriptContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -32,11 +33,7 @@ import org.codehaus.groovy.runtime.Invok
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.base.util.*;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.service.ExecutionServiceException;
import org.ofbiz.service.config.ServiceConfigUtil;
@@ -47,6 +44,25 @@ public class GroovyEventHandler implemen
public static final String module = GroovyEventHandler.class.getName();
protected static final Object[] EMPTY_ARGS = {};
+ private static final Set<String> protectedKeys = createProtectedKeys();
+
+ private static Set<String> createProtectedKeys() {
+ Set<String> newSet = new HashSet<String>();
+ newSet.add("request");
+ newSet.add("response");
+ newSet.add("session");
+ newSet.add("dispatcher");
+ newSet.add("delegator");
+ newSet.add("security");
+ newSet.add("locale");
+ newSet.add("timeZone");
+ newSet.add("userLogin");
+ /* Commenting out for now because some scripts write to the parameters
Map - which should not be allowed.
+ newSet.add(ScriptUtil.PARAMETERS_KEY);
+ */
+ return Collections.unmodifiableSet(newSet);
+ }
+
private GroovyClassLoader groovyClassLoader;
public void init(ServletContext context) throws EventHandlerException {
@@ -65,34 +81,52 @@ public class GroovyEventHandler implemen
public String invoke(Event event, RequestMap requestMap,
HttpServletRequest request, HttpServletResponse response) throws
EventHandlerException {
try {
- Map<String, Object> groovyContext = FastMap.newInstance();
- groovyContext.put("request", request);
- groovyContext.put("response", response);
+ Map<String, Object> context = new HashMap<String, Object>();
+ context.put("request", request);
+ context.put("response", response);
HttpSession session = request.getSession();
- groovyContext.put("session", session);
-
- groovyContext.put("dispatcher",
request.getAttribute("dispatcher"));
- groovyContext.put("delegator", request.getAttribute("delegator"));
- groovyContext.put("security", request.getAttribute("security"));
- groovyContext.put("locale", UtilHttp.getLocale(request));
- groovyContext.put("timeZone", UtilHttp.getTimeZone(request));
- groovyContext.put("userLogin", session.getAttribute("userLogin"));
- groovyContext.put("parameters", UtilHttp.getCombinedMap(request,
UtilMisc.toSet("delegator", "dispatcher", "security", "locale", "timeZone",
"userLogin")));
-
+ context.put("session", session);
+ context.put("dispatcher", request.getAttribute("dispatcher"));
+ context.put("delegator", request.getAttribute("delegator"));
+ context.put("security", request.getAttribute("security"));
+ context.put("locale", UtilHttp.getLocale(request));
+ context.put("timeZone", UtilHttp.getTimeZone(request));
+ context.put("userLogin", session.getAttribute("userLogin"));
+ context.put(ScriptUtil.PARAMETERS_KEY,
UtilHttp.getCombinedMap(request, UtilMisc.toSet("delegator", "dispatcher",
"security", "locale", "timeZone", "userLogin")));
Object result = null;
try {
- Script script =
InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(event.path,
groovyClassLoader), GroovyUtil.getBinding(groovyContext));
+ ScriptContext scriptContext =
ScriptUtil.createScriptContext(context, protectedKeys);
+ ScriptHelper scriptHelper =
(ScriptHelper)scriptContext.getAttribute(ScriptUtil.SCRIPT_HELPER_KEY);
+ if (scriptHelper != null) {
+ context.put(ScriptUtil.SCRIPT_HELPER_KEY, scriptHelper);
+ }
+ Script script =
InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(event.path,
groovyClassLoader), GroovyUtil.getBinding(context));
if (UtilValidate.isEmpty(event.invoke)) {
result = script.run();
} else {
result = script.invokeMethod(event.invoke, EMPTY_ARGS);
}
- } catch (GenericEntityException gee) {
- return "error";
- } catch (ExecutionServiceException ese) {
+ if (result == null) {
+ result = scriptContext.getAttribute(ScriptUtil.RESULT_KEY);
+ }
+ } catch (Exception e) {
+ Debug.logWarning(e, "Error running event " + event.path + ":
", module);
+ request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
return "error";
}
// check the result
+ if (result instanceof Map) {
+ Map resultMap = (Map)result;
+ String successMessage =
(String)resultMap.get("_event_message_");
+ if (successMessage != null) {
+ request.setAttribute("_EVENT_MESSAGE_", successMessage);
+ }
+ String errorMessage = (String)resultMap.get("_error_message_");
+ if (errorMessage != null) {
+ request.setAttribute("_ERROR_MESSAGE_", errorMessage);
+ }
+ return (String)resultMap.get("_response_code_");
+ }
if (result != null && !(result instanceof String)) {
throw new EventHandlerException("Event did not return a String
result, it returned a " + result.getClass().getName());
}