Skip to content

Commit ec87dff

Browse files
committed
1.优化部分代码
2.处理远程函数language列内容不是SCRIPT_EXECUTOR_MAP中的内容时报错的问题 3.处理远程函数加载中demo和test不通过,导致无法加载之后的远程函数,从而引起无法调用相应远程函数的问题
1 parent bd78636 commit ec87dff

1 file changed

Lines changed: 48 additions & 48 deletions

File tree

src/main/java/apijson/framework/APIJSONFunctionParser.java

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414

1515
package apijson.framework;
1616

17-
import static apijson.RequestMethod.DELETE;
18-
import static apijson.RequestMethod.GET;
19-
import static apijson.RequestMethod.GETS;
20-
import static apijson.RequestMethod.HEAD;
21-
import static apijson.RequestMethod.HEADS;
22-
import static apijson.RequestMethod.POST;
23-
import static apijson.RequestMethod.PUT;
24-
import static apijson.framework.APIJSONConstant.FUNCTION_;
25-
import static apijson.framework.APIJSONConstant.SCRIPT_;
17+
import apijson.*;
18+
import apijson.orm.AbstractFunctionParser;
19+
import apijson.orm.JSONRequest;
20+
import apijson.orm.script.JavaScriptExecutor;
21+
import apijson.orm.script.ScriptExecutor;
22+
import com.alibaba.fastjson.JSONArray;
23+
import com.alibaba.fastjson.JSONObject;
24+
import jakarta.servlet.http.HttpSession;
25+
import unitauto.MethodUtil;
26+
import unitauto.MethodUtil.Argument;
2627

2728
import java.io.IOException;
2829
import java.rmi.ServerException;
@@ -31,23 +32,9 @@
3132
import java.util.List;
3233
import java.util.Map;
3334

34-
import jakarta.servlet.http.HttpSession;
35-
36-
import com.alibaba.fastjson.JSONArray;
37-
import com.alibaba.fastjson.JSONObject;
38-
39-
import apijson.JSON;
40-
import apijson.JSONResponse;
41-
import apijson.Log;
42-
import apijson.NotNull;
43-
import apijson.RequestMethod;
44-
import apijson.StringUtil;
45-
import apijson.orm.AbstractFunctionParser;
46-
import apijson.orm.JSONRequest;
47-
import apijson.orm.script.JavaScriptExecutor;
48-
import apijson.orm.script.ScriptExecutor;
49-
import unitauto.MethodUtil;
50-
import unitauto.MethodUtil.Argument;
35+
import static apijson.RequestMethod.*;
36+
import static apijson.framework.APIJSONConstant.FUNCTION_;
37+
import static apijson.framework.APIJSONConstant.SCRIPT_;
5138

5239

5340
/**可远程调用的函数类
@@ -188,7 +175,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
188175

189176
{ // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
190177
JSONObject function = isAll ? new JSONRequest() : table;
191-
if (Log.DEBUG == false) {
178+
if (!Log.DEBUG) {
192179
function.put(APIJSONConstant.KEY_DEBUG, 0);
193180
}
194181
item.put(FUNCTION_, function);
@@ -206,7 +193,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
206193

207194

208195
JSONObject response = creator.createParser().setMethod(GET).setNeedVerify(true).parseResponse(request);
209-
if (JSONResponse.isSuccess(response) == false) {
196+
if (!JSONResponse.isSuccess(response)) {
210197
onServerError("\n\n\n\n\n !!!! 查询远程函数异常 !!!\n" + response.getString(JSONResponse.KEY_MSG) + "\n\n\n\n\n", shutdownWhenServerError);
211198
}
212199

@@ -221,7 +208,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
221208

222209
Map<String, JSONObject> scriptMap = new HashMap<>();
223210
JSONArray scriptList = response.getJSONArray("[]"); // response.getJSONArray(SCRIPT_ + "[]");
224-
if (scriptList != null && scriptList.isEmpty() == false) {
211+
if (scriptList != null && !scriptList.isEmpty()) {
225212
//if (isAll) {
226213
// SCRIPT_MAP = new LinkedHashMap<>();
227214
//}
@@ -235,7 +222,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
235222
}
236223

237224
String n = item.getString("name");
238-
if (StringUtil.isName(n) == false) {
225+
if (!StringUtil.isName(n)) {
239226
onServerError("Script 表字段 name 的值 " + n + " 不合法!必须为合法的方法名字符串!", shutdownWhenServerError);
240227
}
241228

@@ -251,7 +238,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
251238

252239
JSONArray list = scriptList; // response.getJSONArray(FUNCTION_ + "[]");
253240
int size = list == null ? 0 : list.size();
254-
if (isAll && size <= 0) {
241+
if (isAll && size == 0) {
255242
Log.w(TAG, "init isAll && size <= 0,,没有可用的远程函数");
256243
return response;
257244
}
@@ -271,42 +258,55 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
271258

272259
JSONObject demo = JSON.parseObject(item.getString("demo"));
273260
if (demo == null) {
274-
onServerError("字段 demo 的值必须为合法且非 null 的 JSONObejct 字符串!", shutdownWhenServerError);
261+
try {
262+
onServerError("字段 demo 的值必须为合法且非 null 的 JSONObejct 字符串!", shutdownWhenServerError);
263+
} catch (Exception e) {
264+
throw new RuntimeException(e);
265+
}
275266
}
276267
String name = item.getString("name");
277-
if (demo.containsKey("result()") == false) {
278-
demo.put("result()", getFunctionCall(name, item.getString("arguments")));
279-
}
280-
// demo.put(JSONRequest.KEY_TAG, item.getString(JSONRequest.KEY_TAG));
268+
// demo.put(JSONRequest.KEY_TAG, item.getString(JSONRequest.KEY_TAG));
281269
// demo.put(JSONRequest.KEY_VERSION, item.getInteger(JSONRequest.KEY_VERSION));
282270
//加载脚本
283271
if (item.get("language") != null) {
284272
String language = item.getString("language");
285-
if (SCRIPT_EXECUTOR_MAP.get(language) == null) {
286-
onServerError("找不到脚本语言 " + language + " 对应的执行引擎!请先依赖相关库并在后端 APIJSONFunctionParser<T> 中注册!", shutdownWhenServerError);
273+
// if (SCRIPT_EXECUTOR_MAP.get(language) == null) {
274+
// onServerError("找不到脚本语言 " + language + " 对应的执行引擎!请先依赖相关库并在后端 APIJSONFunctionParser<T> 中注册!", shutdownWhenServerError);
275+
// }
276+
//脚本语言执行
277+
if (SCRIPT_EXECUTOR_MAP.containsKey(language)){
278+
ScriptExecutor scriptExecutor = SCRIPT_EXECUTOR_MAP.get(language);
279+
scriptExecutor.load(name, scriptMap.get(name).getString("script"));
287280
}
288-
ScriptExecutor scriptExecutor = SCRIPT_EXECUTOR_MAP.get(language);
289-
scriptExecutor.load(name, scriptMap.get(name).getString("script"));
290281
}
291282
newMap.put(name, item); // 必须在测试 invoke 前把配置 put 进 FUNCTION_MAP!
292283

293284
String[] methods = StringUtil.split(item.getString("methods"));
294285

295-
if (methods == null || methods.length <= 0) {
286+
if (methods == null || methods.length == 0) {
296287
methods = ALL_METHODS;
297288
}
298289

299-
demo.put(JSONRequest.KEY_TAG, item.get(JSONRequest.KEY_TAG));
300-
demo.put(JSONRequest.KEY_VERSION, item.get(JSONRequest.KEY_VERSION));
290+
if (demo != null){
291+
if (!demo.containsKey("result()")) {
292+
demo.put("result()", getFunctionCall(name, item.getString("arguments")));
293+
}
294+
demo.put(JSONRequest.KEY_TAG, item.get(JSONRequest.KEY_TAG));
295+
demo.put(JSONRequest.KEY_VERSION, item.get(JSONRequest.KEY_VERSION));
296+
}
301297

302298
for (String method : methods) {
303299
JSONObject r = APIJSON_CREATOR.createParser()
304300
.setMethod(RequestMethod.valueOf(method))
305301
.setNeedVerify(false)
306302
.parseResponse(demo);
307303

308-
if (JSONResponse.isSuccess(r) == false) {
309-
onServerError(JSONResponse.getMsg(r), shutdownWhenServerError);
304+
if (!JSONResponse.isSuccess(r)) {
305+
try {
306+
onServerError(JSONResponse.getMsg(r), shutdownWhenServerError);
307+
} catch (Exception e) {
308+
throw new RuntimeException(e);
309+
}
310310
}
311311
}
312312
}
@@ -400,7 +400,7 @@ public JSONObject getFunctionDemo(@NotNull JSONObject curObj) {
400400
if (demo == null) {
401401
demo = new JSONObject();
402402
}
403-
if (demo.containsKey("result()") == false) {
403+
if (!demo.containsKey("result()")) {
404404
demo.put("result()", getFunctionCall(curObj.getString("name"), curObj.getString("arguments")));
405405
}
406406
return demo;
@@ -774,11 +774,11 @@ public String getMethodRequest(@NotNull JSONObject curObj) {
774774
req += "\n \"static\": " + true;
775775
comma = true;
776776
}
777-
if (StringUtil.isEmpty(methodArgs, true) == false) {
777+
if (!StringUtil.isEmpty(methodArgs, true)) {
778778
req += (comma ? "," : "") + "\n \"methodArgs\": " + methodArgs;
779779
comma = true;
780780
}
781-
if (StringUtil.isEmpty(classArgs, true) == false) {
781+
if (!StringUtil.isEmpty(classArgs, true)) {
782782
req += (comma ? "," : "") + "\n \"classArgs\": " + classArgs;
783783
}
784784
req += "\n}";

0 commit comments

Comments
 (0)