2626
2727import java .io .IOException ;
2828import java .rmi .ServerException ;
29+ import java .util .HashMap ;
2930import java .util .LinkedHashMap ;
3031import java .util .List ;
3132import java .util .Map ;
4344import apijson .StringUtil ;
4445import apijson .orm .AbstractFunctionParser ;
4546import apijson .orm .JSONRequest ;
47+ import apijson .orm .script .JavaScriptExecutor ;
48+ import apijson .orm .script .ScriptExecutor ;
4649import unitauto .MethodUtil ;
4750import unitauto .MethodUtil .Argument ;
4851
@@ -206,7 +209,17 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
206209 if (JSONResponse .isSuccess (response ) == false ) {
207210 onServerError ("\n \n \n \n \n !!!! 查询远程函数异常 !!!\n " + response .getString (JSONResponse .KEY_MSG ) + "\n \n \n \n \n " , shutdownWhenServerError );
208211 }
212+
213+ //初始化默认脚本引擎,避免增量
214+ if (SCRIPT_EXECUTOR_MAP .get ("js" ) == null || isAll ) {
215+ ScriptExecutor javaScriptExecutor = new JavaScriptExecutor ();
216+ javaScriptExecutor .init ();
217+ SCRIPT_EXECUTOR_MAP .put ("js" , javaScriptExecutor );
218+ SCRIPT_EXECUTOR_MAP .put ("JavaScript" , javaScriptExecutor );
219+ SCRIPT_EXECUTOR_MAP .put ("javascript" , javaScriptExecutor );
220+ }
209221
222+ Map <String , JSONObject > scriptMap = new HashMap <>();
210223 JSONArray scriptList = response .getJSONArray ("[]" ); // response.getJSONArray(SCRIPT_ + "[]");
211224 if (scriptList != null && scriptList .isEmpty () == false ) {
212225 //if (isAll) {
@@ -230,11 +243,10 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
230243 if (StringUtil .isEmpty (s , true )) {
231244 onServerError ("Script 表字段 script 的值 " + s + " 不合法!不能为空!" , shutdownWhenServerError );
232245 }
233-
234246 newMap .put (n , item );
235247 }
236248
237- SCRIPT_MAP = newMap ;
249+ scriptMap = newMap ;
238250 }
239251
240252 JSONArray list = scriptList ; // response.getJSONArray(FUNCTION_ + "[]");
@@ -267,7 +279,15 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
267279 }
268280 // demo.put(JSONRequest.KEY_TAG, item.getString(JSONRequest.KEY_TAG));
269281 // demo.put(JSONRequest.KEY_VERSION, item.getInteger(JSONRequest.KEY_VERSION));
270-
282+ //加载脚本
283+ if (item .get ("language" ) != null ) {
284+ String language = item .getString ("language" );
285+ if (SCRIPT_EXECUTOR_MAP .get (language ) == null ) {
286+ onServerError ("找不到脚本语言 " + language + " 对应的执行引擎!请先依赖相关库并在后端 APIJSONFunctionParser 中注册!" , shutdownWhenServerError );
287+ }
288+ ScriptExecutor scriptExecutor = SCRIPT_EXECUTOR_MAP .get (language );
289+ scriptExecutor .load (name , scriptMap .get (name ).getString ("script" ));
290+ }
271291 newMap .put (name , item ); // 必须在测试 invoke 前把配置 put 进 FUNCTION_MAP!
272292
273293 String [] methods = StringUtil .split (item .getString ("methods" ));
0 commit comments