From 625a92ce8ecff9abf6c3eb243e944ead484452dd Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Mon, 25 May 2026 20:10:02 +0800 Subject: [PATCH 01/10] add GroovyMetadata --- .../example-app-cmd-domain/pom.xml | 2 +- .../example-app/example-app-cmd-meta/pom.xml | 2 +- example/example-app/example-app-query/pom.xml | 2 +- example/example-app/pom.xml | 2 +- .../example-domain-leave/pom.xml | 2 +- .../example-domain-user/pom.xml | 2 +- example/example-domain/pom.xml | 2 +- .../example-infra/example-infra-flow/pom.xml | 2 +- .../example-infra/example-infra-jpa/pom.xml | 2 +- .../example-infra-security/pom.xml | 2 +- example/example-infra/pom.xml | 2 +- example/example-interface/pom.xml | 2 +- example/example-server/pom.xml | 2 +- example/pom.xml | 2 +- pom.xml | 2 +- springboot-starter-data-authorization/pom.xml | 2 +- springboot-starter-data-fast/pom.xml | 2 +- springboot-starter-flow/pom.xml | 2 +- springboot-starter-security/pom.xml | 2 +- springboot-starter/pom.xml | 2 +- .../script/GroovyScriptRunningContext.java | 12 +-- .../script/annotation/ScriptFunction.java | 33 ++++++ .../script/annotation/ScriptType.java | 29 +++++ .../framework/script/meta/GroovyFunction.java | 45 ++++++++ .../framework/script/meta/GroovyMetadata.java | 53 +++++++++ .../framework/script/meta/GroovyType.java | 89 +++++++++++++++ ...eBindObject.java => GroovyBindObject.java} | 9 +- .../script/request/GroovyMethodScript.java | 59 ---------- .../script/request/GroovyRunningScript.java | 102 ++++++++++++++++++ .../service/GroovyMetadataParserService.java | 74 +++++++++++++ .../script/service/GroovyTypeParser.java | 93 ++++++++++++++++ .../script/service/TempGroovyTypeCache.java | 57 ++++++++++ .../GroovyScriptRunningContextTest.java | 28 ++++- .../script/request/MyScriptRequest.java | 39 +++++++ 34 files changed, 673 insertions(+), 89 deletions(-) create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptFunction.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptType.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyFunction.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java rename springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/{RuntimeBindObject.java => GroovyBindObject.java} (65%) delete mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyMethodScript.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyMetadataParserService.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/TempGroovyTypeCache.java create mode 100644 springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java diff --git a/example/example-app/example-app-cmd-domain/pom.xml b/example/example-app/example-app-cmd-domain/pom.xml index c0698a4c..cdbea2e3 100644 --- a/example/example-app/example-app-cmd-domain/pom.xml +++ b/example/example-app/example-app-cmd-domain/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-app - 3.4.44 + 3.4.45 ../pom.xml diff --git a/example/example-app/example-app-cmd-meta/pom.xml b/example/example-app/example-app-cmd-meta/pom.xml index 66f5138c..6c9b04b3 100644 --- a/example/example-app/example-app-cmd-meta/pom.xml +++ b/example/example-app/example-app-cmd-meta/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-app - 3.4.44 + 3.4.45 ../pom.xml diff --git a/example/example-app/example-app-query/pom.xml b/example/example-app/example-app-query/pom.xml index ac105c9b..0645e7ee 100644 --- a/example/example-app/example-app-query/pom.xml +++ b/example/example-app/example-app-query/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-app - 3.4.44 + 3.4.45 ../pom.xml diff --git a/example/example-app/pom.xml b/example/example-app/pom.xml index f0841b3e..9f076296 100644 --- a/example/example-app/pom.xml +++ b/example/example-app/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot springboot-example - 3.4.44 + 3.4.45 ../pom.xml pom diff --git a/example/example-domain/example-domain-leave/pom.xml b/example/example-domain/example-domain-leave/pom.xml index feca9e30..d7c2ada9 100644 --- a/example/example-domain/example-domain-leave/pom.xml +++ b/example/example-domain/example-domain-leave/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-domain - 3.4.44 + 3.4.45 ../pom.xml diff --git a/example/example-domain/example-domain-user/pom.xml b/example/example-domain/example-domain-user/pom.xml index c03892c6..2f188b31 100644 --- a/example/example-domain/example-domain-user/pom.xml +++ b/example/example-domain/example-domain-user/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-domain - 3.4.44 + 3.4.45 ../pom.xml diff --git a/example/example-domain/pom.xml b/example/example-domain/pom.xml index 64b78ab4..98a5ca27 100644 --- a/example/example-domain/pom.xml +++ b/example/example-domain/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot springboot-example - 3.4.44 + 3.4.45 ../pom.xml 4.0.0 diff --git a/example/example-infra/example-infra-flow/pom.xml b/example/example-infra/example-infra-flow/pom.xml index 7c6f1875..ef2c201d 100644 --- a/example/example-infra/example-infra-flow/pom.xml +++ b/example/example-infra/example-infra-flow/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot example-infra - 3.4.44 + 3.4.45 ../pom.xml diff --git a/example/example-infra/example-infra-jpa/pom.xml b/example/example-infra/example-infra-jpa/pom.xml index 2d561e57..c6e9ba9d 100644 --- a/example/example-infra/example-infra-jpa/pom.xml +++ b/example/example-infra/example-infra-jpa/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot example-infra - 3.4.44 + 3.4.45 ../pom.xml diff --git a/example/example-infra/example-infra-security/pom.xml b/example/example-infra/example-infra-security/pom.xml index f1a1c3c2..33fa1f61 100644 --- a/example/example-infra/example-infra-security/pom.xml +++ b/example/example-infra/example-infra-security/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot example-infra - 3.4.44 + 3.4.45 ../pom.xml diff --git a/example/example-infra/pom.xml b/example/example-infra/pom.xml index 36605591..ce319bb6 100644 --- a/example/example-infra/pom.xml +++ b/example/example-infra/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot springboot-example - 3.4.44 + 3.4.45 ../pom.xml pom diff --git a/example/example-interface/pom.xml b/example/example-interface/pom.xml index 5f23d1e0..270dfbed 100644 --- a/example/example-interface/pom.xml +++ b/example/example-interface/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot springboot-example - 3.4.44 + 3.4.45 example-interface diff --git a/example/example-server/pom.xml b/example/example-server/pom.xml index e6a43cd7..a62efec3 100644 --- a/example/example-server/pom.xml +++ b/example/example-server/pom.xml @@ -5,7 +5,7 @@ springboot-example com.codingapi.springboot - 3.4.44 + 3.4.45 4.0.0 diff --git a/example/pom.xml b/example/pom.xml index f73e7ee6..cc114850 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -19,7 +19,7 @@ springboot-example - 3.4.44 + 3.4.45 springboot-example springboot-example project for Spring Boot diff --git a/pom.xml b/pom.xml index 2fc26ce5..ecbbd600 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.codingapi.springboot springboot-parent - 3.4.44 + 3.4.45 https://github.com/codingapi/springboot-framewrok springboot-parent diff --git a/springboot-starter-data-authorization/pom.xml b/springboot-starter-data-authorization/pom.xml index d89f6d06..fdcccbc7 100644 --- a/springboot-starter-data-authorization/pom.xml +++ b/springboot-starter-data-authorization/pom.xml @@ -6,7 +6,7 @@ com.codingapi.springboot springboot-parent - 3.4.44 + 3.4.45 springboot-starter-data-authorization diff --git a/springboot-starter-data-fast/pom.xml b/springboot-starter-data-fast/pom.xml index 9f44ba99..6fe6420c 100644 --- a/springboot-starter-data-fast/pom.xml +++ b/springboot-starter-data-fast/pom.xml @@ -5,7 +5,7 @@ springboot-parent com.codingapi.springboot - 3.4.44 + 3.4.45 4.0.0 diff --git a/springboot-starter-flow/pom.xml b/springboot-starter-flow/pom.xml index ef49d453..6e6a60a9 100644 --- a/springboot-starter-flow/pom.xml +++ b/springboot-starter-flow/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.4.44 + 3.4.45 springboot-starter-flow diff --git a/springboot-starter-security/pom.xml b/springboot-starter-security/pom.xml index 761cdbac..9b411bd8 100644 --- a/springboot-starter-security/pom.xml +++ b/springboot-starter-security/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 3.4.44 + 3.4.45 springboot-starter-security diff --git a/springboot-starter/pom.xml b/springboot-starter/pom.xml index 8181bd0d..eeae0aaa 100644 --- a/springboot-starter/pom.xml +++ b/springboot-starter/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot springboot-parent - 3.4.44 + 3.4.45 springboot-starter diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java index f430dcb1..1bedd671 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java @@ -1,8 +1,8 @@ package com.codingapi.springboot.framework.script; import com.codingapi.springboot.framework.crypto.SHA256; -import com.codingapi.springboot.framework.script.request.RuntimeBindObject; -import com.codingapi.springboot.framework.script.request.GroovyMethodScript; +import com.codingapi.springboot.framework.script.request.GroovyBindObject; +import com.codingapi.springboot.framework.script.request.GroovyRunningScript; import groovy.lang.GroovyClassLoader; import groovy.lang.GroovyShell; import groovy.lang.Script; @@ -99,7 +99,7 @@ public void compile(String script) { * @param args 函数参数 * @return 返回数据 */ - public T run(String method, String script, Class returnType, List binds, Object... args) { + public T run(String method, String script, Class returnType, List binds, Object... args) { String key = SHA256.sha256(script); Script runtime = this.cache.get(key); if (runtime == null) { @@ -108,8 +108,8 @@ public T run(String method, String script, Class returnType, List T run(String method, String script, Class returnType, Object... ar * @param request 脚本参数 * @return 返回数据 */ - public T run(GroovyMethodScript request) { + public T run(GroovyRunningScript request) { return this.run(request.getMethod(),request.getScript(),request.getReturnType(),request.getBinds(),request.getParams()); } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptFunction.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptFunction.java new file mode 100644 index 00000000..c2920ab4 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptFunction.java @@ -0,0 +1,33 @@ +package com.codingapi.springboot.framework.script.annotation; + +import java.lang.annotation.*; + +/** + * 脚本函数对象定义 + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface ScriptFunction { + + /** + * 名称 + */ + String name(); + + /** + * 描述 + */ + String description() default ""; + + /** + * 参数对象 + */ + ScriptType[] requests() default {}; + + /** + * 返回数据类型 + */ + Class returnType(); + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptType.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptType.java new file mode 100644 index 00000000..f89ae836 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptType.java @@ -0,0 +1,29 @@ +package com.codingapi.springboot.framework.script.annotation; + +import java.lang.annotation.*; + +/** + * 脚本对象 + */ +@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface ScriptType { + + /** + * 名称 + */ + String name() default ""; + + /** + * 描述 + */ + String description() default ""; + + /** + * 数据类型 + */ + Class dataType(); + + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyFunction.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyFunction.java new file mode 100644 index 00000000..6184ba5b --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyFunction.java @@ -0,0 +1,45 @@ +package com.codingapi.springboot.framework.script.meta; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +/** + * 脚本函数对象 + */ +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class GroovyFunction { + + /** + * 对象访问名称 + */ + private String name; + /** + * 对象描述信息 + */ + private String description; + /** + * 返回类型 + */ + private GroovyType returnType; + /** + * 参数类型 + */ + private List requests; + + + public void addRequest(GroovyType request){ + if(this.requests ==null){ + this.requests = new ArrayList<>(); + } + this.requests.add(request); + } + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java new file mode 100644 index 00000000..a6bf75f0 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java @@ -0,0 +1,53 @@ +package com.codingapi.springboot.framework.script.meta; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + + +/** + * 脚本对象元数据结构 + */ +@Getter +@Setter +@AllArgsConstructor +@NoArgsConstructor +public class GroovyMetadata { + + /** + * 请求参数 + */ + private List requests; + + /** + * 绑定参数 + */ + private List binds; + + /** + * 返回类型 + */ + private GroovyType returnType; + + + public void addRequest(GroovyType request){ + if(this.requests==null){ + this.requests = new ArrayList<>(); + } + this.requests.add(request); + } + + + public void addBind(GroovyType bind){ + if(this.binds==null){ + this.binds = new ArrayList<>(); + } + this.binds.add(bind); + } + + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java new file mode 100644 index 00000000..0c23f9ab --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java @@ -0,0 +1,89 @@ +package com.codingapi.springboot.framework.script.meta; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +/** + * 脚本对象类型 + */ +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class GroovyType { + + /** + * 访问名称 + */ + @Setter + private String name; + + /** + * 描述信息 + */ + @Setter + private String description; + + /** + * 数据类型 + */ + @Setter + private Class dataType; + + /** + * 属性列表 + */ + private List fields; + + /** + * 函数列表 + */ + private List functions; + + + public void initFields(){ + if(this.fields ==null){ + this.fields = new ArrayList<>(); + } + } + + + public void initFunctions(){ + if(this.functions==null){ + this.functions = new ArrayList<>(); + } + } + + public void addFunction(GroovyFunction function){ + if(this.functions==null){ + this.functions = new ArrayList<>(); + } + + this.functions.add(function); + } + + + public void addField(GroovyType field){ + if(this.fields ==null){ + this.fields = new ArrayList<>(); + } + this.fields.add(field); + } + + + /** + * 保留函数和属性 + */ + public GroovyType cacheFieldsAndMethods(){ + GroovyType cache = new GroovyType(); + cache.functions = this.functions; + cache.fields = this.fields; + cache.dataType = this.dataType; + return cache; + } + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/RuntimeBindObject.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyBindObject.java similarity index 65% rename from springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/RuntimeBindObject.java rename to springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyBindObject.java index 8797b1ba..aa518529 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/RuntimeBindObject.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyBindObject.java @@ -8,8 +8,15 @@ */ @Getter @AllArgsConstructor -public class RuntimeBindObject { +public class GroovyBindObject { + /** + * 绑定的访问对象 + */ private String key; + /** + * 绑定的对象实例 + */ private Object object; + } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyMethodScript.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyMethodScript.java deleted file mode 100644 index 3c92807a..00000000 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyMethodScript.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.codingapi.springboot.framework.script.request; - -import lombok.Getter; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -/** - * 脚本执行对象 - * - * @param 返回格式 - */ -@Getter -public class GroovyMethodScript { - - private final String method; - - private final String script; - - private final Class returnType; - - private final List request; - - private final List binds; - - public GroovyMethodScript(String method, String script, Class returnType, Object... request) { - this.method = method; - this.script = script; - this.returnType = returnType; - this.request = new ArrayList<>(); - this.binds = new ArrayList<>(); - if (request != null) { - this.request.addAll(Arrays.asList(request)); - } - } - - public GroovyMethodScript(String script, Class returnType, Object... request) { - this("run", script, returnType, request); - } - - /** - * 获取参数 - */ - public Object[] getParams() { - return this.request.toArray(); - } - - /** - * 添加绑定对象 - * @param key 绑定key - * @param bind 绑定对象 - */ - public void addBindObject(String key, Object bind) { - this.binds.add(new RuntimeBindObject(key, bind)); - } - - -} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java new file mode 100644 index 00000000..648e882e --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java @@ -0,0 +1,102 @@ +package com.codingapi.springboot.framework.script.request; + +import com.codingapi.springboot.framework.script.meta.GroovyMetadata; +import com.codingapi.springboot.framework.script.service.GroovyMetadataParserService; +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * 脚本执行对象 + * + * @param 返回格式 + */ +@Getter +public class GroovyRunningScript { + + /** + * 脚本函数名称 + */ + private final String method; + + /** + * 脚本代码 + */ + private final String script; + + /** + * 返回数据类型 + */ + private final Class returnType; + + /** + * 请求参数对象 + */ + private final List requests; + + /** + * 绑定数据对象 + */ + private final List binds; + + /** + * 脚本元数据信息 + */ + @Setter + private GroovyMetadata groovyMetadata; + + + public GroovyRunningScript(String method, String script, Class returnType, GroovyMetadata groovyMetadata, List binds, Object... requests) { + this.method = method; + this.script = script; + this.returnType = returnType; + this.requests = new ArrayList<>(); + this.binds = binds; + if (requests != null) { + this.requests.addAll(Arrays.asList(requests)); + } + this.groovyMetadata = groovyMetadata; + + if (this.groovyMetadata == null) { + this.resetMetaData(); + } + } + + public GroovyRunningScript(String script, Class returnType, GroovyMetadata groovyMetadata, List binds, Object... request) { + this("run", script, returnType, groovyMetadata, binds, request); + } + + public GroovyRunningScript(String script, Class returnType, Object... request) { + this("run", script, returnType, null, null, request); + } + + /** + * 获取参数 + */ + public Object[] getParams() { + return this.requests.toArray(); + } + + /** + * 添加绑定对象 + * + * @param key 绑定key + * @param bind 绑定对象 + */ + public void addBindObject(String key, Object bind) { + this.binds.add(new GroovyBindObject(key, bind)); + this.resetMetaData(); + } + + /** + * 重置元数据对象 + */ + private void resetMetaData() { + GroovyMetadataParserService groovyMetaDataParserService = new GroovyMetadataParserService(this); + this.groovyMetadata = groovyMetaDataParserService.parser(); + } + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyMetadataParserService.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyMetadataParserService.java new file mode 100644 index 00000000..4b725821 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyMetadataParserService.java @@ -0,0 +1,74 @@ +package com.codingapi.springboot.framework.script.service; + +import com.codingapi.springboot.framework.script.meta.GroovyMetadata; +import com.codingapi.springboot.framework.script.meta.GroovyType; +import com.codingapi.springboot.framework.script.request.GroovyBindObject; +import com.codingapi.springboot.framework.script.request.GroovyRunningScript; + +import java.util.List; + +/** + * 脚本元数据解析对象 + */ +public class GroovyMetadataParserService { + + private final GroovyRunningScript script; + + private final GroovyMetadata groovyMetadata; + + public GroovyMetadataParserService(GroovyRunningScript script) { + this.script = script; + this.groovyMetadata = new GroovyMetadata(); + } + + + private void loadRequests() { + List requests = script.getRequests(); + if (requests != null && !requests.isEmpty()) { + for (Object obj : requests) { + if (obj != null) { + Class objClass = obj.getClass(); + GroovyType groovyType = this.parserGroovyObject(objClass); + this.groovyMetadata.addRequest(groovyType); + } + } + } + } + + private void loadBinds() { + List binds = script.getBinds(); + if (binds != null && !binds.isEmpty()) { + for (GroovyBindObject bindObject : binds) { + if (bindObject != null) { + String name = bindObject.getKey(); + Class objClass = bindObject.getObject().getClass(); + GroovyType groovyType = this.parserGroovyObject(objClass); + groovyType.setName(name); + this.groovyMetadata.addBind(groovyType); + } + } + } + } + + private void loadReturnType() { + Class returnTypeClass = this.script.getReturnType(); + GroovyType groovyType = this.parserGroovyObject(returnTypeClass); + this.groovyMetadata.setReturnType(groovyType); + } + + + private GroovyType parserGroovyObject(Class clazz) { + GroovyTypeParser parser = new GroovyTypeParser(clazz); + return parser.parser(); + } + + + public GroovyMetadata parser() { + TempGroovyTypeCache.getInstance().clear(); + this.loadRequests(); + this.loadBinds(); + this.loadReturnType(); + return groovyMetadata; + } + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java new file mode 100644 index 00000000..65ca4f5b --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java @@ -0,0 +1,93 @@ +package com.codingapi.springboot.framework.script.service; + +import com.codingapi.springboot.framework.script.annotation.ScriptFunction; +import com.codingapi.springboot.framework.script.annotation.ScriptType; +import com.codingapi.springboot.framework.script.meta.GroovyFunction; +import com.codingapi.springboot.framework.script.meta.GroovyType; +import org.springframework.util.StringUtils; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; + +/** + * 脚本类型解析对象 + */ +public class GroovyTypeParser { + + private final Class clazz; + + private final GroovyType object; + + public GroovyTypeParser(Class clazz) { + this.clazz = clazz; + this.object = TempGroovyTypeCache.getInstance().getOrCreate(clazz); + this.object.setDataType(clazz); + } + + + private void resetObjectMeta(GroovyType target, ScriptType scriptType) { + target.setDataType(scriptType.dataType()); + target.setName(scriptType.name()); + target.setDescription(scriptType.description()); + } + + private void loadObjectMeta() { + ScriptType scriptType = clazz.getAnnotation(ScriptType.class); + if (scriptType != null) { + this.resetObjectMeta(object, scriptType); + } + } + + private void loadFields() { + Field[] fields = this.clazz.getDeclaredFields(); + for (Field field : fields) { + ScriptType scriptType = field.getAnnotation(ScriptType.class); + if (scriptType != null) { + GroovyType groovyType = TempGroovyTypeCache.getInstance().getOrCreate(scriptType.dataType()); + this.resetObjectMeta(groovyType, scriptType); + this.object.addField(groovyType); + } + } + } + + private void loadMethods() { + Method[] methods = this.clazz.getDeclaredMethods(); + for (Method method : methods) { + ScriptFunction scriptFunction = method.getAnnotation(ScriptFunction.class); + if (scriptFunction != null) { + GroovyFunction groovyFunction = new GroovyFunction(); + if (StringUtils.hasText(scriptFunction.name())) { + groovyFunction.setName(scriptFunction.name()); + } else { + groovyFunction.setName(method.getName()); + } + + if (StringUtils.hasText(scriptFunction.description())) { + groovyFunction.setDescription(scriptFunction.description()); + } + + + ScriptType[] requests = scriptFunction.requests(); + if (requests != null) { + for (ScriptType request : requests) { + GroovyType requestObject = TempGroovyTypeCache.getInstance().getOrCreate(request.dataType()); + this.resetObjectMeta(requestObject, request); + groovyFunction.addRequest(requestObject); + } + } + + Class returnType = scriptFunction.returnType(); + groovyFunction.setReturnType(TempGroovyTypeCache.getInstance().getOrCreate(returnType)); + this.object.addFunction(groovyFunction); + } + } + } + + + public GroovyType parser() { + this.loadObjectMeta(); + this.loadFields(); + this.loadMethods(); + return this.object; + } +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/TempGroovyTypeCache.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/TempGroovyTypeCache.java new file mode 100644 index 00000000..aa03b535 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/TempGroovyTypeCache.java @@ -0,0 +1,57 @@ +package com.codingapi.springboot.framework.script.service; + +import com.codingapi.springboot.framework.script.meta.GroovyType; +import lombok.Getter; + +import java.util.HashMap; +import java.util.Map; + +/** + * 脚本对象临时缓存对象 + */ +class TempGroovyTypeCache { + + @Getter + private final static TempGroovyTypeCache instance = new TempGroovyTypeCache(); + + private final ThreadLocal, GroovyType>> cache; + + + private TempGroovyTypeCache() { + this.cache = new ThreadLocal<>(); + } + + public void clear() { + this.cache.remove(); + } + + + public void set(Class key, GroovyType object) { + Map, GroovyType> map = this.cache.get(); + if (map == null) { + map = new HashMap<>(); + this.cache.set(map); + } + map.put(key, object); + } + + + public GroovyType getOrCreate(Class clazz) { + Map, GroovyType> map = this.cache.get(); + if (map == null) { + map = new HashMap<>(); + this.cache.set(map); + } + GroovyType object = map.get(clazz); + if (object == null) { + object = new GroovyType(); + object.setDataType(clazz); + object.initFields(); + object.initFunctions(); + map.put(clazz, object); + } + return object.cacheFieldsAndMethods(); + } + + +} diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java index 7745a60e..1afb389f 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java @@ -1,6 +1,7 @@ package com.codingapi.springboot.framework.script; -import com.codingapi.springboot.framework.script.request.GroovyMethodScript; +import com.codingapi.springboot.framework.script.request.GroovyRunningScript; +import com.codingapi.springboot.framework.script.request.MyScriptRequest; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -18,7 +19,7 @@ def run(request){ GroovyScriptRunningContext.getInstance().compile(script); - GroovyMethodScript request = new GroovyMethodScript<>(script, Void.class, 100); + GroovyRunningScript request = new GroovyRunningScript<>(script, Void.class, 100); request.addBindObject("$request", request); long t1 = System.currentTimeMillis(); @@ -28,6 +29,27 @@ def run(request){ } + + @Test + void metaTest(){ + String script = """ + def run(request){ + return request.count; + } + """; + + MyScriptRequest request = new MyScriptRequest(100); + GroovyRunningScript runningScript = new GroovyRunningScript<>(script, Integer.class, request); + + assertEquals(1,runningScript.getGroovyMetadata().getRequests().size()); + + long t1 = System.currentTimeMillis(); + int result = GroovyScriptRunningContext.getInstance().run(runningScript); + long t2 = System.currentTimeMillis(); + System.out.println("groovy time:" + (t2 - t1)); + assertEquals(100, result); + } + @Test void run() { @@ -38,7 +60,7 @@ def run(request){ } """; - GroovyMethodScript request = new GroovyMethodScript<>(script, Integer.class, 100); + GroovyRunningScript request = new GroovyRunningScript<>(script, Integer.class, 100); request.addBindObject("$request", request); long t1 = System.currentTimeMillis(); diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java new file mode 100644 index 00000000..9dba5360 --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java @@ -0,0 +1,39 @@ +package com.codingapi.springboot.framework.script.request; + +import com.codingapi.springboot.framework.script.annotation.ScriptFunction; +import com.codingapi.springboot.framework.script.annotation.ScriptType; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@ScriptType( + name = "request", + description = "请求参数", + dataType = MyScriptRequest.class +) +@AllArgsConstructor +public class MyScriptRequest { + + @ScriptType(name = "count", description = "总数量", dataType = Integer.class) + private final int count; + + @ScriptType(name = "test", description = "test", dataType = MyScriptRequest.class) + private MyScriptRequest test; + + + @ScriptFunction( + name = "isSupport", + description = "是否匹配", + returnType = Boolean.class, + requests = { + @ScriptType(name = "count", description = "描述信息", dataType = Integer.class), + } + ) + public boolean isSupport(int count) { + return this.count == count; + } + + public MyScriptRequest(int count) { + this.count = count; + } +} From cd2a7c1bf9a6a4591694995ceb23ac529d02e363 Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Mon, 25 May 2026 21:08:11 +0800 Subject: [PATCH 02/10] add TransactionManagerContext --- .../framework/script/GroovyScriptRunner.java | 169 ++++++++++++++++++ .../script/GroovyScriptRunningContext.java | 78 +++----- .../script/request/GroovyRunningScript.java | 5 +- .../TransactionManagerContext.java | 22 +++ ...ransactionManagerContextConfiguration.java | 15 ++ .../TransactionManagerContextRegister.java | 20 +++ .../main/resources/META-INF/spring.factories | 1 + ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../GroovyScriptRunningContextTest.java | 7 +- ...sactionGroovyScriptRunningContextTest.java | 90 ++++++++++ .../script/request/MyScriptRequest.java | 11 ++ 11 files changed, 357 insertions(+), 62 deletions(-) create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextConfiguration.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextRegister.java create mode 100644 springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java new file mode 100644 index 00000000..c4e0b4dd --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java @@ -0,0 +1,169 @@ +package com.codingapi.springboot.framework.script; + +import com.codingapi.springboot.framework.crypto.SHA256; +import com.codingapi.springboot.framework.script.request.GroovyBindObject; +import com.codingapi.springboot.framework.script.request.GroovyRunningScript; +import com.codingapi.springboot.framework.transaction.TransactionManagerContext; +import groovy.lang.GroovyClassLoader; +import groovy.lang.GroovyShell; +import groovy.lang.Script; +import lombok.Getter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.DefaultTransactionDefinition; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * groovy script 执行上下文对象 + */ +public class GroovyScriptRunner { + + private final GroovyShell shell; + + private final Map cache; + + @Getter + private final int maxCacheSize; + + @Getter + private final boolean readOnly; + + public GroovyScriptRunner(int maxCacheSize){ + this(maxCacheSize,false); + } + + public GroovyScriptRunner(int maxCacheSize, boolean readOnly) { + this.maxCacheSize = maxCacheSize; + this.readOnly = readOnly; + GroovyClassLoader groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader()); + this.shell = new GroovyShell(groovyClassLoader); + this.cache = new LinkedHashMap<>(16, 0.75F, true) { + @Override + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > maxCacheSize; + } + }; + } + + /** + * 清空缓存对象 + */ + public void clearCache() { + this.cache.clear(); + } + + /** + * 缓存大小 + */ + public int cacheSize() { + return this.cache.size(); + } + + + /** + * 编译脚本 + * + * @param script 脚本内容 + * @param cache 是否缓存 + */ + public void compile(String script, boolean cache) { + if (cache) { + String key = SHA256.sha256(script); + Script runtime = this.cache.get(key); + if (runtime == null) { + runtime = this.shell.parse(script); + this.cache.put(key, runtime); + } + } else { + this.shell.parse(script); + } + } + + + /** + * 编译脚本,非缓存模式 + * + * @param script 脚本内容 + */ + public void compile(String script) { + this.compile(script, false); + } + + /** + * 执行脚本 + * + * @param method 函数名称 + * @param script 脚本内容 + * @param returnType 返回类型 + * @param binds 绑定数据对象 + * @param args 函数参数 + * @return 返回数据 + */ + public T run(String method, String script, Class returnType, List binds, Object... args) { + String key = SHA256.sha256(script); + Script runtime = this.cache.get(key); + if (runtime == null) { + runtime = this.shell.parse(script); + this.cache.put(key, runtime); + } + + if (binds != null && !binds.isEmpty()) { + for (GroovyBindObject groovyBindObject : binds) { + runtime.setProperty(groovyBindObject.getKey(), groovyBindObject.getObject()); + } + } + + PlatformTransactionManager transactionManager = TransactionManagerContext.getInstance().getPlatformTransactionManager(); + if (transactionManager != null) { + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); + if (readOnly) { + def.setReadOnly(true); + } + TransactionStatus transactionStatus = transactionManager.getTransaction(def); + try { + T result = (T) runtime.invokeMethod(method, args); + if (readOnly) { + transactionManager.rollback(transactionStatus); + } else { + transactionManager.commit(transactionStatus); + } + return result; + } catch (Exception e) { + transactionManager.rollback(transactionStatus); + throw e; + } + } + return (T) runtime.invokeMethod(method, args); + } + + + /** + * 运行脚本 + * + * @param method 函数名称 + * @param script 脚本内容 + * @param returnType 返回类型 + * @param args 函数参数 + * @return 返回数据 + */ + public T run(String method, String script, Class returnType, Object... args) { + return this.run(method, script, returnType, null, args); + } + + + /** + * 执行脚本 + * + * @param request 脚本参数 + * @return 返回数据 + */ + public T run(GroovyRunningScript request) { + return this.run(request.getMethod(), request.getScript(), request.getReturnType(), request.getBinds(), request.getParams()); + } + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java index 1bedd671..8dd37e2b 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java @@ -1,28 +1,17 @@ package com.codingapi.springboot.framework.script; -import com.codingapi.springboot.framework.crypto.SHA256; import com.codingapi.springboot.framework.script.request.GroovyBindObject; import com.codingapi.springboot.framework.script.request.GroovyRunningScript; -import groovy.lang.GroovyClassLoader; -import groovy.lang.GroovyShell; -import groovy.lang.Script; import lombok.Getter; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; /** - * groovy script 执行上下文对象 + * groovy script 执行上下文对象,事务模式为可写入 */ public class GroovyScriptRunningContext { - private final GroovyShell shell; - - private final Map cache; - - @Getter - private final int maxCacheSize; + private final GroovyScriptRunner scriptRunner; public static final int MAX_CACHE_SIZE = 1000; @@ -30,15 +19,7 @@ public class GroovyScriptRunningContext { private static GroovyScriptRunningContext instance = new GroovyScriptRunningContext(MAX_CACHE_SIZE); private GroovyScriptRunningContext(int maxCacheSize) { - this.maxCacheSize = maxCacheSize; - GroovyClassLoader groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader()); - this.shell = new GroovyShell(groovyClassLoader); - this.cache = new LinkedHashMap<>(16, 0.75F, true) { - @Override - protected boolean removeEldestEntry(Map.Entry eldest) { - return size() > maxCacheSize; - } - }; + this.scriptRunner = new GroovyScriptRunner(maxCacheSize, false); } /** @@ -52,14 +33,14 @@ public void setMaxCacheSize(int maxCacheSize) { * 清空缓存对象 */ public void clearCache() { - this.cache.clear(); + this.scriptRunner.clearCache(); } /** * 缓存大小 */ - public int cacheSize(){ - return this.cache.size(); + public int cacheSize() { + return this.scriptRunner.cacheSize(); } /** @@ -69,21 +50,13 @@ public int cacheSize(){ * @param cache 是否缓存 */ public void compile(String script, boolean cache) { - if (cache) { - String key = SHA256.sha256(script); - Script runtime = this.cache.get(key); - if (runtime == null) { - runtime = this.shell.parse(script); - this.cache.put(key, runtime); - } - } else { - this.shell.parse(script); - } + this.scriptRunner.compile(script, cache); } /** * 编译脚本,非缓存模式 + * * @param script 脚本内容 */ public void compile(String script) { @@ -92,49 +65,40 @@ public void compile(String script) { /** * 执行脚本 - * @param method 函数名称 - * @param script 脚本内容 + * + * @param method 函数名称 + * @param script 脚本内容 * @param returnType 返回类型 - * @param binds 绑定数据对象 - * @param args 函数参数 + * @param binds 绑定数据对象 + * @param args 函数参数 * @return 返回数据 */ public T run(String method, String script, Class returnType, List binds, Object... args) { - String key = SHA256.sha256(script); - Script runtime = this.cache.get(key); - if (runtime == null) { - runtime = this.shell.parse(script); - this.cache.put(key, runtime); - } - - if (binds != null && !binds.isEmpty()) { - for (GroovyBindObject groovyBindObject : binds) { - runtime.setProperty(groovyBindObject.getKey(), groovyBindObject.getObject()); - } - } - return (T) runtime.invokeMethod(method, args); + return this.scriptRunner.run(method,script,returnType,binds,args); } /** * 运行脚本 - * @param method 函数名称 - * @param script 脚本内容 + * + * @param method 函数名称 + * @param script 脚本内容 * @param returnType 返回类型 - * @param args 函数参数 + * @param args 函数参数 * @return 返回数据 */ public T run(String method, String script, Class returnType, Object... args) { - return this.run(method,script,returnType,null,args); + return this.run(method, script, returnType, null, args); } /** * 执行脚本 + * * @param request 脚本参数 * @return 返回数据 */ public T run(GroovyRunningScript request) { - return this.run(request.getMethod(),request.getScript(),request.getReturnType(),request.getBinds(),request.getParams()); + return this.run(request.getMethod(), request.getScript(), request.getReturnType(), request.getBinds(), request.getParams()); } } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java index 648e882e..aa0acd98 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java @@ -40,7 +40,7 @@ public class GroovyRunningScript { /** * 绑定数据对象 */ - private final List binds; + private List binds; /** * 脚本元数据信息 @@ -87,6 +87,9 @@ public Object[] getParams() { * @param bind 绑定对象 */ public void addBindObject(String key, Object bind) { + if(this.binds==null){ + this.binds = new ArrayList<>(); + } this.binds.add(new GroovyBindObject(key, bind)); this.resetMetaData(); } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java new file mode 100644 index 00000000..ed9a6d1f --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java @@ -0,0 +1,22 @@ +package com.codingapi.springboot.framework.transaction; + +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import org.springframework.transaction.PlatformTransactionManager; + +@Slf4j +public class TransactionManagerContext { + + @Getter + private PlatformTransactionManager platformTransactionManager; + + @Getter + private final static TransactionManagerContext instance = new TransactionManagerContext(); + + private TransactionManagerContext() {} + + public void setPlatformTransactionManager(PlatformTransactionManager platformTransactionManager) { + this.platformTransactionManager = platformTransactionManager; + log.info("platformTransactionManager:{} load success",platformTransactionManager); + } +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextConfiguration.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextConfiguration.java new file mode 100644 index 00000000..29e42ed1 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextConfiguration.java @@ -0,0 +1,15 @@ +package com.codingapi.springboot.framework.transaction; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +@Configuration +public class TransactionManagerContextConfiguration { + + @Bean + public TransactionManagerContextRegister transactionManagerContextRegister(PlatformTransactionManager platformTransactionManager){ + return new TransactionManagerContextRegister(platformTransactionManager); + } + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextRegister.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextRegister.java new file mode 100644 index 00000000..5c53e9eb --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextRegister.java @@ -0,0 +1,20 @@ +package com.codingapi.springboot.framework.transaction; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.transaction.PlatformTransactionManager; + +public class TransactionManagerContextRegister implements InitializingBean { + + private final PlatformTransactionManager transactionManager; + + public TransactionManagerContextRegister(PlatformTransactionManager transactionManager) { + this.transactionManager = transactionManager; + } + + @Override + public void afterPropertiesSet() throws Exception { + TransactionManagerContext.getInstance().setPlatformTransactionManager(transactionManager); + } + + +} diff --git a/springboot-starter/src/main/resources/META-INF/spring.factories b/springboot-starter/src/main/resources/META-INF/spring.factories index 3fd6c549..046b912b 100644 --- a/springboot-starter/src/main/resources/META-INF/spring.factories +++ b/springboot-starter/src/main/resources/META-INF/spring.factories @@ -2,6 +2,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.codingapi.springboot.framework.AutoConfiguration,\ com.codingapi.springboot.framework.event.SpringEventConfiguration,\ com.codingapi.springboot.framework.exception.ExceptionConfiguration,\ +com.codingapi.springboot.framework.transaction.TransactionManagerContextConfiguration,\ com.codingapi.springboot.framework.event.HandlerBeanDefinitionRegistrar,\ com.codingapi.springboot.framework.event.SpringHandlerConfiguration,\ com.codingapi.springboot.framework.servlet.BasicHandlerExceptionResolverConfiguration diff --git a/springboot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/springboot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 8aa6f4cd..0d489bbc 100644 --- a/springboot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/springboot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,6 +1,7 @@ com.codingapi.springboot.framework.AutoConfiguration com.codingapi.springboot.framework.event.SpringEventConfiguration com.codingapi.springboot.framework.exception.ExceptionConfiguration +com.codingapi.springboot.framework.transaction.TransactionManagerContextConfiguration com.codingapi.springboot.framework.event.HandlerBeanDefinitionRegistrar com.codingapi.springboot.framework.event.SpringHandlerConfiguration com.codingapi.springboot.framework.servlet.BasicHandlerExceptionResolverConfiguration diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java index 1afb389f..df2861db 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java @@ -4,7 +4,7 @@ import com.codingapi.springboot.framework.script.request.MyScriptRequest; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; class GroovyScriptRunningContextTest { @@ -13,10 +13,9 @@ void voidRun() { String script = """ def run(request){ - println($request.getRequest()) + println($request.getRequests()) } """; - GroovyScriptRunningContext.getInstance().compile(script); GroovyRunningScript request = new GroovyRunningScript<>(script, Void.class, 100); @@ -55,7 +54,7 @@ void run() { String script = """ def run(request){ - println($request.getRequest()) + println($request.getRequests()) return request; } """; diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java new file mode 100644 index 00000000..08cb0f6b --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java @@ -0,0 +1,90 @@ +package com.codingapi.springboot.framework.script; + +import com.codingapi.springboot.framework.entity.MyTest; +import com.codingapi.springboot.framework.repository.MyTestRepository; +import com.codingapi.springboot.framework.script.request.GroovyRunningScript; +import com.codingapi.springboot.framework.script.request.MyScriptRequest; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@SpringBootTest +@Transactional +class TransactionGroovyScriptRunningContextTest { + + @Autowired + private MyTestRepository myTestRepository; + + @Test + void transactionCommitRun() { + + String script = """ + def run(request){ + request.addData($repository); + println($request.count); + } + """; + + MyScriptRequest request = new MyScriptRequest(100); + + myTestRepository.deleteAll(); + List list = myTestRepository.findAll(); + assertTrue(list.isEmpty()); + GroovyScriptRunner scriptRunner = new GroovyScriptRunner(100); + scriptRunner.compile(script); + GroovyRunningScript runningScript = new GroovyRunningScript<>(script, Void.class, request); + runningScript.addBindObject("$request", request); + runningScript.addBindObject("$repository", myTestRepository); + + long t1 = System.currentTimeMillis(); + scriptRunner.run(runningScript); + long t2 = System.currentTimeMillis(); + System.out.println("groovy time:" + (t2 - t1)); + + list = myTestRepository.findAll(); + assertFalse(list.isEmpty()); + + } + + @Test + void transactionOnlyReadRun() { + + String script = """ + def run(request){ + request.addData($repository); + println($request.count); + } + """; + + MyScriptRequest request = new MyScriptRequest(100); + + + myTestRepository.deleteAll(); + List list = myTestRepository.findAll(); + assertTrue(list.isEmpty()); + + GroovyScriptRunner scriptRunner = new GroovyScriptRunner(100, true); + scriptRunner.compile(script); + + GroovyRunningScript runningScript = new GroovyRunningScript<>(script, Void.class, request); + runningScript.addBindObject("$request", request); + runningScript.addBindObject("$repository", myTestRepository); + + long t1 = System.currentTimeMillis(); + scriptRunner.run(runningScript); + long t2 = System.currentTimeMillis(); + System.out.println("groovy time:" + (t2 - t1)); + + list = myTestRepository.findAll(); + System.out.println(list.size()); + assertTrue(list.isEmpty()); + + } + +} \ No newline at end of file diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java index 9dba5360..505fbf74 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java @@ -1,5 +1,7 @@ package com.codingapi.springboot.framework.script.request; +import com.codingapi.springboot.framework.entity.MyTest; +import com.codingapi.springboot.framework.repository.MyTestRepository; import com.codingapi.springboot.framework.script.annotation.ScriptFunction; import com.codingapi.springboot.framework.script.annotation.ScriptType; import lombok.AllArgsConstructor; @@ -36,4 +38,13 @@ public boolean isSupport(int count) { public MyScriptRequest(int count) { this.count = count; } + + + public void addData(MyTestRepository testRepository){ + System.out.println(testRepository); + MyTest myTest = new MyTest(); + myTest.setName("test"); + testRepository.save(myTest); + } + } From 7e85b79e27502542400dac3878561cd0a1924be8 Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Tue, 26 May 2026 08:23:50 +0800 Subject: [PATCH 03/10] add GroovySchema --- .../framework/script/meta/GroovyMetadata.java | 5 + .../framework/script/meta/GroovyType.java | 4 + .../framework/script/schema/GroovySchema.java | 104 ++++++++++++++++++ .../framework/script/schema/SchemaField.java | 34 ++++++ .../script/schema/SchemaFunction.java | 45 ++++++++ .../framework/script/schema/SchemaType.java | 51 +++++++++ .../script/service/GroovyTypeParser.java | 4 +- .../script/service/TempGroovyTypeCache.java | 6 +- .../springboot/framework/entity/MyTest.java | 5 + .../GroovyScriptRunningContextTest.java | 3 + .../script/request/MyScriptRequest.java | 9 +- 11 files changed, 260 insertions(+), 10 deletions(-) create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/GroovySchema.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaField.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaFunction.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaType.java diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java index a6bf75f0..3164786b 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java @@ -1,5 +1,6 @@ package com.codingapi.springboot.framework.script.meta; +import com.codingapi.springboot.framework.script.schema.GroovySchema; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -50,4 +51,8 @@ public void addBind(GroovyType bind){ } + public GroovySchema toSchema(){ + return new GroovySchema(this); + } + } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java index 0c23f9ab..a1cafe41 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java @@ -51,6 +51,10 @@ public void initFields(){ } } + public String getDataClassName(){ + return dataType.getSimpleName(); + } + public void initFunctions(){ if(this.functions==null){ diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/GroovySchema.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/GroovySchema.java new file mode 100644 index 00000000..1d39231d --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/GroovySchema.java @@ -0,0 +1,104 @@ +package com.codingapi.springboot.framework.script.schema; + +import com.alibaba.fastjson.JSONObject; +import com.codingapi.springboot.framework.script.meta.GroovyMetadata; +import com.codingapi.springboot.framework.script.meta.GroovyType; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 脚本提示信息 + */ +public class GroovySchema { + + @Getter + private final Map types; + + @Getter + private final List requests; + + @Getter + private final List binds; + + @Getter + private final String returnType; + + private transient final GroovyMetadata metadata; + + public GroovySchema(GroovyMetadata metadata) { + this.metadata = metadata; + this.types = new HashMap<>(); + this.requests = new ArrayList<>(); + this.binds = new ArrayList<>(); + + this.loadRequestTypes(); + this.loadBindTypes(); + this.loadReturnType(); + + this.loadRequests(); + this.loadBinds(); + + this.returnType = metadata.getReturnType().getDataClassName(); + } + + + public String toJson() { + return JSONObject.toJSONString(this); + } + + private void loadRequests() { + if (this.metadata.getRequests() != null) { + for (GroovyType groovyType : this.metadata.getRequests()) { + SchemaField schemaField = new SchemaField(groovyType); + this.requests.add(schemaField); + } + } + } + + + private void loadBinds() { + if (this.metadata.getBinds() != null) { + for (GroovyType groovyType : this.metadata.getBinds()) { + SchemaField schemaField = new SchemaField(groovyType); + this.binds.add(schemaField); + } + } + } + + private void loadRequestTypes() { + if (this.metadata.getRequests() != null) { + for (GroovyType groovyType : this.metadata.getRequests()) { + this.loadSchemaType(groovyType); + } + } + } + + public void loadSchemaType(GroovyType groovyType) { + String key = groovyType.getDataClassName(); + if (this.types.get(key) == null) { + SchemaType schemaType = new SchemaType(groovyType, this); + this.types.put(groovyType.getDataClassName(), schemaType); + } + } + + private void loadBindTypes() { + if (this.metadata.getBinds() != null) { + for (GroovyType groovyType : this.metadata.getBinds()) { + this.loadSchemaType(groovyType); + } + } + } + + private void loadReturnType() { + GroovyType groovyType = this.metadata.getReturnType(); + if (groovyType != null) { + this.loadSchemaType(groovyType); + } + } + + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaField.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaField.java new file mode 100644 index 00000000..a7f376ee --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaField.java @@ -0,0 +1,34 @@ +package com.codingapi.springboot.framework.script.schema; + +import com.codingapi.springboot.framework.script.meta.GroovyType; +import lombok.Getter; +import lombok.Setter; + +/** + * 字段定义 + */ +@Setter +public class SchemaField { + + @Getter + private String name; + @Getter + private String description; + @Getter + private String dataType; + + public SchemaField(GroovyType filed) { + this(filed,null); + } + + public SchemaField(GroovyType filed,GroovySchema schema) { + this.name = filed.getName(); + this.description = filed.getDescription(); + this.dataType = filed.getDataClassName(); + + if(schema!=null){ + schema.loadSchemaType(filed); + } + } + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaFunction.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaFunction.java new file mode 100644 index 00000000..9d3ad984 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaFunction.java @@ -0,0 +1,45 @@ +package com.codingapi.springboot.framework.script.schema; + +import com.codingapi.springboot.framework.script.meta.GroovyFunction; +import com.codingapi.springboot.framework.script.meta.GroovyType; +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +/** + * 函数定义 + */ +@Setter +public class SchemaFunction { + + @Getter + private String name; + @Getter + private String description; + @Getter + private String returnType; + @Getter + private List requests; + + public SchemaFunction(GroovyFunction function) { + this(function,null); + } + + public SchemaFunction(GroovyFunction function,GroovySchema schema) { + this.name = function.getName(); + this.description = function.getDescription(); + this.returnType = function.getReturnType().getDataClassName(); + this.requests = new ArrayList<>(); + + for(GroovyType request: function.getRequests()){ + this.requests.add(new SchemaField(request,schema)); + } + + if(schema!=null){ + schema.loadSchemaType(function.getReturnType()); + } + } + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaType.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaType.java new file mode 100644 index 00000000..cfae75b0 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaType.java @@ -0,0 +1,51 @@ +package com.codingapi.springboot.framework.script.schema; + +import com.codingapi.springboot.framework.script.meta.GroovyFunction; +import com.codingapi.springboot.framework.script.meta.GroovyType; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.List; + +/** + * 数据类型 + */ +public class SchemaType { + + @Getter + private List fields; + + @Getter + private List functions; + + private final GroovySchema schema; + + public boolean isEmpty(){ + return (fields==null || fields.isEmpty()) && (functions==null || functions.isEmpty()); + } + + public SchemaType(GroovyType groovyType,GroovySchema schema) { + this.schema = schema; + this.addFields(groovyType.getFields()); + this.addFunctions(groovyType.getFunctions()); + } + + public void addFields(List fields) { + if(this.fields == null){ + this.fields = new ArrayList<>(); + } + for(GroovyType field:fields){ + this.fields.add(new SchemaField(field,this.schema)); + } + } + + public void addFunctions(List functions) { + if(this.functions ==null){ + this.functions = new ArrayList<>(); + } + for(GroovyFunction function:functions){ + SchemaFunction schemaFunction = new SchemaFunction(function,this.schema); + this.functions.add(schemaFunction); + } + } +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java index 65ca4f5b..344d3b54 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java @@ -20,8 +20,10 @@ public class GroovyTypeParser { public GroovyTypeParser(Class clazz) { this.clazz = clazz; - this.object = TempGroovyTypeCache.getInstance().getOrCreate(clazz); + this.object = new GroovyType(); this.object.setDataType(clazz); + this.object.initFields(); + this.object.initFunctions(); } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/TempGroovyTypeCache.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/TempGroovyTypeCache.java index aa03b535..1dafef46 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/TempGroovyTypeCache.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/TempGroovyTypeCache.java @@ -44,10 +44,8 @@ public GroovyType getOrCreate(Class clazz) { } GroovyType object = map.get(clazz); if (object == null) { - object = new GroovyType(); - object.setDataType(clazz); - object.initFields(); - object.initFunctions(); + GroovyTypeParser parser = new GroovyTypeParser(clazz); + object = parser.parser(); map.put(clazz, object); } return object.cacheFieldsAndMethods(); diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java index 8061cb32..730ec7a2 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java @@ -1,5 +1,6 @@ package com.codingapi.springboot.framework.entity; +import com.codingapi.springboot.framework.script.annotation.ScriptType; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -8,10 +9,14 @@ @Data @Entity +@ScriptType(dataType = MyTest.class) public class MyTest { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @ScriptType(name = "id", description = "id", dataType = Long.class) private Long id; + + @ScriptType(name = "name", description = "name", dataType = String.class) private String name; } diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java index df2861db..6f8832ed 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java @@ -2,6 +2,7 @@ import com.codingapi.springboot.framework.script.request.GroovyRunningScript; import com.codingapi.springboot.framework.script.request.MyScriptRequest; +import com.codingapi.springboot.framework.script.schema.GroovySchema; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -40,6 +41,8 @@ def run(request){ MyScriptRequest request = new MyScriptRequest(100); GroovyRunningScript runningScript = new GroovyRunningScript<>(script, Integer.class, request); + GroovySchema schema = runningScript.getGroovyMetadata().toSchema(); + System.out.println(schema.toJson()); assertEquals(1,runningScript.getGroovyMetadata().getRequests().size()); long t1 = System.currentTimeMillis(); diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java index 505fbf74..1ab2da5b 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java @@ -16,11 +16,11 @@ @AllArgsConstructor public class MyScriptRequest { - @ScriptType(name = "count", description = "总数量", dataType = Integer.class) + @ScriptType(name = "count", description = "总数量", dataType = int.class) private final int count; - @ScriptType(name = "test", description = "test", dataType = MyScriptRequest.class) - private MyScriptRequest test; + @ScriptType(name = "test", description = "test", dataType = MyTest.class) + private MyTest test; @ScriptFunction( @@ -28,7 +28,7 @@ public class MyScriptRequest { description = "是否匹配", returnType = Boolean.class, requests = { - @ScriptType(name = "count", description = "描述信息", dataType = Integer.class), + @ScriptType(name = "count", description = "描述信息", dataType = int.class), } ) public boolean isSupport(int count) { @@ -41,7 +41,6 @@ public MyScriptRequest(int count) { public void addData(MyTestRepository testRepository){ - System.out.println(testRepository); MyTest myTest = new MyTest(); myTest.setName("test"); testRepository.save(myTest); From c17afd5a4699cf49ecd7daa92042708c74443a3b Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Tue, 26 May 2026 08:40:51 +0800 Subject: [PATCH 04/10] add GroovySchema --- .../script/annotation/ScriptFunction.java | 9 ++--- .../script/annotation/ScriptType.java | 10 ++---- .../script/service/GroovyTypeParser.java | 36 +++++++++++++------ .../springboot/framework/entity/MyTest.java | 6 ++-- .../script/request/MyScriptRequest.java | 14 ++++---- 5 files changed, 38 insertions(+), 37 deletions(-) diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptFunction.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptFunction.java index c2920ab4..93611fa7 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptFunction.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptFunction.java @@ -21,13 +21,8 @@ String description() default ""; /** - * 参数对象 + * 参数 */ - ScriptType[] requests() default {}; - - /** - * 返回数据类型 - */ - Class returnType(); + ScriptType[] parameters() default {}; } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptType.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptType.java index f89ae836..24daa663 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptType.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptType.java @@ -3,9 +3,9 @@ import java.lang.annotation.*; /** - * 脚本对象 + * 脚本对象 */ -@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD}) +@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ScriptType { @@ -20,10 +20,4 @@ */ String description() default ""; - /** - * 数据类型 - */ - Class dataType(); - - } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java index 344d3b54..1f223e15 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java @@ -8,6 +8,9 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.util.HashMap; +import java.util.Map; /** * 脚本类型解析对象 @@ -28,9 +31,12 @@ public GroovyTypeParser(Class clazz) { private void resetObjectMeta(GroovyType target, ScriptType scriptType) { - target.setDataType(scriptType.dataType()); - target.setName(scriptType.name()); - target.setDescription(scriptType.description()); + if (StringUtils.hasText(scriptType.name())) { + target.setName(scriptType.name()); + } + if (StringUtils.hasText(scriptType.description())) { + target.setDescription(scriptType.description()); + } } private void loadObjectMeta() { @@ -45,7 +51,7 @@ private void loadFields() { for (Field field : fields) { ScriptType scriptType = field.getAnnotation(ScriptType.class); if (scriptType != null) { - GroovyType groovyType = TempGroovyTypeCache.getInstance().getOrCreate(scriptType.dataType()); + GroovyType groovyType = TempGroovyTypeCache.getInstance().getOrCreate(field.getType()); this.resetObjectMeta(groovyType, scriptType); this.object.addField(groovyType); } @@ -68,17 +74,25 @@ private void loadMethods() { groovyFunction.setDescription(scriptFunction.description()); } + Parameter[] methodParameters = method.getParameters(); + Map parameterMap = new HashMap<>(); + for (Parameter parameter : methodParameters) { + parameterMap.put(parameter.getName(), parameter); + } - ScriptType[] requests = scriptFunction.requests(); - if (requests != null) { - for (ScriptType request : requests) { - GroovyType requestObject = TempGroovyTypeCache.getInstance().getOrCreate(request.dataType()); - this.resetObjectMeta(requestObject, request); - groovyFunction.addRequest(requestObject); + ScriptType[] parameters = scriptFunction.parameters(); + if (parameters != null) { + for (ScriptType parameter : parameters) { + Parameter methodParameter = parameterMap.get(parameter.name()); + if (methodParameter != null) { + GroovyType requestObject = TempGroovyTypeCache.getInstance().getOrCreate(methodParameter.getType()); + this.resetObjectMeta(requestObject, parameter); + groovyFunction.addRequest(requestObject); + } } } - Class returnType = scriptFunction.returnType(); + Class returnType = method.getReturnType(); groovyFunction.setReturnType(TempGroovyTypeCache.getInstance().getOrCreate(returnType)); this.object.addFunction(groovyFunction); } diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java index 730ec7a2..29a66859 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java @@ -9,14 +9,14 @@ @Data @Entity -@ScriptType(dataType = MyTest.class) +@ScriptType public class MyTest { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @ScriptType(name = "id", description = "id", dataType = Long.class) + @ScriptType(name = "id", description = "id") private Long id; - @ScriptType(name = "name", description = "name", dataType = String.class) + @ScriptType(name = "name", description = "name") private String name; } diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java index 1ab2da5b..3668a3e3 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java @@ -10,25 +10,23 @@ @Getter @ScriptType( name = "request", - description = "请求参数", - dataType = MyScriptRequest.class + description = "请求参数" ) @AllArgsConstructor public class MyScriptRequest { - @ScriptType(name = "count", description = "总数量", dataType = int.class) + @ScriptType(name = "count", description = "总数量") private final int count; - @ScriptType(name = "test", description = "test", dataType = MyTest.class) + @ScriptType(name = "test", description = "test") private MyTest test; @ScriptFunction( name = "isSupport", description = "是否匹配", - returnType = Boolean.class, - requests = { - @ScriptType(name = "count", description = "描述信息", dataType = int.class), + parameters = { + @ScriptType(name = "count", description = "描述信息") } ) public boolean isSupport(int count) { @@ -40,7 +38,7 @@ public MyScriptRequest(int count) { } - public void addData(MyTestRepository testRepository){ + public void addData(MyTestRepository testRepository) { MyTest myTest = new MyTest(); myTest.setName("test"); testRepository.save(myTest); From d30d6f2494628fbe1b7c021d3011625e441a1199 Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Tue, 26 May 2026 08:53:08 +0800 Subject: [PATCH 05/10] add GroovySchema --- .../framework/script/meta/GroovyFunction.java | 10 +++++----- .../framework/script/schema/SchemaFunction.java | 12 ++++-------- .../framework/script/schema/SchemaType.java | 7 +++++++ .../framework/script/service/GroovyTypeParser.java | 2 +- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyFunction.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyFunction.java index 6184ba5b..a0e061d3 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyFunction.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyFunction.java @@ -32,14 +32,14 @@ public class GroovyFunction { /** * 参数类型 */ - private List requests; + private List parameters; - public void addRequest(GroovyType request){ - if(this.requests ==null){ - this.requests = new ArrayList<>(); + public void addParameter(GroovyType parameter){ + if(this.parameters ==null){ + this.parameters = new ArrayList<>(); } - this.requests.add(request); + this.parameters.add(parameter); } } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaFunction.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaFunction.java index 9d3ad984..7e1d1ced 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaFunction.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaFunction.java @@ -21,20 +21,16 @@ public class SchemaFunction { @Getter private String returnType; @Getter - private List requests; - - public SchemaFunction(GroovyFunction function) { - this(function,null); - } + private List parameters; public SchemaFunction(GroovyFunction function,GroovySchema schema) { this.name = function.getName(); this.description = function.getDescription(); this.returnType = function.getReturnType().getDataClassName(); - this.requests = new ArrayList<>(); + this.parameters = new ArrayList<>(); - for(GroovyType request: function.getRequests()){ - this.requests.add(new SchemaField(request,schema)); + for(GroovyType request: function.getParameters()){ + this.parameters.add(new SchemaField(request,schema)); } if(schema!=null){ diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaType.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaType.java index cfae75b0..637feb98 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaType.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaType.java @@ -20,10 +20,17 @@ public class SchemaType { private final GroovySchema schema; + public SchemaType(List functions, List fields) { + this.functions = functions; + this.fields = fields; + this.schema = null; + } + public boolean isEmpty(){ return (fields==null || fields.isEmpty()) && (functions==null || functions.isEmpty()); } + public SchemaType(GroovyType groovyType,GroovySchema schema) { this.schema = schema; this.addFields(groovyType.getFields()); diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java index 1f223e15..035508a4 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java @@ -87,7 +87,7 @@ private void loadMethods() { if (methodParameter != null) { GroovyType requestObject = TempGroovyTypeCache.getInstance().getOrCreate(methodParameter.getType()); this.resetObjectMeta(requestObject, parameter); - groovyFunction.addRequest(requestObject); + groovyFunction.addParameter(requestObject); } } } From 12d2c875ad948c69a1eda614add58d58be548a8f Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Tue, 26 May 2026 10:46:02 +0800 Subject: [PATCH 06/10] fix GroovyScriptRunningContext --- .../framework/script/GroovyScriptRunner.java | 2 +- .../script/annotation/ScriptField.java | 23 ++++ .../script/annotation/ScriptFunction.java | 4 +- .../script/annotation/ScriptType.java | 7 +- .../framework/script/meta/GroovyField.java | 30 +++++ .../framework/script/meta/GroovyFunction.java | 18 ++- .../framework/script/meta/GroovyMetadata.java | 57 ++++++--- .../framework/script/meta/GroovyType.java | 59 ++------- .../script/request/GroovyBindObject.java | 114 +++++++++++++++++- .../request/GroovyBindObjectBuilder.java | 27 +++++ .../script/request/GroovyRunningScript.java | 62 +++++----- .../framework/script/schema/GroovySchema.java | 104 ---------------- .../framework/script/schema/SchemaField.java | 34 ------ .../script/schema/SchemaFunction.java | 41 ------- .../framework/script/schema/SchemaType.java | 58 --------- .../service/GroovyMetadataParserService.java | 71 ++++++++--- .../script/service/GroovyTypeParser.java | 77 ++++++------ .../script/service/TempGroovyTypeCache.java | 55 --------- .../springboot/framework/entity/MyTest.java | 7 +- .../GroovyScriptRunningContextTest.java | 32 +++-- ...sactionGroovyScriptRunningContextTest.java | 17 ++- .../script/request/MyScriptRequest.java | 12 +- 22 files changed, 410 insertions(+), 501 deletions(-) create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptField.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyField.java create mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyBindObjectBuilder.java delete mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/GroovySchema.java delete mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaField.java delete mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaFunction.java delete mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaType.java delete mode 100644 springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/TempGroovyTypeCache.java diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java index c4e0b4dd..7f5cd576 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java @@ -113,7 +113,7 @@ public T run(String method, String script, Class returnType, List parameters; + private final List parameters; + public GroovyFunction() { + this.parameters = new ArrayList<>(); + } - public void addParameter(GroovyType parameter){ - if(this.parameters ==null){ - this.parameters = new ArrayList<>(); - } + public void addParameter(GroovyField parameter) { this.parameters.add(parameter); } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java index 3164786b..1e289922 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java @@ -1,58 +1,77 @@ package com.codingapi.springboot.framework.script.meta; -import com.codingapi.springboot.framework.script.schema.GroovySchema; -import lombok.AllArgsConstructor; +import com.codingapi.springboot.framework.script.annotation.ScriptType; +import com.codingapi.springboot.framework.script.service.GroovyTypeParser; import lombok.Getter; -import lombok.NoArgsConstructor; import lombok.Setter; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * 脚本对象元数据结构 */ @Getter -@Setter -@AllArgsConstructor -@NoArgsConstructor public class GroovyMetadata { /** * 请求参数 */ - private List requests; + private final List requests; /** * 绑定参数 */ - private List binds; + private final List binds; /** * 返回类型 */ - private GroovyType returnType; + @Setter + private String returnType; + /** + * 字段类型 + */ + private final Map types; + + + public GroovyMetadata() { + this.types = new HashMap<>(); + this.requests = new ArrayList<>(); + this.binds = new ArrayList<>(); + } - public void addRequest(GroovyType request){ - if(this.requests==null){ - this.requests = new ArrayList<>(); + public void buildType(Class clazz) { + String dataType = clazz.getSimpleName(); + GroovyType groovyType = this.types.get(dataType); + if (groovyType == null) { + GroovyTypeParser groovyTypeParser = new GroovyTypeParser(clazz, this); + groovyType = groovyTypeParser.parser(); + groovyType.setDataType(dataType); + ScriptType scriptType = clazz.getAnnotation(ScriptType.class); + if(scriptType!=null) { + groovyType.setDescription(scriptType.description()); + } + this.types.put(dataType, groovyType); } + } + + public void addRequest(GroovyField request) { this.requests.add(request); } - public void addBind(GroovyType bind){ - if(this.binds==null){ - this.binds = new ArrayList<>(); - } - this.binds.add(bind); + public GroovyType getType(String dataType){ + return this.types.get(dataType); } - public GroovySchema toSchema(){ - return new GroovySchema(this); + public void addBind(GroovyField bind) { + this.binds.add(bind); } } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java index a1cafe41..0ad3f73e 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java @@ -1,8 +1,6 @@ package com.codingapi.springboot.framework.script.meta; -import lombok.AllArgsConstructor; import lombok.Getter; -import lombok.NoArgsConstructor; import lombok.Setter; import java.util.ArrayList; @@ -12,18 +10,10 @@ * 脚本对象类型 */ @Getter -@AllArgsConstructor -@NoArgsConstructor public class GroovyType { /** - * 访问名称 - */ - @Setter - private String name; - - /** - * 描述信息 + * 对象描述 */ @Setter private String description; @@ -32,62 +22,31 @@ public class GroovyType { * 数据类型 */ @Setter - private Class dataType; + private String dataType; /** * 属性列表 */ - private List fields; + private final List fields; /** * 函数列表 */ - private List functions; + private final List functions; - - public void initFields(){ - if(this.fields ==null){ - this.fields = new ArrayList<>(); - } - } - - public String getDataClassName(){ - return dataType.getSimpleName(); + public GroovyType() { + this.fields = new ArrayList<>(); + this.functions = new ArrayList<>(); } - public void initFunctions(){ - if(this.functions==null){ - this.functions = new ArrayList<>(); - } - } - - public void addFunction(GroovyFunction function){ - if(this.functions==null){ - this.functions = new ArrayList<>(); - } - + public void addFunction(GroovyFunction function) { this.functions.add(function); } - public void addField(GroovyType field){ - if(this.fields ==null){ - this.fields = new ArrayList<>(); - } + public void addField(GroovyField field) { this.fields.add(field); } - - /** - * 保留函数和属性 - */ - public GroovyType cacheFieldsAndMethods(){ - GroovyType cache = new GroovyType(); - cache.functions = this.functions; - cache.fields = this.fields; - cache.dataType = this.dataType; - return cache; - } - } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyBindObject.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyBindObject.java index aa518529..4acf4f32 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyBindObject.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyBindObject.java @@ -3,8 +3,11 @@ import lombok.AllArgsConstructor; import lombok.Getter; +import java.util.ArrayList; +import java.util.List; + /** - * 运行时绑定对象 + * 运行时绑定对象 */ @Getter @AllArgsConstructor @@ -13,10 +16,117 @@ public class GroovyBindObject { /** * 绑定的访问对象 */ - private String key; + private String name; /** * 绑定的对象实例 */ private Object object; + + public static List of(String key, Object value) { + List list = new ArrayList<>(); + list.add(new GroovyBindObject(key, value)); + return list; + } + + public static List of(String key1, Object value1, String key2, Object value2) { + List list = new ArrayList<>(); + list.add(new GroovyBindObject(key1, value1)); + list.add(new GroovyBindObject(key2, value2)); + return list; + } + + public static List of(String key1, Object value1, String key2, Object value2, String key3, Object value3) { + List list = new ArrayList<>(); + list.add(new GroovyBindObject(key1, value1)); + list.add(new GroovyBindObject(key2, value2)); + list.add(new GroovyBindObject(key3, value3)); + return list; + } + + public static List of(String key1, Object value1, String key2, Object value2, String key3, Object value3, String key4, Object value4) { + List list = new ArrayList<>(); + list.add(new GroovyBindObject(key1, value1)); + list.add(new GroovyBindObject(key2, value2)); + list.add(new GroovyBindObject(key3, value3)); + list.add(new GroovyBindObject(key4, value4)); + return list; + } + + public static List of(String key1, Object value1, String key2, Object value2, String key3, Object value3, String key4, Object value4, String key5, Object value5) { + List list = new ArrayList<>(); + list.add(new GroovyBindObject(key1, value1)); + list.add(new GroovyBindObject(key2, value2)); + list.add(new GroovyBindObject(key3, value3)); + list.add(new GroovyBindObject(key4, value4)); + list.add(new GroovyBindObject(key5, value5)); + return list; + } + + + public static List of(String key1, Object value1, String key2, Object value2, String key3, Object value3, String key4, Object value4, String key5, Object value5, String key6, Object value6) { + List list = new ArrayList<>(); + list.add(new GroovyBindObject(key1, value1)); + list.add(new GroovyBindObject(key2, value2)); + list.add(new GroovyBindObject(key3, value3)); + list.add(new GroovyBindObject(key4, value4)); + list.add(new GroovyBindObject(key5, value5)); + list.add(new GroovyBindObject(key6, value6)); + return list; + } + + public static List of(String key1, Object value1, String key2, Object value2, String key3, Object value3, String key4, Object value4, String key5, Object value5, String key6, Object value6, String key7, Object value7) { + List list = new ArrayList<>(); + list.add(new GroovyBindObject(key1, value1)); + list.add(new GroovyBindObject(key2, value2)); + list.add(new GroovyBindObject(key3, value3)); + list.add(new GroovyBindObject(key4, value4)); + list.add(new GroovyBindObject(key5, value5)); + list.add(new GroovyBindObject(key6, value6)); + list.add(new GroovyBindObject(key7, value7)); + return list; + } + + public static List of(String key1, Object value1, String key2, Object value2, String key3, Object value3, String key4, Object value4, String key5, Object value5, String key6, Object value6, String key7, Object value7, String key8, Object value8) { + List list = new ArrayList<>(); + list.add(new GroovyBindObject(key1, value1)); + list.add(new GroovyBindObject(key2, value2)); + list.add(new GroovyBindObject(key3, value3)); + list.add(new GroovyBindObject(key4, value4)); + list.add(new GroovyBindObject(key5, value5)); + list.add(new GroovyBindObject(key6, value6)); + list.add(new GroovyBindObject(key7, value7)); + list.add(new GroovyBindObject(key8, value8)); + return list; + } + + public static List of(String key1, Object value1, String key2, Object value2, String key3, Object value3, String key4, Object value4, String key5, Object value5, String key6, Object value6, String key7, Object value7, String key8, Object value8, String key9, Object value9) { + List list = new ArrayList<>(); + list.add(new GroovyBindObject(key1, value1)); + list.add(new GroovyBindObject(key2, value2)); + list.add(new GroovyBindObject(key3, value3)); + list.add(new GroovyBindObject(key4, value4)); + list.add(new GroovyBindObject(key5, value5)); + list.add(new GroovyBindObject(key6, value6)); + list.add(new GroovyBindObject(key7, value7)); + list.add(new GroovyBindObject(key8, value8)); + list.add(new GroovyBindObject(key9, value9)); + return list; + } + + public static List of(String key1, Object value1, String key2, Object value2, String key3, Object value3, String key4, Object value4, String key5, Object value5, String key6, Object value6, String key7, Object value7, String key8, Object value8, String key9, Object value9, String key10, Object value10) { + List list = new ArrayList<>(); + list.add(new GroovyBindObject(key1, value1)); + list.add(new GroovyBindObject(key2, value2)); + list.add(new GroovyBindObject(key3, value3)); + list.add(new GroovyBindObject(key4, value4)); + list.add(new GroovyBindObject(key5, value5)); + list.add(new GroovyBindObject(key6, value6)); + list.add(new GroovyBindObject(key7, value7)); + list.add(new GroovyBindObject(key8, value8)); + list.add(new GroovyBindObject(key9, value9)); + list.add(new GroovyBindObject(key10, value10)); + return list; + } + } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyBindObjectBuilder.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyBindObjectBuilder.java new file mode 100644 index 00000000..07b69ce5 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyBindObjectBuilder.java @@ -0,0 +1,27 @@ +package com.codingapi.springboot.framework.script.request; + +import java.util.ArrayList; +import java.util.List; + +public class GroovyBindObjectBuilder { + + private final List list; + + private GroovyBindObjectBuilder() { + this.list = new ArrayList<>(); + } + + public static GroovyBindObjectBuilder builder(){ + return new GroovyBindObjectBuilder(); + } + + public GroovyBindObjectBuilder add(String name, Object value) { + this.list.add(new GroovyBindObject(name, value)); + return this; + } + + + public List build() { + return this.list; + } +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java index aa0acd98..2fc83458 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java @@ -3,10 +3,8 @@ import com.codingapi.springboot.framework.script.meta.GroovyMetadata; import com.codingapi.springboot.framework.script.service.GroovyMetadataParserService; import lombok.Getter; -import lombok.Setter; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; /** @@ -35,71 +33,65 @@ public class GroovyRunningScript { /** * 请求参数对象 */ - private final List requests; + private final List requests; /** * 绑定数据对象 */ - private List binds; + private final List binds; /** * 脚本元数据信息 */ - @Setter - private GroovyMetadata groovyMetadata; + private GroovyMetadata metadata; - public GroovyRunningScript(String method, String script, Class returnType, GroovyMetadata groovyMetadata, List binds, Object... requests) { + public GroovyRunningScript(String method, String script, Class returnType, List binds, List requests) { this.method = method; this.script = script; this.returnType = returnType; - this.requests = new ArrayList<>(); + this.requests = requests; this.binds = binds; - if (requests != null) { - this.requests.addAll(Arrays.asList(requests)); - } - this.groovyMetadata = groovyMetadata; + } - if (this.groovyMetadata == null) { - this.resetMetaData(); - } + public GroovyRunningScript(String method, String script, Class returnType, GroovyBindObjectBuilder bindBuilder, GroovyBindObjectBuilder requestBuilder) { + this(method, script, returnType, bindBuilder != null ? bindBuilder.build() : null, requestBuilder != null ? requestBuilder.build() : null); + } + + + public GroovyRunningScript(String method, String script, Class returnType, GroovyBindObjectBuilder requestBuilder) { + this(method, script, returnType, null, requestBuilder); } - public GroovyRunningScript(String script, Class returnType, GroovyMetadata groovyMetadata, List binds, Object... request) { - this("run", script, returnType, groovyMetadata, binds, request); + public GroovyRunningScript(String script, Class returnType, GroovyBindObjectBuilder bindBuilder, GroovyBindObjectBuilder requestBuilder) { + this("run", script, returnType, bindBuilder, requestBuilder); } - public GroovyRunningScript(String script, Class returnType, Object... request) { - this("run", script, returnType, null, null, request); + public GroovyRunningScript(String script, Class returnType, GroovyBindObjectBuilder requestBuilder) { + this("run", script, returnType, null, requestBuilder); } /** * 获取参数 */ public Object[] getParams() { - return this.requests.toArray(); + List objects = new ArrayList<>(); + for(GroovyBindObject bindObject:this.requests){ + objects.add(bindObject.getObject()); + } + return objects.toArray(); } - /** - * 添加绑定对象 - * - * @param key 绑定key - * @param bind 绑定对象 - */ - public void addBindObject(String key, Object bind) { - if(this.binds==null){ - this.binds = new ArrayList<>(); - } - this.binds.add(new GroovyBindObject(key, bind)); - this.resetMetaData(); + public void resetMetadata(GroovyMetadata metadata) { + this.metadata = metadata; } /** - * 重置元数据对象 + * 构建元数据信息 */ - private void resetMetaData() { + public void buildMetadata() { GroovyMetadataParserService groovyMetaDataParserService = new GroovyMetadataParserService(this); - this.groovyMetadata = groovyMetaDataParserService.parser(); + this.metadata = groovyMetaDataParserService.parser(); } } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/GroovySchema.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/GroovySchema.java deleted file mode 100644 index 1d39231d..00000000 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/GroovySchema.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.codingapi.springboot.framework.script.schema; - -import com.alibaba.fastjson.JSONObject; -import com.codingapi.springboot.framework.script.meta.GroovyMetadata; -import com.codingapi.springboot.framework.script.meta.GroovyType; -import lombok.Getter; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * 脚本提示信息 - */ -public class GroovySchema { - - @Getter - private final Map types; - - @Getter - private final List requests; - - @Getter - private final List binds; - - @Getter - private final String returnType; - - private transient final GroovyMetadata metadata; - - public GroovySchema(GroovyMetadata metadata) { - this.metadata = metadata; - this.types = new HashMap<>(); - this.requests = new ArrayList<>(); - this.binds = new ArrayList<>(); - - this.loadRequestTypes(); - this.loadBindTypes(); - this.loadReturnType(); - - this.loadRequests(); - this.loadBinds(); - - this.returnType = metadata.getReturnType().getDataClassName(); - } - - - public String toJson() { - return JSONObject.toJSONString(this); - } - - private void loadRequests() { - if (this.metadata.getRequests() != null) { - for (GroovyType groovyType : this.metadata.getRequests()) { - SchemaField schemaField = new SchemaField(groovyType); - this.requests.add(schemaField); - } - } - } - - - private void loadBinds() { - if (this.metadata.getBinds() != null) { - for (GroovyType groovyType : this.metadata.getBinds()) { - SchemaField schemaField = new SchemaField(groovyType); - this.binds.add(schemaField); - } - } - } - - private void loadRequestTypes() { - if (this.metadata.getRequests() != null) { - for (GroovyType groovyType : this.metadata.getRequests()) { - this.loadSchemaType(groovyType); - } - } - } - - public void loadSchemaType(GroovyType groovyType) { - String key = groovyType.getDataClassName(); - if (this.types.get(key) == null) { - SchemaType schemaType = new SchemaType(groovyType, this); - this.types.put(groovyType.getDataClassName(), schemaType); - } - } - - private void loadBindTypes() { - if (this.metadata.getBinds() != null) { - for (GroovyType groovyType : this.metadata.getBinds()) { - this.loadSchemaType(groovyType); - } - } - } - - private void loadReturnType() { - GroovyType groovyType = this.metadata.getReturnType(); - if (groovyType != null) { - this.loadSchemaType(groovyType); - } - } - - -} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaField.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaField.java deleted file mode 100644 index a7f376ee..00000000 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaField.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.codingapi.springboot.framework.script.schema; - -import com.codingapi.springboot.framework.script.meta.GroovyType; -import lombok.Getter; -import lombok.Setter; - -/** - * 字段定义 - */ -@Setter -public class SchemaField { - - @Getter - private String name; - @Getter - private String description; - @Getter - private String dataType; - - public SchemaField(GroovyType filed) { - this(filed,null); - } - - public SchemaField(GroovyType filed,GroovySchema schema) { - this.name = filed.getName(); - this.description = filed.getDescription(); - this.dataType = filed.getDataClassName(); - - if(schema!=null){ - schema.loadSchemaType(filed); - } - } - -} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaFunction.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaFunction.java deleted file mode 100644 index 7e1d1ced..00000000 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaFunction.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.codingapi.springboot.framework.script.schema; - -import com.codingapi.springboot.framework.script.meta.GroovyFunction; -import com.codingapi.springboot.framework.script.meta.GroovyType; -import lombok.Getter; -import lombok.Setter; - -import java.util.ArrayList; -import java.util.List; - -/** - * 函数定义 - */ -@Setter -public class SchemaFunction { - - @Getter - private String name; - @Getter - private String description; - @Getter - private String returnType; - @Getter - private List parameters; - - public SchemaFunction(GroovyFunction function,GroovySchema schema) { - this.name = function.getName(); - this.description = function.getDescription(); - this.returnType = function.getReturnType().getDataClassName(); - this.parameters = new ArrayList<>(); - - for(GroovyType request: function.getParameters()){ - this.parameters.add(new SchemaField(request,schema)); - } - - if(schema!=null){ - schema.loadSchemaType(function.getReturnType()); - } - } - -} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaType.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaType.java deleted file mode 100644 index 637feb98..00000000 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/schema/SchemaType.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.codingapi.springboot.framework.script.schema; - -import com.codingapi.springboot.framework.script.meta.GroovyFunction; -import com.codingapi.springboot.framework.script.meta.GroovyType; -import lombok.Getter; - -import java.util.ArrayList; -import java.util.List; - -/** - * 数据类型 - */ -public class SchemaType { - - @Getter - private List fields; - - @Getter - private List functions; - - private final GroovySchema schema; - - public SchemaType(List functions, List fields) { - this.functions = functions; - this.fields = fields; - this.schema = null; - } - - public boolean isEmpty(){ - return (fields==null || fields.isEmpty()) && (functions==null || functions.isEmpty()); - } - - - public SchemaType(GroovyType groovyType,GroovySchema schema) { - this.schema = schema; - this.addFields(groovyType.getFields()); - this.addFunctions(groovyType.getFunctions()); - } - - public void addFields(List fields) { - if(this.fields == null){ - this.fields = new ArrayList<>(); - } - for(GroovyType field:fields){ - this.fields.add(new SchemaField(field,this.schema)); - } - } - - public void addFunctions(List functions) { - if(this.functions ==null){ - this.functions = new ArrayList<>(); - } - for(GroovyFunction function:functions){ - SchemaFunction schemaFunction = new SchemaFunction(function,this.schema); - this.functions.add(schemaFunction); - } - } -} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyMetadataParserService.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyMetadataParserService.java index 4b725821..61fa046f 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyMetadataParserService.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyMetadataParserService.java @@ -1,5 +1,6 @@ package com.codingapi.springboot.framework.script.service; +import com.codingapi.springboot.framework.script.meta.GroovyField; import com.codingapi.springboot.framework.script.meta.GroovyMetadata; import com.codingapi.springboot.framework.script.meta.GroovyType; import com.codingapi.springboot.framework.script.request.GroovyBindObject; @@ -22,29 +23,25 @@ public GroovyMetadataParserService(GroovyRunningScript script) { } - private void loadRequests() { - List requests = script.getRequests(); + private void loadRequestTypes() { + List requests = script.getRequests(); if (requests != null && !requests.isEmpty()) { - for (Object obj : requests) { - if (obj != null) { - Class objClass = obj.getClass(); - GroovyType groovyType = this.parserGroovyObject(objClass); - this.groovyMetadata.addRequest(groovyType); + for (GroovyBindObject request : requests) { + if (request != null) { + Class objClass = request.getObject().getClass(); + this.groovyMetadata.buildType(objClass); } } } } - private void loadBinds() { + private void loadBindTypes() { List binds = script.getBinds(); if (binds != null && !binds.isEmpty()) { for (GroovyBindObject bindObject : binds) { if (bindObject != null) { - String name = bindObject.getKey(); Class objClass = bindObject.getObject().getClass(); - GroovyType groovyType = this.parserGroovyObject(objClass); - groovyType.setName(name); - this.groovyMetadata.addBind(groovyType); + this.groovyMetadata.buildType(objClass); } } } @@ -52,22 +49,58 @@ private void loadBinds() { private void loadReturnType() { Class returnTypeClass = this.script.getReturnType(); - GroovyType groovyType = this.parserGroovyObject(returnTypeClass); - this.groovyMetadata.setReturnType(groovyType); + this.groovyMetadata.buildType(returnTypeClass); } - private GroovyType parserGroovyObject(Class clazz) { - GroovyTypeParser parser = new GroovyTypeParser(clazz); - return parser.parser(); + private void loadRequests(){ + List requests = script.getRequests(); + if (requests != null && !requests.isEmpty()) { + for (GroovyBindObject request : requests) { + if (request != null) { + Class objClass = request.getObject().getClass(); + String dataType = objClass.getSimpleName(); + GroovyField groovyField = new GroovyField(); + groovyField.setDataType(dataType); + groovyField.setName(request.getName()); + GroovyType groovyType = this.groovyMetadata.getType(dataType); + if(groovyType!=null){ + groovyField.setDescription(groovyType.getDescription()); + } + this.groovyMetadata.addRequest(groovyField); + } + } + } + } + + private void loadBinds(){ + List binds = script.getBinds(); + if (binds != null && !binds.isEmpty()) { + for (GroovyBindObject bind : binds) { + if (bind != null) { + Class objClass = bind.getClass(); + GroovyField groovyField = new GroovyField(); + String dataType = objClass.getSimpleName(); + groovyField.setDataType(dataType); + groovyField.setName(bind.getName()); + GroovyType groovyType = this.groovyMetadata.getType(dataType); + if(groovyType!=null){ + groovyField.setDescription(groovyType.getDescription()); + } + this.groovyMetadata.addBind(groovyField); + } + } + } } public GroovyMetadata parser() { - TempGroovyTypeCache.getInstance().clear(); + this.loadRequestTypes(); + this.loadBindTypes(); + this.loadReturnType(); + this.loadRequests(); this.loadBinds(); - this.loadReturnType(); return groovyMetadata; } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java index 035508a4..21dbb3a6 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java @@ -1,8 +1,10 @@ package com.codingapi.springboot.framework.script.service; +import com.codingapi.springboot.framework.script.annotation.ScriptField; import com.codingapi.springboot.framework.script.annotation.ScriptFunction; -import com.codingapi.springboot.framework.script.annotation.ScriptType; +import com.codingapi.springboot.framework.script.meta.GroovyField; import com.codingapi.springboot.framework.script.meta.GroovyFunction; +import com.codingapi.springboot.framework.script.meta.GroovyMetadata; import com.codingapi.springboot.framework.script.meta.GroovyType; import org.springframework.util.StringUtils; @@ -21,39 +23,33 @@ public class GroovyTypeParser { private final GroovyType object; - public GroovyTypeParser(Class clazz) { + private final GroovyMetadata metadata; + + public GroovyTypeParser(Class clazz, GroovyMetadata metadata) { this.clazz = clazz; + this.metadata = metadata; this.object = new GroovyType(); - this.object.setDataType(clazz); - this.object.initFields(); - this.object.initFunctions(); } - private void resetObjectMeta(GroovyType target, ScriptType scriptType) { - if (StringUtils.hasText(scriptType.name())) { - target.setName(scriptType.name()); - } - if (StringUtils.hasText(scriptType.description())) { - target.setDescription(scriptType.description()); - } - } - - private void loadObjectMeta() { - ScriptType scriptType = clazz.getAnnotation(ScriptType.class); - if (scriptType != null) { - this.resetObjectMeta(object, scriptType); - } - } - private void loadFields() { Field[] fields = this.clazz.getDeclaredFields(); for (Field field : fields) { - ScriptType scriptType = field.getAnnotation(ScriptType.class); - if (scriptType != null) { - GroovyType groovyType = TempGroovyTypeCache.getInstance().getOrCreate(field.getType()); - this.resetObjectMeta(groovyType, scriptType); - this.object.addField(groovyType); + ScriptField scriptField = field.getAnnotation(ScriptField.class); + if (scriptField != null) { + Class clazz = field.getType(); + this.metadata.buildType(field.getType()); + GroovyField groovyField = new GroovyField(); + groovyField.setDataType(clazz.getSimpleName()); + groovyField.setName(field.getName()); + + if (StringUtils.hasText(scriptField.name())) { + groovyField.setName(scriptField.name()); + } + if (StringUtils.hasText(scriptField.description())) { + groovyField.setDescription(scriptField.description()); + } + this.object.addField(groovyField); } } } @@ -64,10 +60,10 @@ private void loadMethods() { ScriptFunction scriptFunction = method.getAnnotation(ScriptFunction.class); if (scriptFunction != null) { GroovyFunction groovyFunction = new GroovyFunction(); + groovyFunction.setName(method.getName()); + if (StringUtils.hasText(scriptFunction.name())) { groovyFunction.setName(scriptFunction.name()); - } else { - groovyFunction.setName(method.getName()); } if (StringUtils.hasText(scriptFunction.description())) { @@ -80,20 +76,32 @@ private void loadMethods() { parameterMap.put(parameter.getName(), parameter); } - ScriptType[] parameters = scriptFunction.parameters(); + ScriptField[] parameters = scriptFunction.parameters(); if (parameters != null) { - for (ScriptType parameter : parameters) { + for (ScriptField parameter : parameters) { Parameter methodParameter = parameterMap.get(parameter.name()); if (methodParameter != null) { - GroovyType requestObject = TempGroovyTypeCache.getInstance().getOrCreate(methodParameter.getType()); - this.resetObjectMeta(requestObject, parameter); - groovyFunction.addParameter(requestObject); + Class clazz = methodParameter.getType(); + this.metadata.buildType(clazz); + + GroovyField groovyParameter = new GroovyField(); + groovyParameter.setDataType(clazz.getSimpleName()); + groovyParameter.setName(methodParameter.getName()); + + if (StringUtils.hasText(parameter.name())) { + groovyParameter.setName(parameter.name()); + } + if (StringUtils.hasText(parameter.description())) { + groovyParameter.setDescription(parameter.description()); + } + + groovyFunction.addParameter(groovyParameter); } } } Class returnType = method.getReturnType(); - groovyFunction.setReturnType(TempGroovyTypeCache.getInstance().getOrCreate(returnType)); + this.metadata.buildType(returnType); this.object.addFunction(groovyFunction); } } @@ -101,7 +109,6 @@ private void loadMethods() { public GroovyType parser() { - this.loadObjectMeta(); this.loadFields(); this.loadMethods(); return this.object; diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/TempGroovyTypeCache.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/TempGroovyTypeCache.java deleted file mode 100644 index 1dafef46..00000000 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/TempGroovyTypeCache.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.codingapi.springboot.framework.script.service; - -import com.codingapi.springboot.framework.script.meta.GroovyType; -import lombok.Getter; - -import java.util.HashMap; -import java.util.Map; - -/** - * 脚本对象临时缓存对象 - */ -class TempGroovyTypeCache { - - @Getter - private final static TempGroovyTypeCache instance = new TempGroovyTypeCache(); - - private final ThreadLocal, GroovyType>> cache; - - - private TempGroovyTypeCache() { - this.cache = new ThreadLocal<>(); - } - - public void clear() { - this.cache.remove(); - } - - - public void set(Class key, GroovyType object) { - Map, GroovyType> map = this.cache.get(); - if (map == null) { - map = new HashMap<>(); - this.cache.set(map); - } - map.put(key, object); - } - - - public GroovyType getOrCreate(Class clazz) { - Map, GroovyType> map = this.cache.get(); - if (map == null) { - map = new HashMap<>(); - this.cache.set(map); - } - GroovyType object = map.get(clazz); - if (object == null) { - GroovyTypeParser parser = new GroovyTypeParser(clazz); - object = parser.parser(); - map.put(clazz, object); - } - return object.cacheFieldsAndMethods(); - } - - -} diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java index 29a66859..2550faf7 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java @@ -1,6 +1,6 @@ package com.codingapi.springboot.framework.entity; -import com.codingapi.springboot.framework.script.annotation.ScriptType; +import com.codingapi.springboot.framework.script.annotation.ScriptField; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -9,14 +9,13 @@ @Data @Entity -@ScriptType public class MyTest { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @ScriptType(name = "id", description = "id") + @ScriptField(name = "id", description = "id") private Long id; - @ScriptType(name = "name", description = "name") + @ScriptField(name = "name", description = "name") private String name; } diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java index 6f8832ed..2cc80590 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java @@ -1,8 +1,8 @@ package com.codingapi.springboot.framework.script; +import com.codingapi.springboot.framework.script.request.GroovyBindObjectBuilder; import com.codingapi.springboot.framework.script.request.GroovyRunningScript; import com.codingapi.springboot.framework.script.request.MyScriptRequest; -import com.codingapi.springboot.framework.script.schema.GroovySchema; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -14,13 +14,16 @@ void voidRun() { String script = """ def run(request){ - println($request.getRequests()) + println(request) } """; GroovyScriptRunningContext.getInstance().compile(script); - GroovyRunningScript request = new GroovyRunningScript<>(script, Void.class, 100); - request.addBindObject("$request", request); + GroovyRunningScript request = new GroovyRunningScript<>( + script, + Void.class, + GroovyBindObjectBuilder.builder().add("request", 100) + ); long t1 = System.currentTimeMillis(); GroovyScriptRunningContext.getInstance().run(request); @@ -31,7 +34,7 @@ def run(request){ @Test - void metaTest(){ + void metaTest() { String script = """ def run(request){ return request.count; @@ -39,11 +42,15 @@ def run(request){ """; MyScriptRequest request = new MyScriptRequest(100); - GroovyRunningScript runningScript = new GroovyRunningScript<>(script, Integer.class, request); + GroovyRunningScript runningScript = new GroovyRunningScript<>( + script, + Integer.class, + GroovyBindObjectBuilder.builder().add("request", request) + ); - GroovySchema schema = runningScript.getGroovyMetadata().toSchema(); - System.out.println(schema.toJson()); - assertEquals(1,runningScript.getGroovyMetadata().getRequests().size()); + runningScript.buildMetadata(); + System.out.println(runningScript.getMetadata()); + assertEquals(1, runningScript.getMetadata().getRequests().size()); long t1 = System.currentTimeMillis(); int result = GroovyScriptRunningContext.getInstance().run(runningScript); @@ -57,13 +64,14 @@ void run() { String script = """ def run(request){ - println($request.getRequests()) return request; } """; - GroovyRunningScript request = new GroovyRunningScript<>(script, Integer.class, 100); - request.addBindObject("$request", request); + GroovyRunningScript request = new GroovyRunningScript<>(script, + Integer.class, + GroovyBindObjectBuilder.builder().add("request", 100) + ); long t1 = System.currentTimeMillis(); int result = GroovyScriptRunningContext.getInstance().run(request); diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java index 08cb0f6b..6fe04baa 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java @@ -2,6 +2,7 @@ import com.codingapi.springboot.framework.entity.MyTest; import com.codingapi.springboot.framework.repository.MyTestRepository; +import com.codingapi.springboot.framework.script.request.GroovyBindObjectBuilder; import com.codingapi.springboot.framework.script.request.GroovyRunningScript; import com.codingapi.springboot.framework.script.request.MyScriptRequest; import org.junit.jupiter.api.Test; @@ -38,9 +39,11 @@ def run(request){ assertTrue(list.isEmpty()); GroovyScriptRunner scriptRunner = new GroovyScriptRunner(100); scriptRunner.compile(script); - GroovyRunningScript runningScript = new GroovyRunningScript<>(script, Void.class, request); - runningScript.addBindObject("$request", request); - runningScript.addBindObject("$repository", myTestRepository); + GroovyRunningScript runningScript = new GroovyRunningScript<>(script, + Void.class, + GroovyBindObjectBuilder.builder().add("request",request), + GroovyBindObjectBuilder.builder().add("$repository",myTestRepository) + ); long t1 = System.currentTimeMillis(); scriptRunner.run(runningScript); @@ -72,9 +75,11 @@ def run(request){ GroovyScriptRunner scriptRunner = new GroovyScriptRunner(100, true); scriptRunner.compile(script); - GroovyRunningScript runningScript = new GroovyRunningScript<>(script, Void.class, request); - runningScript.addBindObject("$request", request); - runningScript.addBindObject("$repository", myTestRepository); + GroovyRunningScript runningScript = new GroovyRunningScript<>(script, + Void.class, + GroovyBindObjectBuilder.builder().add("request",request), + GroovyBindObjectBuilder.builder().add("$repository",myTestRepository) + ); long t1 = System.currentTimeMillis(); scriptRunner.run(runningScript); diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java index 3668a3e3..8faaa01a 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java @@ -2,23 +2,19 @@ import com.codingapi.springboot.framework.entity.MyTest; import com.codingapi.springboot.framework.repository.MyTestRepository; +import com.codingapi.springboot.framework.script.annotation.ScriptField; import com.codingapi.springboot.framework.script.annotation.ScriptFunction; -import com.codingapi.springboot.framework.script.annotation.ScriptType; import lombok.AllArgsConstructor; import lombok.Getter; @Getter -@ScriptType( - name = "request", - description = "请求参数" -) @AllArgsConstructor public class MyScriptRequest { - @ScriptType(name = "count", description = "总数量") + @ScriptField(name = "count", description = "总数量") private final int count; - @ScriptType(name = "test", description = "test") + @ScriptField(name = "test", description = "test") private MyTest test; @@ -26,7 +22,7 @@ public class MyScriptRequest { name = "isSupport", description = "是否匹配", parameters = { - @ScriptType(name = "count", description = "描述信息") + @ScriptField(name = "count", description = "描述信息") } ) public boolean isSupport(int count) { From 34b7b072dd3bcb1bafee10e6d47d54c4e04a5cf6 Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Tue, 26 May 2026 10:53:38 +0800 Subject: [PATCH 07/10] fix GroovyScriptRunningContext --- .../flow/script/GroovyShellContext.java | 18 ++--- .../framework/script/GroovyScriptRunner.java | 68 +++++++++++++------ .../script/GroovyScriptRunningContext.java | 2 +- 3 files changed, 58 insertions(+), 30 deletions(-) diff --git a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/script/GroovyShellContext.java b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/script/GroovyShellContext.java index 8381bddc..8d7a9386 100644 --- a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/script/GroovyShellContext.java +++ b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/script/GroovyShellContext.java @@ -1,6 +1,6 @@ package com.codingapi.springboot.flow.script; -import com.codingapi.springboot.framework.script.GroovyScriptRunningContext; +import com.codingapi.springboot.framework.script.GroovyScriptRunner; import lombok.Getter; public class GroovyShellContext { @@ -11,25 +11,25 @@ public class GroovyShellContext { @Getter private final static GroovyShellContext instance = new GroovyShellContext(); + private final GroovyScriptRunner scriptRunner; + private GroovyShellContext() { - if(GroovyScriptRunningContext.getInstance().getMaxCacheSize()!=MAX_CACHE_SIZE) { - GroovyScriptRunningContext.getInstance().setMaxCacheSize(MAX_CACHE_SIZE); - } + scriptRunner = new GroovyScriptRunner(MAX_CACHE_SIZE); } public T run(String script, Class returnType, Object... args) { - return GroovyScriptRunningContext.getInstance().run("run", script, returnType, args); + return scriptRunner.run("run", script, returnType, args); } - public int size(){ - return GroovyScriptRunningContext.getInstance().cacheSize(); + public int size() { + return scriptRunner.cacheSize(); } - public void clear(){ - GroovyScriptRunningContext.getInstance().clearCache(); + public void clear() { + scriptRunner.clearCache(); } } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java index 7f5cd576..75ece0f3 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java @@ -30,15 +30,26 @@ public class GroovyScriptRunner { private final int maxCacheSize; @Getter - private final boolean readOnly; + private final TransactionMode transactionMode; - public GroovyScriptRunner(int maxCacheSize){ - this(maxCacheSize,false); + + public static enum TransactionMode { + // 默认不处理 + DEFAULT, + // 事务提交模式 + COMMIT, + // 事务只读模式 + READONLY + } + + + public GroovyScriptRunner(int maxCacheSize) { + this(maxCacheSize, TransactionMode.DEFAULT); } - public GroovyScriptRunner(int maxCacheSize, boolean readOnly) { + public GroovyScriptRunner(int maxCacheSize, TransactionMode transactionMode) { this.maxCacheSize = maxCacheSize; - this.readOnly = readOnly; + this.transactionMode = transactionMode; GroovyClassLoader groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader()); this.shell = new GroovyShell(groovyClassLoader); this.cache = new LinkedHashMap<>(16, 0.75F, true) { @@ -117,27 +128,44 @@ public T run(String method, String script, Class returnType, List Date: Tue, 26 May 2026 11:02:03 +0800 Subject: [PATCH 08/10] fix GroovyScriptRunningContext --- .../framework/script/GroovyScriptRunner.java | 41 ++++------------ .../TransactionManagerContext.java | 48 ++++++++++++++++++- ...sactionGroovyScriptRunningContextTest.java | 16 +++---- 3 files changed, 62 insertions(+), 43 deletions(-) diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java index 75ece0f3..922af6b6 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java @@ -8,10 +8,6 @@ import groovy.lang.GroovyShell; import groovy.lang.Script; import lombok.Getter; -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.TransactionDefinition; -import org.springframework.transaction.TransactionStatus; -import org.springframework.transaction.support.DefaultTransactionDefinition; import java.util.LinkedHashMap; import java.util.List; @@ -130,39 +126,18 @@ public T run(String method, String script, Class returnType, List{ + return (T) finalRuntime.invokeMethod(method, args); + }); } // 事务制提交模式 if (transactionMode == TransactionMode.COMMIT) { - PlatformTransactionManager transactionManager = TransactionManagerContext.getInstance().getPlatformTransactionManager(); - if (transactionManager != null) { - DefaultTransactionDefinition def = new DefaultTransactionDefinition(); - def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); - TransactionStatus transactionStatus = transactionManager.getTransaction(def); - try { - T result = (T) runtime.invokeMethod(method, args); - transactionManager.commit(transactionStatus); - return result; - } catch (Exception e) { - transactionManager.rollback(transactionStatus); - throw e; - } - } + Script finalRuntime = runtime; + return TransactionManagerContext.getInstance().commit(()->{ + return (T) finalRuntime.invokeMethod(method, args); + }); } // 默认处理模式 diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java index ed9a6d1f..c99e0678 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java @@ -3,6 +3,11 @@ import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.DefaultTransactionDefinition; + +import java.util.function.Supplier; @Slf4j public class TransactionManagerContext { @@ -13,10 +18,49 @@ public class TransactionManagerContext { @Getter private final static TransactionManagerContext instance = new TransactionManagerContext(); - private TransactionManagerContext() {} + private TransactionManagerContext() { + } public void setPlatformTransactionManager(PlatformTransactionManager platformTransactionManager) { this.platformTransactionManager = platformTransactionManager; - log.info("platformTransactionManager:{} load success",platformTransactionManager); + log.info("platformTransactionManager:{} load success", platformTransactionManager); + } + + + public T commit(Supplier supplier) { + PlatformTransactionManager transactionManager = TransactionManagerContext.getInstance().getPlatformTransactionManager(); + if (transactionManager != null) { + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); + TransactionStatus transactionStatus = transactionManager.getTransaction(def); + try { + T result = supplier.get(); + transactionManager.commit(transactionStatus); + return result; + } catch (Exception e) { + transactionManager.rollback(transactionStatus); + throw e; + } + } + return supplier.get(); + } + + public T readOnly(Supplier supplier){ + PlatformTransactionManager transactionManager = TransactionManagerContext.getInstance().getPlatformTransactionManager(); + if (transactionManager != null) { + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); + def.setReadOnly(true); + TransactionStatus transactionStatus = transactionManager.getTransaction(def); + try { + T result = supplier.get(); + transactionManager.rollback(transactionStatus); + return result; + } catch (Exception e) { + transactionManager.rollback(transactionStatus); + throw e; + } + } + return supplier.get(); } } diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java index 6fe04baa..54b774d0 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java @@ -28,7 +28,7 @@ void transactionCommitRun() { String script = """ def run(request){ request.addData($repository); - println($request.count); + println(request.count); } """; @@ -37,12 +37,12 @@ def run(request){ myTestRepository.deleteAll(); List list = myTestRepository.findAll(); assertTrue(list.isEmpty()); - GroovyScriptRunner scriptRunner = new GroovyScriptRunner(100); + GroovyScriptRunner scriptRunner = new GroovyScriptRunner(100,GroovyScriptRunner.TransactionMode.COMMIT); scriptRunner.compile(script); GroovyRunningScript runningScript = new GroovyRunningScript<>(script, Void.class, - GroovyBindObjectBuilder.builder().add("request",request), - GroovyBindObjectBuilder.builder().add("$repository",myTestRepository) + GroovyBindObjectBuilder.builder().add("$repository",myTestRepository), + GroovyBindObjectBuilder.builder().add("request",request) ); long t1 = System.currentTimeMillis(); @@ -61,7 +61,7 @@ void transactionOnlyReadRun() { String script = """ def run(request){ request.addData($repository); - println($request.count); + println(request.count); } """; @@ -72,13 +72,13 @@ def run(request){ List list = myTestRepository.findAll(); assertTrue(list.isEmpty()); - GroovyScriptRunner scriptRunner = new GroovyScriptRunner(100, true); + GroovyScriptRunner scriptRunner = new GroovyScriptRunner(100, GroovyScriptRunner.TransactionMode.READONLY); scriptRunner.compile(script); GroovyRunningScript runningScript = new GroovyRunningScript<>(script, Void.class, - GroovyBindObjectBuilder.builder().add("request",request), - GroovyBindObjectBuilder.builder().add("$repository",myTestRepository) + GroovyBindObjectBuilder.builder().add("$repository",myTestRepository), + GroovyBindObjectBuilder.builder().add("request",request) ); long t1 = System.currentTimeMillis(); From 161143f9907b6c669dac76ef7e4ac0ba253e2c39 Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Tue, 26 May 2026 11:31:20 +0800 Subject: [PATCH 09/10] fix GroovyScriptRunningContext --- .../springboot/fast/script/ScriptMapping.java | 2 +- .../springboot/fast/script/ScriptRuntime.java | 30 +++++--- .../fast/script/ScriptRuntimeTest.java | 2 +- .../flow/script/GroovyShellContext.java | 2 +- .../framework/script/GroovyScriptRunner.java | 76 ++++++++++++++++--- .../script/GroovyScriptRunningContext.java | 37 +++++++-- .../script/request/GroovyRunningScript.java | 6 +- .../springboot/framework/entity/MyTest.java | 2 + .../GroovyScriptRunningContextTest.java | 34 +++++++-- ...sactionGroovyScriptRunningContextTest.java | 4 +- 10 files changed, 152 insertions(+), 43 deletions(-) diff --git a/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/script/ScriptMapping.java b/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/script/ScriptMapping.java index 3fc32f3b..c59640b2 100644 --- a/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/script/ScriptMapping.java +++ b/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/script/ScriptMapping.java @@ -29,7 +29,7 @@ public ScriptMapping(String mapping, ScriptMethod scriptMethod, String script) { @ResponseBody Response execute() { - Object result = ScriptRuntime.running(script); + Object result = ScriptRuntime.getInstance().running(script); if (result instanceof List || result.getClass().isArray()) { return SingleResponse.of(result); } else { diff --git a/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/script/ScriptRuntime.java b/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/script/ScriptRuntime.java index ef33effc..609a61a1 100644 --- a/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/script/ScriptRuntime.java +++ b/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/script/ScriptRuntime.java @@ -4,28 +4,36 @@ import com.codingapi.springboot.fast.jdbc.JdbcQueryContext; import com.codingapi.springboot.fast.jpa.JpaQuery; import com.codingapi.springboot.fast.jpa.JpaQueryContext; -import groovy.lang.Binding; -import groovy.lang.GroovyShell; -import groovy.lang.Script; +import com.codingapi.springboot.framework.script.GroovyScriptRunner; +import com.codingapi.springboot.framework.script.request.GroovyBindObjectBuilder; +import lombok.Getter; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; public class ScriptRuntime { - static Object running(String script) { - Binding binding = new Binding(); + + @Getter + private final static ScriptRuntime instance = new ScriptRuntime(); + + private final GroovyScriptRunner scriptRunner; + + private ScriptRuntime(){ + this.scriptRunner = new GroovyScriptRunner(1000); + } + + Object running(String script) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); ScriptRequest request = new ScriptRequest(attributes.getRequest()); JdbcQuery jdbcQuery = JdbcQueryContext.getInstance().getJdbcQuery(); JpaQuery jpaQuery = JpaQueryContext.getInstance().getJpaQuery(); - binding.setVariable("$request", request); - binding.setVariable("$jpa", jpaQuery); - binding.setVariable("$jdbc", jdbcQuery); + GroovyBindObjectBuilder bindBuilder = GroovyBindObjectBuilder.builder(); + bindBuilder.add("$request", request); + bindBuilder.add("$jpa", jpaQuery); + bindBuilder.add("$jdbc", jdbcQuery); - GroovyShell groovyShell = new GroovyShell(binding); - Script userScript = groovyShell.parse(script); - return userScript.run(); + return this.scriptRunner.run(script,Object.class,bindBuilder.build()); } } diff --git a/springboot-starter-data-fast/src/test/java/com/codingapi/springboot/fast/script/ScriptRuntimeTest.java b/springboot-starter-data-fast/src/test/java/com/codingapi/springboot/fast/script/ScriptRuntimeTest.java index 19e758db..4f5892b3 100644 --- a/springboot-starter-data-fast/src/test/java/com/codingapi/springboot/fast/script/ScriptRuntimeTest.java +++ b/springboot-starter-data-fast/src/test/java/com/codingapi/springboot/fast/script/ScriptRuntimeTest.java @@ -10,7 +10,7 @@ class ScriptRuntimeTest { @Test void running() { - Object res = ScriptRuntime.running("return 1"); + Object res = ScriptRuntime.getInstance().running("return 1"); assertEquals(1, res); } } \ No newline at end of file diff --git a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/script/GroovyShellContext.java b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/script/GroovyShellContext.java index 8d7a9386..28c076c0 100644 --- a/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/script/GroovyShellContext.java +++ b/springboot-starter-flow/src/main/java/com/codingapi/springboot/flow/script/GroovyShellContext.java @@ -20,7 +20,7 @@ private GroovyShellContext() { public T run(String script, Class returnType, Object... args) { - return scriptRunner.run("run", script, returnType, args); + return scriptRunner.invoke("run", script, returnType, args); } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java index 922af6b6..110f9538 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java @@ -29,7 +29,7 @@ public class GroovyScriptRunner { private final TransactionMode transactionMode; - public static enum TransactionMode { + public enum TransactionMode { // 默认不处理 DEFAULT, // 事务提交模式 @@ -101,7 +101,7 @@ public void compile(String script) { } /** - * 执行脚本 + * 执行函数脚本 * * @param method 函数名称 * @param script 脚本内容 @@ -110,7 +110,7 @@ public void compile(String script) { * @param args 函数参数 * @return 返回数据 */ - public T run(String method, String script, Class returnType, List binds, Object... args) { + public T invoke(String method, String script, Class returnType, List binds, Object... args) { String key = SHA256.sha256(script); Script runtime = this.cache.get(key); if (runtime == null) { @@ -127,7 +127,7 @@ public T run(String method, String script, Class returnType, List{ + return TransactionManagerContext.getInstance().readOnly(() -> { return (T) finalRuntime.invokeMethod(method, args); }); } @@ -135,7 +135,7 @@ public T run(String method, String script, Class returnType, List{ + return TransactionManagerContext.getInstance().commit(() -> { return (T) finalRuntime.invokeMethod(method, args); }); } @@ -146,7 +146,61 @@ public T run(String method, String script, Class returnType, List T run(String script, Class returnType, List binds) { + String key = SHA256.sha256(script); + Script runtime = this.cache.get(key); + if (runtime == null) { + runtime = this.shell.parse(script); + this.cache.put(key, runtime); + } + + if (binds != null && !binds.isEmpty()) { + for (GroovyBindObject groovyBindObject : binds) { + runtime.setProperty(groovyBindObject.getName(), groovyBindObject.getObject()); + } + } + + // 事务制只读模式 + if (transactionMode == TransactionMode.READONLY) { + Script finalRuntime = runtime; + return TransactionManagerContext.getInstance().readOnly(() -> { + return (T) finalRuntime.run(); + }); + } + + // 事务制提交模式 + if (transactionMode == TransactionMode.COMMIT) { + Script finalRuntime = runtime; + return TransactionManagerContext.getInstance().commit(() -> { + return (T) finalRuntime.run(); + }); + } + + // 默认处理模式 + return (T) runtime.run(); + } + + + /** + * 执行脚本 + * + * @param script 脚本 + * @param returnType 返回类型 + * @return 返回数据 + */ + public T run(String script, Class returnType) { + return this.run(script, returnType, null); + } + + /** + * 执行函数脚本 * * @param method 函数名称 * @param script 脚本内容 @@ -154,19 +208,19 @@ public T run(String method, String script, Class returnType, List T run(String method, String script, Class returnType, Object... args) { - return this.run(method, script, returnType, null, args); + public T invoke(String method, String script, Class returnType, Object... args) { + return this.invoke(method, script, returnType, null, args); } /** - * 执行脚本 + * 执行函数脚本 * * @param request 脚本参数 * @return 返回数据 */ - public T run(GroovyRunningScript request) { - return this.run(request.getMethod(), request.getScript(), request.getReturnType(), request.getBinds(), request.getParams()); + public T invoke(GroovyRunningScript request) { + return this.invoke(request.getMethod(), request.getScript(), request.getReturnType(), request.getBinds(), request.getParams()); } } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java index 285265ff..aefdf8c2 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java @@ -64,7 +64,30 @@ public void compile(String script) { } /** - * 执行脚本 + * 执行函数脚本 + * + * @param script 脚本内容 + * @param returnType 返回类型 + * @return 返回数据 + */ + public T run(String script, Class returnType) { + return this.scriptRunner.run(script, returnType, null); + } + + /** + * 执行函数脚本 + * + * @param script 脚本内容 + * @param returnType 返回类型 + * @param binds 绑定数据对象 + * @return 返回数据 + */ + public T run(String script, Class returnType, List binds) { + return this.scriptRunner.run(script, returnType, binds); + } + + /** + * 执行函数脚本 * * @param method 函数名称 * @param script 脚本内容 @@ -73,8 +96,8 @@ public void compile(String script) { * @param args 函数参数 * @return 返回数据 */ - public T run(String method, String script, Class returnType, List binds, Object... args) { - return this.scriptRunner.run(method,script,returnType,binds,args); + public T invoke(String method, String script, Class returnType, List binds, Object... args) { + return this.scriptRunner.invoke(method, script, returnType, binds, args); } /** @@ -86,8 +109,8 @@ public T run(String method, String script, Class returnType, List T run(String method, String script, Class returnType, Object... args) { - return this.run(method, script, returnType, null, args); + public T invoke(String method, String script, Class returnType, Object... args) { + return this.invoke(method, script, returnType, null, args); } @@ -97,8 +120,8 @@ public T run(String method, String script, Class returnType, Object... ar * @param request 脚本参数 * @return 返回数据 */ - public T run(GroovyRunningScript request) { - return this.run(request.getMethod(), request.getScript(), request.getReturnType(), request.getBinds(), request.getParams()); + public T invoke(GroovyRunningScript request) { + return this.invoke(request.getMethod(), request.getScript(), request.getReturnType(), request.getBinds(), request.getParams()); } } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java index 2fc83458..633f7d2d 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java @@ -76,8 +76,10 @@ public GroovyRunningScript(String script, Class returnType, GroovyBindObjectB */ public Object[] getParams() { List objects = new ArrayList<>(); - for(GroovyBindObject bindObject:this.requests){ - objects.add(bindObject.getObject()); + if(this.requests!=null) { + for (GroovyBindObject bindObject : this.requests) { + objects.add(bindObject.getObject()); + } } return objects.toArray(); } diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java index 2550faf7..7f4dceed 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java @@ -1,6 +1,7 @@ package com.codingapi.springboot.framework.entity; import com.codingapi.springboot.framework.script.annotation.ScriptField; +import com.codingapi.springboot.framework.script.annotation.ScriptType; import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -9,6 +10,7 @@ @Data @Entity +@ScriptType(description = "test") public class MyTest { @Id diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java index 2cc80590..51b4bdbd 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContextTest.java @@ -10,7 +10,7 @@ class GroovyScriptRunningContextTest { @Test - void voidRun() { + void voidInvoke() { String script = """ def run(request){ @@ -26,7 +26,7 @@ def run(request){ ); long t1 = System.currentTimeMillis(); - GroovyScriptRunningContext.getInstance().run(request); + GroovyScriptRunningContext.getInstance().invoke(request); long t2 = System.currentTimeMillis(); System.out.println("groovy time:" + (t2 - t1)); @@ -53,7 +53,7 @@ def run(request){ assertEquals(1, runningScript.getMetadata().getRequests().size()); long t1 = System.currentTimeMillis(); - int result = GroovyScriptRunningContext.getInstance().run(runningScript); + int result = GroovyScriptRunningContext.getInstance().invoke(runningScript); long t2 = System.currentTimeMillis(); System.out.println("groovy time:" + (t2 - t1)); assertEquals(100, result); @@ -62,6 +62,26 @@ def run(request){ @Test void run() { + String script = " return $request; "; + + GroovyRunningScript request = new GroovyRunningScript<>( + script, + Integer.class, + GroovyBindObjectBuilder.builder().add("$request", 100), + null + ); + + long t1 = System.currentTimeMillis(); + int result = GroovyScriptRunningContext.getInstance().invoke(request); + long t2 = System.currentTimeMillis(); + System.out.println("groovy time:" + (t2 - t1)); + + assertEquals(100, result); + } + + @Test + void invoke() { + String script = """ def run(request){ return request; @@ -74,7 +94,7 @@ def run(request){ ); long t1 = System.currentTimeMillis(); - int result = GroovyScriptRunningContext.getInstance().run(request); + int result = GroovyScriptRunningContext.getInstance().invoke(request); long t2 = System.currentTimeMillis(); System.out.println("groovy time:" + (t2 - t1)); @@ -82,12 +102,12 @@ def run(request){ } @Test - void batchTest() { + void batchInvoke() { int count = 100; - run(); + invoke(); long t1 = System.currentTimeMillis(); for (int i = 0; i < count; i++) { - run(); + invoke(); } long t2 = System.currentTimeMillis(); System.out.println("total time:" + (t2 - t1)); diff --git a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java index 54b774d0..43564fd5 100644 --- a/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java @@ -46,7 +46,7 @@ def run(request){ ); long t1 = System.currentTimeMillis(); - scriptRunner.run(runningScript); + scriptRunner.invoke(runningScript); long t2 = System.currentTimeMillis(); System.out.println("groovy time:" + (t2 - t1)); @@ -82,7 +82,7 @@ def run(request){ ); long t1 = System.currentTimeMillis(); - scriptRunner.run(runningScript); + scriptRunner.invoke(runningScript); long t2 = System.currentTimeMillis(); System.out.println("groovy time:" + (t2 - t1)); From 398bf849fec4bc5a7348d70a3068e1e2d625f46e Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Tue, 26 May 2026 11:38:21 +0800 Subject: [PATCH 10/10] fix GroovyScriptRunningContext --- .../TransactionManagerContext.java | 24 +++++++++---------- ...ransactionManagerContextConfiguration.java | 3 ++- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java index c99e0678..7cdd1826 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java @@ -23,41 +23,41 @@ private TransactionManagerContext() { public void setPlatformTransactionManager(PlatformTransactionManager platformTransactionManager) { this.platformTransactionManager = platformTransactionManager; - log.info("platformTransactionManager:{} load success", platformTransactionManager); + if (platformTransactionManager != null) { + log.info("platformTransactionManager:{} load success", platformTransactionManager); + } } public T commit(Supplier supplier) { - PlatformTransactionManager transactionManager = TransactionManagerContext.getInstance().getPlatformTransactionManager(); - if (transactionManager != null) { + if (platformTransactionManager != null) { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); - TransactionStatus transactionStatus = transactionManager.getTransaction(def); + TransactionStatus transactionStatus = platformTransactionManager.getTransaction(def); try { T result = supplier.get(); - transactionManager.commit(transactionStatus); + platformTransactionManager.commit(transactionStatus); return result; } catch (Exception e) { - transactionManager.rollback(transactionStatus); + platformTransactionManager.rollback(transactionStatus); throw e; } } return supplier.get(); } - public T readOnly(Supplier supplier){ - PlatformTransactionManager transactionManager = TransactionManagerContext.getInstance().getPlatformTransactionManager(); - if (transactionManager != null) { + public T readOnly(Supplier supplier) { + if (platformTransactionManager != null) { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); def.setReadOnly(true); - TransactionStatus transactionStatus = transactionManager.getTransaction(def); + TransactionStatus transactionStatus = platformTransactionManager.getTransaction(def); try { T result = supplier.get(); - transactionManager.rollback(transactionStatus); + platformTransactionManager.rollback(transactionStatus); return result; } catch (Exception e) { - transactionManager.rollback(transactionStatus); + platformTransactionManager.rollback(transactionStatus); throw e; } } diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextConfiguration.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextConfiguration.java index 29e42ed1..4e019944 100644 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextConfiguration.java +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextConfiguration.java @@ -1,5 +1,6 @@ package com.codingapi.springboot.framework.transaction; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.PlatformTransactionManager; @@ -8,7 +9,7 @@ public class TransactionManagerContextConfiguration { @Bean - public TransactionManagerContextRegister transactionManagerContextRegister(PlatformTransactionManager platformTransactionManager){ + public TransactionManagerContextRegister transactionManagerContextRegister(@Autowired(required = false) PlatformTransactionManager platformTransactionManager){ return new TransactionManagerContextRegister(platformTransactionManager); }