From 07bdd4703fafa3e040ddb650946385e9917d0c93 Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Wed, 22 Feb 2023 23:17:25 +0800 Subject: [PATCH 01/24] =?UTF-8?q?qinluo:=20=20=20=20=20=20-=20=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E8=B7=9F=E8=B8=AA=E4=BC=98=E5=8C=96=20=20=20=20=20=20?= =?UTF-8?q?-=20listener=E4=BC=98=E5=8C=96=20=20=20=20=20=20-=20=E9=93=BE?= =?UTF-8?q?=E8=B7=AF=E4=B8=8A=E6=8A=A5=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../smartboot/flow/core/EngineContext.java | 6 +- .../flow/core/ExecutionListener.java | 49 +++++++++++++-- .../flow/core/ExecutionListenerSupport.java | 2 +- .../flow/core/ExecutionListeners.java | 4 +- .../org/smartboot/flow/core/Pipeline.java | 20 ++++-- .../flow/core/component/AdapterComponent.java | 13 +--- .../flow/core/component/ChooseComponent.java | 54 +++++++--------- .../flow/core/component/Component.java | 6 ++ .../flow/core/component/IfComponent.java | 35 ++++------- .../core/component/PipelineComponent.java | 7 +-- .../core/executable/ExecutableAdapter.java | 12 +--- .../smartboot/flow/core/invoker/Invoker.java | 2 +- .../flow/core/invoker/WrappedInvoker.java | 62 +++++++++++++++++++ .../core/parser/AbstractElementParser.java | 1 + .../flow/manager/ManagerConfiguration.java | 14 +++++ .../metric/MetricExecutionListener.java | 4 +- .../trace/ManagerExecutionListener.java | 4 +- .../flow/manager/trace/TraceReporter.java | 29 +++++++-- 18 files changed, 218 insertions(+), 106 deletions(-) create mode 100644 smart-flow-core/src/main/java/org/smartboot/flow/core/invoker/WrappedInvoker.java create mode 100644 smart-flow-manager/src/main/java/org/smartboot/flow/manager/ManagerConfiguration.java diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/EngineContext.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/EngineContext.java index 5d4b2b7..e41c913 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/EngineContext.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/EngineContext.java @@ -218,9 +218,13 @@ public class EngineContext{ } public void exit(Object obj) { + exit(obj, null); + } + + public void exit(Object obj, Throwable ex) { this.tracer.exit(); if (executing == EXECUTING) { - listener.afterExecute(this, obj); + listener.afterExecute(this, obj, ex); } else if (executing == ROLLBACK) { listener.afterRollback(this, obj); } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListener.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListener.java index d9b624f..6d697c7 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListener.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListener.java @@ -7,11 +7,48 @@ package org.smartboot.flow.core; */ public interface ExecutionListener { - void start(EngineContext context); - void completed(EngineContext context); + /** + * 开始执行 + * @param ctx ctx 上下文 + */ + void start(EngineContext ctx); - void beforeExecute(EngineContext context, Object object); - void afterExecute(EngineContext context, Object object); - void beforeRollback(EngineContext context, Object object); - void afterRollback(EngineContext context, Object object); + /** + * 结束执行 + * @param ctx ctx 上下文 + */ + void completed(EngineContext ctx); + + /** + * 开始执行组件分支 + * + * @param ctx ctx 上下文 + * @param object 组件/分支 + */ + void beforeExecute(EngineContext ctx, Object object); + + /** + * 结束执行组件分支 + * + * @param ctx ctx 上下文 + * @param object 组件/分支 + * @param ex 执行错误信息 + */ + void afterExecute(EngineContext ctx, Object object, Throwable ex); + + /** + * 开始回滚组件分支 + * + * @param ctx ctx 上下文 + * @param object 组件/分支 + */ + void beforeRollback(EngineContext ctx, Object object); + + /** + * 结束执行组件分支 + * + * @param ctx ctx 上下文 + * @param object 组件/分支 + */ + void afterRollback(EngineContext ctx, Object object); } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListenerSupport.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListenerSupport.java index c502bc0..a386172 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListenerSupport.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListenerSupport.java @@ -23,7 +23,7 @@ public class ExecutionListenerSupport implements ExecutionListener { } @Override - public void afterExecute(EngineContext context, Object object) { + public void afterExecute(EngineContext context, Object object, Throwable ex) { } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListeners.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListeners.java index e4eb249..5b89a16 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListeners.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/ExecutionListeners.java @@ -54,10 +54,10 @@ public class ExecutionListeners implements ExecutionListener { } @Override - public void afterExecute(EngineContext context, Object object) { + public void afterExecute(EngineContext context, Object object, Throwable ex) { for (ExecutionListener listener : listeners) { try { - listener.afterExecute(context, object); + listener.afterExecute(context, object, ex); } catch (Throwable e) { LOGGER.warn("execute listener {} failed", listener.getClass().getName(), e); } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/Pipeline.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/Pipeline.java index 52b8505..48a8333 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/Pipeline.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/Pipeline.java @@ -5,7 +5,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.smartboot.flow.core.component.Component; import org.smartboot.flow.core.invoker.InvokeListener; +import org.smartboot.flow.core.invoker.WrappedInvoker; import org.smartboot.flow.core.util.AssertUtil; +import org.smartboot.flow.core.util.AuxiliaryUtils; import org.smartboot.flow.core.visitor.ComponentVisitor; import org.smartboot.flow.core.visitor.PipelineVisitor; @@ -41,7 +43,9 @@ public class Pipeline implements Rollback, Describable, Validator, M context.putExt(Key.of(this), executed); // Enter record track - context.enter(this); + if (!AuxiliaryUtils.isAnonymous(name)) { + context.enter(this); + } for (Component component : components) { if (context.isBroken()) { @@ -60,7 +64,9 @@ public class Pipeline implements Rollback, Describable, Validator, M } // Exit record track - context.exit(this); + if (!AuxiliaryUtils.isAnonymous(name)) { + context.exit(this); + } context.ensureFinished(); } @@ -72,19 +78,23 @@ public class Pipeline implements Rollback, Describable, Validator, M return; } - context.enter(this); + if (!AuxiliaryUtils.isAnonymous(name)) { + context.enter(this); + } // Execute rollback desc. for (int i = executed.size() - 1; i >= 0; i--) { Component component = executed.get(i); try { - component.rollback(context); + WrappedInvoker.rollback(context, component); } catch (Exception e) { LOGGER.error("{} rollback failed {}", this.name, component.describe()); } } - context.exit(this); + if (!AuxiliaryUtils.isAnonymous(name)) { + context.exit(this); + } } public boolean isRollbackable(EngineContext context) { diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/AdapterComponent.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/AdapterComponent.java index 07ddbf1..c7c6345 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/AdapterComponent.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/AdapterComponent.java @@ -5,6 +5,7 @@ import org.smartboot.flow.core.EngineContext; import org.smartboot.flow.core.Key; import org.smartboot.flow.core.common.ComponentType; import org.smartboot.flow.core.common.Pair; +import org.smartboot.flow.core.invoker.WrappedInvoker; import org.smartboot.flow.core.util.AssertUtil; import org.smartboot.flow.core.visitor.ComponentVisitor; @@ -45,7 +46,7 @@ public class AdapterComponent extends Component { if (newContext == null) { return; } - component.rollback(newContext); + WrappedInvoker.rollback(newContext, component); } @Override @@ -62,8 +63,6 @@ public class AdapterComponent extends Component { @Override public int invoke(EngineContext context) throws Throwable { - context.enter(this); - // Convert. Pair pair = adapter.before(context); AssertUtil.notNull(pair, "adapter[" + getName() + "] result must not be null"); @@ -79,13 +78,7 @@ public class AdapterComponent extends Component { // Store converted objects. context.putExt(Key.of(this), newContext); - int invoke; - try { - invoke = component.invoke(newContext); - } finally { - context.exit(this); - } - + int invoke = WrappedInvoker.invoke(newContext, component); // Apply result to parent context. adapter.after(context, newContext); return invoke; diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/ChooseComponent.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/ChooseComponent.java index 862af6d..b145564 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/ChooseComponent.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/ChooseComponent.java @@ -5,6 +5,7 @@ import org.smartboot.flow.core.Condition; import org.smartboot.flow.core.EngineContext; import org.smartboot.flow.core.Key; import org.smartboot.flow.core.common.ComponentType; +import org.smartboot.flow.core.invoker.WrappedInvoker; import org.smartboot.flow.core.util.AssertUtil; import org.smartboot.flow.core.visitor.ComponentVisitor; import org.smartboot.flow.core.visitor.ConditionVisitor; @@ -50,34 +51,29 @@ public class ChooseComponent extends Component { @Override public int invoke(EngineContext context) throws Throwable { - context.enter(this); - try { - Object branch = condition.test(context); - Component execute = null; - - // Compatible - if (branch != null && allBranchWasString) { - branch = String.valueOf(branch); - } + Object branch = condition.test(context); + Component execute = null; - if (branch != null && branches.containsKey(branch)) { - execute = branches.get(branch); - } else if (defaultBranch != null) { - execute = defaultBranch; - branch = "default"; - } + // Compatible + if (branch != null && allBranchWasString) { + branch = String.valueOf(branch); + } - if (execute != null) { - context.putExt(Key.of(this), execute); - context.enter("branch##" + branch); - try { - execute.invoke(context); - } finally { - context.exit("branch##" + branch); - } + if (branch != null && branches.containsKey(branch)) { + execute = branches.get(branch); + } else if (defaultBranch != null) { + execute = defaultBranch; + branch = "default"; + } + + if (execute != null) { + context.putExt(Key.of(this), execute); + context.enter("branch##" + branch); + try { + WrappedInvoker.invoke(context, execute); + } finally { + context.exit("branch##" + branch); } - } finally { - context.exit(this); } return 1; @@ -96,13 +92,7 @@ public class ChooseComponent extends Component { return; } - context.enter(this); - try { - executed.rollback(context); - } finally { - context.exit(this); - } - + WrappedInvoker.rollback(context, executed); } @Override diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/Component.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/Component.java index 32363ec..0bb905c 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/Component.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/Component.java @@ -114,6 +114,12 @@ public abstract class Component implements Rollback, Describable, Va public abstract int invoke(EngineContext context) throws Throwable; + /** + * Check current is rollbackable. + * + * @param context ctx + * @return true/false + */ public boolean isRollbackable(EngineContext context) { return rollback; } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/IfComponent.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/IfComponent.java index 89369f2..99d24f1 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/IfComponent.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/IfComponent.java @@ -5,6 +5,7 @@ import org.smartboot.flow.core.Condition; import org.smartboot.flow.core.EngineContext; import org.smartboot.flow.core.Key; import org.smartboot.flow.core.common.ComponentType; +import org.smartboot.flow.core.invoker.WrappedInvoker; import org.smartboot.flow.core.util.AssertUtil; import org.smartboot.flow.core.visitor.ComponentVisitor; import org.smartboot.flow.core.visitor.ConditionVisitor; @@ -42,24 +43,19 @@ public class IfComponent extends Component{ @Override public int invoke(EngineContext context) throws Throwable { - context.enter(this); - try { - Object test = condition.test(context); - Component execute = null; - - if (test != null && Boolean.parseBoolean(String.valueOf(test))) { - execute = this.thenComponent; - } else if (elseComponent != null) { - execute = this.elseComponent; - } + Object test = condition.test(context); + Component execute = null; + if (test != null && Boolean.parseBoolean(String.valueOf(test))) { + execute = this.thenComponent; + } else if (elseComponent != null) { + execute = this.elseComponent; + } - if (execute != null) { - context.putExt(Key.of(this), execute); - execute.invoke(context); - } - } finally { - context.exit(this); + + if (execute != null) { + context.putExt(Key.of(this), execute); + WrappedInvoker.invoke(context, execute); } return 1; @@ -78,12 +74,7 @@ public class IfComponent extends Component{ return; } - context.enter(this); - try { - executed.rollback(context); - } finally { - context.exit(this); - } + WrappedInvoker.rollback(context, executed); } @Override diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/PipelineComponent.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/PipelineComponent.java index 7564a08..7764932 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/PipelineComponent.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/PipelineComponent.java @@ -5,6 +5,7 @@ import org.smartboot.flow.core.EngineContext; import org.smartboot.flow.core.Pipeline; import org.smartboot.flow.core.common.ComponentType; import org.smartboot.flow.core.util.AssertUtil; +import org.smartboot.flow.core.util.AuxiliaryUtils; import org.smartboot.flow.core.visitor.ComponentVisitor; import org.smartboot.flow.core.visitor.PipelineVisitor; @@ -41,7 +42,7 @@ public class PipelineComponent extends Component { subprocess.apply(); } - if (subprocess.getRollback() && !isAnonymous()) { + if (subprocess.getRollback() && !AuxiliaryUtils.isAnonymous(pipeline.describe())) { subprocess.setExecuting(EngineContext.ROLLBACK); this.rollback(subprocess); } @@ -53,10 +54,6 @@ public class PipelineComponent extends Component { return 1; } - private boolean isAnonymous() { - return pipeline.describe().contains("anonymous-pipeline"); - } - @Override public boolean isRollbackable(EngineContext context) { return pipeline.isRollbackable(context); diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/executable/ExecutableAdapter.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/executable/ExecutableAdapter.java index e11d123..f6dfadd 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/executable/ExecutableAdapter.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/executable/ExecutableAdapter.java @@ -32,8 +32,6 @@ public class ExecutableAdapter extends Component implements Rollback< @Override public int invoke(EngineContext context) { - context.enter(this); - try { executable.execute(context); } catch (Throwable e) { @@ -49,8 +47,6 @@ public class ExecutableAdapter extends Component implements Rollback< ((DegradeCallback) executable).doWithDegrade(context, e); } - } finally { - context.exit(this); } return 1; @@ -59,13 +55,7 @@ public class ExecutableAdapter extends Component implements Rollback< @Override public void rollback(EngineContext context) { - context.enter(this); - - try { - executable.rollback(context); - } finally { - context.exit(this); - } + executable.rollback(context); } @Override diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/invoker/Invoker.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/invoker/Invoker.java index 51ac717..cc0b094 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/invoker/Invoker.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/invoker/Invoker.java @@ -63,7 +63,7 @@ public class Invoker { Throwable fatal = null; try { - component.invoke(context); + WrappedInvoker.invoke(context, component); } catch (Throwable e) { fatal = e; } finally { diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/invoker/WrappedInvoker.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/invoker/WrappedInvoker.java new file mode 100644 index 0000000..ccfd059 --- /dev/null +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/invoker/WrappedInvoker.java @@ -0,0 +1,62 @@ +package org.smartboot.flow.core.invoker; + +import org.smartboot.flow.core.EngineContext; +import org.smartboot.flow.core.component.Component; +import org.smartboot.flow.core.component.PipelineComponent; + +/** + * @author qinluo + * @date 2023/2/20 22:06 + * @since 1.0.0 + */ +public class WrappedInvoker { + + public static int invoke(EngineContext context, Component component) throws Throwable { + delegateEnter(context, component); + + Throwable e = null; + + try { + return component.invoke(context); + } catch (Throwable ex) { + e = ex; + throw ex; + } finally { + delegateExit(context, component, e); + } + } + + public static void rollback(EngineContext context, Component component) { + // Enter + delegateEnter(context, component); + + Throwable e = null; + + try { + component.rollback(context); + } catch (Throwable ex) { + e = ex; + throw ex; + } finally { + delegateExit(context, component, e); + } + } + + private static void delegateEnter(EngineContext context, Component component) { + boolean isSubprocess = component instanceof PipelineComponent; + + if (!isSubprocess) { + // Enter + context.enter(component); + } + } + + private static void delegateExit(EngineContext context, Component component, Throwable ex) { + boolean isSubprocess = component instanceof PipelineComponent; + + if (!isSubprocess) { + // Enter + context.exit(component, ex); + } + } +} diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/AbstractElementParser.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/AbstractElementParser.java index f44d2f8..ad4454c 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/AbstractElementParser.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/AbstractElementParser.java @@ -73,6 +73,7 @@ public abstract class AbstractElementParser implements ElementParser { // Wrap as pipeline. PipelineDefinition pipelineDef = new PipelineDefinition(); + pipelineDef.setName(pipelineIdentifier); pipelineDef.setIdentifier(pipelineIdentifier); diff --git a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/ManagerConfiguration.java b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/ManagerConfiguration.java new file mode 100644 index 0000000..4f5ec79 --- /dev/null +++ b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/ManagerConfiguration.java @@ -0,0 +1,14 @@ +package org.smartboot.flow.manager; + +/** + * @author qinluo + * @date 2023/1/30 22:43 + * @since 1.0.7 + */ +public class ManagerConfiguration { + + /** + * 异常信息上报最大堆栈神对,默认10 + */ + public static int reportMaxStackDepth = 10; +} diff --git a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/metric/MetricExecutionListener.java b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/metric/MetricExecutionListener.java index b27d33b..3a6e275 100644 --- a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/metric/MetricExecutionListener.java +++ b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/metric/MetricExecutionListener.java @@ -46,7 +46,7 @@ public class MetricExecutionListener extends ExecutionListenerSupport { } @Override - public void afterExecute(EngineContext context, Object object) { + public void afterExecute(EngineContext context, Object object, Throwable ex) { if (!(object instanceof Measurable)) { return; } @@ -68,7 +68,7 @@ public class MetricExecutionListener extends ExecutionListenerSupport { } DefaultMetrics metrics = (DefaultMetrics) m; - if (context.getFatal() != null) { + if (ex != null) { metrics.addMetric(NamedMetrics.FAIL, 1); } long now = System.currentTimeMillis(); diff --git a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/trace/ManagerExecutionListener.java b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/trace/ManagerExecutionListener.java index fcfaba7..b583392 100644 --- a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/trace/ManagerExecutionListener.java +++ b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/trace/ManagerExecutionListener.java @@ -58,7 +58,7 @@ public class ManagerExecutionListener extends ExecutionListenerSupport { } @Override - public void afterExecute(EngineContext context, Object object) { + public void afterExecute(EngineContext context, Object object, Throwable ex) { TraceData trace = context.getExt(TRACES); if (trace == null) { return; @@ -74,7 +74,7 @@ public class ManagerExecutionListener extends ExecutionListenerSupport { } data.setEscape(System.currentTimeMillis() - data.getStart()); - data.setEx(context.getFatal()); + data.setEx(ex); } @Override diff --git a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/trace/TraceReporter.java b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/trace/TraceReporter.java index c628c86..42e28c3 100644 --- a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/trace/TraceReporter.java +++ b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/trace/TraceReporter.java @@ -13,6 +13,7 @@ import org.smartboot.flow.core.util.AssertUtil; import org.smartboot.flow.manager.FlatEngine; import org.smartboot.flow.manager.FlatManager; import org.smartboot.flow.manager.HostUtils; +import org.smartboot.flow.manager.ManagerConfiguration; import org.smartboot.flow.manager.ManagerConstants; import org.smartboot.flow.manager.NamedThreadFactory; import org.smartboot.flow.manager.UpdateContentTask; @@ -102,11 +103,7 @@ public class TraceReporter { request.setEngineName(trace.getEngineName()); request.setSuccess(trace.getEx() == null); if (!request.getSuccess()) { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - PrintWriter writer = new PrintWriter(bos); - trace.getEx().printStackTrace(writer); - writer.flush(); - request.setEx(bos.toString()); + request.setEx(serialExToString(trace.getEx())); } if (flatEngine.getReportContent()) { @@ -121,7 +118,7 @@ public class TraceReporter { item.put("type", k.getType()); item.put("start", v.getStart()); item.put("escape", v.getEscape()); - item.put("ex", v.getEx()); + item.put("ex", serialExToString(v.getEx())); item.put("rollbackStart", v.getRollbackStart()); item.put("rollbackEnd", v.getRollbackEnd()); @@ -151,6 +148,26 @@ public class TraceReporter { } } + private String serialExToString(Throwable ex) { + if (ex == null) { + return null; + } + + int maxDepth = ManagerConfiguration.reportMaxStackDepth; + StackTraceElement[] stackTrace = ex.getStackTrace(); + if (stackTrace.length > maxDepth) { + StackTraceElement[] newTrace = new StackTraceElement[maxDepth]; + System.arraycopy(stackTrace, 0, newTrace, 0, newTrace.length); + ex.setStackTrace(newTrace); + } + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + PrintWriter writer = new PrintWriter(bos); + ex.printStackTrace(writer); + writer.flush(); + return bos.toString(); + } + public void start() { URL parsedUrl; try { -- Gitee From 476ae53c4d40a00326b1409e7fdaa5f2b83ddc03 Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Sat, 25 Feb 2023 00:52:36 +0800 Subject: [PATCH 02/24] =?UTF-8?q?qinluo:=20=20=20=20=20=20-=20http?= =?UTF-8?q?=E4=B8=8A=E6=8A=A5=E7=BB=9F=E8=AE=A1=E6=95=B0=E6=8D=AE=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flow/manager/metric/DefaultMetrics.java | 14 +++++++++++++- .../flow/manager/report/HttpReporter.java | 5 +---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/metric/DefaultMetrics.java b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/metric/DefaultMetrics.java index 99864e8..ecd25c0 100644 --- a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/metric/DefaultMetrics.java +++ b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/metric/DefaultMetrics.java @@ -1,5 +1,6 @@ package org.smartboot.flow.manager.metric; +import com.alibaba.fastjson.JSONObject; import org.smartboot.flow.core.metrics.Metrics; import org.smartboot.flow.core.util.AssertUtil; @@ -14,7 +15,8 @@ import java.util.concurrent.ConcurrentHashMap; */ public class DefaultMetrics extends Metrics { - private final Map COUNTERS = new ConcurrentHashMap<>(); + protected final Map COUNTERS = new ConcurrentHashMap<>(); + protected long started = System.currentTimeMillis(); public void addMetric(String name, long value) { this.addMetric(MetricKind.ACCUMULATE, name, value); @@ -43,5 +45,15 @@ public class DefaultMetrics extends Metrics { @Override public void reset() { COUNTERS.forEach((k, v) -> v.reset()); + this.started = System.currentTimeMillis(); + } + + @Override + public String toString() { + JSONObject metrics = new JSONObject(); + metrics.put("started", started); + COUNTERS.forEach((k, v) -> metrics.put(k, v.getValue())); + + return metrics.toJSONString(); } } diff --git a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/report/HttpReporter.java b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/report/HttpReporter.java index 847a9b2..4ac7dbc 100644 --- a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/report/HttpReporter.java +++ b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/report/HttpReporter.java @@ -22,9 +22,7 @@ import org.smartboot.flow.manager.FlatManager; import org.smartboot.flow.manager.HostUtils; import org.smartboot.flow.manager.ManagerConstants; import org.smartboot.flow.manager.UpdateContentTask; -import org.smartboot.flow.manager.metric.Counter; import org.smartboot.flow.manager.metric.DefaultMetrics; -import org.smartboot.flow.manager.metric.MaxCounter; import org.smartboot.flow.manager.metric.MetricExecutionListener; import org.smartboot.http.client.HttpClient; import org.smartboot.http.client.HttpPost; @@ -46,8 +44,7 @@ public class HttpReporter extends AbstractReporter { private static final SerializeConfig SC = new SerializeConfig(); static { - SC.put(Counter.class, (jsonSerializer, o, o1, type, i) -> jsonSerializer.write(o.toString())); - SC.put(MaxCounter.class, (jsonSerializer, o, o1, type, i) -> jsonSerializer.write(o.toString())); + SC.put(DefaultMetrics.class, (jsonSerializer, o, o1, type, i) -> jsonSerializer.write(o.toString())); } /** -- Gitee From c7b904486fb8ef821d77d5996b6be2b1249597d4 Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Sat, 11 Mar 2023 21:51:25 +0800 Subject: [PATCH 03/24] qinluo: 1.0.8-SNAPSHOT --- pom.xml | 4 ++-- smart-flow-admin/pom.xml | 2 +- smart-flow-core/pom.xml | 2 +- smart-flow-helper/pom.xml | 2 +- smart-flow-manager/pom.xml | 2 +- smart-flow-script-condition/pom.xml | 2 +- smart-flow-script-condition/smart-flow-script-groovy/pom.xml | 2 +- smart-flow-script-condition/smart-flow-script-ognl/pom.xml | 2 +- .../smart-flow-script-qlexpress/pom.xml | 2 +- smart-flow-spring-extension/pom.xml | 2 +- smart-flow-springboot-starter/pom.xml | 2 +- 11 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 71a7d69..97c2082 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.smartboot.flow smart-flow-parent - 1.0.7 + 1.0.8-SNAPSHOT 4.0.0 pom smart-flow @@ -22,7 +22,7 @@ - 1.0.7 + 1.0.8-SNAPSHOT https://gitee.com/smartboot/smart-flow diff --git a/smart-flow-admin/pom.xml b/smart-flow-admin/pom.xml index be362f6..ff4e60b 100644 --- a/smart-flow-admin/pom.xml +++ b/smart-flow-admin/pom.xml @@ -5,7 +5,7 @@ smart-flow-parent org.smartboot.flow - 1.0.7 + 1.0.8-SNAPSHOT 4.0.0 diff --git a/smart-flow-core/pom.xml b/smart-flow-core/pom.xml index 55e79d2..545ffaf 100644 --- a/smart-flow-core/pom.xml +++ b/smart-flow-core/pom.xml @@ -5,7 +5,7 @@ org.smartboot.flow smart-flow-parent - 1.0.7 + 1.0.8-SNAPSHOT 4.0.0 diff --git a/smart-flow-helper/pom.xml b/smart-flow-helper/pom.xml index f341413..745d118 100644 --- a/smart-flow-helper/pom.xml +++ b/smart-flow-helper/pom.xml @@ -5,7 +5,7 @@ smart-flow-parent org.smartboot.flow - 1.0.7 + 1.0.8-SNAPSHOT 4.0.0 diff --git a/smart-flow-manager/pom.xml b/smart-flow-manager/pom.xml index f9e713f..276234a 100644 --- a/smart-flow-manager/pom.xml +++ b/smart-flow-manager/pom.xml @@ -5,7 +5,7 @@ smart-flow-parent org.smartboot.flow - 1.0.7 + 1.0.8-SNAPSHOT 4.0.0 diff --git a/smart-flow-script-condition/pom.xml b/smart-flow-script-condition/pom.xml index 16c47f0..5075b87 100644 --- a/smart-flow-script-condition/pom.xml +++ b/smart-flow-script-condition/pom.xml @@ -5,7 +5,7 @@ smart-flow-parent org.smartboot.flow - 1.0.7 + 1.0.8-SNAPSHOT 4.0.0 pom diff --git a/smart-flow-script-condition/smart-flow-script-groovy/pom.xml b/smart-flow-script-condition/smart-flow-script-groovy/pom.xml index 1bfa394..8446091 100644 --- a/smart-flow-script-condition/smart-flow-script-groovy/pom.xml +++ b/smart-flow-script-condition/smart-flow-script-groovy/pom.xml @@ -5,7 +5,7 @@ smart-flow-script-condition org.smartboot.flow - 1.0.7 + 1.0.8-SNAPSHOT 4.0.0 diff --git a/smart-flow-script-condition/smart-flow-script-ognl/pom.xml b/smart-flow-script-condition/smart-flow-script-ognl/pom.xml index 3dc13ca..f4e2d49 100644 --- a/smart-flow-script-condition/smart-flow-script-ognl/pom.xml +++ b/smart-flow-script-condition/smart-flow-script-ognl/pom.xml @@ -5,7 +5,7 @@ smart-flow-script-condition org.smartboot.flow - 1.0.7 + 1.0.8-SNAPSHOT 4.0.0 diff --git a/smart-flow-script-condition/smart-flow-script-qlexpress/pom.xml b/smart-flow-script-condition/smart-flow-script-qlexpress/pom.xml index d1cc4b4..35abfbd 100644 --- a/smart-flow-script-condition/smart-flow-script-qlexpress/pom.xml +++ b/smart-flow-script-condition/smart-flow-script-qlexpress/pom.xml @@ -5,7 +5,7 @@ smart-flow-script-condition org.smartboot.flow - 1.0.7 + 1.0.8-SNAPSHOT 4.0.0 diff --git a/smart-flow-spring-extension/pom.xml b/smart-flow-spring-extension/pom.xml index 1c04dbb..51daf96 100644 --- a/smart-flow-spring-extension/pom.xml +++ b/smart-flow-spring-extension/pom.xml @@ -5,7 +5,7 @@ smart-flow-parent org.smartboot.flow - 1.0.7 + 1.0.8-SNAPSHOT 4.0.0 diff --git a/smart-flow-springboot-starter/pom.xml b/smart-flow-springboot-starter/pom.xml index 6b0a46f..b6563bf 100644 --- a/smart-flow-springboot-starter/pom.xml +++ b/smart-flow-springboot-starter/pom.xml @@ -5,7 +5,7 @@ smart-flow-parent org.smartboot.flow - 1.0.7 + 1.0.8-SNAPSHOT 4.0.0 -- Gitee From 63420ee3f1ef7d0f524b69f8fc2a10f51714e5e2 Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Sun, 12 Mar 2023 00:09:57 +0800 Subject: [PATCH 04/24] =?UTF-8?q?qinluo:=20=E5=A4=96=E7=BD=AE=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/parser/BuilderDefinitionVisitor.java | 4 +- .../flow/core/parser/DefaultParser.java | 8 + .../flow/core/parser/ScriptLoader.java | 205 ++++++++++++++++++ .../flow/core/util/AuxiliaryUtils.java | 18 ++ .../flow/manager/trace/TraceReporter.java | 20 -- 5 files changed, 233 insertions(+), 22 deletions(-) create mode 100644 smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ScriptLoader.java diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/BuilderDefinitionVisitor.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/BuilderDefinitionVisitor.java index 7049ef1..3a30e0a 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/BuilderDefinitionVisitor.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/BuilderDefinitionVisitor.java @@ -64,9 +64,9 @@ public class BuilderDefinitionVisitor implements DefinitionVisitor { Class javaType = sed.getJavaType(); ScriptCondition condition; if (javaType != null) { - condition = objectCreator.create(javaType.getName(), ScriptCondition.class, true); + condition = objectCreator.create(javaType.getName(), ScriptCondition.class, false); } else { - condition = objectCreator.create(sed.getType(), ScriptCondition.class, true); + condition = objectCreator.create(sed.getType(), ScriptCondition.class, false); } condition.setName(sed.getName()); diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/DefaultParser.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/DefaultParser.java index dbdfdf0..9d76a3b 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/DefaultParser.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/DefaultParser.java @@ -32,6 +32,11 @@ public class DefaultParser implements Parser { private final ParserContext context = new ParserContext(); private ObjectCreator objectCreator = DefaultObjectCreator.getInstance(); private boolean assemble = true; + private final ScriptLoader scriptLoader = new ScriptLoader(); + + public ScriptLoader scriptLoader() { + return this.scriptLoader; + } @Override public void parse(InputStream is, InputStream... streams) { @@ -67,6 +72,9 @@ public class DefaultParser implements Parser { useCache = useCache || Boolean.parseBoolean(root.getAttribute("useCache")); } + // Load script in locations. + scriptLoader.load(context); + if (assemble) { this.visitor = new BuilderDefinitionVisitor(useCache, objectCreator); context.getRegistered().forEach(ElementDefinition::validate); diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ScriptLoader.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ScriptLoader.java new file mode 100644 index 0000000..a6f9619 --- /dev/null +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ScriptLoader.java @@ -0,0 +1,205 @@ +package org.smartboot.flow.core.parser; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.smartboot.flow.core.parser.definition.ScriptDefinition; +import org.smartboot.flow.core.util.AuxiliaryUtils; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.HashSet; +import java.util.Set; + +/** + * @author qinluo + * @date 2023/3/11 21:53 + * @since 1.0.0 + */ +public class ScriptLoader { + + private static final Logger LOGGER = LoggerFactory.getLogger(ScriptLoader.class); + + /** + * Script locations + */ + private final Set locations = new HashSet<>(); + + /** + * Accept file extensions. + */ + private final Set acceptExtensions = new HashSet<>(); + + /** + * Exclude file extensions. + */ + private final Set excludeExtensions = new HashSet<>(); + + public ScriptLoader() { + this.exclude("xml", "properties"); + } + + public void locations(String ...fileLocations) { + if (fileLocations == null) { + return; + } + + for (String fileLocation : fileLocations) { + if (fileLocation == null) { + continue; + } + + File file = new File(fileLocation); + if (!file.exists()) { + file = null; + URL resource = this.getClass().getResource(fileLocation); + + if (resource == null && !fileLocation.startsWith("/")) { + resource = this.getClass().getResource("/" + fileLocation); + } + + if (resource != null) { + file = new File(resource.getPath()); + } + } + + if (file == null || !file.exists() || !file.canRead()) { + continue; + } + + locations.add(file); + } + } + + public void locations(File ...fileLocations) { + if (fileLocations == null) { + return; + } + + for (File file : fileLocations) { + if (file == null || !file.exists() || !file.canRead()) { + continue; + } + + locations.add(file); + } + } + + public void accept(String ...extensions) { + this.acceptExtensions.addAll(AuxiliaryUtils.asList(extensions)); + } + + public void exclude(String ...extensions) { + this.excludeExtensions.addAll(AuxiliaryUtils.asList(extensions)); + } + + /** + * Load additional script from specified locations. + * + * @param ctx ctx. + */ + public void load(ParserContext ctx) { + // foreach locations + Set visitedLocations = new HashSet<>(locations.size()); + for (File file : locations) { + if (file.isFile() && !accept(file)) { + continue; + } + + // Found duplicated file in locations. + if (!visitedLocations.add(file.getAbsolutePath())) { + LOGGER.info("duplicated location {}", file.getAbsolutePath()); + continue; + } + + if (file.isFile()) { + doReadAndRegister(file, ctx); + } else if (file.isDirectory()){ + doReadDirectory(file, ctx); + } + } + + this.clear(); + } + + private void doReadDirectory(File file, ParserContext ctx) { + File[] files = file.listFiles(); + if (files == null) { + return; + } + + for (File f : files) { + if (f.isFile() && !accept(f)) { + continue; + } + + if (f.isFile()) { + doReadAndRegister(f, ctx); + } else if (f.isDirectory()){ + doReadDirectory(f, ctx); + } + } + } + + private void doReadAndRegister(File file, ParserContext ctx) { + String type = getExtension(file); + String name = file.getName().substring(0, file.getName().lastIndexOf(".")); + + // Additional script has lowest priority. + if (ctx.getRegistered(name) != null) { + return; + } + + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + byte[] buffer = new byte[4096]; + int n; + BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); + while ((n = bis.read(buffer, 0, buffer.length)) != -1) { + bos.write(buffer, 0, n); + } + + String content = new String(bos.toByteArray(), StandardCharsets.UTF_8); + ScriptDefinition def = new ScriptDefinition(); + def.setScript(content); + def.setName(name); + def.setType(type); + def.setIdentifier(name); + ctx.register(def); + } catch (Exception e) { + LOGGER.error("read file {} failed", file.getPath(), e); + } + + } + + private void clear() { + this.locations.clear(); + } + + private boolean accept(File file) { + String extension = getExtension(file); + if (AuxiliaryUtils.isBlank(extension)) { + return false; + } + + if (excludeExtensions.contains(extension)) { + return false; + } + + return acceptExtensions.isEmpty() || acceptExtensions.contains(extension); + } + + private String getExtension(File file) { + String name = file.getName(); + int index = name.lastIndexOf("."); + if (index == -1) { + return ""; + } + + return name.substring(index + 1); + } + +} diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/util/AuxiliaryUtils.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/util/AuxiliaryUtils.java index 411dbed..b5da7a7 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/util/AuxiliaryUtils.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/util/AuxiliaryUtils.java @@ -1,5 +1,8 @@ package org.smartboot.flow.core.util; +import java.util.ArrayList; +import java.util.List; + /** * @author qinluo * @date 2022-11-20 11:32:23 @@ -49,4 +52,19 @@ public final class AuxiliaryUtils { public static boolean isAnonymous(String name) { return name != null && name.contains("anonymous"); } + + public static List asList(T ...args) { + List values = new ArrayList<>(0); + if (args == null || args.length == 0) { + return values; + } + + for (T arg : args) { + if (arg != null) { + values.add(arg); + } + } + + return values; + } } diff --git a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/trace/TraceReporter.java b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/trace/TraceReporter.java index c5636a1..5cc938a 100644 --- a/smart-flow-manager/src/main/java/org/smartboot/flow/manager/trace/TraceReporter.java +++ b/smart-flow-manager/src/main/java/org/smartboot/flow/manager/trace/TraceReporter.java @@ -164,26 +164,6 @@ public class TraceReporter { return bos.toString(); } - private String serialExToString(Throwable ex) { - if (ex == null) { - return null; - } - - int maxDepth = ManagerConfiguration.reportMaxStackDepth; - StackTraceElement[] stackTrace = ex.getStackTrace(); - if (stackTrace.length > maxDepth) { - StackTraceElement[] newTrace = new StackTraceElement[maxDepth]; - System.arraycopy(stackTrace, 0, newTrace, 0, newTrace.length); - ex.setStackTrace(newTrace); - } - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - PrintWriter writer = new PrintWriter(bos); - ex.printStackTrace(writer); - writer.flush(); - return bos.toString(); - } - public void start() { URL parsedUrl; try { -- Gitee From 4c8c254eb281d49d4a8bb6fad8ea3d4a40740bc0 Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Sun, 12 Mar 2023 01:01:04 +0800 Subject: [PATCH 05/24] =?UTF-8?q?qinluo:=20=E8=84=9A=E6=9C=AC=E7=BB=84?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flow/admin/g6/G6ComponentVisitor.java | 23 ++++++----- .../core/parser/BuilderDefinitionVisitor.java | 20 ++++++---- .../parser/definition/ScriptDefinition.java | 6 +-- .../flow/core/script/ScriptCondition.java | 40 +++++-------------- .../flow/core/script/ScriptDetector.java | 12 +++--- .../flow/helper/mock/FakeObjectCreator.java | 6 +-- ...Condition.java => FakeScriptExecutor.java} | 8 ++-- .../flow/helper/view/PlantumlComponent.java | 4 +- .../flow/helper/view/XmlComponentVisitor.java | 11 +++-- ...ndition.java => GroovyScriptExecutor.java} | 8 ++-- ...Condition.java => JavaScriptExecutor.java} | 2 +- .../main/resources/META-INF/smart-flow-script | 4 +- ...Condition.java => OgnlScriptExecutor.java} | 8 ++-- .../main/resources/META-INF/smart-flow-script | 2 +- ...tion.java => QlExpressScriptExecutor.java} | 8 ++-- .../main/resources/META-INF/smart-flow-script | 2 +- .../extension/BeanDefinitionVisitor.java | 15 ++++--- 17 files changed, 89 insertions(+), 90 deletions(-) rename smart-flow-helper/src/main/java/org/smartboot/flow/helper/mock/{FakeScriptCondition.java => FakeScriptExecutor.java} (75%) rename smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/{GroovyScriptCondition.java => GroovyScriptExecutor.java} (89%) rename smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/{JavaScriptCondition.java => JavaScriptExecutor.java} (73%) rename smart-flow-script-condition/smart-flow-script-ognl/src/main/java/org/smartboot/flow/condition/extension/ognl/{OgnlScriptCondition.java => OgnlScriptExecutor.java} (87%) rename smart-flow-script-condition/smart-flow-script-qlexpress/src/main/java/org/smartboot/flow/condition/extension/qlexpress/{QlExpressScriptCondition.java => QlExpressScriptExecutor.java} (87%) diff --git a/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java b/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java index bb23e81..065f447 100644 --- a/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java +++ b/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java @@ -5,10 +5,12 @@ import org.smartboot.flow.core.attribute.AttributeHolder; import org.smartboot.flow.core.common.ComponentType; import org.smartboot.flow.core.component.Component; import org.smartboot.flow.core.script.ScriptCondition; +import org.smartboot.flow.core.script.ScriptExecutor; import org.smartboot.flow.core.util.AssertUtil; import org.smartboot.flow.core.util.AuxiliaryUtils; import org.smartboot.flow.core.visitor.ComponentVisitor; import org.smartboot.flow.core.visitor.ConditionVisitor; +import org.smartboot.flow.core.visitor.ExecutableVisitor; import org.smartboot.flow.core.visitor.PipelineVisitor; import java.util.ArrayList; @@ -40,11 +42,6 @@ public class G6ComponentVisitor extends ComponentVisitor { this.type = type; } - @Override - public void visitSource(Component component) { - - } - @Override public PipelineVisitor visitPipeline(String pipeline) { this.pipeline = new G6PipelineVisitor(pipeline); @@ -69,6 +66,13 @@ public class G6ComponentVisitor extends ComponentVisitor { this.attributes.addAll(attributes); } + @Override + public ExecutableVisitor visitExecutable(String executable) { + // TODO process scriptExecutor. + + return null; + } + @Override public ComponentVisitor visitBranch(Object branch, ComponentType type, String name, String describe) { G6ComponentVisitor component = new G6ComponentVisitor(type, name, describe); @@ -318,10 +322,11 @@ public class G6ComponentVisitor extends ComponentVisitor { public void visitSource(Condition condition) { if (condition instanceof ScriptCondition) { ScriptCondition nc = (ScriptCondition) condition; - G6ComponentVisitor.this.scriptName = nc.getName(); - G6ComponentVisitor.this.scriptType = nc.getType(); - G6ComponentVisitor.this.script = nc.getScript(); - G6ComponentVisitor.this.condition = nc.getName(); + ScriptExecutor scriptExecutor = nc.getScriptExecutor(); + G6ComponentVisitor.this.scriptName = scriptExecutor.getName(); + G6ComponentVisitor.this.scriptType = scriptExecutor.getType(); + G6ComponentVisitor.this.script = scriptExecutor.getScript(); + G6ComponentVisitor.this.condition = scriptExecutor.getName(); } } } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/BuilderDefinitionVisitor.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/BuilderDefinitionVisitor.java index 3a30e0a..d50b6c9 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/BuilderDefinitionVisitor.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/BuilderDefinitionVisitor.java @@ -25,6 +25,7 @@ import org.smartboot.flow.core.parser.definition.PipelineComponentDefinition; import org.smartboot.flow.core.parser.definition.PipelineDefinition; import org.smartboot.flow.core.parser.definition.ScriptDefinition; import org.smartboot.flow.core.script.ScriptCondition; +import org.smartboot.flow.core.script.ScriptExecutor; import org.smartboot.flow.core.util.AssertUtil; import org.smartboot.flow.core.util.AuxiliaryUtils; @@ -46,7 +47,7 @@ public class BuilderDefinitionVisitor implements DefinitionVisitor { private final Map> namedPipelines = new ConcurrentHashMap<>(); private final Map> namedEngines = new ConcurrentHashMap<>(); private final Map> callbacks = new ConcurrentHashMap<>(); - private final Map scriptConditions = new ConcurrentHashMap<>(); + private final Map scriptConditions = new ConcurrentHashMap<>(); private final ObjectCreator objectCreator; private final boolean useCache; private final AttributeValueResolver valueResolver; @@ -62,16 +63,16 @@ public class BuilderDefinitionVisitor implements DefinitionVisitor { if (ed instanceof ScriptDefinition) { ScriptDefinition sed = (ScriptDefinition) ed; Class javaType = sed.getJavaType(); - ScriptCondition condition; + ScriptExecutor scriptExecutor; if (javaType != null) { - condition = objectCreator.create(javaType.getName(), ScriptCondition.class, false); + scriptExecutor = objectCreator.create(javaType.getName(), ScriptExecutor.class, false); } else { - condition = objectCreator.create(sed.getType(), ScriptCondition.class, false); + scriptExecutor = objectCreator.create(sed.getType(), ScriptExecutor.class, false); } - condition.setName(sed.getName()); - condition.setScript(sed.getScript()); - scriptConditions.put(ed.getIdentifier(), condition); + scriptExecutor.setName(sed.getName()); + scriptExecutor.setScript(sed.getScript()); + scriptConditions.put(ed.getIdentifier(), scriptExecutor); } else { ed.visit(this); } @@ -158,6 +159,9 @@ public class BuilderDefinitionVisitor implements DefinitionVisitor { // resolve value type. builder.apply(p.getAttribute(), p.getValue()); }); + + // TODO wrap script executor by script condition + Component component = builder.newAdapter(newInstance(ed.getExecute(), Executable.class)); namedComponents.put(ed.getIdentifier(), component); } @@ -173,6 +177,7 @@ public class BuilderDefinitionVisitor implements DefinitionVisitor { if (AuxiliaryUtils.isType(test)) { condition = newInstance(test, Condition.class); } else if (ed.getContext().getRegistered(test) != null) { + // TODO wrap script executor by script condition condition = getInternalObject(test, ed.getContext()); } else { condition = newInstance(test, Condition.class); @@ -213,6 +218,7 @@ public class BuilderDefinitionVisitor implements DefinitionVisitor { if (AuxiliaryUtils.isType(test)) { condition = newInstance(test, Condition.class); } else if (ed.getContext().getRegistered(test) != null) { + // TODO wrap script executor by script condition condition = getInternalObject(test, ed.getContext()); } else { condition = newInstance(test, Condition.class); diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/definition/ScriptDefinition.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/definition/ScriptDefinition.java index 659fb28..64e7c48 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/definition/ScriptDefinition.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/definition/ScriptDefinition.java @@ -1,7 +1,7 @@ package org.smartboot.flow.core.parser.definition; -import org.smartboot.flow.core.script.ScriptCondition; import org.smartboot.flow.core.script.ScriptDetector; +import org.smartboot.flow.core.script.ScriptExecutor; import org.smartboot.flow.core.util.AssertUtil; import org.smartboot.flow.core.util.AuxiliaryUtils; @@ -29,8 +29,8 @@ public class ScriptDefinition extends ElementDefinition { } if (javaType != null) { - AssertUtil.isTrue(ScriptCondition.class.isAssignableFrom(javaType), "script type must be a subclass of ScriptCondition"); - AssertUtil.isTrue(ScriptCondition.class != javaType, "script type must be a subclass of ScriptCondition"); + AssertUtil.isTrue(ScriptExecutor.class.isAssignableFrom(javaType), "script type must be a subclass of ScriptExecutor"); + AssertUtil.isTrue(ScriptExecutor.class != javaType, "script type must be a subclass of ScriptExecutor"); } } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/script/ScriptCondition.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/script/ScriptCondition.java index ff1b712..878e545 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/script/ScriptCondition.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/script/ScriptCondition.java @@ -3,35 +3,23 @@ package org.smartboot.flow.core.script; import org.smartboot.flow.core.EngineContext; import org.smartboot.flow.core.NamedCondition; -import java.util.Map; - /** * @author qinluo * @date 2022/11/28 19:41 * @since 1.0.0 */ -public abstract class ScriptCondition extends NamedCondition { +public class ScriptCondition extends NamedCondition { - /** - * 执行脚本 - */ - protected String script; + private ScriptExecutor scriptExecutor; - public String getScript() { - return script; + public ScriptExecutor getScriptExecutor() { + return scriptExecutor; } - public void setScript(String script) { - this.script = script; + public void setScriptExecutor(ScriptExecutor scriptExecutor) { + this.scriptExecutor = scriptExecutor; } - /** - * 获取脚本条件的类型 - * - * @return type string - */ - public abstract String getType(); - /** * Don't override this method. */ @@ -47,22 +35,12 @@ public abstract class ScriptCondition extends NamedCondition { * @return 执行结果 */ @Override - public abstract Object test(EngineContext context); - - /** - * 允许用户在脚本上下文中绑定自定义的变量,用于执行脚本。 - * 其中key为脚本中用到的变量名,内置变量名参考 {@link org.smartboot.flow.core.script.ScriptConstants} - * - * @since 1.0.5 - * @param context engine ctx. - * @return bound keys. - */ - protected Map bindCustomized(EngineContext context) { - return ScriptVariableManager.getRegistered(context.getEngineName()); + public Object test(EngineContext context) { + return scriptExecutor.execute(context); } @Override public String describe() { - return "script-" + getType() + "-" + super.describe(); + return scriptExecutor.describe(); } } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/script/ScriptDetector.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/script/ScriptDetector.java index 3f69015..1086df5 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/script/ScriptDetector.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/script/ScriptDetector.java @@ -19,12 +19,12 @@ import java.util.concurrent.ConcurrentHashMap; public class ScriptDetector { /** - * The phrase for {@link org.smartboot.flow.core.script.ScriptCondition} location. + * The phrase for {@link org.smartboot.flow.core.script.ScriptExecutor} location. */ private static final String LOCATION = "META-INF/smart-flow-script"; /** - * The stored phrase for {@link org.smartboot.flow.core.script.ScriptCondition} + * The stored phrase for {@link org.smartboot.flow.core.script.ScriptExecutor} */ private final Map> javaTypes = new ConcurrentHashMap<>(); @@ -55,8 +55,8 @@ public class ScriptDetector { AssertUtil.notBlank(type, "script " + key + " type must not be blank"); Class javaType = AuxiliaryUtils.asClass(type); AssertUtil.notNull(javaType, "script " + key + " type must be a javaType"); - AssertUtil.isTrue(ScriptCondition.class.isAssignableFrom(javaType), "script " + key + " type must be a subclass of ScriptCondition"); - AssertUtil.isTrue(ScriptCondition.class != (javaType), "script " + key + " type must be a subclass of ScriptCondition"); + AssertUtil.isTrue(ScriptExecutor.class.isAssignableFrom(javaType), "script " + key + " type must be a subclass of ScriptExecutor"); + AssertUtil.isTrue(ScriptExecutor.class != (javaType), "script " + key + " type must be a subclass of ScriptExecutor"); javaTypes.put(key.toLowerCase(), javaType); } } @@ -74,8 +74,8 @@ public class ScriptDetector { public void register(String phrase, Class javaType) { AssertUtil.notBlank(phrase, "phrase must not blank"); AssertUtil.notNull(javaType, "type must be null"); - AssertUtil.isTrue(ScriptCondition.class.isAssignableFrom(javaType), "type must be a subclass of ScriptCondition"); - AssertUtil.isTrue(ScriptCondition.class != (javaType), "type must be a subclass of ScriptCondition"); + AssertUtil.isTrue(ScriptExecutor.class.isAssignableFrom(javaType), "type must be a subclass of ScriptExecutor"); + AssertUtil.isTrue(ScriptExecutor.class != (javaType), "type must be a subclass of ScriptExecutor"); javaTypes.put(phrase, javaType); } diff --git a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/mock/FakeObjectCreator.java b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/mock/FakeObjectCreator.java index d367eee..8c7210c 100644 --- a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/mock/FakeObjectCreator.java +++ b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/mock/FakeObjectCreator.java @@ -6,7 +6,7 @@ import org.smartboot.flow.core.DegradeCallback; import org.smartboot.flow.core.executable.Executable; import org.smartboot.flow.core.parser.DefaultObjectCreator; import org.smartboot.flow.core.parser.ObjectCreator; -import org.smartboot.flow.core.script.ScriptCondition; +import org.smartboot.flow.core.script.ScriptExecutor; /** * Fake object creator, Return prepared fake object. @@ -26,8 +26,8 @@ public class FakeObjectCreator implements ObjectCreator { if (expectType == Condition.class) { return (T)new FakeCondition(type); - } else if (expectType == ScriptCondition.class) { - return (T)new FakeScriptCondition(type); + } else if (expectType == ScriptExecutor.class) { + return (T)new FakeScriptExecutor(type); } else if (expectType == Executable.class) { return (T)new FakeExecutable(type); } else if (expectType == Adapter.class) { diff --git a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/mock/FakeScriptCondition.java b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/mock/FakeScriptExecutor.java similarity index 75% rename from smart-flow-helper/src/main/java/org/smartboot/flow/helper/mock/FakeScriptCondition.java rename to smart-flow-helper/src/main/java/org/smartboot/flow/helper/mock/FakeScriptExecutor.java index b215f97..e1e4fc3 100644 --- a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/mock/FakeScriptCondition.java +++ b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/mock/FakeScriptExecutor.java @@ -1,8 +1,8 @@ package org.smartboot.flow.helper.mock; import org.smartboot.flow.core.EngineContext; -import org.smartboot.flow.core.script.ScriptCondition; import org.smartboot.flow.core.script.ScriptDetector; +import org.smartboot.flow.core.script.ScriptExecutor; import org.smartboot.flow.core.util.AuxiliaryUtils; /** @@ -12,11 +12,11 @@ import org.smartboot.flow.core.util.AuxiliaryUtils; * @date 2023/1/27 12:35 * @since 1.0.0 */ -public class FakeScriptCondition extends ScriptCondition { +public class FakeScriptExecutor extends ScriptExecutor { private final String type; - public FakeScriptCondition(String type) { + public FakeScriptExecutor(String type) { this.type = type; } @@ -31,7 +31,7 @@ public class FakeScriptCondition extends ScriptCondition { } @Override - public Object test(EngineContext context) { + public Object execute(EngineContext context) { // do-noting return null; } diff --git a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/PlantumlComponent.java b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/PlantumlComponent.java index 248dd7c..38ada00 100644 --- a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/PlantumlComponent.java +++ b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/PlantumlComponent.java @@ -6,6 +6,7 @@ import org.smartboot.flow.core.attribute.AttributeHolder; import org.smartboot.flow.core.attribute.Attributes; import org.smartboot.flow.core.component.Component; import org.smartboot.flow.core.script.ScriptCondition; +import org.smartboot.flow.core.script.ScriptExecutor; import org.smartboot.flow.core.util.AuxiliaryUtils; import org.smartboot.flow.core.visitor.ComponentVisitor; import org.smartboot.flow.core.visitor.ConditionVisitor; @@ -225,7 +226,8 @@ public class PlantumlComponent extends ComponentVisitor { public void visitSource(Condition condition) { if (condition instanceof ScriptCondition) { ScriptCondition sc = (ScriptCondition) condition; - PlantumlComponent.this.condition = sc.getName() + "-" + sc.getType(); + ScriptExecutor scriptExecutor = sc.getScriptExecutor(); + PlantumlComponent.this.condition = scriptExecutor.getName() + "-" + scriptExecutor.getType(); } } } diff --git a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java index 9a5bce9..c8e623b 100644 --- a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java +++ b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java @@ -5,6 +5,7 @@ import org.smartboot.flow.core.attribute.AttributeHolder; import org.smartboot.flow.core.attribute.Attributes; import org.smartboot.flow.core.common.ComponentType; import org.smartboot.flow.core.script.ScriptCondition; +import org.smartboot.flow.core.script.ScriptExecutor; import org.smartboot.flow.core.util.AuxiliaryUtils; import org.smartboot.flow.core.visitor.ComponentVisitor; import org.smartboot.flow.core.visitor.ConditionVisitor; @@ -41,6 +42,7 @@ public class XmlComponentVisitor extends ComponentVisitor { @Override public ExecutableVisitor visitExecutable(String executable) { this.executable = executable; + // TODO scriptExecutor visitor return null; } @@ -190,10 +192,11 @@ public class XmlComponentVisitor extends ComponentVisitor { public void visitSource(Condition condition) { if (condition instanceof ScriptCondition) { ScriptCondition sc = (ScriptCondition) condition; - XmlComponentVisitor.this.scriptName = sc.getName(); - XmlComponentVisitor.this.condition = sc.getName(); - XmlComponentVisitor.this.scriptType = sc.getType(); - XmlComponentVisitor.this.script = sc.getScript(); + ScriptExecutor scriptExecutor = sc.getScriptExecutor(); + XmlComponentVisitor.this.scriptName = scriptExecutor.getName(); + XmlComponentVisitor.this.condition = scriptExecutor.getName(); + XmlComponentVisitor.this.scriptType = scriptExecutor.getType(); + XmlComponentVisitor.this.script = scriptExecutor.getScript(); } } } diff --git a/smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/GroovyScriptCondition.java b/smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/GroovyScriptExecutor.java similarity index 89% rename from smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/GroovyScriptCondition.java rename to smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/GroovyScriptExecutor.java index 475b0b6..cb4cccf 100644 --- a/smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/GroovyScriptCondition.java +++ b/smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/GroovyScriptExecutor.java @@ -4,8 +4,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.smartboot.flow.core.EngineContext; import org.smartboot.flow.core.exception.FlowException; -import org.smartboot.flow.core.script.ScriptCondition; import org.smartboot.flow.core.script.ScriptConstants; +import org.smartboot.flow.core.script.ScriptExecutor; import javax.script.Bindings; import javax.script.ScriptEngine; @@ -17,9 +17,9 @@ import java.util.Map; * @date 2022/11/29 21:01 * @since 1.0.0 */ -public class GroovyScriptCondition extends ScriptCondition { +public class GroovyScriptExecutor extends ScriptExecutor { - private static final Logger LOGGER = LoggerFactory.getLogger(GroovyScriptCondition.class); + private static final Logger LOGGER = LoggerFactory.getLogger(GroovyScriptExecutor.class); private static final ScriptEngineManager MANAGER = new ScriptEngineManager(); protected String getScriptLang() { @@ -27,7 +27,7 @@ public class GroovyScriptCondition extends ScriptCondition { } @Override - public Object test(EngineContext engineContext) { + public Object execute(EngineContext engineContext) { try { ScriptEngine engine = MANAGER.getEngineByName(getScriptLang()); Bindings data = engine.createBindings(); diff --git a/smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/JavaScriptCondition.java b/smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/JavaScriptExecutor.java similarity index 73% rename from smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/JavaScriptCondition.java rename to smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/JavaScriptExecutor.java index a21e89b..add780d 100644 --- a/smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/JavaScriptCondition.java +++ b/smart-flow-script-condition/smart-flow-script-groovy/src/main/java/org/smartboot/flow/condition/extension/groovy/JavaScriptExecutor.java @@ -5,7 +5,7 @@ package org.smartboot.flow.condition.extension.groovy; * @date 2022/11/29 21:01 * @since 1.0.0 */ -public class JavaScriptCondition extends GroovyScriptCondition { +public class JavaScriptExecutor extends GroovyScriptExecutor { @Override protected String getScriptLang() { diff --git a/smart-flow-script-condition/smart-flow-script-groovy/src/main/resources/META-INF/smart-flow-script b/smart-flow-script-condition/smart-flow-script-groovy/src/main/resources/META-INF/smart-flow-script index f679dcb..32cafc6 100644 --- a/smart-flow-script-condition/smart-flow-script-groovy/src/main/resources/META-INF/smart-flow-script +++ b/smart-flow-script-condition/smart-flow-script-groovy/src/main/resources/META-INF/smart-flow-script @@ -1,2 +1,2 @@ -groovy=org.smartboot.flow.condition.extension.groovy.GroovyScriptCondition -javascript=org.smartboot.flow.condition.extension.groovy.JavaScriptCondition \ No newline at end of file +groovy=org.smartboot.flow.condition.extension.groovy.GroovyScriptExecutor +javascript=org.smartboot.flow.condition.extension.groovy.JavaScriptExecutor \ No newline at end of file diff --git a/smart-flow-script-condition/smart-flow-script-ognl/src/main/java/org/smartboot/flow/condition/extension/ognl/OgnlScriptCondition.java b/smart-flow-script-condition/smart-flow-script-ognl/src/main/java/org/smartboot/flow/condition/extension/ognl/OgnlScriptExecutor.java similarity index 87% rename from smart-flow-script-condition/smart-flow-script-ognl/src/main/java/org/smartboot/flow/condition/extension/ognl/OgnlScriptCondition.java rename to smart-flow-script-condition/smart-flow-script-ognl/src/main/java/org/smartboot/flow/condition/extension/ognl/OgnlScriptExecutor.java index 796434a..e022195 100644 --- a/smart-flow-script-condition/smart-flow-script-ognl/src/main/java/org/smartboot/flow/condition/extension/ognl/OgnlScriptCondition.java +++ b/smart-flow-script-condition/smart-flow-script-ognl/src/main/java/org/smartboot/flow/condition/extension/ognl/OgnlScriptExecutor.java @@ -5,8 +5,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.smartboot.flow.core.EngineContext; import org.smartboot.flow.core.exception.FlowException; -import org.smartboot.flow.core.script.ScriptCondition; import org.smartboot.flow.core.script.ScriptConstants; +import org.smartboot.flow.core.script.ScriptExecutor; import java.util.HashMap; import java.util.Map; @@ -16,12 +16,12 @@ import java.util.Map; * @date 2022/11/29 21:01 * @since 1.0.0 */ -public class OgnlScriptCondition extends ScriptCondition { +public class OgnlScriptExecutor extends ScriptExecutor { - private static final Logger LOGGER = LoggerFactory.getLogger(OgnlScriptCondition.class); + private static final Logger LOGGER = LoggerFactory.getLogger(OgnlScriptExecutor.class); @Override - public Object test(EngineContext engineContext) { + public Object execute(EngineContext engineContext) { try { Map context = new HashMap<>(8); context.put(ScriptConstants.REQ, engineContext.getReq()); diff --git a/smart-flow-script-condition/smart-flow-script-ognl/src/main/resources/META-INF/smart-flow-script b/smart-flow-script-condition/smart-flow-script-ognl/src/main/resources/META-INF/smart-flow-script index a3d2334..03b2018 100644 --- a/smart-flow-script-condition/smart-flow-script-ognl/src/main/resources/META-INF/smart-flow-script +++ b/smart-flow-script-condition/smart-flow-script-ognl/src/main/resources/META-INF/smart-flow-script @@ -1 +1 @@ -ognl=org.smartboot.flow.condition.extension.ognl.OgnlScriptCondition \ No newline at end of file +ognl=org.smartboot.flow.condition.extension.ognl.OgnlScriptExecutor \ No newline at end of file diff --git a/smart-flow-script-condition/smart-flow-script-qlexpress/src/main/java/org/smartboot/flow/condition/extension/qlexpress/QlExpressScriptCondition.java b/smart-flow-script-condition/smart-flow-script-qlexpress/src/main/java/org/smartboot/flow/condition/extension/qlexpress/QlExpressScriptExecutor.java similarity index 87% rename from smart-flow-script-condition/smart-flow-script-qlexpress/src/main/java/org/smartboot/flow/condition/extension/qlexpress/QlExpressScriptCondition.java rename to smart-flow-script-condition/smart-flow-script-qlexpress/src/main/java/org/smartboot/flow/condition/extension/qlexpress/QlExpressScriptExecutor.java index b74f17b..d1d52eb 100644 --- a/smart-flow-script-condition/smart-flow-script-qlexpress/src/main/java/org/smartboot/flow/condition/extension/qlexpress/QlExpressScriptCondition.java +++ b/smart-flow-script-condition/smart-flow-script-qlexpress/src/main/java/org/smartboot/flow/condition/extension/qlexpress/QlExpressScriptExecutor.java @@ -6,8 +6,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.smartboot.flow.core.EngineContext; import org.smartboot.flow.core.exception.FlowException; -import org.smartboot.flow.core.script.ScriptCondition; import org.smartboot.flow.core.script.ScriptConstants; +import org.smartboot.flow.core.script.ScriptExecutor; import java.util.Map; @@ -16,12 +16,12 @@ import java.util.Map; * @date 2022/11/29 21:01 * @since 1.0.0 */ -public class QlExpressScriptCondition extends ScriptCondition { +public class QlExpressScriptExecutor extends ScriptExecutor { - private static final Logger LOGGER = LoggerFactory.getLogger(QlExpressScriptCondition.class); + private static final Logger LOGGER = LoggerFactory.getLogger(QlExpressScriptExecutor.class); @Override - public Object test(EngineContext engineContext) { + public Object execute(EngineContext engineContext) { try { DefaultContext qlContext = new DefaultContext<>(); diff --git a/smart-flow-script-condition/smart-flow-script-qlexpress/src/main/resources/META-INF/smart-flow-script b/smart-flow-script-condition/smart-flow-script-qlexpress/src/main/resources/META-INF/smart-flow-script index ec9ffd1..0681942 100644 --- a/smart-flow-script-condition/smart-flow-script-qlexpress/src/main/resources/META-INF/smart-flow-script +++ b/smart-flow-script-condition/smart-flow-script-qlexpress/src/main/resources/META-INF/smart-flow-script @@ -1 +1 @@ -qlexpress=org.smartboot.flow.condition.extension.qlexpress.QlExpressScriptCondition \ No newline at end of file +qlexpress=org.smartboot.flow.condition.extension.qlexpress.QlExpressScriptExecutor \ No newline at end of file diff --git a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java index 79f94cb..521baa2 100644 --- a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java +++ b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java @@ -13,7 +13,7 @@ import org.smartboot.flow.core.parser.definition.IfElementDefinition; import org.smartboot.flow.core.parser.definition.PipelineComponentDefinition; import org.smartboot.flow.core.parser.definition.PipelineDefinition; import org.smartboot.flow.core.parser.definition.ScriptDefinition; -import org.smartboot.flow.core.script.ScriptCondition; +import org.smartboot.flow.core.script.ScriptExecutor; import org.smartboot.flow.core.util.AssertUtil; import org.smartboot.flow.core.util.AuxiliaryUtils; import org.springframework.beans.BeansException; @@ -73,10 +73,11 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar // Maybe a ref. if (javaType == null) { BeanDefinition beanDefinition = register.getBeanDefinition(sed.getName()); + AssertUtil.notNull(beanDefinition, "Could not found ScriptExecutor " + sed.getName() + " in spring container"); String beanClassName = beanDefinition.getBeanClassName(); Class defJavaType = AuxiliaryUtils.asClass(beanClassName); - AssertUtil.notNull(defJavaType, "bean " + sed.getName() + " javaType must be subclass of ScriptCondition"); - AssertUtil.isTrue(ScriptCondition.class.isAssignableFrom(defJavaType), "bean " + sed.getName() + " javaType must be subclass of ScriptCondition"); + AssertUtil.notNull(defJavaType, "bean " + sed.getName() + " javaType must be subclass of ScriptExecutor"); + AssertUtil.isTrue(ScriptExecutor.class.isAssignableFrom(defJavaType), "bean " + sed.getName() + " javaType must be subclass of ScriptExecutor"); // Register script content. beanDefinition.getPropertyValues().add("script", sed.getScript()); @@ -84,7 +85,6 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar if (AuxiliaryUtils.isNotBlank(sed.getName())) { beanDefinition.getPropertyValues().add("name", sed.getName()); } - } else { RootBeanDefinition definition = new RootBeanDefinition(); definition.setBeanClass(javaType); @@ -92,7 +92,7 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar PropertyValue name = new PropertyValue("name", sed.getName()); definition.getPropertyValues().addPropertyValue(name); definition.getPropertyValues().addPropertyValue(script); - register.registerBeanDefinition(ed.getIdentifier(), definition, true); + register.registerBeanDefinition(ed.getIdentifier(), definition, false); } } else { @@ -200,6 +200,7 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar conditionDef.setBeanClassName(ed.getExecute()); definition.getPropertyValues().add("executable", conditionDef); } else { + // TODO wrap scriptExecutor with script condition AssertUtil.notBlank(ed.getExecute(), "component element ref must not be null!"); definition.getPropertyValues().add("executable", new RuntimeBeanReference(ed.getExecute())); } @@ -247,6 +248,9 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar definition.getPropertyValues().add("condition", conditionDef); } else { AssertUtil.notBlank(ed.getTest(), "if element ref must not be null!"); + + // TODO wrap scriptExecutor with script condition + definition.getPropertyValues().add("condition", new RuntimeBeanReference(ed.getTest())); } @@ -280,6 +284,7 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar conditionDef.setBeanClassName(ed.getTest()); definition.getPropertyValues().add("condition", conditionDef); } else { + // TODO wrap scriptExecutor with script condition definition.getPropertyValues().add("condition", new RuntimeBeanReference(ed.getTest())); } -- Gitee From b7cff16002819dc4216381353c90490ecbbaa042 Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Sun, 12 Mar 2023 14:23:20 +0800 Subject: [PATCH 06/24] =?UTF-8?q?qinluo:=20=E8=84=9A=E6=9C=AC=E7=BB=84?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../smart/flow/admin/g6/ComponentInfo.java | 28 ++++ .../flow/admin/g6/G6ComponentVisitor.java | 37 ++++- .../core/parser/AdapterElementParser.java | 2 + .../core/parser/BuilderDefinitionVisitor.java | 43 ++++-- .../flow/core/parser/ChooseElementParser.java | 1 + .../flow/core/parser/DefinitionVisitor.java | 16 ++ .../flow/core/parser/IfElementParser.java | 2 + .../parser/definition/ScriptDefinition.java | 11 ++ .../flow/helper/view/XmlComponentVisitor.java | 38 ++++- .../flow/helper/view/XmlEngineVisitor.java | 2 +- .../extension/BeanDefinitionRegister.java | 4 + .../extension/BeanDefinitionVisitor.java | 140 ++++++++++-------- .../flow/spring/extension/ProxyParser.java | 23 ++- .../SmartFlowBeanFactoryRegistry.java | 36 +++++ .../spring/extension/SmartFlowRegistrar.java | 15 +- .../extension/SpringNamespaceHandler.java | 2 +- 16 files changed, 313 insertions(+), 87 deletions(-) create mode 100644 smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SmartFlowBeanFactoryRegistry.java diff --git a/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/ComponentInfo.java b/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/ComponentInfo.java index cdd8000..8afc554 100644 --- a/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/ComponentInfo.java +++ b/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/ComponentInfo.java @@ -20,6 +20,10 @@ public class ComponentInfo implements Serializable { private String scriptName; private String script; + private String rollbackScriptName; + private String rollbackScriptType; + private String rollbackScript; + /** * 属性集合 */ @@ -88,4 +92,28 @@ public class ComponentInfo implements Serializable { public void setAttributes(List attributes) { this.attributes = attributes; } + + public String getRollbackScriptName() { + return rollbackScriptName; + } + + public void setRollbackScriptName(String rollbackScriptName) { + this.rollbackScriptName = rollbackScriptName; + } + + public String getRollbackScriptType() { + return rollbackScriptType; + } + + public void setRollbackScriptType(String rollbackScriptType) { + this.rollbackScriptType = rollbackScriptType; + } + + public String getRollbackScript() { + return rollbackScript; + } + + public void setRollbackScript(String rollbackScript) { + this.rollbackScript = rollbackScript; + } } diff --git a/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java b/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java index 065f447..3a02bbb 100644 --- a/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java +++ b/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java @@ -3,8 +3,9 @@ package org.smartboot.smart.flow.admin.g6; import org.smartboot.flow.core.Condition; import org.smartboot.flow.core.attribute.AttributeHolder; import org.smartboot.flow.core.common.ComponentType; -import org.smartboot.flow.core.component.Component; +import org.smartboot.flow.core.executable.Executable; import org.smartboot.flow.core.script.ScriptCondition; +import org.smartboot.flow.core.script.ScriptExecutable; import org.smartboot.flow.core.script.ScriptExecutor; import org.smartboot.flow.core.util.AssertUtil; import org.smartboot.flow.core.util.AuxiliaryUtils; @@ -34,6 +35,9 @@ public class G6ComponentVisitor extends ComponentVisitor { private String script; private String scriptType; private String scriptName; + private String rollbackScriptName; + private String rollbackScriptType; + private String rollbackScript; public G6ComponentVisitor(ComponentType type, String name, String describe) { @@ -68,9 +72,7 @@ public class G6ComponentVisitor extends ComponentVisitor { @Override public ExecutableVisitor visitExecutable(String executable) { - // TODO process scriptExecutor. - - return null; + return new G6ExecutableVisitor(); } @Override @@ -285,8 +287,12 @@ public class G6ComponentVisitor extends ComponentVisitor { ci.setScriptName(scriptName); ci.setScriptType(scriptType); + ci.setRollbackScript(rollbackScript); + ci.setRollbackScriptName(rollbackScriptName); + ci.setRollbackScriptType(rollbackScriptType); + if (type == ComponentType.BASIC) { - ci.setTypeDesc("基本组件"); + ci.setTypeDesc(script != null ? "基本组件(脚本)" : "基本组件"); } else if (type == ComponentType.IF) { ci.setTypeDesc("IF组件"); } else if (type == ComponentType.CHOOSE) { @@ -330,4 +336,25 @@ public class G6ComponentVisitor extends ComponentVisitor { } } } + + private class G6ExecutableVisitor extends ExecutableVisitor { + + @Override + public void visitSource(Executable executable) { + if (executable instanceof ScriptExecutable) { + ScriptExecutable sc = (ScriptExecutable) executable; + ScriptExecutor scriptExecutor = sc.getScriptExecutor(); + G6ComponentVisitor.this.scriptName = scriptExecutor.getName(); + G6ComponentVisitor.this.scriptType = scriptExecutor.getType(); + G6ComponentVisitor.this.script = scriptExecutor.getScript(); + + ScriptExecutor rollbackExecutor = sc.getRollbackExecutor(); + if (rollbackExecutor != null) { + G6ComponentVisitor.this.rollbackScriptName = rollbackExecutor.getName(); + G6ComponentVisitor.this.rollbackScriptType = rollbackExecutor.getType(); + G6ComponentVisitor.this.rollbackScript = rollbackExecutor.getScript(); + } + } + } + } } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/AdapterElementParser.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/AdapterElementParser.java index a8756b3..2bd8687 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/AdapterElementParser.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/AdapterElementParser.java @@ -32,6 +32,8 @@ public class AdapterElementParser extends AbstractElementParser{ definition.setIdentifier(identifier); definition.getAttributes().addAll(ElementUtils.extraAttributes(element)); + AssertUtil.notBlank(definition.getExecute(), "attribute [execute] cannot be null"); + ElementDefinition def = parseSubElements(element, context); definition.setPipelineElement(def); context.register(definition); diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/BuilderDefinitionVisitor.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/BuilderDefinitionVisitor.java index d50b6c9..324e793 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/BuilderDefinitionVisitor.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/BuilderDefinitionVisitor.java @@ -25,6 +25,7 @@ import org.smartboot.flow.core.parser.definition.PipelineComponentDefinition; import org.smartboot.flow.core.parser.definition.PipelineDefinition; import org.smartboot.flow.core.parser.definition.ScriptDefinition; import org.smartboot.flow.core.script.ScriptCondition; +import org.smartboot.flow.core.script.ScriptExecutable; import org.smartboot.flow.core.script.ScriptExecutor; import org.smartboot.flow.core.util.AssertUtil; import org.smartboot.flow.core.util.AuxiliaryUtils; @@ -47,7 +48,7 @@ public class BuilderDefinitionVisitor implements DefinitionVisitor { private final Map> namedPipelines = new ConcurrentHashMap<>(); private final Map> namedEngines = new ConcurrentHashMap<>(); private final Map> callbacks = new ConcurrentHashMap<>(); - private final Map scriptConditions = new ConcurrentHashMap<>(); + private final Map scriptExecutors = new ConcurrentHashMap<>(); private final ObjectCreator objectCreator; private final boolean useCache; private final AttributeValueResolver valueResolver; @@ -72,7 +73,7 @@ public class BuilderDefinitionVisitor implements DefinitionVisitor { scriptExecutor.setName(sed.getName()); scriptExecutor.setScript(sed.getScript()); - scriptConditions.put(ed.getIdentifier(), scriptExecutor); + scriptExecutors.put(ed.getIdentifier(), scriptExecutor); } else { ed.visit(this); } @@ -160,9 +161,27 @@ public class BuilderDefinitionVisitor implements DefinitionVisitor { builder.apply(p.getAttribute(), p.getValue()); }); - // TODO wrap script executor by script condition + Executable executable; + if (AuxiliaryUtils.isType(ed.getExecute())) { + executable = newInstance(ed.getExecute(), Executable.class); + } else if (ed.getContext().getRegistered(ed.getExecute()) != null) { + executable = new ScriptExecutable(); + ScriptExecutor scriptExecutor = getInternalObject(ed.getExecute(), ed.getContext()); + AssertUtil.notNull(scriptExecutor, "script executor is null"); + ((ScriptExecutable)executable).setScriptExecutor(scriptExecutor); + + String rollback = getRollbackScriptName(ed.getExecute()); + if (ed.getContext().getRegistered(rollback) != null) { + scriptExecutor = getInternalObject(rollback, ed.getContext()); + AssertUtil.notNull(scriptExecutor, "script executor is null"); + ((ScriptExecutable)executable).setRollbackExecutor(scriptExecutor); + } + } else { + executable = newInstance(ed.getExecute(), Executable.class); + } - Component component = builder.newAdapter(newInstance(ed.getExecute(), Executable.class)); + AssertUtil.notNull(executable, "executable " + executable + " is null"); + Component component = builder.newAdapter(executable); namedComponents.put(ed.getIdentifier(), component); } @@ -177,8 +196,10 @@ public class BuilderDefinitionVisitor implements DefinitionVisitor { if (AuxiliaryUtils.isType(test)) { condition = newInstance(test, Condition.class); } else if (ed.getContext().getRegistered(test) != null) { - // TODO wrap script executor by script condition - condition = getInternalObject(test, ed.getContext()); + ScriptExecutor scriptExecutor = getInternalObject(test, ed.getContext()); + AssertUtil.notNull(scriptExecutor, "script executor is null"); + condition = new ScriptCondition(); + ((ScriptCondition)condition).setScriptExecutor(scriptExecutor); } else { condition = newInstance(test, Condition.class); } @@ -204,11 +225,11 @@ public class BuilderDefinitionVisitor implements DefinitionVisitor { this.namedComponents.put(ed.getIdentifier(), build); } - public T getInternalObject(String test, ParserContext context) { + public ScriptExecutor getInternalObject(String test, ParserContext context) { ElementDefinition registered = context.getRegistered(test); AssertUtil.notNull(registered, "registered condition def[" + test + "] not found"); this.visit(registered); - return (T)scriptConditions.get(test); + return scriptExecutors.get(test); } @Override @@ -218,8 +239,10 @@ public class BuilderDefinitionVisitor implements DefinitionVisitor { if (AuxiliaryUtils.isType(test)) { condition = newInstance(test, Condition.class); } else if (ed.getContext().getRegistered(test) != null) { - // TODO wrap script executor by script condition - condition = getInternalObject(test, ed.getContext()); + ScriptExecutor scriptExecutor = getInternalObject(test, ed.getContext()); + AssertUtil.notNull(scriptExecutor, "script executor is null"); + condition = new ScriptCondition(); + ((ScriptCondition)condition).setScriptExecutor(scriptExecutor); } else { condition = newInstance(test, Condition.class); } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ChooseElementParser.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ChooseElementParser.java index 5c4bb4a..bc6c218 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ChooseElementParser.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ChooseElementParser.java @@ -64,6 +64,7 @@ public class ChooseElementParser extends AbstractElementParser{ String type = subElement.getAttribute(ParseConstants.EXECUTE); if (AuxiliaryUtils.isNotBlank(type)) { + currentDef.setContext(context); continue; } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/DefinitionVisitor.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/DefinitionVisitor.java index 8f07dc2..47ef807 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/DefinitionVisitor.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/DefinitionVisitor.java @@ -55,4 +55,20 @@ public interface DefinitionVisitor { default void init(ParserContext ctx) { } + + /** + * Return rollback script name of name. + * + *

+ * initScript --> initScript-rollback. + * reduceStock --> reduceStock-rollback. + * + *

+ * + * @param name name + * @return rollback name + */ + default String getRollbackScriptName(String name) { + return name + "-rollback"; + } } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/IfElementParser.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/IfElementParser.java index 7269f95..fb4c60c 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/IfElementParser.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/IfElementParser.java @@ -56,11 +56,13 @@ public class IfElementParser extends AbstractElementParser { ElementDefinition elseDefinition = new ElementDefinition(); ElementDefinition.build(elseDefinition, subElement); elseDefinition.setIdentifier(super.getIdentifier(subElement, context)); + elseDefinition.setContext(context); elseDef = elseDefinition; } else { ElementDefinition thenDefinition = new ElementDefinition(); ElementDefinition.build(thenDefinition, subElement); thenDefinition.setIdentifier(super.getIdentifier(subElement, context)); + thenDefinition.setContext(context); thenDef = thenDefinition; } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/definition/ScriptDefinition.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/definition/ScriptDefinition.java index 64e7c48..d31bac2 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/definition/ScriptDefinition.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/definition/ScriptDefinition.java @@ -1,5 +1,6 @@ package org.smartboot.flow.core.parser.definition; +import org.smartboot.flow.core.parser.DefinitionVisitor; import org.smartboot.flow.core.script.ScriptDetector; import org.smartboot.flow.core.script.ScriptExecutor; import org.smartboot.flow.core.util.AssertUtil; @@ -48,6 +49,16 @@ public class ScriptDefinition extends ElementDefinition { return javaType; } + @Override + public Class resolveType() { + return getJavaType(); + } + + @Override + protected void visit0(DefinitionVisitor visitor) { + visitor.visit(this); + } + public String getType() { return type; } diff --git a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java index c8e623b..457878a 100644 --- a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java +++ b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java @@ -4,7 +4,9 @@ import org.smartboot.flow.core.Condition; import org.smartboot.flow.core.attribute.AttributeHolder; import org.smartboot.flow.core.attribute.Attributes; import org.smartboot.flow.core.common.ComponentType; +import org.smartboot.flow.core.executable.Executable; import org.smartboot.flow.core.script.ScriptCondition; +import org.smartboot.flow.core.script.ScriptExecutable; import org.smartboot.flow.core.script.ScriptExecutor; import org.smartboot.flow.core.util.AuxiliaryUtils; import org.smartboot.flow.core.visitor.ComponentVisitor; @@ -29,12 +31,15 @@ public class XmlComponentVisitor extends ComponentVisitor { private String branch; private final List components = new ArrayList<>(); private String executable; + private final String name; private String script; private String scriptName; - private final String name; private String scriptType; + private String rollbackScript; + private String rollbackScriptName; + private String rollbackScriptType; - public XmlComponentVisitor(ComponentType type, String name, String describe) { + public XmlComponentVisitor(ComponentType type, String name, String ignoredDescribe) { this.type = type; this.name = name; } @@ -42,8 +47,7 @@ public class XmlComponentVisitor extends ComponentVisitor { @Override public ExecutableVisitor visitExecutable(String executable) { this.executable = executable; - // TODO scriptExecutor visitor - return null; + return new XmlExecutableVisitor(); } @Override @@ -104,6 +108,11 @@ public class XmlComponentVisitor extends ComponentVisitor { ScriptCollector.collect(scriptType, script, scriptName); } + if (rollbackScript != null) { + ScriptCollector.collect(rollbackScriptType, rollbackScript, rollbackScriptName); + } + + if (type == ComponentType.BASIC) { AuxiliaryUtils.appendTab(content, numbersOfTab); content.append(" void visitSource(Executable executable) { + if (executable instanceof ScriptExecutable) { + ScriptExecutable sc = (ScriptExecutable) executable; + ScriptExecutor scriptExecutor = sc.getScriptExecutor(); + XmlComponentVisitor.this.scriptName = scriptExecutor.getName(); + XmlComponentVisitor.this.scriptType = scriptExecutor.getType(); + XmlComponentVisitor.this.script = scriptExecutor.getScript(); + + ScriptExecutor rollbackExecutor = sc.getRollbackExecutor(); + if (rollbackExecutor != null) { + XmlComponentVisitor.this.rollbackScriptName = rollbackExecutor.getName(); + XmlComponentVisitor.this.rollbackScriptType = rollbackExecutor.getType(); + XmlComponentVisitor.this.rollbackScript = rollbackExecutor.getScript(); + } + } + } + } } diff --git a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlEngineVisitor.java b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlEngineVisitor.java index 30bc305..901c05d 100644 --- a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlEngineVisitor.java +++ b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlEngineVisitor.java @@ -59,7 +59,7 @@ public class XmlEngineVisitor extends EngineVisitor { if (scripts != null && scripts.size() > 0) { scripts.forEach((k, v) -> content.append("\n\t")); + .append("\">").append(v.getRight()).append("")); } content.append("\n").append(END).append("\n"); diff --git a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionRegister.java b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionRegister.java index fa0b792..8d6f671 100644 --- a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionRegister.java +++ b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionRegister.java @@ -40,4 +40,8 @@ public class BeanDefinitionRegister { registered.put(identifier, def); } } + + public boolean isSpringBean(String identifier) { + return registered.get(identifier) == null; + } } diff --git a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java index 521baa2..f0ba265 100644 --- a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java +++ b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java @@ -13,6 +13,8 @@ import org.smartboot.flow.core.parser.definition.IfElementDefinition; import org.smartboot.flow.core.parser.definition.PipelineComponentDefinition; import org.smartboot.flow.core.parser.definition.PipelineDefinition; import org.smartboot.flow.core.parser.definition.ScriptDefinition; +import org.smartboot.flow.core.script.ScriptCondition; +import org.smartboot.flow.core.script.ScriptExecutable; import org.smartboot.flow.core.script.ScriptExecutor; import org.smartboot.flow.core.util.AssertUtil; import org.smartboot.flow.core.util.AuxiliaryUtils; @@ -86,11 +88,8 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar beanDefinition.getPropertyValues().add("name", sed.getName()); } } else { - RootBeanDefinition definition = new RootBeanDefinition(); - definition.setBeanClass(javaType); + RootBeanDefinition definition = asSpringDefinition(ed); PropertyValue script = new PropertyValue("script", sed.getScript()); - PropertyValue name = new PropertyValue("name", sed.getName()); - definition.getPropertyValues().addPropertyValue(name); definition.getPropertyValues().addPropertyValue(script); register.registerBeanDefinition(ed.getIdentifier(), definition, false); } @@ -102,12 +101,9 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar @Override public void visit(EngineDefinition ed) { - RootBeanDefinition definition = new RootBeanDefinition(); - PropertyValue name = new PropertyValue("name", ed.getName()); + RootBeanDefinition definition = asSpringDefinition(ed); PropertyValue pipeline = new PropertyValue("pipeline", new RuntimeBeanReference(ed.getPipeline())); - definition.setBeanClass(ed.resolveType()); - definition.getPropertyValues().addPropertyValue(name); definition.getPropertyValues().addPropertyValue(pipeline); definition.setInitMethodName("validate"); register.registerBeanDefinition(ed.getIdentifier(), definition, true); @@ -115,12 +111,9 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar @Override public void visit(PipelineDefinition ed) { - RootBeanDefinition definition = new RootBeanDefinition(); - PropertyValue name = new PropertyValue("name", ed.getName()); + RootBeanDefinition definition = asSpringDefinition(ed); ManagedList components = new ManagedList<>(); - definition.setBeanClass(ed.resolveType()); - definition.getPropertyValues().addPropertyValue(name); definition.getPropertyValues().add("components", components); ed.getChildren().forEach(p -> p.visit(this)); @@ -134,19 +127,15 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar components.add(beanDefinition); } - register.registerBeanDefinition(ed.getIdentifier(), definition, !isAnonymous(ed.getIdentifier())); + register.registerBeanDefinition(ed.getIdentifier(), definition, !AuxiliaryUtils.isAnonymous(ed.getIdentifier())); } @Override public void visit(PipelineComponentDefinition ed) { - RootBeanDefinition definition = new RootBeanDefinition(); - PropertyValue name = new PropertyValue("name", ed.getName()); + RootBeanDefinition definition = asSpringDefinition(ed); PropertyValue pipeline = new PropertyValue("pipeline", new RuntimeBeanReference(ed.getPipeline())); - definition.setBeanClass(ed.resolveType()); - definition.getPropertyValues().addPropertyValue(name); - - if (isAnonymous(ed.getPipeline())) { + if (AuxiliaryUtils.isAnonymous(ed.getPipeline())) { context.getRegistered(ed.getPipeline()).visit(this); definition.getPropertyValues().add("pipeline", register.getBeanDefinition(ed.getPipeline())); } else { @@ -157,10 +146,6 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar register.registerBeanDefinition(ed.getIdentifier(), definition); } - private boolean isAnonymous(String name) { - return name.contains("anonymous-pipeline"); - } - private void appendAttributes(ElementDefinition ed, RootBeanDefinition definition) { List attributes = ed.getAttributes(); AttributeValueResolver resolver = AttributeValueResolver.getInstance(); @@ -180,8 +165,6 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar } PropertyValue value = new PropertyValue(attribute.getName(), holder.getValue()); definition.getPropertyValues().addPropertyValue(value); - - } definition.getPropertyValues().add("attributes", ed.getAttributes()); @@ -189,20 +172,39 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar @Override public void visitBasic(ElementDefinition ed) { - RootBeanDefinition definition = new RootBeanDefinition(); - PropertyValue name = new PropertyValue("name", ed.getName()); + RootBeanDefinition definition = asSpringDefinition(ed); - definition.setBeanClass(ed.resolveType()); - definition.getPropertyValues().addPropertyValue(name); + String execute = ed.getExecute(); - if (isType(ed.getExecute())) { + if (isType(execute)) { RootBeanDefinition conditionDef = new RootBeanDefinition(); - conditionDef.setBeanClassName(ed.getExecute()); + conditionDef.setBeanClassName(execute); definition.getPropertyValues().add("executable", conditionDef); + } else if (ed.getContext().getRegistered(execute) != null) { + ed.getContext().getRegistered(execute).visit(this); + BeanDefinition beanDefinition = register.getBeanDefinition(execute); + Class beanType = beanDefinition != null ? AuxiliaryUtils.asClass(beanDefinition.getBeanClassName()) : null; + if (beanType != null && ScriptExecutor.class.isAssignableFrom(beanType)) { + RootBeanDefinition rbd = new RootBeanDefinition(); + rbd.setBeanClass(ScriptExecutable.class); + rbd.getPropertyValues().addPropertyValue("scriptExecutor", register.isSpringBean(execute) ? new RuntimeBeanReference(execute) : beanDefinition); + + String rollbackName = getRollbackScriptName(execute); + + if (ed.getContext().getRegistered(rollbackName) != null) { + ed.getContext().getRegistered(rollbackName).visit(this); + BeanDefinition rollbackRbd = register.getBeanDefinition(execute); + beanType = rollbackRbd != null ? AuxiliaryUtils.asClass(rollbackRbd.getBeanClassName()) : null; + if (beanType != null && ScriptExecutor.class.isAssignableFrom(beanType)) { + rbd.getPropertyValues().addPropertyValue("rollbackExecutor", register.isSpringBean(rollbackName) ? new RuntimeBeanReference(rollbackName) : rollbackRbd); + } + } + definition.getPropertyValues().add("executable", rbd); + } else { + definition.getPropertyValues().add("executable", new RuntimeBeanReference(execute)); + } } else { - // TODO wrap scriptExecutor with script condition - AssertUtil.notBlank(ed.getExecute(), "component element ref must not be null!"); - definition.getPropertyValues().add("executable", new RuntimeBeanReference(ed.getExecute())); + definition.getPropertyValues().add("executable", new RuntimeBeanReference(execute)); } AttributeHolder degradeCallback = ed.getAttributes().stream() @@ -236,22 +238,28 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar @Override public void visit(IfElementDefinition ed) { - RootBeanDefinition definition = new RootBeanDefinition(); - PropertyValue name = new PropertyValue("name", ed.getName()); + RootBeanDefinition definition = asSpringDefinition(ed); - definition.setBeanClass(ed.resolveType()); - definition.getPropertyValues().addPropertyValue(name); + String test = ed.getTest(); - if (isType(ed.getTest())) { + if (isType(test)) { RootBeanDefinition conditionDef = new RootBeanDefinition(); - conditionDef.setBeanClassName(ed.getTest()); + conditionDef.setBeanClassName(test); definition.getPropertyValues().add("condition", conditionDef); + } else if (ed.getContext().getRegistered(test) != null) { + ed.getContext().getRegistered(test).visit(this); + BeanDefinition beanDefinition = register.getBeanDefinition(test); + Class beanType = beanDefinition != null ? AuxiliaryUtils.asClass(beanDefinition.getBeanClassName()) : null; + if (beanType != null && ScriptExecutor.class.isAssignableFrom(beanType)) { + RootBeanDefinition rbd = new RootBeanDefinition(); + rbd.setBeanClass(ScriptCondition.class); + rbd.getPropertyValues().addPropertyValue("scriptExecutor", register.isSpringBean(test) ? new RuntimeBeanReference(test) : beanDefinition); + definition.getPropertyValues().add("condition", rbd); + } else { + definition.getPropertyValues().add("condition", new RuntimeBeanReference(test)); + } } else { - AssertUtil.notBlank(ed.getTest(), "if element ref must not be null!"); - - // TODO wrap scriptExecutor with script condition - - definition.getPropertyValues().add("condition", new RuntimeBeanReference(ed.getTest())); + definition.getPropertyValues().add("condition", new RuntimeBeanReference(test)); } ed.getIfThenRef().visit(this); @@ -272,20 +280,29 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar @Override public void visit(ChooseDefinition ed) { - RootBeanDefinition definition = new RootBeanDefinition(); - PropertyValue name = new PropertyValue("name", ed.getName()); - - definition.setBeanClass(ed.resolveType()); - definition.getPropertyValues().addPropertyValue(name); + RootBeanDefinition definition = asSpringDefinition(ed); definition.getPropertyValues().add("allBranchWasString", true); - if (isType(ed.getTest())) { + String test = ed.getTest(); + + if (isType(test)) { RootBeanDefinition conditionDef = new RootBeanDefinition(); - conditionDef.setBeanClassName(ed.getTest()); + conditionDef.setBeanClassName(test); definition.getPropertyValues().add("condition", conditionDef); + } else if (ed.getContext().getRegistered(test) != null) { + ed.getContext().getRegistered(test).visit(this); + BeanDefinition beanDefinition = register.getBeanDefinition(test); + Class beanType = beanDefinition != null ? AuxiliaryUtils.asClass(beanDefinition.getBeanClassName()) : null; + if (beanType != null && ScriptExecutor.class.isAssignableFrom(beanType)) { + RootBeanDefinition rbd = new RootBeanDefinition(); + rbd.setBeanClass(ScriptCondition.class); + rbd.getPropertyValues().addPropertyValue("scriptExecutor", register.isSpringBean(test) ? new RuntimeBeanReference(test) : beanDefinition); + definition.getPropertyValues().add("condition", rbd); + } else { + definition.getPropertyValues().add("condition", new RuntimeBeanReference(test)); + } } else { - // TODO wrap scriptExecutor with script condition - definition.getPropertyValues().add("condition", new RuntimeBeanReference(ed.getTest())); + definition.getPropertyValues().add("condition", new RuntimeBeanReference(test)); } ManagedMap managedMap = new ManagedMap<>(); @@ -312,18 +329,13 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar @Override public void visit(AdapterDefinition ed) { - RootBeanDefinition definition = new RootBeanDefinition(); - PropertyValue name = new PropertyValue("name", ed.getName()); - - definition.setBeanClass(ed.resolveType()); - definition.getPropertyValues().addPropertyValue(name); + RootBeanDefinition definition = asSpringDefinition(ed); if (isType(ed.getExecute())) { RootBeanDefinition adapterDef = new RootBeanDefinition(); adapterDef.setBeanClassName(ed.getExecute()); definition.getPropertyValues().add("adapter", adapterDef); } else { - AssertUtil.notNull(ed.getExecute(), "adapter[execute] must not be null"); definition.getPropertyValues().add("adapter", new RuntimeBeanReference(ed.getExecute())); } @@ -335,4 +347,12 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar register.registerBeanDefinition(ed.getIdentifier(), definition); } + + private RootBeanDefinition asSpringDefinition(ElementDefinition ed) { + RootBeanDefinition definition = new RootBeanDefinition(); + PropertyValue name = new PropertyValue("name", ed.getName()); + definition.setBeanClass(ed.resolveType()); + definition.getPropertyValues().addPropertyValue(name); + return definition; + } } diff --git a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/ProxyParser.java b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/ProxyParser.java index 4e6d94e..e83dc60 100644 --- a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/ProxyParser.java +++ b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/ProxyParser.java @@ -16,10 +16,25 @@ import org.w3c.dom.Element; */ public class ProxyParser implements BeanDefinitionParser { + private static final ProxyParser INSTANCE = new ProxyParser(); + + private org.smartboot.flow.core.parser.ParserContext context; + + public static ProxyParser getInstance() { + return INSTANCE; + } + + public org.smartboot.flow.core.parser.ParserContext getContext() { + return context; + } + @Override public BeanDefinition parse(Element element, ParserContext parserContext) { + if (context == null) { + context = new org.smartboot.flow.core.parser.ParserContext(); + } + SmartFlowRegistrar.registerAll(parserContext.getRegistry()); - org.smartboot.flow.core.parser.ParserContext context = new org.smartboot.flow.core.parser.ParserContext(); // Ensure identifier in spring scope is unique. context.setIdentifierManager(new SpringIdentifierManager(parserContext.getRegistry())); @@ -27,8 +42,10 @@ public class ProxyParser implements BeanDefinitionParser { AssertUtil.notNull(parser, "Could not find parse for element " + ElementUtils.getName(element)); parser.parseElement(element, context); - BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(parserContext.getRegistry(), context); - context.getRegistered().forEach(visitor::visit); return null; } + + public void reset() { + this.context = null; + } } diff --git a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SmartFlowBeanFactoryRegistry.java b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SmartFlowBeanFactoryRegistry.java new file mode 100644 index 0000000..0df368a --- /dev/null +++ b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SmartFlowBeanFactoryRegistry.java @@ -0,0 +1,36 @@ +package org.smartboot.flow.spring.extension; + +import org.smartboot.flow.core.parser.ParserContext; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; + +/** + * @author qinluo + * @date 2023/3/12 12:41 + * @since 1.0.0 + */ +public class SmartFlowBeanFactoryRegistry implements BeanDefinitionRegistryPostProcessor { + + private BeanDefinitionRegistry registry; + + @Override + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { + this.registry = registry; + } + + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + try { + // Touch visit all parsed elements after all bean definition loaded. + ParserContext ctx = ProxyParser.getInstance().getContext(); + if (ctx != null) { + BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(registry, ctx); + ctx.getRegistered().forEach(visitor::visit); + } + } finally { + ProxyParser.getInstance().reset(); + } + } +} diff --git a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SmartFlowRegistrar.java b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SmartFlowRegistrar.java index cc3818e..abadd1f 100644 --- a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SmartFlowRegistrar.java +++ b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SmartFlowRegistrar.java @@ -13,8 +13,8 @@ import org.springframework.core.type.AnnotationMetadata; */ public class SmartFlowRegistrar implements ImportBeanDefinitionRegistrar { - private static volatile boolean registered = false; private static final String NOTIFIER_PROCESS = "smart-flow-notifer-processor"; + private static final String REGISTRY_NAME = "smart-flow-registry-processor"; @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) { @@ -23,15 +23,24 @@ public class SmartFlowRegistrar implements ImportBeanDefinitionRegistrar { public static void registerAll(BeanDefinitionRegistry registry) { registerNotifier(registry); + registerRegistry(registry); } private static void registerNotifier(BeanDefinitionRegistry registry) { - if (registered) { + if (registry.containsBeanDefinition(NOTIFIER_PROCESS)) { return; } - registered = true; RootBeanDefinition definition = new RootBeanDefinition(); definition.setBeanClass(NotifierProcessor.class); registry.registerBeanDefinition(NOTIFIER_PROCESS, definition); } + + private static void registerRegistry(BeanDefinitionRegistry registry) { + if (registry.containsBeanDefinition(REGISTRY_NAME)) { + return; + } + RootBeanDefinition definition = new RootBeanDefinition(); + definition.setBeanClass(SmartFlowBeanFactoryRegistry.class); + registry.registerBeanDefinition(REGISTRY_NAME, definition); + } } diff --git a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SpringNamespaceHandler.java b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SpringNamespaceHandler.java index 621c1b3..cef2a25 100644 --- a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SpringNamespaceHandler.java +++ b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SpringNamespaceHandler.java @@ -11,7 +11,7 @@ public class SpringNamespaceHandler extends NamespaceHandlerSupport { @Override public void init() { - ProxyParser proxyParser = new ProxyParser(); + ProxyParser proxyParser = ProxyParser.getInstance(); this.registerBeanDefinitionParser("pipeline", proxyParser); this.registerBeanDefinitionParser("engine", proxyParser); this.registerBeanDefinitionParser("script", proxyParser); -- Gitee From 517f67fab809265fccc67be853f487bda4eae0b5 Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Sun, 12 Mar 2023 14:31:59 +0800 Subject: [PATCH 07/24] =?UTF-8?q?qinluo:=20js=E5=88=AB=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/META-INF/smart-flow-script | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/smart-flow-script-condition/smart-flow-script-groovy/src/main/resources/META-INF/smart-flow-script b/smart-flow-script-condition/smart-flow-script-groovy/src/main/resources/META-INF/smart-flow-script index 32cafc6..d6e4f86 100644 --- a/smart-flow-script-condition/smart-flow-script-groovy/src/main/resources/META-INF/smart-flow-script +++ b/smart-flow-script-condition/smart-flow-script-groovy/src/main/resources/META-INF/smart-flow-script @@ -1,2 +1,3 @@ groovy=org.smartboot.flow.condition.extension.groovy.GroovyScriptExecutor -javascript=org.smartboot.flow.condition.extension.groovy.JavaScriptExecutor \ No newline at end of file +javascript=org.smartboot.flow.condition.extension.groovy.JavaScriptExecutor +js=org.smartboot.flow.condition.extension.groovy.JavaScriptExecutor \ No newline at end of file -- Gitee From c103204e7c39dd95544202cafa05c30573d5ff64 Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Sun, 12 Mar 2023 14:45:09 +0800 Subject: [PATCH 08/24] =?UTF-8?q?qinluo:=20=E5=9B=9E=E6=BB=BE=E9=8C=AF?= =?UTF-8?q?=E8=AA=A4=E6=97=A5=E5=BF=97=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/org/smartboot/flow/core/Pipeline.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/Pipeline.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/Pipeline.java index 5902e04..cbbbf45 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/Pipeline.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/Pipeline.java @@ -81,7 +81,7 @@ public class Pipeline implements Rollback, Describable, Validator, M try { WrappedInvoker.rollback(context, component); } catch (Exception e) { - LOGGER.error("{} rollback failed {}", this.name, component.describe()); + LOGGER.error("{} rollback failed {}", this.name, component.describe(), e); } } -- Gitee From de7513e2d4c24739262f9a2033e024aceec005ca Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Sun, 12 Mar 2023 15:16:06 +0800 Subject: [PATCH 09/24] =?UTF-8?q?qinluo:=20g6Visitor=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java b/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java index 3a02bbb..539d4b5 100644 --- a/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java +++ b/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java @@ -332,7 +332,7 @@ public class G6ComponentVisitor extends ComponentVisitor { G6ComponentVisitor.this.scriptName = scriptExecutor.getName(); G6ComponentVisitor.this.scriptType = scriptExecutor.getType(); G6ComponentVisitor.this.script = scriptExecutor.getScript(); - G6ComponentVisitor.this.condition = scriptExecutor.getName(); + G6ComponentVisitor.this.condition = nc.getName(); } } } -- Gitee From 71fccdb26f9cbd6e465473b1385b77354d3e7a97 Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Sun, 12 Mar 2023 15:26:30 +0800 Subject: [PATCH 10/24] =?UTF-8?q?qinluo:=20=E9=87=8D=E5=A4=8D=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../extension/BeanDefinitionVisitor.java | 68 +++++++------------ 1 file changed, 26 insertions(+), 42 deletions(-) diff --git a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java index f0ba265..f225c5c 100644 --- a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java +++ b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java @@ -240,27 +240,7 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar public void visit(IfElementDefinition ed) { RootBeanDefinition definition = asSpringDefinition(ed); - String test = ed.getTest(); - - if (isType(test)) { - RootBeanDefinition conditionDef = new RootBeanDefinition(); - conditionDef.setBeanClassName(test); - definition.getPropertyValues().add("condition", conditionDef); - } else if (ed.getContext().getRegistered(test) != null) { - ed.getContext().getRegistered(test).visit(this); - BeanDefinition beanDefinition = register.getBeanDefinition(test); - Class beanType = beanDefinition != null ? AuxiliaryUtils.asClass(beanDefinition.getBeanClassName()) : null; - if (beanType != null && ScriptExecutor.class.isAssignableFrom(beanType)) { - RootBeanDefinition rbd = new RootBeanDefinition(); - rbd.setBeanClass(ScriptCondition.class); - rbd.getPropertyValues().addPropertyValue("scriptExecutor", register.isSpringBean(test) ? new RuntimeBeanReference(test) : beanDefinition); - definition.getPropertyValues().add("condition", rbd); - } else { - definition.getPropertyValues().add("condition", new RuntimeBeanReference(test)); - } - } else { - definition.getPropertyValues().add("condition", new RuntimeBeanReference(test)); - } + processCondition(ed, definition); ed.getIfThenRef().visit(this); BeanDefinition beanDefinition = register.getBeanDefinition(ed.getIfThenRef().getIdentifier()); @@ -283,27 +263,7 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar RootBeanDefinition definition = asSpringDefinition(ed); definition.getPropertyValues().add("allBranchWasString", true); - String test = ed.getTest(); - - if (isType(test)) { - RootBeanDefinition conditionDef = new RootBeanDefinition(); - conditionDef.setBeanClassName(test); - definition.getPropertyValues().add("condition", conditionDef); - } else if (ed.getContext().getRegistered(test) != null) { - ed.getContext().getRegistered(test).visit(this); - BeanDefinition beanDefinition = register.getBeanDefinition(test); - Class beanType = beanDefinition != null ? AuxiliaryUtils.asClass(beanDefinition.getBeanClassName()) : null; - if (beanType != null && ScriptExecutor.class.isAssignableFrom(beanType)) { - RootBeanDefinition rbd = new RootBeanDefinition(); - rbd.setBeanClass(ScriptCondition.class); - rbd.getPropertyValues().addPropertyValue("scriptExecutor", register.isSpringBean(test) ? new RuntimeBeanReference(test) : beanDefinition); - definition.getPropertyValues().add("condition", rbd); - } else { - definition.getPropertyValues().add("condition", new RuntimeBeanReference(test)); - } - } else { - definition.getPropertyValues().add("condition", new RuntimeBeanReference(test)); - } + processCondition(ed, definition); ManagedMap managedMap = new ManagedMap<>(); List chooseCaseList = ed.getChooseCaseList(); @@ -327,6 +287,30 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar register.registerBeanDefinition(ed.getIdentifier(), definition); } + private void processCondition(ElementDefinition ed, RootBeanDefinition definition) { + String test = ed.getTest(); + + if (isType(test)) { + RootBeanDefinition conditionDef = new RootBeanDefinition(); + conditionDef.setBeanClassName(test); + definition.getPropertyValues().add("condition", conditionDef); + } else if (ed.getContext().getRegistered(test) != null) { + ed.getContext().getRegistered(test).visit(this); + BeanDefinition beanDefinition = register.getBeanDefinition(test); + Class beanType = beanDefinition != null ? AuxiliaryUtils.asClass(beanDefinition.getBeanClassName()) : null; + if (beanType != null && ScriptExecutor.class.isAssignableFrom(beanType)) { + RootBeanDefinition rbd = new RootBeanDefinition(); + rbd.setBeanClass(ScriptCondition.class); + rbd.getPropertyValues().addPropertyValue("scriptExecutor", register.isSpringBean(test) ? new RuntimeBeanReference(test) : beanDefinition); + definition.getPropertyValues().add("condition", rbd); + } else { + definition.getPropertyValues().add("condition", new RuntimeBeanReference(test)); + } + } else { + definition.getPropertyValues().add("condition", new RuntimeBeanReference(test)); + } + } + @Override public void visit(AdapterDefinition ed) { RootBeanDefinition definition = asSpringDefinition(ed); -- Gitee From b315d1f45e0b1999bf6a96d068c20727438b7da5 Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Mon, 13 Mar 2023 00:24:56 +0800 Subject: [PATCH 11/24] =?UTF-8?q?qinluo:=20=E8=84=9A=E6=9C=AC=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=99=A8=E6=A0=87=E7=AD=BE=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flow/core/parser/DefaultParser.java | 6 +++- .../core/parser/ElementParserRegistry.java | 1 + .../flow/core/parser/ParseConstants.java | 1 + .../flow/core/parser/ParserContext.java | 15 +++++++++ .../flow/core/parser/ScriptLoaderParser.java | 31 +++++++++++++++++++ .../flow/core/util/AuxiliaryUtils.java | 17 ++++++++++ .../src/main/resources/smart-flow-1.0.1.xsd | 27 ++++++++++++++++ .../flow/helper/view/XmlComponentVisitor.java | 4 ++- .../SmartFlowBeanFactoryRegistry.java | 2 ++ 9 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ScriptLoaderParser.java diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/DefaultParser.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/DefaultParser.java index 9d76a3b..5f3c34c 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/DefaultParser.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/DefaultParser.java @@ -26,6 +26,7 @@ public class DefaultParser implements Parser { ALLOWED.add(ParseConstants.ENGINE); ALLOWED.add(ParseConstants.PIPELINE); ALLOWED.add(ParseConstants.SCRIPT); + ALLOWED.add(ParseConstants.SCRIPT_LOADER); } private BuilderDefinitionVisitor visitor; @@ -72,7 +73,10 @@ public class DefaultParser implements Parser { useCache = useCache || Boolean.parseBoolean(root.getAttribute("useCache")); } - // Load script in locations. + // Load script inside xml. + context.getScriptLoaders().forEach(p -> p.load(context)); + + // Load script outside xml. scriptLoader.load(context); if (assemble) { diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ElementParserRegistry.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ElementParserRegistry.java index 250a95b..5334ba3 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ElementParserRegistry.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ElementParserRegistry.java @@ -21,6 +21,7 @@ public class ElementParserRegistry { this.register("engine", new EngineElementParser()); this.register("script", new ScriptElementParser()); this.register("adapter", new AdapterElementParser()); + this.register("script-loader", new ScriptLoaderParser()); } public static ElementParserRegistry getInstance() { diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ParseConstants.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ParseConstants.java index 2689c8d..300a0c5 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ParseConstants.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ParseConstants.java @@ -17,6 +17,7 @@ public interface ParseConstants { String COMPONENT = "component"; String ADAPTER = "adapter"; String SCRIPT = "script"; + String SCRIPT_LOADER = "script-loader"; /** * Attributes; diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ParserContext.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ParserContext.java index 6dc1592..0671609 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ParserContext.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ParserContext.java @@ -39,6 +39,21 @@ public class ParserContext { private final Map generatedIdentifiers = new ConcurrentHashMap<>(); private IdentifierManager identifierManager = new DefaultIdentifierManager(); + /** + * Script loaders in xml. + * + * @since 1.0.8 + */ + private final List scriptLoaders = new ArrayList<>(); + + public List getScriptLoaders() { + return scriptLoaders; + } + + public void addScriptLoad(ScriptLoader loader) { + this.scriptLoaders.add(loader); + } + public IdentifierManager getIdentifierManager() { return identifierManager; } diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ScriptLoaderParser.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ScriptLoaderParser.java new file mode 100644 index 0000000..4b98566 --- /dev/null +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/parser/ScriptLoaderParser.java @@ -0,0 +1,31 @@ +package org.smartboot.flow.core.parser; + +import org.smartboot.flow.core.util.AssertUtil; +import org.smartboot.flow.core.util.AuxiliaryUtils; +import org.w3c.dom.Element; + +/** + * @author qinluo + * @date 2023/3/12 23:57 + * @since 1.0.0 + */ +public class ScriptLoaderParser extends AbstractElementParser { + + @Override + public String getElementName() { + return ParseConstants.SCRIPT_LOADER; + } + + @Override + public void doParse(Element element, ParserContext context) { + String locations = element.getAttribute("locations"); + AssertUtil.notBlank(locations, "script-loader locations must not be null!"); + + ScriptLoader loader = new ScriptLoader(); + loader.locations(AuxiliaryUtils.splitByComma(locations).toArray(new String[0])); + + loader.exclude(AuxiliaryUtils.splitByComma(element.getAttribute("exclude")).toArray(new String[0])); + loader.accept(AuxiliaryUtils.splitByComma(element.getAttribute("accept")).toArray(new String[0])); + context.addScriptLoad(loader); + } +} diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/util/AuxiliaryUtils.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/util/AuxiliaryUtils.java index b5da7a7..7c2e93a 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/util/AuxiliaryUtils.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/util/AuxiliaryUtils.java @@ -1,6 +1,7 @@ package org.smartboot.flow.core.util; import java.util.ArrayList; +import java.util.Collections; import java.util.List; /** @@ -67,4 +68,20 @@ public final class AuxiliaryUtils { return values; } + + public static List splitByComma(String value) { + if (value == null || value.trim().length() == 0) { + return Collections.emptyList(); + } + + List values = new ArrayList<>(); + String[] split = value.split(",+"); + for (String val : split) { + if (val.trim().length() != 0) { + values.add(val); + } + } + + return values; + } } diff --git a/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd b/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd index bf9a97b..5c42fc2 100644 --- a/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd +++ b/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd @@ -108,6 +108,7 @@ + @@ -115,6 +116,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java index 457878a..f18c12d 100644 --- a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java +++ b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java @@ -112,7 +112,6 @@ public class XmlComponentVisitor extends ComponentVisitor { ScriptCollector.collect(rollbackScriptType, rollbackScript, rollbackScriptName); } - if (type == ComponentType.BASIC) { AuxiliaryUtils.appendTab(content, numbersOfTab); content.append(" rollbackExecutor = sc.getRollbackExecutor(); if (rollbackExecutor != null) { XmlComponentVisitor.this.rollbackScriptName = rollbackExecutor.getName(); diff --git a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SmartFlowBeanFactoryRegistry.java b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SmartFlowBeanFactoryRegistry.java index 0df368a..559dfd8 100644 --- a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SmartFlowBeanFactoryRegistry.java +++ b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SmartFlowBeanFactoryRegistry.java @@ -26,6 +26,8 @@ public class SmartFlowBeanFactoryRegistry implements BeanDefinitionRegistryPostP // Touch visit all parsed elements after all bean definition loaded. ParserContext ctx = ProxyParser.getInstance().getContext(); if (ctx != null) { + // Load script inside xml. + ctx.getScriptLoaders().forEach(p -> p.load(ctx)); BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(registry, ctx); ctx.getRegistered().forEach(visitor::visit); } -- Gitee From 8d7a1da1b28c4044a5d78f8aa9b1645719ecc2bd Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Mon, 13 Mar 2023 00:43:46 +0800 Subject: [PATCH 12/24] =?UTF-8?q?qinluo:=20script-loader=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E6=9D=A1=E4=BB=B6=E7=BA=A6=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/smart-flow-1.0.1.xsd | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd b/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd index 5c42fc2..86750af 100644 --- a/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd +++ b/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd @@ -126,18 +126,33 @@ + + + + + + + + + + + + + + + -- Gitee From 30971282cea45bf02a8322f67afe26a675b9f43d Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Mon, 13 Mar 2023 00:44:34 +0800 Subject: [PATCH 13/24] =?UTF-8?q?qinluo:=20script-loader=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E6=9D=A1=E4=BB=B6=E7=BA=A6=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd b/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd index 86750af..050b3ea 100644 --- a/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd +++ b/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd @@ -128,7 +128,7 @@ - + -- Gitee From 5687155029cf84ec6e3ace0d6c247b3348b9d977 Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Mon, 13 Mar 2023 14:37:54 +0800 Subject: [PATCH 14/24] qinluo: SpringNamespaceHandler --- .../smartboot/flow/core/util/AuxiliaryUtils.java | 2 +- .../src/main/resources/smart-flow-1.0.1.xsd | 15 --------------- .../spring/extension/SpringNamespaceHandler.java | 1 + 3 files changed, 2 insertions(+), 16 deletions(-) diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/util/AuxiliaryUtils.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/util/AuxiliaryUtils.java index 7c2e93a..69ddde8 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/util/AuxiliaryUtils.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/util/AuxiliaryUtils.java @@ -78,7 +78,7 @@ public final class AuxiliaryUtils { String[] split = value.split(",+"); for (String val : split) { if (val.trim().length() != 0) { - values.add(val); + values.add(val.trim()); } } diff --git a/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd b/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd index 050b3ea..5c42fc2 100644 --- a/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd +++ b/smart-flow-core/src/main/resources/smart-flow-1.0.1.xsd @@ -126,33 +126,18 @@ - - - - - - - - - - - - - - - diff --git a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SpringNamespaceHandler.java b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SpringNamespaceHandler.java index cef2a25..e33379c 100644 --- a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SpringNamespaceHandler.java +++ b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/SpringNamespaceHandler.java @@ -15,5 +15,6 @@ public class SpringNamespaceHandler extends NamespaceHandlerSupport { this.registerBeanDefinitionParser("pipeline", proxyParser); this.registerBeanDefinitionParser("engine", proxyParser); this.registerBeanDefinitionParser("script", proxyParser); + this.registerBeanDefinitionParser("script-loader", proxyParser); } } -- Gitee From 00477272236820d69b528461fa325d66b3c53d67 Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Thu, 16 Mar 2023 09:34:06 +0800 Subject: [PATCH 15/24] =?UTF-8?q?qinluo:=20G6=E5=85=BC=E5=AE=B9=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E7=BB=84=E4=BB=B6=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/views/Engines/history-detail.vue | 1 + .../src/views/ReportManager/trace_detail.vue | 40 +++++++++++++++---- .../admin/controller/EnginesController.java | 2 +- .../flow/admin/g6/G6ComponentVisitor.java | 17 +++++--- .../extension/BeanDefinitionVisitor.java | 2 +- 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/dashboard/src/views/Engines/history-detail.vue b/dashboard/src/views/Engines/history-detail.vue index cf41ecd..3433733 100644 --- a/dashboard/src/views/Engines/history-detail.vue +++ b/dashboard/src/views/Engines/history-detail.vue @@ -106,6 +106,7 @@ export default { selectionClipboard: false, // 选择剪切板 automaticLayout: true, // 自动布局 codeLens: false, // 代码镜头 + tabCompletion: 'on', scrollBeyondLastLine: false, // 滚动完最后一行后再滚动一屏幕 colorDecorators: true, // 颜色装饰器 accessibilitySupport: "off", // 辅助功能支持 "auto" | "off" | "on" diff --git a/dashboard/src/views/ReportManager/trace_detail.vue b/dashboard/src/views/ReportManager/trace_detail.vue index 1ece85f..0a043c3 100644 --- a/dashboard/src/views/ReportManager/trace_detail.vue +++ b/dashboard/src/views/ReportManager/trace_detail.vue @@ -43,20 +43,46 @@ + + + + + + + +
- +
- + +
+ +
-
- +
+
- + +
+
+ + + + + + + + + +
+
+
-
diff --git a/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/controller/EnginesController.java b/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/controller/EnginesController.java index edc9f58..6249b12 100644 --- a/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/controller/EnginesController.java +++ b/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/controller/EnginesController.java @@ -180,7 +180,7 @@ public class EnginesController { PlantumlEngineVisitor visitor = new PlantumlEngineVisitor(plantuml.getAbsolutePath().substring(0, plantuml.getAbsolutePath().lastIndexOf("."))); visitor.visit(parser.getEngine(engineNames.get(0))); - SourceFileReader reader = new SourceFileReader(plantuml); + SourceFileReader reader = new SourceFileReader(plantuml, plantuml.getAbsoluteFile().getParentFile(), "UTF-8"); FileInputStream fis = new FileInputStream(reader.getGeneratedImages().get(0).getPngFile()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; diff --git a/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java b/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java index 539d4b5..67cc5ab 100644 --- a/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java +++ b/smart-flow-admin/src/main/java/org/smartboot/smart/flow/admin/g6/G6ComponentVisitor.java @@ -2,6 +2,7 @@ package org.smartboot.smart.flow.admin.g6; import org.smartboot.flow.core.Condition; import org.smartboot.flow.core.attribute.AttributeHolder; +import org.smartboot.flow.core.attribute.Attributes; import org.smartboot.flow.core.common.ComponentType; import org.smartboot.flow.core.executable.Executable; import org.smartboot.flow.core.script.ScriptCondition; @@ -16,6 +17,7 @@ import org.smartboot.flow.core.visitor.PipelineVisitor; import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * @author qinluo @@ -284,11 +286,18 @@ public class G6ComponentVisitor extends ComponentVisitor { ci.setDescribe(describe); ci.setType(type.name()); ci.setScript(script); - ci.setScriptName(scriptName); + + if (ci.getScript() != null) { + ci.setScriptName(scriptName + "." + scriptType); + } ci.setScriptType(scriptType); ci.setRollbackScript(rollbackScript); - ci.setRollbackScriptName(rollbackScriptName); + + if (ci.getRollbackScript() != null) { + ci.setRollbackScriptName(rollbackScriptName + "." + rollbackScriptType); + } + ci.setRollbackScriptType(rollbackScriptType); if (type == ComponentType.BASIC) { @@ -307,7 +316,7 @@ public class G6ComponentVisitor extends ComponentVisitor { ci.setAttributes(attributes); for (AttributeHolder ah : this.attributes) { - if (ah.getValue() == null) { + if (ah.getValue() == null || Objects.equals(ah.getAttribute(), Attributes.NAME)) { continue; } @@ -315,9 +324,7 @@ public class G6ComponentVisitor extends ComponentVisitor { ai.setName(ah.getAttribute().getName()); ai.setValue(String.valueOf(ah.getValue())); ai.setRemark(ah.getAttribute().getDescription()); - attributes.add(ai); - } return ci; diff --git a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java index f225c5c..a1051de 100644 --- a/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java +++ b/smart-flow-spring-extension/src/main/java/org/smartboot/flow/spring/extension/BeanDefinitionVisitor.java @@ -91,7 +91,7 @@ public class BeanDefinitionVisitor implements DefinitionVisitor, BeanFactoryAwar RootBeanDefinition definition = asSpringDefinition(ed); PropertyValue script = new PropertyValue("script", sed.getScript()); definition.getPropertyValues().addPropertyValue(script); - register.registerBeanDefinition(ed.getIdentifier(), definition, false); + register.registerBeanDefinition(ed.getIdentifier(), definition, true); } } else { -- Gitee From 8d89baffcb0c2dd4035f3f9e54606f446a5d6f0e Mon Sep 17 00:00:00 2001 From: qinluo <1558642210@qq.com> Date: Thu, 16 Mar 2023 20:02:59 +0800 Subject: [PATCH 16/24] =?UTF-8?q?qinluo:=20=E9=A1=B5=E9=9D=A2=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dashboard/package-lock.json | 19 +++++ dashboard/package.json | 2 + dashboard/src/main.ts | 30 +++++++ .../src/views/ReportManager/trace_detail.vue | 84 ++++++++----------- 4 files changed, 85 insertions(+), 50 deletions(-) diff --git a/dashboard/package-lock.json b/dashboard/package-lock.json index 1d1ab39..151fdc2 100644 --- a/dashboard/package-lock.json +++ b/dashboard/package-lock.json @@ -11,9 +11,11 @@ "@antv/g2": "v4.2.8", "@antv/g6": "^4.8.5", "@antv/vis-predict-engine": "^0.1.1", + "@highlightjs/vue-plugin": "^2.1.0", "@layui/layui-vue": "1.8.4", "axios": "^1.2.1", "echarts": "^5.4.1", + "highlight.js": "^11.7.0", "js-base64": "^3.7.2", "mockjs": "^1.1.0", "monaco-editor": "^0.34.1", @@ -848,6 +850,15 @@ "node": ">=12" } }, + "node_modules/@highlightjs/vue-plugin": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/@highlightjs/vue-plugin/-/vue-plugin-2.1.0.tgz", + "integrity": "sha512-E+bmk4ncca+hBEYRV2a+1aIzIV0VSY/e5ArjpuSN9IO7wBJrzUE2u4ESCwrbQD7sAy+jWQjkV5qCCWgc+pu7CQ==", + "peerDependencies": { + "highlight.js": "^11.0.1", + "vue": "^3" + } + }, "node_modules/@intlify/core-base": { "version": "9.1.10", "resolved": "https://registry.npmmirror.com/@intlify/core-base/-/core-base-9.1.10.tgz", @@ -2449,6 +2460,14 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/highlight.js": { + "version": "11.7.0", + "resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-11.7.0.tgz", + "integrity": "sha512-1rRqesRFhMO/PRF+G86evnyJkCgaZFOI+Z6kdj15TA18funfoqJXvgPCLSf0SWq3SRfg1j3HlDs8o4s3EGq1oQ==", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/htmlparser2": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-7.2.0.tgz", diff --git a/dashboard/package.json b/dashboard/package.json index 7ba33b7..201ab52 100644 --- a/dashboard/package.json +++ b/dashboard/package.json @@ -11,9 +11,11 @@ "@antv/g2": "v4.2.8", "@antv/g6": "^4.8.5", "@antv/vis-predict-engine": "^0.1.1", + "@highlightjs/vue-plugin": "^2.1.0", "@layui/layui-vue": "1.8.4", "axios": "^1.2.1", "echarts": "^5.4.1", + "highlight.js": "^11.7.0", "js-base64": "^3.7.2", "mockjs": "^1.1.0", "monaco-editor": "^0.34.1", diff --git a/dashboard/src/main.ts b/dashboard/src/main.ts index 7c98cff..7173ca1 100644 --- a/dashboard/src/main.ts +++ b/dashboard/src/main.ts @@ -13,3 +13,33 @@ app.use(Router); app.directive("permission",permission); app.mount('#app'); + +// 代码高亮插件 +import hljs from 'highlight.js'; +// 必须导入 否则没样式 +import 'highlight.js/styles/github-dark-dimmed.css'; +import 'highlight.js/lib/common'; +import hjsVuePlugin from '@highlightjs/vue-plugin'; +const high: any = { + deep: true, + bind: function (el: any, binding: any) { + const targets = el.querySelectorAll('code') + targets.forEach((target: any) => { + if (binding.value) { + target.textContent = binding.value; + } + (hljs as any).highlightBlock(target); + }) + }, + componentUpdated: function (el: any, binding: any) { + const targets = el.querySelectorAll('code') + targets.forEach((target: any) => { + if (binding.value) { + target.textContent = binding.value; + (hljs as any).highlightBlock(target); + } + }) + } +} +app.directive('highlightjs', high) +app.use(hjsVuePlugin); diff --git a/dashboard/src/views/ReportManager/trace_detail.vue b/dashboard/src/views/ReportManager/trace_detail.vue index 0a043c3..a65ede8 100644 --- a/dashboard/src/views/ReportManager/trace_detail.vue +++ b/dashboard/src/views/ReportManager/trace_detail.vue @@ -30,7 +30,6 @@
-
@@ -43,21 +42,10 @@
- - - - - - - -
-
-
- +
@@ -70,20 +58,12 @@ - - - - - - - -
-
- +
+ +
@@ -92,10 +72,8 @@
-
- +
@@ -122,11 +100,6 @@ + + + + + + + + +
+
+
+
+
+ + + diff --git a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/AdapterComponent.java b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/AdapterComponent.java index 7e7101f..f1f5c2f 100644 --- a/smart-flow-core/src/main/java/org/smartboot/flow/core/component/AdapterComponent.java +++ b/smart-flow-core/src/main/java/org/smartboot/flow/core/component/AdapterComponent.java @@ -87,6 +87,8 @@ public class AdapterComponent extends Component { @Override public void accept(ComponentVisitor visitor) { visitor.visitAttributes(attributes); + // Ignored executable visitor. + visitor.visitExecutable(this.adapter.describe()); visitor.visitSource(this); ComponentVisitor componentVisitor = visitor.visitComponent(component.getType(), component.getName(), component.describe()); if (componentVisitor != null) { diff --git a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/ScriptCollector.java b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/ScriptCollector.java index e291e9e..f34770b 100644 --- a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/ScriptCollector.java +++ b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/ScriptCollector.java @@ -15,16 +15,20 @@ import java.util.Map; public class ScriptCollector { private static final ThreadLocal HOLDER = new ThreadLocal<>(); + + /** + * script-name : script-content : script-type + */ private final Map> scripts = new HashMap<>(); public static void start() { HOLDER.set(new ScriptCollector()); } - public static void collect(String condition, String script, String name) { + public static void collect(String name, String script, String type) { ScriptCollector collector = HOLDER.get(); if (collector != null) { - collector.scripts.put(condition, Pair.of(name, script)); + collector.scripts.put(name, Pair.of(script, type)); } } diff --git a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java index f18c12d..34aa108 100644 --- a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java +++ b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlComponentVisitor.java @@ -105,11 +105,11 @@ public class XmlComponentVisitor extends ComponentVisitor { } if (script != null) { - ScriptCollector.collect(scriptType, script, scriptName); + ScriptCollector.collect(scriptName, script, scriptType); } if (rollbackScript != null) { - ScriptCollector.collect(rollbackScriptType, rollbackScript, rollbackScriptName); + ScriptCollector.collect(rollbackScriptName, rollbackScript, rollbackScriptType); } if (type == ComponentType.BASIC) { diff --git a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlEngineVisitor.java b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlEngineVisitor.java index 901c05d..9aa646c 100644 --- a/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlEngineVisitor.java +++ b/smart-flow-helper/src/main/java/org/smartboot/flow/helper/view/XmlEngineVisitor.java @@ -57,9 +57,9 @@ public class XmlEngineVisitor extends EngineVisitor { Map> scripts = ScriptCollector.end(); if (scripts != null && scripts.size() > 0) { - scripts.forEach((k, v) -> content.append("\n\t")); + scripts.forEach((k, v) -> content.append("\n\t")); } content.append("\n").append(END).append("\n"); -- Gitee