|
| 1 | +package apijson.orm.script; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.concurrent.ConcurrentHashMap; |
| 6 | + |
| 7 | +import javax.script.Bindings; |
| 8 | +import javax.script.Compilable; |
| 9 | +import javax.script.CompiledScript; |
| 10 | +import javax.script.ScriptEngine; |
| 11 | +import javax.script.ScriptEngineManager; |
| 12 | +import javax.script.SimpleBindings; |
| 13 | + |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +import com.alibaba.fastjson.JSONObject; |
| 18 | + |
| 19 | +import apijson.orm.AbstractFunctionParser; |
| 20 | + |
| 21 | +/** |
| 22 | + * JSR223 script engine的统一实现抽象类 |
| 23 | + */ |
| 24 | +public abstract class JSR223ScriptExecutor implements ScriptExecutor { |
| 25 | + |
| 26 | + protected final Logger log = LoggerFactory.getLogger(this.getClass()); |
| 27 | + |
| 28 | + protected ScriptEngine scriptEngine; |
| 29 | + |
| 30 | + private final Map<String, CompiledScript> compiledScriptMap = new ConcurrentHashMap<>(); |
| 31 | + |
| 32 | + @Override |
| 33 | + public ScriptExecutor init() { |
| 34 | + ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); |
| 35 | + scriptEngine = scriptEngineManager.getEngineByName(scriptEngineName()); |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + protected abstract String scriptEngineName(); |
| 40 | + |
| 41 | + protected abstract Object extendParameter(AbstractFunctionParser parser, JSONObject currentObject, String methodName, Object[] args); |
| 42 | + |
| 43 | + protected abstract boolean isLockScript(String methodName); |
| 44 | + |
| 45 | + protected String convertScript(String script) { |
| 46 | + return script; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void load(String name, String script) { |
| 51 | + try { |
| 52 | + CompiledScript compiledScript = ((Compilable) scriptEngine).compile(convertScript(script)); |
| 53 | + compiledScriptMap.put(name, compiledScript); |
| 54 | + } catch (Exception e) { |
| 55 | + e.printStackTrace(); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public Object execute(AbstractFunctionParser parser, JSONObject currentObject, String methodName, Object[] args) throws Exception { |
| 62 | + CompiledScript compiledScript = compiledScriptMap.get(methodName); |
| 63 | + Bindings bindings = new SimpleBindings(); |
| 64 | + // 往脚本上下文里放入元数据 |
| 65 | + // 把 RequestMethod method, String tag, int version, @NotNull JSONObject request, |
| 66 | + // HttpSession session 等参数作为全局参数传进去供脚本使用 |
| 67 | + |
| 68 | + // 加载扩展属性 |
| 69 | + Object extendParameter = this.extendParameter(parser, currentObject, methodName, args); |
| 70 | + if(extendParameter != null) { |
| 71 | + bindings.put("extParam", extendParameter); |
| 72 | + } |
| 73 | + |
| 74 | + Map<String, Object> metaMap = new HashMap<>(); |
| 75 | + metaMap.put("version", parser.getVersion()); |
| 76 | + metaMap.put("tag", parser.getTag()); |
| 77 | + metaMap.put("args", args); |
| 78 | + bindings.put("_meta", metaMap); |
| 79 | + return compiledScript.eval(bindings); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void cleanCache() { |
| 84 | + compiledScriptMap.clear(); |
| 85 | + } |
| 86 | +} |
0 commit comments