Ejemplo n.º 1
0
  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 = FastMap.newInstance();
    params.putAll(context);
    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)) {
        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), "OUT"));
      return result;
    } catch (GeneralException ge) {
      throw new GenericServiceException(ge);
    } catch (Exception e) {
      return ServiceUtil.returnError(e.getMessage());
    }
  }