feature #I44FT8 支持脚本语言的组件,并支持动态刷新脚本(版本特性)
This commit is contained in:
parent
a51ecd5ca1
commit
4bbfd3a9ab
|
@ -0,0 +1,74 @@
|
|||
package com.yomahub.liteflow.script.groovy;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.entity.data.DataBus;
|
||||
import com.yomahub.liteflow.entity.data.Slot;
|
||||
import com.yomahub.liteflow.script.ScriptExecutor;
|
||||
import com.yomahub.liteflow.script.exception.ScriptExecuteException;
|
||||
import com.yomahub.liteflow.script.exception.ScriptLoadException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.script.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Groovy脚本语言的执行器实现
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.5.11
|
||||
*/
|
||||
public class GroovyScriptExecutor implements ScriptExecutor {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
private ScriptEngine scriptEngine;
|
||||
|
||||
private Map<String, CompiledScript> compiledScriptMap = new HashMap<>();
|
||||
|
||||
@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);
|
||||
if (!compiledScriptMap.containsKey(nodeId)){
|
||||
compiledScriptMap.put(nodeId, compiledScript);
|
||||
}
|
||||
}catch (Exception e){
|
||||
String errorMsg = StrUtil.format("script loading error for node[{}]", nodeId);
|
||||
throw new ScriptLoadException(errorMsg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object execute(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();
|
||||
Slot slot = DataBus.getSlot(slotIndex);
|
||||
bindings.put("slot", slot);
|
||||
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
|
|
@ -14,6 +14,11 @@ import org.slf4j.LoggerFactory;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 阿里QLExpress脚本语言的执行器实现
|
||||
* @author Bryan.Zhang
|
||||
* @since 2.5.11
|
||||
*/
|
||||
public class QLExpressScriptExecutor implements ScriptExecutor {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
|
|
Loading…
Reference in New Issue