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