Skip to content

apijson function支持脚本引擎,比如JavaScript、lua等 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/main/java/apijson/framework/APIJSONFunctionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.IOException;
import java.rmi.ServerException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -43,6 +44,8 @@
import apijson.StringUtil;
import apijson.orm.AbstractFunctionParser;
import apijson.orm.JSONRequest;
import apijson.orm.script.JavaScriptExecutor;
import apijson.orm.script.ScriptExecutor;
import unitauto.MethodUtil;
import unitauto.MethodUtil.Argument;

Expand Down Expand Up @@ -206,7 +209,17 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
if (JSONResponse.isSuccess(response) == false) {
onServerError("\n\n\n\n\n !!!! 查询远程函数异常 !!!\n" + response.getString(JSONResponse.KEY_MSG) + "\n\n\n\n\n", shutdownWhenServerError);
}

//初始化默认脚本引擎,避免增量
if(SCRIPT_EXECUTOR_MAP.get("js") == null || isAll) {
ScriptExecutor javaScriptExecutor = new JavaScriptExecutor();
javaScriptExecutor.init();
SCRIPT_EXECUTOR_MAP.put("js", javaScriptExecutor);
SCRIPT_EXECUTOR_MAP.put("JavaScript", javaScriptExecutor);
SCRIPT_EXECUTOR_MAP.put("javascript", javaScriptExecutor);
}

Map<String, JSONObject> scriptMap = new HashMap<>();
JSONArray scriptList = response.getJSONArray("[]"); // response.getJSONArray(SCRIPT_ + "[]");
if (scriptList != null && scriptList.isEmpty() == false) {
//if (isAll) {
Expand All @@ -230,11 +243,10 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
if (StringUtil.isEmpty(s, true)) {
onServerError("Script 表字段 script 的值 " + s + " 不合法!不能为空!", shutdownWhenServerError);
}

newMap.put(n, item);
}

SCRIPT_MAP = newMap;
scriptMap = newMap;
}

JSONArray list = scriptList; // response.getJSONArray(FUNCTION_ + "[]");
Expand Down Expand Up @@ -267,7 +279,15 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
}
// demo.put(JSONRequest.KEY_TAG, item.getString(JSONRequest.KEY_TAG));
// demo.put(JSONRequest.KEY_VERSION, item.getInteger(JSONRequest.KEY_VERSION));

//加载脚本
if (item.get("language") != null) {
String language = item.getString("language");
if(SCRIPT_EXECUTOR_MAP.get(language) == null) {
onServerError("找不到脚本语言 " + language + " 对应的执行引擎!请先依赖相关库并在后端 APIJSONFunctionParser 中注册!", shutdownWhenServerError);
}
ScriptExecutor scriptExecutor = SCRIPT_EXECUTOR_MAP.get(language);
scriptExecutor.load(name, scriptMap.get(name).getString("script"));
}
newMap.put(name, item); // 必须在测试 invoke 前把配置 put 进 FUNCTION_MAP!

String[] methods = StringUtil.split(item.getString("methods"));
Expand Down