mirror of
https://gitee.com/dromara/liteFlow.git
synced 2026-05-14 20:22:07 +08:00
!87 重构初始化模块以及一些细小的例行维护
Merge pull request !87 from 爱码士/optimized/refactoring-parser-init
This commit is contained in:
@@ -13,22 +13,19 @@ import cn.hutool.core.util.BooleanUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.enums.FlowParserTypeEnum;
|
||||
import com.yomahub.liteflow.exception.*;
|
||||
import com.yomahub.liteflow.flow.FlowBus;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
import com.yomahub.liteflow.flow.element.Chain;
|
||||
import com.yomahub.liteflow.flow.element.Node;
|
||||
import com.yomahub.liteflow.flow.id.IdGeneratorHolder;
|
||||
import com.yomahub.liteflow.parser.*;
|
||||
import com.yomahub.liteflow.parser.base.FlowParser;
|
||||
import com.yomahub.liteflow.parser.el.*;
|
||||
import com.yomahub.liteflow.parser.factory.FlowParserProvider;
|
||||
import com.yomahub.liteflow.property.LiteflowConfig;
|
||||
import com.yomahub.liteflow.property.LiteflowConfigGetter;
|
||||
import com.yomahub.liteflow.slot.DataBus;
|
||||
import com.yomahub.liteflow.slot.DefaultContext;
|
||||
import com.yomahub.liteflow.slot.Slot;
|
||||
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
||||
import com.yomahub.liteflow.spi.holder.ContextCmpInitHolder;
|
||||
import com.yomahub.liteflow.thread.ExecutorHelper;
|
||||
import org.slf4j.Logger;
|
||||
@@ -49,35 +46,7 @@ public class FlowExecutor {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FlowExecutor.class);
|
||||
|
||||
private static final String ZK_CONFIG_REGEX = "[\\w\\d][\\w\\d\\.]+\\:(\\d)+(\\,[\\w\\d][\\w\\d\\.]+\\:(\\d)+)*";
|
||||
|
||||
private static final String LOCAL_XML_CONFIG_REGEX = "^[\\w\\:\\-\\@\\/\\\\\\*]+\\.xml$";
|
||||
|
||||
private static final String LOCAL_EL_XML_CONFIG_REGEX = "^[\\w\\:\\-\\@\\/\\\\\\*]+\\.el\\.xml$";
|
||||
private static final String LOCAL_JSON_CONFIG_REGEX = "^[\\w\\:\\-\\@\\/\\\\\\*]+\\.json$";
|
||||
|
||||
private static final String LOCAL_EL_JSON_CONFIG_REGEX = "^[\\w\\:\\-\\@\\/\\\\\\*]+\\.el\\.json$";
|
||||
private static final String LOCAL_YML_CONFIG_REGEX = "^[\\w\\:\\-\\@\\/\\\\\\*]+\\.yml$";
|
||||
|
||||
private static final String LOCAL_EL_YML_CONFIG_REGEX = "^[\\w\\:\\-\\@\\/\\\\\\*]+\\.el\\.yml$";
|
||||
|
||||
private static final String FORMATE_XML_CONFIG_REGEX = "xml:.+";
|
||||
|
||||
private static final String FORMATE_EL_XML_CONFIG_REGEX = "el_xml:.+";
|
||||
|
||||
private static final String FORMATE_JSON_CONFIG_REGEX = "json:.+";
|
||||
|
||||
private static final String FORMATE_EL_JSON_CONFIG_REGEX = "el_json:.+";
|
||||
|
||||
private static final String FORMATE_YML_CONFIG_REGEX = "yml:.+";
|
||||
|
||||
private static final String FORMATE_EL_YML_CONFIG_REGEX = "el_yml:.+";
|
||||
|
||||
private static final String PREFIX_FORMAT_CONFIG_REGEX = "xml:|json:|yml:";
|
||||
|
||||
private static final String PREFIX_EL_FORMAT_CONFIG_REGEX = "el_xml:|el_json:|el_yml:";
|
||||
|
||||
private static final String CLASS_CONFIG_REGEX = "^\\w+(\\.\\w+)*$";
|
||||
private static final String PREFIX_FORMAT_CONFIG_REGEX = "xml:|json:|yml:|el_xml:|el_json:|el_yml:";
|
||||
|
||||
private LiteflowConfig liteflowConfig;
|
||||
|
||||
@@ -130,30 +99,11 @@ public class FlowExecutor {
|
||||
List<String> rulePathList = new ArrayList<>();
|
||||
for (String path : sourceRulePathList) {
|
||||
try {
|
||||
//根据path获得pattern类型
|
||||
FlowParserTypeEnum pattern = matchFormatConfig(path);
|
||||
if (pattern == null){
|
||||
String errorMsg = StrUtil.format("can't support the path:{}", path);
|
||||
throw new ErrorSupportPathException(errorMsg);
|
||||
}
|
||||
|
||||
if (pattern.getType().startsWith("el")){
|
||||
path = ReUtil.replaceAll(path, PREFIX_EL_FORMAT_CONFIG_REGEX, "");
|
||||
}else{
|
||||
path = ReUtil.replaceAll(path, PREFIX_FORMAT_CONFIG_REGEX, "");
|
||||
}
|
||||
|
||||
|
||||
//获得parser
|
||||
parser = matchFormatParser(path, pattern);
|
||||
|
||||
if (parser == null){
|
||||
String errorMsg = StrUtil.format("can't find the parser for path:{}", path);
|
||||
throw new ErrorSupportPathException(errorMsg);
|
||||
}
|
||||
|
||||
// 查找对应的解析器
|
||||
parser = FlowParserProvider.lookup(path);
|
||||
parserNameSet.add(parser.getClass().getName());
|
||||
|
||||
// 替换掉前缀标识(如:xml:/json:),保留剩下的完整地址
|
||||
path = ReUtil.replaceAll(path, PREFIX_FORMAT_CONFIG_REGEX, "");
|
||||
rulePathList.add(path);
|
||||
|
||||
//支持多类型的配置文件,分别解析
|
||||
@@ -201,135 +151,6 @@ public class FlowExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 匹配路径配置,生成对应的解析器
|
||||
*/
|
||||
private FlowParser matchFormatParser(String path, FlowParserTypeEnum pattern) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
|
||||
boolean isLocalFile = isLocalConfig(path);
|
||||
if (isLocalFile) {
|
||||
LOG.info("flow info loaded from local file,path={},format type={}", path, pattern.getType());
|
||||
switch (pattern) {
|
||||
case TYPE_XML:
|
||||
return new LocalXmlFlowParser();
|
||||
case TYPE_JSON:
|
||||
return new LocalJsonFlowParser();
|
||||
case TYPE_YML:
|
||||
return new LocalYmlFlowParser();
|
||||
case TYPE_EL_XML:
|
||||
return new LocalXmlFlowELParser();
|
||||
case TYPE_EL_JSON:
|
||||
return new LocalJsonFlowELParser();
|
||||
case TYPE_EL_YML:
|
||||
return new LocalYmlFlowELParser();
|
||||
default:
|
||||
}
|
||||
} else if (isClassConfig(path)) {
|
||||
LOG.info("flow info loaded from class config,class={},format type={}", path, pattern.getType());
|
||||
Class<?> c = Class.forName(path);
|
||||
switch (pattern) {
|
||||
case TYPE_XML:
|
||||
return (XmlFlowParser) ContextAwareHolder.loadContextAware().registerBean(c);
|
||||
case TYPE_JSON:
|
||||
return (JsonFlowParser) ContextAwareHolder.loadContextAware().registerBean(c);
|
||||
case TYPE_YML:
|
||||
return (YmlFlowParser) ContextAwareHolder.loadContextAware().registerBean(c);
|
||||
case TYPE_EL_XML:
|
||||
return (XmlFlowELParser) ContextAwareHolder.loadContextAware().registerBean(c);
|
||||
case TYPE_EL_JSON:
|
||||
return (JsonFlowELParser) ContextAwareHolder.loadContextAware().registerBean(c);
|
||||
case TYPE_EL_YML:
|
||||
return (YmlFlowELParser) ContextAwareHolder.loadContextAware().registerBean(c);
|
||||
default:
|
||||
}
|
||||
} else if (isZKConfig(path)) {
|
||||
LOG.info("flow info loaded from Zookeeper,zkNode={},format type={}", path, pattern.getType());
|
||||
switch (pattern) {
|
||||
case TYPE_XML:
|
||||
return new ZookeeperXmlFlowParser(liteflowConfig.getZkNode());
|
||||
case TYPE_JSON:
|
||||
return new ZookeeperJsonFlowParser(liteflowConfig.getZkNode());
|
||||
case TYPE_YML:
|
||||
return new ZookeeperYmlFlowParser(liteflowConfig.getZkNode());
|
||||
case TYPE_EL_XML:
|
||||
return new ZookeeperXmlFlowELParser(liteflowConfig.getZkNode());
|
||||
case TYPE_EL_JSON:
|
||||
return new ZookeeperJsonFlowELParser(liteflowConfig.getZkNode());
|
||||
case TYPE_EL_YML:
|
||||
return new ZookeeperYmlFlowELParser(liteflowConfig.getZkNode());
|
||||
default:
|
||||
}
|
||||
}
|
||||
LOG.info("load flow info error, path={}, pattern={}", path, pattern.getType());
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判定是否为本地文件
|
||||
*/
|
||||
private boolean isLocalConfig(String path) {
|
||||
return ReUtil.isMatch(LOCAL_XML_CONFIG_REGEX, path)
|
||||
|| ReUtil.isMatch(LOCAL_JSON_CONFIG_REGEX, path)
|
||||
|| ReUtil.isMatch(LOCAL_YML_CONFIG_REGEX, path)
|
||||
|| ReUtil.isMatch(LOCAL_EL_XML_CONFIG_REGEX, path)
|
||||
|| ReUtil.isMatch(LOCAL_EL_JSON_CONFIG_REGEX, path)
|
||||
|| ReUtil.isMatch(LOCAL_EL_YML_CONFIG_REGEX, path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判定是否为自定义class配置
|
||||
*/
|
||||
private boolean isClassConfig(String path) {
|
||||
return ReUtil.isMatch(CLASS_CONFIG_REGEX, path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判定是否为zk配置
|
||||
*/
|
||||
private boolean isZKConfig(String path) {
|
||||
return ReUtil.isMatch(ZK_CONFIG_REGEX, path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 匹配文本格式,支持xml,json和yml
|
||||
*/
|
||||
private FlowParserTypeEnum matchFormatConfig(String path) {
|
||||
if (ReUtil.isMatch(LOCAL_XML_CONFIG_REGEX, path) || ReUtil.isMatch(FORMATE_XML_CONFIG_REGEX, path)) {
|
||||
return FlowParserTypeEnum.TYPE_XML;
|
||||
} else if (ReUtil.isMatch(LOCAL_JSON_CONFIG_REGEX, path) || ReUtil.isMatch(FORMATE_JSON_CONFIG_REGEX, path)) {
|
||||
return FlowParserTypeEnum.TYPE_JSON;
|
||||
} else if (ReUtil.isMatch(LOCAL_YML_CONFIG_REGEX, path) || ReUtil.isMatch(FORMATE_YML_CONFIG_REGEX, path)) {
|
||||
return FlowParserTypeEnum.TYPE_YML;
|
||||
} else if (ReUtil.isMatch(LOCAL_EL_XML_CONFIG_REGEX, path) || ReUtil.isMatch(FORMATE_EL_XML_CONFIG_REGEX, path)) {
|
||||
return FlowParserTypeEnum.TYPE_EL_XML;
|
||||
} else if (ReUtil.isMatch(LOCAL_EL_JSON_CONFIG_REGEX, path) || ReUtil.isMatch(FORMATE_EL_JSON_CONFIG_REGEX, path)) {
|
||||
return FlowParserTypeEnum.TYPE_EL_JSON;
|
||||
} else if (ReUtil.isMatch(LOCAL_EL_YML_CONFIG_REGEX, path) || ReUtil.isMatch(FORMATE_EL_YML_CONFIG_REGEX, path)) {
|
||||
return FlowParserTypeEnum.TYPE_EL_YML;
|
||||
} else if (isClassConfig(path)) {
|
||||
//其实整个这个判断块代码可以不要,因为如果是自定义配置源的话,标准写法也要在前面加xml:/json:/yml:这种
|
||||
//但是这块可能是考虑到有些人忘加了,所以再来判断下。如果写了标准的话,是不会走到这块来的
|
||||
try {
|
||||
Class<?> clazz = Class.forName(path);
|
||||
if (ClassXmlFlowParser.class.isAssignableFrom(clazz)) {
|
||||
return FlowParserTypeEnum.TYPE_XML;
|
||||
} else if (ClassJsonFlowParser.class.isAssignableFrom(clazz)) {
|
||||
return FlowParserTypeEnum.TYPE_JSON;
|
||||
} else if (ClassYmlFlowParser.class.isAssignableFrom(clazz)) {
|
||||
return FlowParserTypeEnum.TYPE_YML;
|
||||
} else if (ClassXmlFlowELParser.class.isAssignableFrom(clazz)) {
|
||||
return FlowParserTypeEnum.TYPE_EL_XML;
|
||||
} else if (ClassJsonFlowELParser.class.isAssignableFrom(clazz)) {
|
||||
return FlowParserTypeEnum.TYPE_EL_JSON;
|
||||
} else if (ClassYmlFlowELParser.class.isAssignableFrom(clazz)) {
|
||||
return FlowParserTypeEnum.TYPE_EL_YML;
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
LOG.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
//此方法就是从原有的配置源主动拉取新的进行刷新
|
||||
//和FlowBus.refreshFlowMetaData的区别就是一个为主动拉取,一个为被动监听到新的内容进行刷新
|
||||
public void reloadRule() {
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.yomahub.liteflow.parser.factory;
|
||||
|
||||
import com.yomahub.liteflow.parser.JsonFlowParser;
|
||||
import com.yomahub.liteflow.parser.XmlFlowParser;
|
||||
import com.yomahub.liteflow.parser.YmlFlowParser;
|
||||
import com.yomahub.liteflow.parser.base.BaseJsonFlowParser;
|
||||
import com.yomahub.liteflow.parser.base.BaseXmlFlowParser;
|
||||
import com.yomahub.liteflow.parser.base.BaseYmlFlowParser;
|
||||
import com.yomahub.liteflow.parser.el.JsonFlowELParser;
|
||||
import com.yomahub.liteflow.parser.el.XmlFlowELParser;
|
||||
import com.yomahub.liteflow.parser.el.YmlFlowELParser;
|
||||
import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
|
||||
|
||||
/**
|
||||
* Class文件
|
||||
* <p>
|
||||
*
|
||||
* @author junjun
|
||||
*/
|
||||
public class ClassParserFactory implements FlowParserFactory {
|
||||
|
||||
@Override
|
||||
public JsonFlowParser createJsonParser(String path) throws Exception {
|
||||
Class<?> c = Class.forName(path);
|
||||
return (JsonFlowParser) ContextAwareHolder.loadContextAware().registerBean(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public XmlFlowParser createXmlParser(String path) throws Exception {
|
||||
Class<?> c = Class.forName(path);
|
||||
return (XmlFlowParser) ContextAwareHolder.loadContextAware().registerBean(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public YmlFlowParser createYmlParser(String path) throws Exception {
|
||||
Class<?> c = Class.forName(path);
|
||||
return (YmlFlowParser) ContextAwareHolder.loadContextAware().registerBean(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseJsonFlowParser createJsonELParser(String path) throws Exception {
|
||||
Class<?> c = Class.forName(path);
|
||||
return (JsonFlowELParser) ContextAwareHolder.loadContextAware().registerBean(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseXmlFlowParser createXmlELParser(String path) throws Exception {
|
||||
Class<?> c = Class.forName(path);
|
||||
return (XmlFlowELParser) ContextAwareHolder.loadContextAware().registerBean(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseYmlFlowParser createYmlELParser(String path) throws Exception {
|
||||
Class<?> c = Class.forName(path);
|
||||
return (YmlFlowELParser) ContextAwareHolder.loadContextAware().registerBean(c);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.yomahub.liteflow.parser.factory;
|
||||
|
||||
import com.yomahub.liteflow.parser.JsonFlowParser;
|
||||
import com.yomahub.liteflow.parser.XmlFlowParser;
|
||||
import com.yomahub.liteflow.parser.YmlFlowParser;
|
||||
import com.yomahub.liteflow.parser.base.BaseJsonFlowParser;
|
||||
import com.yomahub.liteflow.parser.base.BaseXmlFlowParser;
|
||||
import com.yomahub.liteflow.parser.base.BaseYmlFlowParser;
|
||||
import com.yomahub.liteflow.parser.el.JsonFlowELParser;
|
||||
import com.yomahub.liteflow.parser.el.XmlFlowELParser;
|
||||
import com.yomahub.liteflow.parser.el.YmlFlowELParser;
|
||||
|
||||
/**
|
||||
* Flow Parser 工厂接口
|
||||
* <p>
|
||||
*
|
||||
* @author junjun
|
||||
*/
|
||||
public interface FlowParserFactory {
|
||||
|
||||
BaseJsonFlowParser createJsonParser(String path) throws Exception;
|
||||
|
||||
BaseXmlFlowParser createXmlParser(String path) throws Exception;
|
||||
|
||||
BaseYmlFlowParser createYmlParser(String path) throws Exception;
|
||||
|
||||
BaseJsonFlowParser createJsonELParser(String path) throws Exception;
|
||||
|
||||
BaseXmlFlowParser createXmlELParser(String path) throws Exception;
|
||||
|
||||
BaseYmlFlowParser createYmlELParser(String path) throws Exception;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package com.yomahub.liteflow.parser.factory;
|
||||
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.exception.ErrorSupportPathException;
|
||||
import com.yomahub.liteflow.parser.ClassJsonFlowParser;
|
||||
import com.yomahub.liteflow.parser.ClassXmlFlowParser;
|
||||
import com.yomahub.liteflow.parser.ClassYmlFlowParser;
|
||||
import com.yomahub.liteflow.parser.base.FlowParser;
|
||||
import com.yomahub.liteflow.parser.el.ClassJsonFlowELParser;
|
||||
import com.yomahub.liteflow.parser.el.ClassXmlFlowELParser;
|
||||
import com.yomahub.liteflow.parser.el.ClassYmlFlowELParser;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static com.yomahub.liteflow.enums.FlowParserTypeEnum.*;
|
||||
|
||||
/**
|
||||
* 解析器提供者
|
||||
* <p>
|
||||
*
|
||||
* @author junjun
|
||||
*/
|
||||
public class FlowParserProvider {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(FlowExecutor.class);
|
||||
|
||||
private static final String LOCAL_XML_CONFIG_REGEX = "^[\\w\\:\\-\\@\\/\\\\\\*]+\\.xml$";
|
||||
private static final String LOCAL_JSON_CONFIG_REGEX = "^[\\w\\:\\-\\@\\/\\\\\\*]+\\.json$";
|
||||
private static final String LOCAL_YML_CONFIG_REGEX = "^[\\w\\:\\-\\@\\/\\\\\\*]+\\.yml$";
|
||||
|
||||
private static final String LOCAL_EL_XML_CONFIG_REGEX = "^[\\w\\:\\-\\@\\/\\\\\\*]+\\.el\\.xml$";
|
||||
|
||||
private static final String LOCAL_EL_JSON_CONFIG_REGEX = "^[\\w\\:\\-\\@\\/\\\\\\*]+\\.el\\.json$";
|
||||
|
||||
private static final String LOCAL_EL_YML_CONFIG_REGEX = "^[\\w\\:\\-\\@\\/\\\\\\*]+\\.el\\.yml$";
|
||||
|
||||
private static final String FORMAT_EL_XML_CONFIG_REGEX = "el_xml:.+";
|
||||
|
||||
private static final String FORMAT_EL_JSON_CONFIG_REGEX = "el_json:.+";
|
||||
|
||||
private static final String FORMAT_EL_YML_CONFIG_REGEX = "el_yml:.+";
|
||||
|
||||
private static final String FORMAT_XML_CONFIG_REGEX = "xml:.+";
|
||||
|
||||
private static final String FORMAT_JSON_CONFIG_REGEX = "json:.+";
|
||||
|
||||
private static final String FORMAT_YML_CONFIG_REGEX = "yml:.+";
|
||||
|
||||
private static final String PREFIX_FORMAT_CONFIG_REGEX = "xml:|json:|yml:el_xml:|el_json:|el_yml:";
|
||||
|
||||
private static final String CLASS_CONFIG_REGEX = "^(xml:|json:|yml:|el_xml:|el_json:|el_yml:)?\\w+(\\.\\w+)*$";
|
||||
|
||||
private static final String ZK_CONFIG_REGEX = "(xml:|json:|yml:|el_xml:|el_json:|el_yml:)?[\\w\\d][\\w\\d\\.]+\\:(\\d)+(\\,[\\w\\d][\\w\\d\\.]+\\:(\\d)+)*";
|
||||
|
||||
/**
|
||||
* 根据配置的地址找到对应的解析器
|
||||
*
|
||||
* @param path
|
||||
* @return
|
||||
*/
|
||||
public static FlowParser lookup(String path) throws Exception {
|
||||
if (isLocalConfig(path)) {
|
||||
FlowParserFactory factory = new LocalParserFactory();
|
||||
if (ReUtil.isMatch(LOCAL_XML_CONFIG_REGEX, path)) {
|
||||
LOG.info("flow info loaded from local file,path={},format type={}", path, TYPE_XML.getType());
|
||||
return factory.createXmlParser(path);
|
||||
}
|
||||
else if (ReUtil.isMatch(LOCAL_JSON_CONFIG_REGEX, path)) {
|
||||
LOG.info("flow info loaded from local file,path={},format type={}", path, TYPE_JSON.getType());
|
||||
return factory.createJsonParser(path);
|
||||
}
|
||||
else if (ReUtil.isMatch(LOCAL_YML_CONFIG_REGEX, path)) {
|
||||
LOG.info("flow info loaded from local file,path={},format type={}", path, TYPE_YML.getType());
|
||||
return factory.createYmlParser(path);
|
||||
}
|
||||
else if (ReUtil.isMatch(LOCAL_EL_XML_CONFIG_REGEX, path)) {
|
||||
LOG.info("flow info loaded from local EL file,path={},format type={}", path, TYPE_EL_XML.getType());
|
||||
return factory.createXmlELParser(path);
|
||||
}
|
||||
else if (ReUtil.isMatch(LOCAL_EL_JSON_CONFIG_REGEX, path)) {
|
||||
LOG.info("flow info loaded from local EL file,path={},format type={}", path, TYPE_EL_JSON.getType());
|
||||
return factory.createJsonELParser(path);
|
||||
}
|
||||
else if (ReUtil.isMatch(LOCAL_EL_YML_CONFIG_REGEX, path)) {
|
||||
LOG.info("flow info loaded from local EL file,path={},format type={}", path, TYPE_EL_YML.getType());
|
||||
return factory.createYmlELParser(path);
|
||||
}
|
||||
}
|
||||
else if (isClassConfig(path)) {
|
||||
// 获取最终的className,因为有些可能className前面带了文件类型的标识,比如json:x.x.x.x
|
||||
String className = ReUtil.replaceAll(path, PREFIX_FORMAT_CONFIG_REGEX, "");
|
||||
FlowParserFactory factory = new ClassParserFactory();
|
||||
Class<?> clazz = Class.forName(className);
|
||||
if (ClassXmlFlowParser.class.isAssignableFrom(clazz)) {
|
||||
LOG.info("flow info loaded from class config,class={},format type={}", className, TYPE_XML.getType());
|
||||
return factory.createXmlParser(className);
|
||||
}
|
||||
else if (ClassJsonFlowParser.class.isAssignableFrom(clazz)) {
|
||||
LOG.info("flow info loaded from class config,class={},format type={}", className, TYPE_JSON.getType());
|
||||
return factory.createJsonParser(className);
|
||||
}
|
||||
else if (ClassYmlFlowParser.class.isAssignableFrom(clazz)) {
|
||||
LOG.info("flow info loaded from class config,class={},format type={}", className, TYPE_YML.getType());
|
||||
return factory.createYmlParser(className);
|
||||
}
|
||||
else if (ClassXmlFlowELParser.class.isAssignableFrom(clazz)) {
|
||||
LOG.info("flow info loaded from class config with el,class={},format type={}", className, TYPE_EL_XML.getType());
|
||||
return factory.createXmlELParser(className);
|
||||
}
|
||||
else if (ClassJsonFlowELParser.class.isAssignableFrom(clazz)) {
|
||||
LOG.info("flow info loaded from class config with el,class={},format type={}", className, TYPE_EL_JSON.getType());
|
||||
return factory.createJsonELParser(className);
|
||||
}
|
||||
else if (ClassYmlFlowELParser.class.isAssignableFrom(clazz)) {
|
||||
LOG.info("flow info loaded from class config with el,class={},format type={}", className, TYPE_EL_YML.getType());
|
||||
return factory.createYmlELParser(className);
|
||||
}
|
||||
// 自定义类必须实现以上实现类,否则报错
|
||||
String errorMsg = StrUtil.format("can't support the format {}", path);
|
||||
throw new ErrorSupportPathException(errorMsg);
|
||||
}
|
||||
else if (isZKConfig(path)) {
|
||||
FlowParserFactory factory = new ZookeeperParserFactory();
|
||||
if (ReUtil.isMatch(FORMAT_XML_CONFIG_REGEX, path)) {
|
||||
LOG.info("flow info loaded from Zookeeper,zkNode={},format type={}", path, TYPE_XML.getType());
|
||||
return factory.createXmlParser(path);
|
||||
}
|
||||
else if (ReUtil.isMatch(FORMAT_JSON_CONFIG_REGEX, path)) {
|
||||
LOG.info("flow info loaded from Zookeeper,zkNode={},format type={}", path, TYPE_JSON.getType());
|
||||
return factory.createJsonParser(path);
|
||||
}
|
||||
else if (ReUtil.isMatch(FORMAT_YML_CONFIG_REGEX, path)) {
|
||||
LOG.info("flow info loaded from Zookeeper,zkNode={},format type={}", path, TYPE_YML.getType());
|
||||
return factory.createYmlParser(path);
|
||||
}
|
||||
else if (ReUtil.isMatch(FORMAT_EL_XML_CONFIG_REGEX, path)) {
|
||||
LOG.info("flow info loaded from Zookeeper with el,zkNode={},format type={}", path, TYPE_EL_XML.getType());
|
||||
return factory.createXmlELParser(path);
|
||||
}
|
||||
else if (ReUtil.isMatch(FORMAT_EL_YML_CONFIG_REGEX, path)) {
|
||||
LOG.info("flow info loaded from Zookeeper with el,zkNode={},format type={}", path, TYPE_EL_YML.getType());
|
||||
return factory.createYmlELParser(path);
|
||||
}
|
||||
else if (ReUtil.isMatch(FORMAT_EL_JSON_CONFIG_REGEX, path)) {
|
||||
LOG.info("flow info loaded from Zookeeper with el,zkNode={},format type={}", path, TYPE_EL_JSON.getType());
|
||||
return factory.createJsonELParser(path);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// not found
|
||||
String errorMsg = StrUtil.format("can't find the parser for path:{}", path);
|
||||
throw new ErrorSupportPathException(errorMsg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判定是否为本地文件
|
||||
*/
|
||||
private static boolean isLocalConfig(String path) {
|
||||
return ReUtil.isMatch(LOCAL_XML_CONFIG_REGEX, path)
|
||||
|| ReUtil.isMatch(LOCAL_JSON_CONFIG_REGEX, path)
|
||||
|| ReUtil.isMatch(LOCAL_YML_CONFIG_REGEX, path)
|
||||
|| ReUtil.isMatch(LOCAL_EL_XML_CONFIG_REGEX, path)
|
||||
|| ReUtil.isMatch(LOCAL_EL_JSON_CONFIG_REGEX, path)
|
||||
|| ReUtil.isMatch(LOCAL_EL_YML_CONFIG_REGEX, path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判定是否为自定义class配置
|
||||
*/
|
||||
private static boolean isClassConfig(String path) {
|
||||
return ReUtil.isMatch(CLASS_CONFIG_REGEX, path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判定是否为zk配置
|
||||
*/
|
||||
private static boolean isZKConfig(String path) {
|
||||
return ReUtil.isMatch(ZK_CONFIG_REGEX, path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.yomahub.liteflow.parser.factory;
|
||||
|
||||
import com.yomahub.liteflow.parser.*;
|
||||
import com.yomahub.liteflow.parser.el.*;
|
||||
|
||||
/**
|
||||
* 本地文件
|
||||
* <p>
|
||||
*
|
||||
* @author junjun
|
||||
*/
|
||||
public class LocalParserFactory implements FlowParserFactory {
|
||||
|
||||
@Override
|
||||
public JsonFlowParser createJsonParser(String path) {
|
||||
return new LocalJsonFlowParser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public XmlFlowParser createXmlParser(String path) {
|
||||
return new LocalXmlFlowParser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public YmlFlowParser createYmlParser(String path) {
|
||||
return new LocalYmlFlowParser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonFlowELParser createJsonELParser(String path) {
|
||||
return new LocalJsonFlowELParser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public XmlFlowELParser createXmlELParser(String path) {
|
||||
return new LocalXmlFlowELParser();
|
||||
}
|
||||
|
||||
@Override
|
||||
public YmlFlowELParser createYmlELParser(String path) {
|
||||
return new LocalYmlFlowELParser();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.yomahub.liteflow.parser.factory;
|
||||
|
||||
import com.yomahub.liteflow.parser.*;
|
||||
import com.yomahub.liteflow.parser.base.BaseJsonFlowParser;
|
||||
import com.yomahub.liteflow.parser.base.BaseXmlFlowParser;
|
||||
import com.yomahub.liteflow.parser.base.BaseYmlFlowParser;
|
||||
import com.yomahub.liteflow.parser.el.*;
|
||||
import com.yomahub.liteflow.property.LiteflowConfigGetter;
|
||||
|
||||
/**
|
||||
* Class文件
|
||||
* <p>
|
||||
*
|
||||
* @author junjun
|
||||
*/
|
||||
public class ZookeeperParserFactory implements FlowParserFactory {
|
||||
|
||||
@Override
|
||||
public BaseJsonFlowParser createJsonParser(String path) {
|
||||
return new ZookeeperJsonFlowParser(LiteflowConfigGetter.get().getZkNode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseXmlFlowParser createXmlParser(String path) {
|
||||
return new ZookeeperXmlFlowParser(LiteflowConfigGetter.get().getZkNode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseYmlFlowParser createYmlParser(String path) {
|
||||
return new ZookeeperYmlFlowParser(LiteflowConfigGetter.get().getZkNode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseJsonFlowParser createJsonELParser(String path) {
|
||||
return new ZookeeperJsonFlowELParser(LiteflowConfigGetter.get().getZkNode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseXmlFlowParser createXmlELParser(String path) {
|
||||
return new ZookeeperXmlFlowELParser(LiteflowConfigGetter.get().getZkNode());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseYmlFlowParser createYmlELParser(String path) {
|
||||
return new ZookeeperYmlFlowELParser(LiteflowConfigGetter.get().getZkNode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.yomahub.liteflow.test.parsecustom;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
import com.yomahub.liteflow.test.BaseTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.junit4.SpringRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* springboot环境的自定义yml parser单元测试
|
||||
* 主要测试自定义配置源类是否能引入springboot中的其他依赖
|
||||
*
|
||||
* @author junjun
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(value = "classpath:/parsecustom/application-custom-yml.properties")
|
||||
@SpringBootTest(classes = CustomParserYmlELSpringbootTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.parsecustom.cmp","com.yomahub.liteflow.test.parsecustom.bean"})
|
||||
public class CustomParserYmlELSpringbootTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
//测试springboot场景的自定义json parser
|
||||
@Test
|
||||
public void testYmlCustomParser() {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "args");
|
||||
Assert.assertTrue(response.isSuccess());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.yomahub.liteflow.test.parsecustom.parser;
|
||||
|
||||
import com.yomahub.liteflow.parser.el.ClassYmlFlowELParser;
|
||||
|
||||
/**
|
||||
* 模拟用户自定义源解析
|
||||
*
|
||||
* @author junjun
|
||||
*/
|
||||
public class CustomYmlFlowParser extends ClassYmlFlowELParser {
|
||||
@Override
|
||||
public String parseCustom() {
|
||||
//模拟自定义解析结果
|
||||
return "flow:\n" +
|
||||
" chain:\n" +
|
||||
" - name: chain1\n" +
|
||||
" value: \"THEN(a, b, c);\"";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
liteflow.rule-source=el_yml:com.yomahub.liteflow.test.parsecustom.parser.CustomYmlFlowParser
|
||||
@@ -15,7 +15,7 @@ public class DCmp extends NodeComponent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
System.out.println("CCmp executed!");
|
||||
System.out.println("DCmp executed!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.yomahub.liteflow.test.parsecustom;
|
||||
|
||||
import com.yomahub.liteflow.core.FlowExecutor;
|
||||
import com.yomahub.liteflow.flow.LiteflowResponse;
|
||||
import com.yomahub.liteflow.slot.DefaultContext;
|
||||
import com.yomahub.liteflow.test.BaseTest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.junit4.SpringRunner;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* springboot环境的自定义xml parser单元测试
|
||||
* 主要测试自定义配置源类是否能引入springboot中的其他依赖
|
||||
* @author bryan.zhang
|
||||
* @since 2.5.7
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestPropertySource(value = "classpath:/parsecustom/application-custom-yml.properties")
|
||||
@SpringBootTest(classes = CustomParserXmlSpringbootTest.class)
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan({"com.yomahub.liteflow.test.parsecustom.cmp","com.yomahub.liteflow.test.parsecustom.bean"})
|
||||
public class CustomParserYmlSpringbootTest extends BaseTest {
|
||||
|
||||
@Resource
|
||||
private FlowExecutor flowExecutor;
|
||||
|
||||
//测试springboot场景的自定义json parser
|
||||
@Test
|
||||
public void testYmlCustomParser() {
|
||||
LiteflowResponse response = flowExecutor.execute2Resp("chain1", "args");
|
||||
Assert.assertTrue(response.isSuccess());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.yomahub.liteflow.test.parsecustom.parser;
|
||||
|
||||
import com.yomahub.liteflow.parser.ClassYmlFlowParser;
|
||||
|
||||
/**
|
||||
* springboot环境的自定义yml parser单元测试
|
||||
* 主要测试自定义配置源类是否能引入springboot中的其他依赖
|
||||
* <p>
|
||||
*
|
||||
* @author junjun
|
||||
*/
|
||||
public class CustomYmlFlowParser extends ClassYmlFlowParser {
|
||||
|
||||
@Override
|
||||
public String parseCustom() {
|
||||
return "flow:\n" +
|
||||
" chain:\n" +
|
||||
" - name: chain1\n" +
|
||||
" condition:\n" +
|
||||
" - type: then\n" +
|
||||
" value: 'a,b,c'";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
liteflow.rule-source=com.yomahub.liteflow.test.parsecustom.parser.CustomYmlFlowParser
|
||||
Reference in New Issue
Block a user