Skip to content
Permalink
Browse files

Improved: Inline ‘serviceInvoker’ method in Groovy engine (OFBIZ-10808)

The ‘runSync’ method of the Groovy engine was only passing its
arguments to the serviceInvoker method which was private and used only
by the ‘runSync’ method.  By inlining this private method the work is
now done directly in the ‘runSync’ method.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1854656 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Mar 2, 2019
1 parent 4af4441 commit f58f0697ed2dacaf1cd195be420bd60b5e96bee1
Showing with 20 additions and 19 deletions.
  1. +20 −19 framework/service/src/main/java/org/apache/ofbiz/service/engine/GroovyEngine.java
@@ -75,15 +75,9 @@ public void runSyncIgnore(String localName, ModelService modelService, Map<Strin
runSync(localName, modelService, context);
}

/**
* @see org.apache.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.apache.ofbiz.service.ModelService, java.util.Map)
*/
@Override
public Map<String, Object> runSync(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException {
return serviceInvoker(localName, modelService, context);
}

private Map<String, Object> serviceInvoker(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException {
public Map<String, Object> runSync(String localName, ModelService modelService, Map<String, Object> context)
throws GenericServiceException {
if (UtilValidate.isEmpty(modelService.location)) {
throw new GenericServiceException("Cannot run Groovy service with empty location");
}
@@ -105,28 +99,35 @@ public void runSyncIgnore(String localName, ModelService modelService, Map<Strin
if (scriptHelper != null) {
gContext.put(ScriptUtil.SCRIPT_HELPER_KEY, scriptHelper);
}
Script script = InvokerHelper.createScript(GroovyUtil.getScriptClassFromLocation(this.getLocation(modelService)), GroovyUtil.getBinding(gContext));
Object resultObj = null;
if (UtilValidate.isEmpty(modelService.invoke)) {
resultObj = script.run();
} else {
resultObj = script.invokeMethod(modelService.invoke, EMPTY_ARGS);
}

Script script = InvokerHelper.createScript(
GroovyUtil.getScriptClassFromLocation(getLocation(modelService)),
GroovyUtil.getBinding(gContext));

// Groovy services can either be implemented as a stand-alone script or with a method inside a script.
Object resultObj = UtilValidate.isEmpty(modelService.invoke)
? script.run()
: script.invokeMethod(modelService.invoke, EMPTY_ARGS);

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), ModelService.OUT_PARAM));
result.putAll(modelService.makeValid(
scriptContext.getBindings(ScriptContext.ENGINE_SCOPE),
ModelService.OUT_PARAM));
return result;
} catch (GeneralException ge) {
throw new GenericServiceException(ge);
} catch (Exception e) {
// detailMessage can be null. If it is null, the exception won't be properly returned and logged, and that will
// make spotting problems very difficult. Disabling this for now in favor of returning a proper exception.
throw new GenericServiceException("Error running Groovy method [" + modelService.invoke + "] in Groovy file [" + modelService.location + "]: ", e);
// detailMessage can be null. If it is null, the exception won't be properly returned and logged,
// and that will make spotting problems very difficult.
// Disabling this for now in favor of returning a proper exception.
throw new GenericServiceException("Error running Groovy method [" + modelService.invoke + "]"
+ " in Groovy file [" + modelService.location + "]: ", e);
}
}
}

0 comments on commit f58f069

Please sign in to comment.
You can’t perform that action at this time.