Java Code Examples for groovy.lang.Script#invokeMethod()

The following examples show how to use groovy.lang.Script#invokeMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroovyUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public static Object runScriptAtLocation(String location, String methodName, Map<String, Object> context) throws GeneralException {
    if (!baseScriptInitialized) { initBaseScript(); } // SCIPIO
    Script script = InvokerHelper.createScript(getScriptClassFromLocation(location), getBinding(context));
    Object result = null;
    if (UtilValidate.isEmpty(methodName)) {
        result = script.run();
    } else {
        result = script.invokeMethod(methodName, new Object[] { context });
    }
    return result;
}
 
Example 2
Source File: GroovyEngine.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
private Map<String, Object> serviceInvoker(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");
    }
    Map<String, Object> params = new HashMap<>();
    params.putAll(context);

    Map<String, Object> gContext = new HashMap<>();
    gContext.putAll(context);
    gContext.put(ScriptUtil.PARAMETERS_KEY, params);

    DispatchContext dctx = dispatcher.getLocalContext(localName);
    gContext.put("dctx", dctx);
    gContext.put("security", dctx.getSecurity());
    gContext.put("dispatcher", dctx.getDispatcher());
    gContext.put("delegator", dispatcher.getDelegator());
    try {
        ScriptContext scriptContext = ScriptUtil.createScriptContext(gContext, protectedKeys);
        ScriptHelper scriptHelper = (ScriptHelper)scriptContext.getAttribute(ScriptUtil.SCRIPT_HELPER_KEY);
        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);
        }
        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));
        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);
    }
}