All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.script.engine.common.CommonScriptEngine Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.script.engine.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.commons.crypto.Md5;
import org.zodiac.commons.util.Colls;
import org.zodiac.commons.util.ObjectUtil;
import org.zodiac.script.engine.ExecuteResult;
import org.zodiac.script.engine.ListenerSupportEngine;
import org.zodiac.script.engine.ScriptListener;

import javax.script.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class CommonScriptEngine extends ListenerSupportEngine {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    // 编译器
    protected Compilable compilable;
    // 脚本库
    protected Map scriptBase = new ConcurrentHashMap<>();

    protected Bindings utilBindings;

    protected List scriptListeners;

    public abstract String getScriptName();

    @Override
    public boolean compiled(String id) {
        return scriptBase.containsKey(id);
    }

    @Override
    public boolean remove(String id) {
        return scriptBase.remove(id) != null;
    }

    public CommonScriptEngine() {
        try {
            init();
        } catch (Exception e) {
            logger.warn("init {} error", getScriptName());
        }
    }

    @Override
    public void init(String... contents) throws Exception {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName(getScriptName());
        compilable = (Compilable)engine;
        utilBindings = engine.createBindings();
        CompiledScript script;
        for (String content : contents) {
            script = compilable.compile(content);
            script.eval(utilBindings);
        }
        // scriptBase.clear();
    }

    @Override
    public org.zodiac.script.engine.ScriptContext getContext(String id) {
        return scriptBase.get(id);
    }

    @Override
    public boolean compile(String id, String code) throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("compile {} {} : {}", getScriptName(), id, code);
        }
        if (compilable == null)
            init();
        CompiledScript compiledScript = compilable.compile(code);
        CommonScriptContext scriptContext = new CommonScriptContext(id, Md5.md5Hex(code), compiledScript);
        scriptBase.put(id, scriptContext);
        return true;
    }

    @Override
    public void addListener(ScriptListener scriptListener) {
        scriptListeners = ObjectUtil.defaultIfNull(scriptListeners, Colls.linkedList());
        scriptListeners.add(scriptListener);
    }

    @Override
    public ExecuteResult execute(String id) {
        return execute(id, Colls.map());
    }

    @Override
    public ExecuteResult execute(String id, Map param) {
        long startTime = System.currentTimeMillis();
        if (logger.isDebugEnabled()) {
            logger.debug("execute {} {} : {}", getScriptName(), id);
        }
        ExecuteResult result = new ExecuteResult();
        CommonScriptContext scriptContext = scriptBase.get(id);
        try {
            if (scriptContext != null) {
                doListenerBefore(scriptContext);
                ScriptContext context = new SimpleScriptContext();
                context.setBindings(utilBindings, ScriptContext.GLOBAL_SCOPE);

                for (Map.Entry entry : param.entrySet()) {
                    context.setAttribute(entry.getKey(), entry.getValue(), ScriptContext.ENGINE_SCOPE);
                }
                result.setResult(scriptContext.getScript().eval(context));
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
                result.setResult(null);
                result.setMessage(String.format("script(%s): %s not found!", getScriptName(), id));
            }
        } catch (ScriptException e) {
            result.setException(e);
        }
        result.setUseTime(System.currentTimeMillis() - startTime);
        doListenerAfter(scriptContext, result);
        return result;
    }

    @Override
    public void addGlobalVariable(Map vars) {
        utilBindings.putAll(vars);
    }

    protected class CommonScriptContext extends org.zodiac.script.engine.ScriptContext {
        private CompiledScript script;

        public CommonScriptContext(String id, String md5, CompiledScript script) {
            super(id, md5);
            this.script = script;
        }

        public CompiledScript getScript() {
            return script;
        }
    }

    public Bindings getUtilBindings() {
        return utilBindings;
    }

    public void setUtilBindings(Bindings utilBindings) {
        this.utilBindings = utilBindings;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy