From e649b5415cd7f45fb7e05bc75e915f7588cd4587 Mon Sep 17 00:00:00 2001 From: zendwang Date: Tue, 12 Jul 2022 00:38:57 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E9=AB=98=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E4=B8=8Bgenerate()=E6=96=B9=E6=B3=95=E6=8A=A5requestI?= =?UTF-8?q?dGenerator=E7=A9=BA=E6=8C=87=E9=92=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/flow/id/IdGeneratorHelper.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/id/IdGeneratorHelper.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/id/IdGeneratorHelper.java index e6ead69fa..1354d0044 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/id/IdGeneratorHelper.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/id/IdGeneratorHelper.java @@ -1,6 +1,5 @@ package com.yomahub.liteflow.flow.id; -import cn.hutool.core.util.StrUtil; import com.yomahub.liteflow.exception.RequestIdGeneratorException; import com.yomahub.liteflow.property.LiteflowConfig; import com.yomahub.liteflow.property.LiteflowConfigGetter; @@ -20,7 +19,14 @@ public class IdGeneratorHelper { private volatile static IdGeneratorHelper INSTANCE; private IdGeneratorHelper() { - + LiteflowConfig liteflowConfig = LiteflowConfigGetter.get(); + String requestIdGeneratorClass = liteflowConfig.getRequestIdGeneratorClass(); + try { + Class idGenerateClass = (Class) Class.forName(requestIdGeneratorClass); + requestIdGenerator = ContextAwareHolder.loadContextAware().registerBean(idGenerateClass); + } catch (Exception e) { + throw new RequestIdGeneratorException(e.getMessage()); + } } public static IdGeneratorHelper getInstance() { @@ -30,17 +36,6 @@ public class IdGeneratorHelper { synchronized (IdGeneratorHelper.class) { if (Objects.isNull(INSTANCE)) { INSTANCE = new IdGeneratorHelper(); - LiteflowConfig liteflowConfig = LiteflowConfigGetter.get(); - String requestIdGeneratorClass = liteflowConfig.getRequestIdGeneratorClass(); - if (StrUtil.isBlank(requestIdGeneratorClass)) { - requestIdGenerator = new DefaultRequestIdGenerator(); - } - try { - Class idGenerateClass = (Class) Class.forName(requestIdGeneratorClass); - requestIdGenerator = ContextAwareHolder.loadContextAware().registerBean(idGenerateClass); - } catch (Exception e) { - throw new RequestIdGeneratorException(e.getMessage()); - } } } } From 04a3c9352026739902f655bebe24fc8113ae24dc Mon Sep 17 00:00:00 2001 From: zendwang Date: Tue, 12 Jul 2022 16:13:08 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E9=AB=98=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E4=B8=8Bgenerate()=E6=96=B9=E6=B3=95=E6=8A=A5requestI?= =?UTF-8?q?dGenerator=E7=A9=BA=E6=8C=87=E9=92=88=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yomahub/liteflow/flow/id/IdGeneratorHelper.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/id/IdGeneratorHelper.java b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/id/IdGeneratorHelper.java index 1354d0044..1a548399e 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/flow/id/IdGeneratorHelper.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/flow/id/IdGeneratorHelper.java @@ -14,7 +14,7 @@ import java.util.Objects; */ public class IdGeneratorHelper { - private static RequestIdGenerator requestIdGenerator; + private RequestIdGenerator requestIdGenerator; private volatile static IdGeneratorHelper INSTANCE; @@ -23,7 +23,7 @@ public class IdGeneratorHelper { String requestIdGeneratorClass = liteflowConfig.getRequestIdGeneratorClass(); try { Class idGenerateClass = (Class) Class.forName(requestIdGeneratorClass); - requestIdGenerator = ContextAwareHolder.loadContextAware().registerBean(idGenerateClass); + this.requestIdGenerator = ContextAwareHolder.loadContextAware().registerBean(idGenerateClass); } catch (Exception e) { throw new RequestIdGeneratorException(e.getMessage()); } @@ -44,7 +44,7 @@ public class IdGeneratorHelper { } public String generate() { - return requestIdGenerator.generate(); + return this.requestIdGenerator.generate(); } public void clear(){ From 062609ef78bb837ee1d8e891b3ab062ab9c62269 Mon Sep 17 00:00:00 2001 From: tangkc <1016771049@qq.com> Date: Wed, 7 Sep 2022 16:10:15 +0800 Subject: [PATCH 3/5] =?UTF-8?q?enhancement=20#I5Q3SS=20=E5=AF=B9=E4=BA=8Ee?= =?UTF-8?q?l=E8=A1=A8=E8=BE=BE=E5=BC=8F=E9=87=8C=E7=9A=84=E6=9C=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E7=9A=84=E5=AD=90=E8=A1=A8=E8=BE=BE=E5=BC=8F?= =?UTF-8?q?=E5=92=8C=E6=9C=AA=E6=B3=A8=E5=86=8C=E7=9A=84node=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E7=BC=96=E8=AF=91=E6=97=B6=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../builder/el/LiteFlowChainELBuilder.java | 23 +- .../el/operator/base/BaseOperator.java | 38 +-- .../el/operator/base/OperatorHelper.java | 226 ++++++++++-------- .../exception/DataNofFoundException.java | 32 +++ 4 files changed, 189 insertions(+), 130 deletions(-) create mode 100644 liteflow-core/src/main/java/com/yomahub/liteflow/exception/DataNofFoundException.java diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java index b5698ffb0..a39b733b7 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java @@ -6,6 +6,7 @@ import com.ql.util.express.DefaultContext; import com.ql.util.express.ExpressRunner; import com.ql.util.express.exception.QLException; import com.yomahub.liteflow.builder.el.operator.*; +import com.yomahub.liteflow.exception.DataNofFoundException; import com.yomahub.liteflow.exception.ELParseException; import com.yomahub.liteflow.exception.FlowSystemException; import com.yomahub.liteflow.flow.FlowBus; @@ -14,12 +15,15 @@ import com.yomahub.liteflow.flow.element.Executable; import com.yomahub.liteflow.flow.element.condition.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * Chain基于代码形式的组装器 * EL表达式规则专属组装器 + * * @author Bryan.Zhang * @since 2.8.0 */ @@ -42,7 +46,8 @@ public class LiteFlowChainELBuilder { private final List finallyConditionList; //EL解析引擎 - private final static ExpressRunner EXPRESS_RUNNER = new ExpressRunner();; + private final static ExpressRunner EXPRESS_RUNNER = new ExpressRunner(); + ; static { //初始化QLExpress的Runner @@ -87,13 +92,13 @@ public class LiteFlowChainELBuilder { } public LiteFlowChainELBuilder setEL(String elStr) { - if (StrUtil.isBlank(elStr)){ + if (StrUtil.isBlank(elStr)) { String errMsg = StrUtil.format("no conditionList in this chain[{}]", chain.getChainName()); throw new FlowSystemException(errMsg); } List errorList = new ArrayList<>(); - try{ + try { DefaultContext context = new DefaultContext<>(); //这里一定要先放chain,再放node,因为node优先于chain,所以当重名时,node会覆盖掉chain @@ -112,8 +117,8 @@ public class LiteFlowChainELBuilder { //为什么只寻找第一层,而不往下寻找了呢? //因为这是一个规范,如果在后面的层级中出现pre和finally,语义上也不好理解,所以pre和finally只能定义在第一层 //如果硬是要在后面定义,则执行的时候会忽略,相关代码已做了判断 - for (Executable executable : condition.getExecutableList()){ - if (executable instanceof PreCondition){ + for (Executable executable : condition.getExecutableList()) { + if (executable instanceof PreCondition) { this.preConditionList.add((PreCondition) executable); } else if (executable instanceof FinallyCondition) { this.finallyConditionList.add((FinallyCondition) executable); @@ -123,9 +128,13 @@ public class LiteFlowChainELBuilder { //把主要的condition加入 this.conditionList.add(condition); return this; - }catch (QLException e){ + } catch (QLException e) { + // EL 底层会包装异常,这里是曲线处理 + if (Objects.equals(e.getCause().getMessage(), DataNofFoundException.MSG)) { + throw new ELParseException(String.format("[node/chain is not exist or node/chain not register]elStr=%s", elStr)); + } throw new ELParseException(e.getCause().getMessage()); - }catch (Exception e){ + } catch (Exception e) { throw new ELParseException(e.getMessage()); } } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/base/BaseOperator.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/base/BaseOperator.java index 049e584f8..3488a86cb 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/base/BaseOperator.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/base/BaseOperator.java @@ -12,23 +12,25 @@ import com.yomahub.liteflow.exception.ELParseException; */ public abstract class BaseOperator extends Operator { - @Override - public Object executeInner(Object[] objects) throws Exception { - try { - return buildCondition(objects); - } catch (QLException e) { - throw e; - } catch (Exception e) { - throw new ELParseException("errors occurred in EL parsing"); - } - } + @Override + public Object executeInner(Object[] objects) throws Exception { + try { + // 检查 node 和 chain 是否已经注册 + OperatorHelper.checkNodeAndChainExist(objects); + return buildCondition(objects); + } catch (QLException e) { + throw e; + } catch (Exception e) { + throw new ELParseException("errors occurred in EL parsing"); + } + } - /** - * 构建 EL 条件 - * - * @param objects objects - * @return Condition - * @throws Exception Exception - */ - public abstract Object buildCondition(Object[] objects) throws Exception; + /** + * 构建 EL 条件 + * + * @param objects objects + * @return Condition + * @throws Exception Exception + */ + public abstract Object buildCondition(Object[] objects) throws Exception; } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/base/OperatorHelper.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/base/OperatorHelper.java index 0af7cb399..16edd5a81 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/base/OperatorHelper.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/base/OperatorHelper.java @@ -1,6 +1,9 @@ package com.yomahub.liteflow.builder.el.operator.base; import com.ql.util.express.exception.QLException; +import com.yomahub.liteflow.exception.DataNofFoundException; + +import java.util.Objects; /** * Operator 常用工具类 @@ -10,121 +13,134 @@ import com.ql.util.express.exception.QLException; */ public class OperatorHelper { - /** - * 检查参数数量,不等于1 - * - * @param objects objects - * @throws QLException QLException - */ - public static void checkObjectSizeNeqOne(Object[] objects) throws QLException { - checkObjectSizeNeq(objects, 1); - } + /** + * 检查参数数量,不等于1 + * + * @param objects objects + * @throws QLException QLException + */ + public static void checkObjectSizeNeqOne(Object[] objects) throws QLException { + checkObjectSizeNeq(objects, 1); + } - /** - * 检查参数数量,不等于 size - * - * @param objects objects - * @param size 参数数量 - * @throws QLException QLException - */ - public static void checkObjectSizeNeq(Object[] objects, int size) throws QLException { - checkObjectSizeGtZero(objects); - if (objects.length != size) { - throw new QLException("parameter error"); - } - } + /** + * 检查参数数量,不等于 size + * + * @param objects objects + * @param size 参数数量 + * @throws QLException QLException + */ + public static void checkObjectSizeNeq(Object[] objects, int size) throws QLException { + checkObjectSizeGtZero(objects); + if (objects.length != size) { + throw new QLException("parameter error"); + } + } - /** - * 检查参数数量,大于 0 - * - * @param objects objects - * @throws QLException QLException - */ - public static void checkObjectSizeGtZero(Object[] objects) throws QLException { - if (objects.length == 0) { - throw new QLException("parameter is empty"); - } - } + /** + * 检查参数数量,大于 0 + * + * @param objects objects + * @throws QLException QLException + */ + public static void checkObjectSizeGtZero(Object[] objects) throws QLException { + if (objects.length == 0) { + throw new QLException("parameter is empty"); + } + } - /** - * 检查参数数量,大于 2 - * - * @param objects objects - * @throws QLException QLException - */ - public static void checkObjectSizeGtTwo(Object[] objects) throws QLException { - checkObjectSizeGtZero(objects); - if (objects.length <= 1) { - throw new QLException("parameter error"); - } - } + /** + * 检查参数数量,大于 2 + * + * @param objects objects + * @throws QLException QLException + */ + public static void checkObjectSizeGtTwo(Object[] objects) throws QLException { + checkObjectSizeGtZero(objects); + if (objects.length <= 1) { + throw new QLException("parameter error"); + } + } - /** - * 检查参数数量,等于 2 - * - * @param objects objects - * @throws QLException QLException - */ - public static void checkObjectSizeEqTwo(Object[] objects) throws QLException { - checkObjectSizeEq(objects, 2); - } + /** + * 检查参数数量,等于 2 + * + * @param objects objects + * @throws QLException QLException + */ + public static void checkObjectSizeEqTwo(Object[] objects) throws QLException { + checkObjectSizeEq(objects, 2); + } - /** - * 检查参数数量,等于 3 - * - * @param objects objects - * @throws QLException QLException - */ - public static void checkObjectSizeEqThree(Object[] objects) throws QLException { - checkObjectSizeEq(objects, 3); - } + /** + * 检查参数数量,等于 3 + * + * @param objects objects + * @throws QLException QLException + */ + public static void checkObjectSizeEqThree(Object[] objects) throws QLException { + checkObjectSizeEq(objects, 3); + } - /** - * 检查参数数量,等于 size - * - * @param objects objects - * @param size 参数数量 - * @throws QLException QLException - */ - public static void checkObjectSizeEq(Object[] objects, int size) throws QLException { - checkObjectSizeGtZero(objects); + /** + * 检查参数数量,等于 size + * + * @param objects objects + * @param size 参数数量 + * @throws QLException QLException + */ + public static void checkObjectSizeEq(Object[] objects, int size) throws QLException { + checkObjectSizeGtZero(objects); - if (objects.length != size) { - throw new QLException("parameter error"); - } - } + if (objects.length != size) { + throw new QLException("parameter error"); + } + } - /** - * 检查参数数量,等于 size1 或 size2 - * - * @param objects objects - * @param size1 参数数量1 - * @param size2 参数数量2 - * @throws QLException QLException - */ - public static void checkObjectSizeEq(Object[] objects, int size1, int size2) throws QLException { - checkObjectSizeGtZero(objects); + /** + * 检查参数数量,等于 size1 或 size2 + * + * @param objects objects + * @param size1 参数数量1 + * @param size2 参数数量2 + * @throws QLException QLException + */ + public static void checkObjectSizeEq(Object[] objects, int size1, int size2) throws QLException { + checkObjectSizeGtZero(objects); - if (objects.length != size1 && objects.length != size2) { - throw new QLException("parameter error"); - } - } + if (objects.length != size1 && objects.length != size2) { + throw new QLException("parameter error"); + } + } - /** - * 转换 object 为指定的类型 - * - * @param object object - * @param tClass 指定类型 - * @param 返回类型 - * @return T - * @throws QLException QLException - */ - public static T convert(Object object, Class tClass) throws QLException { - if (tClass.isInstance(object)) { - return (T) object; - } + /** + * 转换 object 为指定的类型 + * + * @param object object + * @param tClass 指定类型 + * @param 返回类型 + * @return T + * @throws QLException QLException + */ + public static T convert(Object object, Class tClass) throws QLException { + if (tClass.isInstance(object)) { + return (T) object; + } - throw new QLException("The caller must be " + tClass.getName() + " item"); - } + throw new QLException("The caller must be " + tClass.getName() + " item"); + } + /** + * 检查 node 和 chain 是否已经注册 + * + * @param objects objects + * @throws QLException QLException + */ + public static void checkNodeAndChainExist(Object[] objects) throws QLException { + for (Object object : objects) { + if (Objects.isNull(object)) { + throw new QLException(DataNofFoundException.MSG); + } + } + } } diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/exception/DataNofFoundException.java b/liteflow-core/src/main/java/com/yomahub/liteflow/exception/DataNofFoundException.java new file mode 100644 index 000000000..e9b44e39a --- /dev/null +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/exception/DataNofFoundException.java @@ -0,0 +1,32 @@ +package com.yomahub.liteflow.exception; + +/** + * @author tangkc + */ +public class DataNofFoundException extends RuntimeException { + public static final String MSG = "DataNofFoundException"; + + private static final long serialVersionUID = 1L; + + /** + * 异常信息 + */ + private String message; + + public DataNofFoundException() { + this.message = MSG; + } + + public DataNofFoundException(String message) { + this.message = message; + } + + @Override + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} From 7b79301f032be5d5b36d82a82524d6d69ae29cbe Mon Sep 17 00:00:00 2001 From: tangkc <1016771049@qq.com> Date: Tue, 13 Sep 2022 14:13:43 +0800 Subject: [PATCH 4/5] =?UTF-8?q?enhancement=20#I5Q3SS=20=E5=AF=B9=E4=BA=8Ee?= =?UTF-8?q?l=E8=A1=A8=E8=BE=BE=E5=BC=8F=E9=87=8C=E7=9A=84=E6=9C=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E7=9A=84=E5=AD=90=E8=A1=A8=E8=BE=BE=E5=BC=8F?= =?UTF-8?q?=E5=92=8C=E6=9C=AA=E6=B3=A8=E5=86=8C=E7=9A=84node=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E7=BC=96=E8=AF=91=E6=97=B6=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java index b56e51306..4a34bbe66 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java @@ -13,8 +13,6 @@ import com.yomahub.liteflow.flow.FlowBus; import com.yomahub.liteflow.flow.element.Chain; import com.yomahub.liteflow.flow.element.Executable; import com.yomahub.liteflow.flow.element.condition.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; From b6925f85c93b39396e8934d08e388d2fbe95d998 Mon Sep 17 00:00:00 2001 From: tangkc <1016771049@qq.com> Date: Tue, 13 Sep 2022 14:15:03 +0800 Subject: [PATCH 5/5] =?UTF-8?q?enhancement=20#I5Q3SS=20=E5=AF=B9=E4=BA=8Ee?= =?UTF-8?q?l=E8=A1=A8=E8=BE=BE=E5=BC=8F=E9=87=8C=E7=9A=84=E6=9C=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E7=9A=84=E5=AD=90=E8=A1=A8=E8=BE=BE=E5=BC=8F?= =?UTF-8?q?=E5=92=8C=E6=9C=AA=E6=B3=A8=E5=86=8C=E7=9A=84node=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E7=BC=96=E8=AF=91=E6=97=B6=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java | 1 - 1 file changed, 1 deletion(-) diff --git a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java index 4a34bbe66..18afa290d 100644 --- a/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java +++ b/liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/LiteFlowChainELBuilder.java @@ -43,7 +43,6 @@ public class LiteFlowChainELBuilder { //EL解析引擎 private final static ExpressRunner EXPRESS_RUNNER = new ExpressRunner(); - ; static { //初始化QLExpress的Runner