public void process()

in saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/handlers/ScriptTaskStateHandler.java [57:130]


    public void process(ProcessContext context) throws EngineExecutionException {

        StateInstruction instruction = context.getInstruction(StateInstruction.class);
        ScriptTaskStateImpl state = (ScriptTaskStateImpl) instruction.getState(context);

        String scriptType = state.getScriptType();
        String scriptContent = state.getScriptContent();

        Object result;
        try {
            List<Object> input = (List<Object>) context.getVariable(DomainConstants.VAR_NAME_INPUT_PARAMS);

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug(">>>>>>>>>>>>>>>>>>>>>> Start to execute ScriptTaskState[{}], ScriptType[{}], Input:{}",
                        state.getName(), scriptType, input);
            }

            StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(
                    DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);

            ScriptEngine scriptEngine = getScriptEngineFromCache(scriptType, stateMachineConfig.getScriptEngineManager());
            if (scriptEngine == null) {
                throw new EngineExecutionException("No such ScriptType[" + scriptType + "]",
                        FrameworkErrorCode.ObjectNotExists);
            }

            Bindings bindings = null;
            Map<String, Object> inputMap = null;
            if (CollectionUtils.isNotEmpty(input) && input.get(0) instanceof Map) {
                inputMap = (Map<String, Object>) input.get(0);
            }
            List<Object> inputExps = state.getInput();
            if (CollectionUtils.isNotEmpty(inputExps) && inputExps.get(0) instanceof Map) {
                Map<String, Object> inputExpMap = (Map<String, Object>) inputExps.get(0);
                if (inputExpMap.size() > 0) {
                    bindings = new SimpleBindings();
                    for (String property : inputExpMap.keySet()) {
                        if (inputMap != null && inputMap.containsKey(property)) {
                            bindings.put(property, inputMap.get(property));
                        } else {
                            //if we do not bind the null value property, groovy will throw MissingPropertyException
                            bindings.put(property, null);
                        }
                    }
                }
            }
            if (bindings != null) {
                result = scriptEngine.eval(scriptContent, bindings);
            }
            else {
                result = scriptEngine.eval(scriptContent);
            }

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("<<<<<<<<<<<<<<<<<<<<<< ScriptTaskState[{}], ScriptType[{}], Execute finish. result: {}",
                        state.getName(), scriptType, result);
            }

            if (result != null) {
                ((HierarchicalProcessContext) context).setVariableLocally(DomainConstants.VAR_NAME_OUTPUT_PARAMS,
                        result);
            }

        } catch (Throwable e) {

            LOGGER.error("<<<<<<<<<<<<<<<<<<<<<< ScriptTaskState[{}], ScriptTaskState[{}] Execute failed.",
                    state.getName(), scriptType, e);

            ((HierarchicalProcessContext) context).setVariableLocally(DomainConstants.VAR_NAME_CURRENT_EXCEPTION, e);

            EngineUtils.handleException(context, state, e);
        }

    }