Skip to content

Commit 46c5e23

Browse files
authored
解决远程函数加载不全,感谢 calmcc 的贡献 #28
#28
2 parents bd78636 + 14f3689 commit 46c5e23

File tree

1 file changed

+113
-118
lines changed

1 file changed

+113
-118
lines changed

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

Lines changed: 113 additions & 118 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
/**可远程调用的函数类
@@ -153,60 +140,60 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
153140
//if (Log.DEBUG == false) {
154141
// function.put(APIJSONConstant.KEY_DEBUG, 0);
155142
//}
156-
//
143+
//
157144
//JSONRequest functionItem = new JSONRequest();
158145
//functionItem.put(FUNCTION_, function);
159-
//
160-
//JSONObject script = new JSONRequest(); // isAll ? new JSONRequest() : table;
161-
//script.put("simple", 0);
162-
//if (Log.DEBUG == false) {
163-
// script.put(APIJSONConstant.KEY_DEBUG, 0);
164-
//}
165-
// 不能用这个来优化,因为可能配置了不校验远程函数是否存在
166-
//{ // name{}@ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
167-
//JSONRequest nameInAt = new JSONRequest();
168-
//nameInAt.put("from", "Function");
169-
//{ // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
170-
// JSONRequest fun = new JSONRequest();
171-
// fun.setColumn("name");
172-
// nameInAt.put("Function", fun);
173-
//} // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
174-
175-
//script.put("name{}@", nameInAt);
176-
//} // name{}@ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
146+
//
147+
//JSONObject script = new JSONRequest(); // isAll ? new JSONRequest() : table;
148+
//script.put("simple", 0);
149+
//if (Log.DEBUG == false) {
150+
// script.put(APIJSONConstant.KEY_DEBUG, 0);
151+
//}
152+
// 不能用这个来优化,因为可能配置了不校验远程函数是否存在
153+
//{ // name{}@ <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
154+
//JSONRequest nameInAt = new JSONRequest();
155+
//nameInAt.put("from", "Function");
156+
//{ // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
157+
// JSONRequest fun = new JSONRequest();
158+
// fun.setColumn("name");
159+
// nameInAt.put("Function", fun);
160+
//} // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
161+
162+
//script.put("name{}@", nameInAt);
163+
//} // name{}@ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
177164

178165
//JSONRequest scriptItem = new JSONRequest();
179-
//scriptItem.put(SCRIPT_, script);
166+
//scriptItem.put(SCRIPT_, script);
180167

181168
JSONObject request = new JSONObject();
182169
//request.putAll(functionItem.toArray(0, 0, FUNCTION_));
183170
//request.putAll(scriptItem.toArray(0, 0, SCRIPT_));
184171

185-
// 可以用它,因为 Function 表必须存在,没有绕过校验的配置 // 不能用这个来优化,因为可能配置了不校验远程函数是否存在
186-
{ // [] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
187-
JSONRequest item = new JSONRequest();
188-
189-
{ // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
190-
JSONObject function = isAll ? new JSONRequest() : table;
191-
if (Log.DEBUG == false) {
192-
function.put(APIJSONConstant.KEY_DEBUG, 0);
193-
}
194-
item.put(FUNCTION_, function);
195-
} // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
196-
197-
if (ENABLE_SCRIPT_FUNCTION) { // Script <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
198-
JSONRequest script = new JSONRequest();
199-
script.put("name@", "/Function/name");
200-
script.put("simple", 0);
201-
item.put(SCRIPT_, script);
202-
} // Script >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
203-
204-
request.putAll(item.toArray(0, 0));
205-
} // [] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
172+
// 可以用它,因为 Function 表必须存在,没有绕过校验的配置 // 不能用这个来优化,因为可能配置了不校验远程函数是否存在
173+
{ // [] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
174+
JSONRequest item = new JSONRequest();
175+
176+
{ // Function <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
177+
JSONObject function = isAll ? new JSONRequest() : table;
178+
if (! Log.DEBUG) {
179+
function.put(APIJSONConstant.KEY_DEBUG, 0);
180+
}
181+
item.put(FUNCTION_, function);
182+
} // Function >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
183+
184+
if (ENABLE_SCRIPT_FUNCTION) { // Script <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
185+
JSONRequest script = new JSONRequest();
186+
script.put("name@", "/Function/name");
187+
script.put("simple", 0);
188+
item.put(SCRIPT_, script);
189+
} // Script >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
190+
191+
request.putAll(item.toArray(0, 0));
192+
} // [] >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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

@@ -220,34 +207,34 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
220207
}
221208

222209
Map<String, JSONObject> scriptMap = new HashMap<>();
223-
JSONArray scriptList = response.getJSONArray("[]"); // response.getJSONArray(SCRIPT_ + "[]");
224-
if (scriptList != null && scriptList.isEmpty() == false) {
225-
//if (isAll) {
226-
// SCRIPT_MAP = new LinkedHashMap<>();
227-
//}
228-
Map<String, JSONObject> newMap = new LinkedHashMap<>();
229-
230-
for (int i = 0; i < scriptList.size(); i++) {
231-
JSONObject item = scriptList.getJSONObject(i);
232-
item = item == null ? null : item.getJSONObject(SCRIPT_);
233-
if (item == null) { // 关联查不到很正常
234-
continue;
235-
}
236-
237-
String n = item.getString("name");
238-
if (StringUtil.isName(n) == false) {
239-
onServerError("Script 表字段 name 的值 " + n + " 不合法!必须为合法的方法名字符串!", shutdownWhenServerError);
240-
}
241-
242-
String s = item.getString("script");
243-
if (StringUtil.isEmpty(s, true)) {
244-
onServerError("Script 表字段 script 的值 " + s + " 不合法!不能为空!", shutdownWhenServerError);
245-
}
246-
newMap.put(n, item);
247-
}
248-
249-
scriptMap = newMap;
250-
}
210+
JSONArray scriptList = response.getJSONArray("[]"); // response.getJSONArray(SCRIPT_ + "[]");
211+
if (scriptList != null && ! scriptList.isEmpty()) {
212+
//if (isAll) {
213+
// SCRIPT_MAP = new LinkedHashMap<>();
214+
//}
215+
Map<String, JSONObject> newMap = new LinkedHashMap<>();
216+
217+
for (int i = 0; i < scriptList.size(); i++) {
218+
JSONObject item = scriptList.getJSONObject(i);
219+
item = item == null ? null : item.getJSONObject(SCRIPT_);
220+
if (item == null) { // 关联查不到很正常
221+
continue;
222+
}
223+
224+
String n = item.getString("name");
225+
if (! StringUtil.isName(n)) {
226+
onServerError("Script 表字段 name 的值 " + n + " 不合法!必须为合法的方法名字符串!", shutdownWhenServerError);
227+
}
228+
229+
String s = item.getString("script");
230+
if (StringUtil.isEmpty(s, true)) {
231+
onServerError("Script 表字段 script 的值 " + s + " 不合法!不能为空!", shutdownWhenServerError);
232+
}
233+
newMap.put(n, item);
234+
}
235+
236+
scriptMap = newMap;
237+
}
251238

252239
JSONArray list = scriptList; // response.getJSONArray(FUNCTION_ + "[]");
253240
int size = list == null ? 0 : list.size();
@@ -264,29 +251,33 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
264251

265252
for (int i = 0; i < size; i++) {
266253
JSONObject item = list.getJSONObject(i);
267-
item = item == null ? null : item.getJSONObject(FUNCTION_);
254+
item = item == null ? null : item.getJSONObject(FUNCTION_);
268255
if (item == null) {
269256
continue;
270257
}
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-
}
280268
// 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

@@ -296,17 +287,26 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
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
}
@@ -385,11 +385,6 @@ public static <T extends Object> void test(APIJSONFunctionParser<T> function) th
385385
}
386386

387387

388-
389-
390-
391-
392-
393388
/**获取远程函数的demo,如果没有就自动补全
394389
* @param curObj
395390
* @return
@@ -400,7 +395,7 @@ public JSONObject getFunctionDemo(@NotNull JSONObject curObj) {
400395
if (demo == null) {
401396
demo = new JSONObject();
402397
}
403-
if (demo.containsKey("result()") == false) {
398+
if (! demo.containsKey("result()")) {
404399
demo.put("result()", getFunctionCall(curObj.getString("name"), curObj.getString("arguments")));
405400
}
406401
return demo;
@@ -774,11 +769,11 @@ public String getMethodRequest(@NotNull JSONObject curObj) {
774769
req += "\n \"static\": " + true;
775770
comma = true;
776771
}
777-
if (StringUtil.isEmpty(methodArgs, true) == false) {
772+
if (! StringUtil.isEmpty(methodArgs, true)) {
778773
req += (comma ? "," : "") + "\n \"methodArgs\": " + methodArgs;
779774
comma = true;
780775
}
781-
if (StringUtil.isEmpty(classArgs, true) == false) {
776+
if (! StringUtil.isEmpty(classArgs, true)) {
782777
req += (comma ? "," : "") + "\n \"classArgs\": " + classArgs;
783778
}
784779
req += "\n}";
@@ -792,4 +787,4 @@ public static String removeComment(String json) {
792787
return json == null ? null: json.replaceAll("(//.*)|(/\\*[\\s\\S]*?\\*/)", "");
793788
}
794789

795-
}
790+
}

0 commit comments

Comments
 (0)