mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 12:12:08 +08:00
enhancement #IC9GTV 脚本ScriptValidator新增返回ValidationResp的API
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* <p>Title: liteflow</p>
|
||||
* <p>Description: 轻量级的组件式流程框架</p>
|
||||
* @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!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
liteflow.parse-mode=PARSE_ONE_ON_FIRST_EXEC
|
||||
@@ -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<Person> 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user