diff --git a/example/example-app/example-app-cmd-domain/pom.xml b/example/example-app/example-app-cmd-domain/pom.xml index c0698a4ce..cdbea2e34 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 66f5138c0..6c9b04b39 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 ac105c9b4..0645e7eec 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 f0841b3ec..9f076296c 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 feca9e30d..d7c2ada99 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 c03892c61..2f188b317 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 64b78ab42..98a5ca278 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 7c6f1875d..ef2c201d8 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 2d561e570..c6e9ba9db 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 f1a1c3c20..33fa1f61c 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 366055914..ce319bb6d 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 5f23d1e05..270dfbed4 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 e6a43cd73..a62efec32 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 f73e7ee62..cc114850c 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 2fc26ce5f..ecbbd6001 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 d89f6d064..fdcccbc77 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 9f44ba99e..6fe6420c0 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-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 3fc32f3b0..c59640b2b 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 ef33effce..609a61a17 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 19e758dbb..4f5892b34 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/pom.xml b/springboot-starter-flow/pom.xml index ef49d4531..6e6a60a93 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-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 8381bddcc..28c076c01 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.invoke("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-security/pom.xml b/springboot-starter-security/pom.xml index 761cdbacb..9b411bd81 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 8181bd0dc..eeae0aaa5 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/GroovyScriptRunner.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java new file mode 100644 index 000000000..110f95385 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java @@ -0,0 +1,226 @@ +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 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 TransactionMode transactionMode; + + + public enum TransactionMode { + // 默认不处理 + DEFAULT, + // 事务提交模式 + COMMIT, + // 事务只读模式 + READONLY + } + + + public GroovyScriptRunner(int maxCacheSize) { + this(maxCacheSize, TransactionMode.DEFAULT); + } + + public GroovyScriptRunner(int maxCacheSize, TransactionMode transactionMode) { + this.maxCacheSize = maxCacheSize; + this.transactionMode = transactionMode; + 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 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) { + 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.invokeMethod(method, args); + }); + } + + // 事务制提交模式 + if (transactionMode == TransactionMode.COMMIT) { + Script finalRuntime = runtime; + return TransactionManagerContext.getInstance().commit(() -> { + return (T) finalRuntime.invokeMethod(method, args); + }); + } + + // 默认处理模式 + return (T) runtime.invokeMethod(method, args); + } + + + /** + * 执行脚本 + * + * @param script 脚本 + * @param returnType 返回数据类型 + * @param binds 绑定对象 + * @return 返回数据 + */ + public 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 脚本内容 + * @param returnType 返回类型 + * @param args 函数参数 + * @return 返回数据 + */ + public T invoke(String method, String script, Class returnType, Object... args) { + return this.invoke(method, script, returnType, null, args); + } + + + /** + * 执行函数脚本 + * + * @param request 脚本参数 + * @return 返回数据 + */ + 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 f430dcb18..aefdf8c2e 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.RuntimeBindObject; -import com.codingapi.springboot.framework.script.request.GroovyMethodScript; -import groovy.lang.GroovyClassLoader; -import groovy.lang.GroovyShell; -import groovy.lang.Script; +import com.codingapi.springboot.framework.script.request.GroovyBindObject; +import com.codingapi.springboot.framework.script.request.GroovyRunningScript; 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); } /** @@ -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) { @@ -91,50 +64,64 @@ public void compile(String script) { } /** - * 执行脚本 - * @param method 函数名称 - * @param 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 脚本内容 * @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 (RuntimeBindObject runtimeBindObject : binds) { - runtime.setProperty(runtimeBindObject.getKey(), runtimeBindObject.getObject()); - } - } - return (T) runtime.invokeMethod(method, args); + public T invoke(String method, String script, Class returnType, List binds, Object... args) { + return this.scriptRunner.invoke(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); + 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(GroovyMethodScript 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/annotation/ScriptField.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptField.java new file mode 100644 index 000000000..fc3b14557 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptField.java @@ -0,0 +1,23 @@ +package com.codingapi.springboot.framework.script.annotation; + +import java.lang.annotation.*; + +/** + * 脚本字段 + */ +@Target({ElementType.METHOD, ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface ScriptField { + + /** + * 名称 + */ + String name() default ""; + + /** + * 描述 + */ + String description() default ""; + +} 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 000000000..f027bc36c --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptFunction.java @@ -0,0 +1,28 @@ +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 ""; + + /** + * 参数 + */ + ScriptField[] 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 new file mode 100644 index 000000000..bb2fb52b8 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/annotation/ScriptType.java @@ -0,0 +1,18 @@ +package com.codingapi.springboot.framework.script.annotation; + +import java.lang.annotation.*; + +/** + * 脚本对象 + */ +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface ScriptType { + + /** + * 描述 + */ + String description() default ""; + +} diff --git a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyField.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyField.java new file mode 100644 index 000000000..23de1518e --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyField.java @@ -0,0 +1,30 @@ +package com.codingapi.springboot.framework.script.meta; + +import lombok.Getter; +import lombok.Setter; + +/** + * 脚本对象类型 + */ +@Getter +@Setter +public class GroovyField { + + /** + * 访问名称 + */ + private String name; + + /** + * 描述信息 + */ + private String description; + + /** + * 数据类型 + */ + private String 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 000000000..d872331f8 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyFunction.java @@ -0,0 +1,43 @@ +package com.codingapi.springboot.framework.script.meta; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +/** + * 脚本函数对象 + */ +@Getter +public class GroovyFunction { + + /** + * 对象访问名称 + */ + @Setter + private String name; + /** + * 对象描述信息 + */ + @Setter + private String description; + /** + * 返回类型 + */ + @Setter + private GroovyType returnType; + /** + * 参数类型 + */ + private final List parameters; + + public GroovyFunction() { + 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 new file mode 100644 index 000000000..1e2899228 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyMetadata.java @@ -0,0 +1,77 @@ +package com.codingapi.springboot.framework.script.meta; + +import com.codingapi.springboot.framework.script.annotation.ScriptType; +import com.codingapi.springboot.framework.script.service.GroovyTypeParser; +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +/** + * 脚本对象元数据结构 + */ +@Getter +public class GroovyMetadata { + + /** + * 请求参数 + */ + private final List requests; + + /** + * 绑定参数 + */ + private final List binds; + + /** + * 返回类型 + */ + @Setter + private String returnType; + + /** + * 字段类型 + */ + private final Map types; + + + public GroovyMetadata() { + this.types = new HashMap<>(); + this.requests = new ArrayList<>(); + this.binds = 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 GroovyType getType(String dataType){ + return this.types.get(dataType); + } + + + 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 new file mode 100644 index 000000000..0ad3f73e2 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/meta/GroovyType.java @@ -0,0 +1,52 @@ +package com.codingapi.springboot.framework.script.meta; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +/** + * 脚本对象类型 + */ +@Getter +public class GroovyType { + + /** + * 对象描述 + */ + @Setter + private String description; + + /** + * 数据类型 + */ + @Setter + private String dataType; + + /** + * 属性列表 + */ + private final List fields; + + /** + * 函数列表 + */ + private final List functions; + + public GroovyType() { + this.fields = new ArrayList<>(); + this.functions = new ArrayList<>(); + } + + + public void addFunction(GroovyFunction function) { + this.functions.add(function); + } + + + public void addField(GroovyField field) { + this.fields.add(field); + } + +} 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 new file mode 100644 index 000000000..4acf4f321 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyBindObject.java @@ -0,0 +1,132 @@ +package com.codingapi.springboot.framework.script.request; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.List; + +/** + * 运行时绑定对象 + */ +@Getter +@AllArgsConstructor +public class GroovyBindObject { + + /** + * 绑定的访问对象 + */ + 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 000000000..07b69ce5a --- /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/GroovyMethodScript.java b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyMethodScript.java deleted file mode 100644 index 3c92807a5..000000000 --- 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 000000000..633f7d2d5 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/GroovyRunningScript.java @@ -0,0 +1,99 @@ +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 java.util.ArrayList; +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; + + /** + * 脚本元数据信息 + */ + private GroovyMetadata metadata; + + + public GroovyRunningScript(String method, String script, Class returnType, List binds, List requests) { + this.method = method; + this.script = script; + this.returnType = returnType; + this.requests = requests; + this.binds = binds; + } + + 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, GroovyBindObjectBuilder bindBuilder, GroovyBindObjectBuilder requestBuilder) { + this("run", script, returnType, bindBuilder, requestBuilder); + } + + public GroovyRunningScript(String script, Class returnType, GroovyBindObjectBuilder requestBuilder) { + this("run", script, returnType, null, requestBuilder); + } + + /** + * 获取参数 + */ + public Object[] getParams() { + List objects = new ArrayList<>(); + if(this.requests!=null) { + for (GroovyBindObject bindObject : this.requests) { + objects.add(bindObject.getObject()); + } + } + return objects.toArray(); + } + + public void resetMetadata(GroovyMetadata metadata) { + this.metadata = metadata; + } + + /** + * 构建元数据信息 + */ + public void buildMetadata() { + GroovyMetadataParserService groovyMetaDataParserService = new GroovyMetadataParserService(this); + this.metadata = groovyMetaDataParserService.parser(); + } + +} 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/RuntimeBindObject.java deleted file mode 100644 index 8797b1ba7..000000000 --- a/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/request/RuntimeBindObject.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.codingapi.springboot.framework.script.request; - -import lombok.AllArgsConstructor; -import lombok.Getter; - -/** - * 运行时绑定对象 - */ -@Getter -@AllArgsConstructor -public class RuntimeBindObject { - - private String key; - private Object object; -} 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 000000000..61fa046f0 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyMetadataParserService.java @@ -0,0 +1,107 @@ +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; +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 loadRequestTypes() { + List requests = script.getRequests(); + if (requests != null && !requests.isEmpty()) { + for (GroovyBindObject request : requests) { + if (request != null) { + Class objClass = request.getObject().getClass(); + this.groovyMetadata.buildType(objClass); + } + } + } + } + + private void loadBindTypes() { + List binds = script.getBinds(); + if (binds != null && !binds.isEmpty()) { + for (GroovyBindObject bindObject : binds) { + if (bindObject != null) { + Class objClass = bindObject.getObject().getClass(); + this.groovyMetadata.buildType(objClass); + } + } + } + } + + private void loadReturnType() { + Class returnTypeClass = this.script.getReturnType(); + this.groovyMetadata.buildType(returnTypeClass); + } + + + 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() { + this.loadRequestTypes(); + this.loadBindTypes(); + this.loadReturnType(); + + this.loadRequests(); + this.loadBinds(); + 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 000000000..21dbb3a65 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/script/service/GroovyTypeParser.java @@ -0,0 +1,116 @@ +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.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; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.util.HashMap; +import java.util.Map; + +/** + * 脚本类型解析对象 + */ +public class GroovyTypeParser { + + private final Class clazz; + + private final GroovyType object; + + private final GroovyMetadata metadata; + + public GroovyTypeParser(Class clazz, GroovyMetadata metadata) { + this.clazz = clazz; + this.metadata = metadata; + this.object = new GroovyType(); + } + + + private void loadFields() { + Field[] fields = this.clazz.getDeclaredFields(); + for (Field field : fields) { + 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); + } + } + } + + 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(); + groovyFunction.setName(method.getName()); + + if (StringUtils.hasText(scriptFunction.name())) { + groovyFunction.setName(scriptFunction.name()); + } + + if (StringUtils.hasText(scriptFunction.description())) { + groovyFunction.setDescription(scriptFunction.description()); + } + + Parameter[] methodParameters = method.getParameters(); + Map parameterMap = new HashMap<>(); + for (Parameter parameter : methodParameters) { + parameterMap.put(parameter.getName(), parameter); + } + + ScriptField[] parameters = scriptFunction.parameters(); + if (parameters != null) { + for (ScriptField parameter : parameters) { + Parameter methodParameter = parameterMap.get(parameter.name()); + if (methodParameter != null) { + 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(); + this.metadata.buildType(returnType); + this.object.addFunction(groovyFunction); + } + } + } + + + public GroovyType parser() { + this.loadFields(); + this.loadMethods(); + return this.object; + } +} 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 000000000..7cdd18268 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContext.java @@ -0,0 +1,66 @@ +package com.codingapi.springboot.framework.transaction; + +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 { + + @Getter + private PlatformTransactionManager platformTransactionManager; + + @Getter + private final static TransactionManagerContext instance = new TransactionManagerContext(); + + private TransactionManagerContext() { + } + + public void setPlatformTransactionManager(PlatformTransactionManager platformTransactionManager) { + this.platformTransactionManager = platformTransactionManager; + if (platformTransactionManager != null) { + log.info("platformTransactionManager:{} load success", platformTransactionManager); + } + } + + + public T commit(Supplier supplier) { + if (platformTransactionManager != null) { + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); + TransactionStatus transactionStatus = platformTransactionManager.getTransaction(def); + try { + T result = supplier.get(); + platformTransactionManager.commit(transactionStatus); + return result; + } catch (Exception e) { + platformTransactionManager.rollback(transactionStatus); + throw e; + } + } + return supplier.get(); + } + + public T readOnly(Supplier supplier) { + if (platformTransactionManager != null) { + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); + def.setReadOnly(true); + TransactionStatus transactionStatus = platformTransactionManager.getTransaction(def); + try { + T result = supplier.get(); + platformTransactionManager.rollback(transactionStatus); + return result; + } catch (Exception e) { + platformTransactionManager.rollback(transactionStatus); + throw e; + } + } + return supplier.get(); + } +} 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 000000000..4e0199449 --- /dev/null +++ b/springboot-starter/src/main/java/com/codingapi/springboot/framework/transaction/TransactionManagerContextConfiguration.java @@ -0,0 +1,16 @@ +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; + +@Configuration +public class TransactionManagerContextConfiguration { + + @Bean + public TransactionManagerContextRegister transactionManagerContextRegister(@Autowired(required = false) 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 000000000..5c53e9ebc --- /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 3fd6c5496..046b912ba 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 8aa6f4cd0..0d489bbc3 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/entity/MyTest.java b/springboot-starter/src/test/java/com/codingapi/springboot/framework/entity/MyTest.java index 8061cb32f..7f4dceed9 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,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; @@ -8,10 +10,14 @@ @Data @Entity +@ScriptType(description = "test") public class MyTest { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @ScriptField(name = "id", description = "id") private Long id; + + @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 7745a60e5..51b4bdbd7 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,48 +1,100 @@ package com.codingapi.springboot.framework.script; -import com.codingapi.springboot.framework.script.request.GroovyMethodScript; +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; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; class GroovyScriptRunningContextTest { @Test - void voidRun() { + void voidInvoke() { String script = """ def run(request){ - println($request.getRequest()) + println(request) } """; - GroovyScriptRunningContext.getInstance().compile(script); - GroovyMethodScript request = new GroovyMethodScript<>(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); + GroovyScriptRunningContext.getInstance().invoke(request); long t2 = System.currentTimeMillis(); System.out.println("groovy time:" + (t2 - t1)); } + + @Test + void metaTest() { + String script = """ + def run(request){ + return request.count; + } + """; + + MyScriptRequest request = new MyScriptRequest(100); + GroovyRunningScript runningScript = new GroovyRunningScript<>( + script, + Integer.class, + GroovyBindObjectBuilder.builder().add("request", request) + ); + + runningScript.buildMetadata(); + System.out.println(runningScript.getMetadata()); + assertEquals(1, runningScript.getMetadata().getRequests().size()); + + long t1 = System.currentTimeMillis(); + int result = GroovyScriptRunningContext.getInstance().invoke(runningScript); + long t2 = System.currentTimeMillis(); + System.out.println("groovy time:" + (t2 - t1)); + assertEquals(100, result); + } + @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){ - println($request.getRequest()) return request; } """; - GroovyMethodScript request = new GroovyMethodScript<>(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); + int result = GroovyScriptRunningContext.getInstance().invoke(request); long t2 = System.currentTimeMillis(); System.out.println("groovy time:" + (t2 - t1)); @@ -50,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 new file mode 100644 index 000000000..43564fd53 --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/TransactionGroovyScriptRunningContextTest.java @@ -0,0 +1,95 @@ +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.GroovyBindObjectBuilder; +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,GroovyScriptRunner.TransactionMode.COMMIT); + scriptRunner.compile(script); + GroovyRunningScript runningScript = new GroovyRunningScript<>(script, + Void.class, + GroovyBindObjectBuilder.builder().add("$repository",myTestRepository), + GroovyBindObjectBuilder.builder().add("request",request) + ); + + long t1 = System.currentTimeMillis(); + scriptRunner.invoke(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, GroovyScriptRunner.TransactionMode.READONLY); + scriptRunner.compile(script); + + GroovyRunningScript runningScript = new GroovyRunningScript<>(script, + Void.class, + GroovyBindObjectBuilder.builder().add("$repository",myTestRepository), + GroovyBindObjectBuilder.builder().add("request",request) + ); + + long t1 = System.currentTimeMillis(); + scriptRunner.invoke(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 new file mode 100644 index 000000000..8faaa01ab --- /dev/null +++ b/springboot-starter/src/test/java/com/codingapi/springboot/framework/script/request/MyScriptRequest.java @@ -0,0 +1,43 @@ +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.ScriptField; +import com.codingapi.springboot.framework.script.annotation.ScriptFunction; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class MyScriptRequest { + + @ScriptField(name = "count", description = "总数量") + private final int count; + + @ScriptField(name = "test", description = "test") + private MyTest test; + + + @ScriptFunction( + name = "isSupport", + description = "是否匹配", + parameters = { + @ScriptField(name = "count", description = "描述信息") + } + ) + public boolean isSupport(int count) { + return this.count == count; + } + + public MyScriptRequest(int count) { + this.count = count; + } + + + public void addData(MyTestRepository testRepository) { + MyTest myTest = new MyTest(); + myTest.setName("test"); + testRepository.save(myTest); + } + +}