From 65cbc664b789738f0cceb89ff4f67f4d4c27697e Mon Sep 17 00:00:00 2001 From: "everywhere.z" Date: Wed, 21 May 2025 17:17:52 +0800 Subject: [PATCH] =?UTF-8?q?enhancement=20#IC9GTV=20=E8=84=9A=E6=9C=ACScrip?= =?UTF-8?q?tValidator=E6=96=B0=E5=A2=9E=E8=BF=94=E5=9B=9EValidationResp?= =?UTF-8?q?=E7=9A=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../builder/el/LiteFlowChainELBuilder.java | 7 +--- .../common/entity/ValidationResp.java | 13 ++++++ .../script/validator/ScriptValidator.java | 41 ++++++++++++++----- .../validate/ScriptJavaxProValidateTest.java | 35 ++++++++++++++++ .../script/javapro/validate/cmp/ACmp.java | 21 ++++++++++ .../resources/validate/application.properties | 1 + .../src/test/resources/validate/s1.java | 33 +++++++++++++++ 7 files changed, 136 insertions(+), 15 deletions(-) create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/java/com/yomahub/liteflow/test/script/javapro/validate/ScriptJavaxProValidateTest.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/java/com/yomahub/liteflow/test/script/javapro/validate/cmp/ACmp.java create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/resources/validate/application.properties create mode 100644 liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/resources/validate/s1.java diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java index b5d28fbee..c76fe8be4 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java @@ -281,16 +281,13 @@ public class LiteFlowChainELBuilder { * @return */ public static ValidationResp validateWithEx(String elStr) { - ValidationResp resp = new ValidationResp(); try { LiteFlowChainELBuilder.createChain().setEL(elStr); - resp.setSuccess(true); + return ValidationResp.success(); } catch (Exception e) { LOG.error("validate error", e); - resp.setSuccess(false); - resp.setCause(e); + return ValidationResp.fail(e); } - return resp; } public void build() { diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/common/entity/ValidationResp.java b/liteflow-core/src/main/java/com/yomahub/liteflow/common/entity/ValidationResp.java index 073dfe79f..aadc485ab 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/common/entity/ValidationResp.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/common/entity/ValidationResp.java @@ -18,6 +18,19 @@ public class ValidationResp { */ private Exception cause; + public ValidationResp(boolean success, Exception cause) { + this.success = success; + this.cause = cause; + } + + public static ValidationResp success(){ + return new ValidationResp(true, null); + } + + public static ValidationResp fail(Exception exception){ + return new ValidationResp(false, exception); + } + public boolean isSuccess() { return success; } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/script/validator/ScriptValidator.java b/liteflow-core/src/main/java/com/yomahub/liteflow/script/validator/ScriptValidator.java index a05ce0606..3cd6c3269 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/script/validator/ScriptValidator.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/script/validator/ScriptValidator.java @@ -2,6 +2,7 @@ package com.yomahub.liteflow.script.validator; import cn.hutool.core.util.StrUtil; import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder; +import com.yomahub.liteflow.common.entity.ValidationResp; import com.yomahub.liteflow.enums.ScriptTypeEnum; import com.yomahub.liteflow.log.LFLog; import com.yomahub.liteflow.log.LFLoggerManager; @@ -37,33 +38,32 @@ public class ScriptValidator { * @param scriptType 脚本类型 * @return boolean */ - private static boolean validateScript(String script, ScriptTypeEnum scriptType){ + private static ValidationResp validateScript(String script, ScriptTypeEnum scriptType){ // 未加载任何脚本模块 if(scriptExecutors.isEmpty()){ - LOG.error("The loaded script modules not found."); - return false; + String errorMsg = "The loaded script modules not found."; + return ValidationResp.fail(new RuntimeException(errorMsg)); } // 指定脚本语言未加载 if (scriptType != null && !scriptExecutors.containsKey(scriptType)) { - LOG.error(StrUtil.format("Specified script language {} was not found.", scriptType)); - return false; + String errorMsg = StrUtil.format("Specified script language {} was not found.", scriptType); + return ValidationResp.fail(new RuntimeException(errorMsg)); } // 加载多个脚本语言需要指定语言验证 if (scriptExecutors.size() > 1 && scriptType == null) { - LOG.error("The loaded script modules more than 1. Please specify the script language."); - return false; + String errorMsg = "The loaded script modules more than 1. Please specify the script language."; + return ValidationResp.fail(new RuntimeException(errorMsg)); } ScriptExecutor scriptExecutor = (scriptType != null) ? scriptExecutors.get(scriptType) : scriptExecutors.values().iterator().next(); try { scriptExecutor.compile(script); } catch (Exception e) { - LOG.error(StrUtil.format("{} Script component validate failure. ", scriptExecutor.scriptType()) + e.getMessage()); - return false; + return ValidationResp.fail(e); } - return true; + return ValidationResp.success(); } /** @@ -73,6 +73,16 @@ public class ScriptValidator { * @return boolean */ public static boolean validate(String script){ + return validateScript(script, null).isSuccess(); + } + + /** + * 只引入一种脚本语言时,可以不指定语言验证 + * + * @param script 脚本 + * @return ValidationResp + */ + public static ValidationResp validateWithEx(String script){ return validateScript(script, null); } @@ -84,6 +94,17 @@ public class ScriptValidator { * @return boolean */ public static boolean validate(String script, ScriptTypeEnum scriptType){ + return validateScript(script, scriptType).isSuccess(); + } + + /** + * 指定脚本语言验证 + * + * @param script 脚本 + * @param scriptType 脚本类型 + * @return boolean + */ + public static ValidationResp validateWithEx(String script, ScriptTypeEnum scriptType){ return validateScript(script, scriptType); } diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/java/com/yomahub/liteflow/test/script/javapro/validate/ScriptJavaxProValidateTest.java b/liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/java/com/yomahub/liteflow/test/script/javapro/validate/ScriptJavaxProValidateTest.java new file mode 100644 index 000000000..2a93c2a4f --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/java/com/yomahub/liteflow/test/script/javapro/validate/ScriptJavaxProValidateTest.java @@ -0,0 +1,35 @@ +package com.yomahub.liteflow.test.script.javapro.validate; + +import cn.hutool.core.io.resource.ResourceUtil; +import com.yomahub.liteflow.common.entity.ValidationResp; +import com.yomahub.liteflow.core.FlowExecutor; +import com.yomahub.liteflow.flow.LiteflowResponse; +import com.yomahub.liteflow.script.validator.ScriptValidator; +import com.yomahub.liteflow.slot.DefaultContext; +import com.yomahub.liteflow.test.BaseTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import javax.annotation.Resource; + +@ExtendWith(SpringExtension.class) +@TestPropertySource(value = "classpath:/validate/application.properties") +@SpringBootTest(classes = ScriptJavaxProValidateTest.class) +@EnableAutoConfiguration +public class ScriptJavaxProValidateTest extends BaseTest { + + @Test + public void testValidate() { + String script = ResourceUtil.readUtf8Str("validate/s1.java"); + + ValidationResp resp = ScriptValidator.validateWithEx(script); + + Assertions.assertFalse(resp.isSuccess()); + } +} \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/java/com/yomahub/liteflow/test/script/javapro/validate/cmp/ACmp.java b/liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/java/com/yomahub/liteflow/test/script/javapro/validate/cmp/ACmp.java new file mode 100644 index 000000000..c178930b9 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/java/com/yomahub/liteflow/test/script/javapro/validate/cmp/ACmp.java @@ -0,0 +1,21 @@ +/** + *

Title: liteflow

+ *

Description: 轻量级的组件式流程框架

+ * @author Bryan.Zhang + * @email weenyc31@163.com + * @Date 2020/4/1 + */ +package com.yomahub.liteflow.test.script.javapro.validate.cmp; + +import com.yomahub.liteflow.annotation.LiteflowComponent; +import com.yomahub.liteflow.core.NodeComponent; + +@LiteflowComponent("a") +public class ACmp extends NodeComponent { + + @Override + public void process() { + System.out.println("ACmp executed!"); + } + +} diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/resources/validate/application.properties b/liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/resources/validate/application.properties new file mode 100644 index 000000000..0b38cf3a7 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/resources/validate/application.properties @@ -0,0 +1 @@ +liteflow.parse-mode=PARSE_ONE_ON_FIRST_EXEC \ No newline at end of file diff --git a/liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/resources/validate/s1.java b/liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/resources/validate/s1.java new file mode 100644 index 000000000..740e30953 --- /dev/null +++ b/liteflow-testcase-el/liteflow-testcase-el-script-javaxpro-springboot/src/test/resources/validate/s1.java @@ -0,0 +1,33 @@ +import cn.hutool.core.collection.ListUtil; +import com.yomahub.liteflow.core.NodeComponent; +import com.yomahub.liteflow.slot.DefaultContext; +import com.yomahub.liteflow.test.script.javaxpro.common.cmp.Person; +import com.yomahub.liteflow.test.script.javaxpro.common.cmp.TestDomain; + +import java.util.List; + +public class Demo extends NodeComponent { + @Override + public void process() throws Exception { + int v1 = 2; + int v2 = 3; + DefaultContext ctx = this.getFirstContextBean(); + ctx.setData("s1", v1 * v2); + + TestDomain domain = ContextAwareHolder.loadContextAware().getBean(TestDomain.class); + System.out.println(domain); + String str = domain.sayHello("jack"); + ctx.setData("hi", str); + + List personList = ListUtil.toList( + new Person("jack", 15000), + new Person("tom", 13500), + new Person("peter", 18600) + ); + + int totalSalary = personList.stream().mapToInt(Person::getSalary).sum(); + + System.out.println(totalSalary); + ctx.setData("salary", 47100); + } +} \ No newline at end of file