mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-06-10 03:07:32 +08:00
工程结构模块的改变
This commit is contained in:
37
liteflow-script-plugin/liteflow-script-groovy/pom.xml
Normal file
37
liteflow-script-plugin/liteflow-script-groovy/pom.xml
Normal file
@@ -0,0 +1,37 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>liteflow-script-plugin</artifactId>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>liteflow-script-groovy</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-script-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-jsr223</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.yomahub.liteflow.script.groovy;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.slot.DataBus;
|
||||
import com.yomahub.liteflow.slot.Slot;
|
||||
import com.yomahub.liteflow.script.ScriptExecutor;
|
||||
import com.yomahub.liteflow.script.exception.ScriptExecuteException;
|
||||
import com.yomahub.liteflow.script.exception.ScriptLoadException;
|
||||
import com.yomahub.liteflow.util.CopyOnWriteHashMap;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.script.*;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Groovy脚本语言的执行器实现
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public class GroovyScriptExecutor implements ScriptExecutor {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
private ScriptEngine scriptEngine;
|
||||
|
||||
private final Map<String, CompiledScript> compiledScriptMap = new CopyOnWriteHashMap<>();
|
||||
|
||||
@Override
|
||||
public ScriptExecutor init() {
|
||||
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
|
||||
scriptEngine = scriptEngineManager.getEngineByName("groovy");
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(String nodeId, String script) {
|
||||
try{
|
||||
CompiledScript compiledScript = ((Compilable) scriptEngine).compile(script);
|
||||
compiledScriptMap.put(nodeId, compiledScript);
|
||||
}catch (Exception e){
|
||||
String errorMsg = StrUtil.format("script loading error for node[{}], error msg:{}", nodeId, e.getMessage());
|
||||
throw new ScriptLoadException(errorMsg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object execute(String currChainName, String nodeId, int slotIndex) {
|
||||
try{
|
||||
if (!compiledScriptMap.containsKey(nodeId)){
|
||||
String errorMsg = StrUtil.format("script for node[{}] is not loaded", nodeId);
|
||||
throw new RuntimeException(errorMsg);
|
||||
}
|
||||
|
||||
CompiledScript compiledScript = compiledScriptMap.get(nodeId);
|
||||
Bindings bindings = new SimpleBindings();
|
||||
|
||||
//往脚本语言绑定表里循环增加绑定上下文的key
|
||||
//key的规则为自定义上下文的simpleName
|
||||
//比如你的自定义上下文为AbcContext,那么key就为:abcContext
|
||||
//这里不统一放一个map的原因是考虑到有些用户会调用上下文里的方法,而不是参数,所以脚本语言的绑定表里也是放多个上下文
|
||||
DataBus.getContextBeanList(slotIndex).forEach(o -> {
|
||||
String key = StrUtil.lowerFirst(o.getClass().getSimpleName());
|
||||
bindings.put(key, o);
|
||||
});
|
||||
|
||||
//放入主Chain的流程参数
|
||||
Slot slot = DataBus.getSlot(slotIndex);
|
||||
bindings.put("requestData", slot.getRequestData());
|
||||
|
||||
//如果有隐试流程,则放入隐式流程的流程参数
|
||||
Object subRequestData = slot.getChainReqData(currChainName);
|
||||
if (ObjectUtil.isNotNull(subRequestData)){
|
||||
bindings.put("subRequestData", subRequestData);
|
||||
}
|
||||
|
||||
return compiledScript.eval(bindings);
|
||||
}catch (Exception e){
|
||||
log.error(e.getMessage(), e);
|
||||
String errorMsg = StrUtil.format("script execute error for node[{}]", nodeId);
|
||||
throw new ScriptExecuteException(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache() {
|
||||
compiledScriptMap.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
# Groovy的实现
|
||||
com.yomahub.liteflow.script.groovy.GroovyScriptExecutor
|
||||
33
liteflow-script-plugin/liteflow-script-qlexpress/pom.xml
Normal file
33
liteflow-script-plugin/liteflow-script-qlexpress/pom.xml
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>liteflow-script-plugin</artifactId>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>liteflow-script-qlexpress</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-script-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>QLExpress</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.yomahub.liteflow.script.qlexpress;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.ql.util.express.DefaultContext;
|
||||
import com.ql.util.express.ExpressLoader;
|
||||
import com.ql.util.express.ExpressRunner;
|
||||
import com.ql.util.express.InstructionSet;
|
||||
import com.yomahub.liteflow.slot.DataBus;
|
||||
import com.yomahub.liteflow.slot.Slot;
|
||||
import com.yomahub.liteflow.script.ScriptExecutor;
|
||||
import com.yomahub.liteflow.script.exception.ScriptExecuteException;
|
||||
import com.yomahub.liteflow.script.exception.ScriptLoadException;
|
||||
import com.yomahub.liteflow.util.CopyOnWriteHashMap;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 阿里QLExpress脚本语言的执行器实现
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.6.0
|
||||
*/
|
||||
public class QLExpressScriptExecutor implements ScriptExecutor {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
private ExpressRunner expressRunner;
|
||||
|
||||
private final Map<String, InstructionSet> compiledScriptMap = new CopyOnWriteHashMap<>();
|
||||
|
||||
@Override
|
||||
public ScriptExecutor init() {
|
||||
expressRunner = new ExpressRunner();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(String nodeId, String script) {
|
||||
try{
|
||||
InstructionSet instructionSet = expressRunner.getInstructionSetFromLocalCache(script);
|
||||
compiledScriptMap.put(nodeId, instructionSet);
|
||||
}catch (Exception e){
|
||||
String errorMsg = StrUtil.format("script loading error for node[{}],error msg:{}", nodeId, e.getMessage());
|
||||
throw new ScriptLoadException(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object execute(String currChainName, String nodeId, int slotIndex) {
|
||||
List<String> errorList = new ArrayList<>();
|
||||
try{
|
||||
if (!compiledScriptMap.containsKey(nodeId)){
|
||||
String errorMsg = StrUtil.format("script for node[{}] is not loaded", nodeId);
|
||||
throw new RuntimeException(errorMsg);
|
||||
}
|
||||
|
||||
InstructionSet instructionSet = compiledScriptMap.get(nodeId);
|
||||
DefaultContext<String, Object> context = new DefaultContext<>();
|
||||
|
||||
//往脚本语言绑定表里循环增加绑定上下文的key
|
||||
//key的规则为自定义上下文的simpleName
|
||||
//比如你的自定义上下文为AbcContext,那么key就为:abcContext
|
||||
//这里不统一放一个map的原因是考虑到有些用户会调用上下文里的方法,而不是参数,所以脚本语言的绑定表里也是放多个上下文
|
||||
DataBus.getContextBeanList(slotIndex).forEach(o -> {
|
||||
String key = StrUtil.lowerFirst(o.getClass().getSimpleName());
|
||||
context.put(key, o);
|
||||
});
|
||||
|
||||
//放入主Chain的流程参数
|
||||
Slot slot = DataBus.getSlot(slotIndex);
|
||||
context.put("requestData", slot.getRequestData());
|
||||
|
||||
//如果有隐试流程,则放入隐式流程的流程参数
|
||||
Object subRequestData = slot.getChainReqData(currChainName);
|
||||
if (ObjectUtil.isNotNull(subRequestData)){
|
||||
context.put("subRequestData", subRequestData);
|
||||
}
|
||||
|
||||
return expressRunner.execute(instructionSet, context, errorList, true, false, null);
|
||||
}catch (Exception e){
|
||||
for (String scriptErrorMsg : errorList){
|
||||
log.error("\n{}", scriptErrorMsg);
|
||||
}
|
||||
String errorMsg = StrUtil.format("script execute error for node[{}]", nodeId);
|
||||
throw new ScriptExecuteException(errorMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache() {
|
||||
compiledScriptMap.clear();
|
||||
expressRunner.clearExpressCache();
|
||||
ReflectUtil.setFieldValue(expressRunner,"loader",new ExpressLoader(expressRunner));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
# QLExpress的实现
|
||||
com.yomahub.liteflow.script.qlexpress.QLExpressScriptExecutor
|
||||
22
liteflow-script-plugin/pom.xml
Normal file
22
liteflow-script-plugin/pom.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>liteflow</artifactId>
|
||||
<groupId>com.yomahub</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
||||
<artifactId>liteflow-script-plugin</artifactId>
|
||||
|
||||
<modules>
|
||||
<module>liteflow-script-qlexpress</module>
|
||||
<module>liteflow-script-groovy</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user