mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 20:22:07 +08:00
feature #I7V6VB https://gitee.com/dromara/liteFlow/issues/I7V6VB
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
<groupId>com.yomahub</groupId>
|
||||
<artifactId>liteflow-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.janino</groupId>
|
||||
|
||||
@@ -1,38 +1,51 @@
|
||||
package com.yomahub.liteflow.script.java;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.enums.ScriptTypeEnum;
|
||||
import com.yomahub.liteflow.script.ScriptExecuteWrap;
|
||||
import com.yomahub.liteflow.script.ScriptExecutor;
|
||||
import com.yomahub.liteflow.script.exception.ScriptLoadException;
|
||||
import com.yomahub.liteflow.util.CopyOnWriteHashMap;
|
||||
import org.codehaus.commons.compiler.CompilerFactoryFactory;
|
||||
import org.codehaus.commons.compiler.IScriptEvaluator;
|
||||
import java.util.Map;
|
||||
|
||||
public class JavaExecutor extends ScriptExecutor {
|
||||
|
||||
private final Map<String, IScriptEvaluator> compiledScriptMap = new CopyOnWriteHashMap<>();
|
||||
|
||||
@Override
|
||||
public void load(String nodeId, String script) {
|
||||
// 创建Janino脚本Evaluator
|
||||
/*IScriptEvaluator se = CompilerFactoryFactory.getDefaultCompilerFactory().newScriptEvaluator();
|
||||
// 返回值类型指定为Object以支持不同脚本
|
||||
se.setReturnType(Object.class);
|
||||
// 指定Janino脚本里的变量名及类型,为通用起见,只设置一个Object类型的变量
|
||||
se.setParameters(new String[] { JANINO_SCRIPT_PARAMETER_NAME }, new Class[] { Object.class });
|
||||
// 编译
|
||||
se.cook(script);
|
||||
// 缓存编译过的Evaluator
|
||||
compiledScriptMap.put(nodeId, se);*/
|
||||
try{
|
||||
IScriptEvaluator se = CompilerFactoryFactory.getDefaultCompilerFactory(this.getClass().getClassLoader()).newScriptEvaluator();
|
||||
se.setReturnType(Object.class);
|
||||
se.setParameters(new String[] {"_meta"}, new Class[] {ScriptExecuteWrap.class});
|
||||
se.cook(script);
|
||||
compiledScriptMap.put(nodeId, se);
|
||||
}catch (Exception e){
|
||||
String errorMsg = StrUtil.format("script loading error for node[{}],error msg:{}", nodeId, e.getMessage());
|
||||
throw new ScriptLoadException(errorMsg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object executeScript(ScriptExecuteWrap wrap) throws Exception {
|
||||
return null;
|
||||
if (!compiledScriptMap.containsKey(wrap.getNodeId())) {
|
||||
String errorMsg = StrUtil.format("script for node[{}] is not loaded", wrap.getNodeId());
|
||||
throw new ScriptLoadException(errorMsg);
|
||||
}
|
||||
IScriptEvaluator se = compiledScriptMap.get(wrap.getNodeId());
|
||||
return se.evaluate(wrap);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache() {
|
||||
|
||||
compiledScriptMap.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScriptTypeEnum scriptType() {
|
||||
return null;
|
||||
return ScriptTypeEnum.JAVA;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# Java的实现
|
||||
com.yomahub.liteflow.script.java.JavaExecutor
|
||||
Reference in New Issue
Block a user