From 0c20e0c051f72b7ee5d19f5fd6006b8c3c116ab1 Mon Sep 17 00:00:00 2001 From: Harrison Date: Tue, 14 Jun 2022 20:57:46 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-73318=E3=80=90=E8=AE=BE=E8=AE=A1?= =?UTF-8?q?=E5=99=A8=E7=8E=AF=E5=A2=83=E7=9B=91=E6=B5=8B=E3=80=91=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8finedb=E6=9C=89=E8=84=8F=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=EF=BC=8C=E8=AE=BE=E8=AE=A1=E5=99=A8=E8=BF=9C=E7=A8=8B=E5=88=87?= =?UTF-8?q?=E6=8D=A2=EF=BC=8C=E8=BF=87=E7=A8=8B=E5=BE=88=E6=85=A2=E4=B8=94?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E8=87=AA=E5=8A=A8=E7=9B=91=E6=B5=8B=E5=BC=B9?= =?UTF-8?q?=E7=AA=97=201-=E7=AE=80=E5=8C=96=E6=93=8D=E4=BD=9C=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E5=8C=BA=E5=88=86=20=E8=AE=BE=E8=AE=A1=E5=99=A8/?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=20=E5=90=AF=E5=8A=A8=E3=80=82?= =?UTF-8?q?=E5=8F=AA=E5=9C=A8=E8=AE=BE=E8=AE=A1=E5=99=A8=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=90=8E=E8=BF=9B=E8=A1=8C=E6=A3=80=E6=B5=8B=202-=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E7=8E=AF=E5=A2=83=E7=9A=84=E6=A3=80=E6=B5=8B=E3=80=82?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E7=8E=AF=E5=A2=83=E5=90=8E=E4=B8=8A=E6=AC=A1?= =?UTF-8?q?=E7=9A=84=E4=BB=BB=E5=8A=A1=E9=9C=80=E8=A6=81=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E3=80=82=203-FineDB=20=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E4=BB=8E=E6=8D=95=E8=8E=B7=E6=A3=80=E6=B5=8B=EF=BC=8C=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E5=88=B0=E5=85=A8=E9=87=8F=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fr/env/detect/EnvDetectorCenter.java | 111 ++++++++---------- .../env/detect/impl/FineDbDirtyDetector.java | 100 ++++++++++++++-- .../impl/converter/FineDbDirtyConverter.java | 110 ----------------- 3 files changed, 138 insertions(+), 183 deletions(-) delete mode 100644 designer-base/src/main/java/com/fr/env/detect/impl/converter/FineDbDirtyConverter.java diff --git a/designer-base/src/main/java/com/fr/env/detect/EnvDetectorCenter.java b/designer-base/src/main/java/com/fr/env/detect/EnvDetectorCenter.java index 37e01f788a..a566caa847 100644 --- a/designer-base/src/main/java/com/fr/env/detect/EnvDetectorCenter.java +++ b/designer-base/src/main/java/com/fr/env/detect/EnvDetectorCenter.java @@ -17,58 +17,42 @@ import com.fr.event.Event; import com.fr.event.EventDispatcher; import com.fr.event.Listener; import com.fr.event.Null; -import com.fr.start.server.EmbedServerEvent; +import com.fr.stable.core.UUID; import com.fr.task.Once; import com.fr.update.delay.DelayHelper; import com.fr.workspace.Workspace; import com.fr.workspace.WorkspaceEvent; +import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; /** * 环境检测中心 - * 如果环境检测 -> * * created by Harrison on 2022/05/27 **/ public class EnvDetectorCenter { - private final Listener AFTER_START_LISTENER = new Listener() { - @Override - public void on(Event event, Null param) { - if (isSameProcess(DetectorProcess.SERVER_LAUNCH)) { - stop(); - } - } - }; - private final Listener BEFORE_START_LISTENER = new Listener() { - @Override - public void on(Event event, Null param) { - PROCESS.set(DetectorProcess.SERVER_LAUNCH); - start(); - } - }; - private final Listener START_UP_COMPLETE_LISTENER = new Listener() { @Override public void on(Event event, Null param) { - if (isSameProcess(DetectorProcess.DESIGN_LAUNCH)) { - stop(); - } + // startup 的监听,执行一次即可 + EventDispatcher.stopListen(this); + stop(); } }; private final Listener AFTER_SWITCH_LISTENER = new Listener() { @Override public void on(Event event, Workspace param) { - if (isSameProcess(DetectorProcess.DESIGN_LAUNCH)) { - stop(); - } + stop(); } }; @@ -82,6 +66,13 @@ public class EnvDetectorCenter { private final AtomicReference PROCESS = new AtomicReference<>(); + /** + * 当前还有什么动作未执行 + * 如果切换环境,这里的动作是要被清空的 + * 从而防止切换环境后, 上一个动作的环境延续过来 + */ + private final Set pendingActions = new HashSet<>(); + public static EnvDetectorCenter getInstance() { return EnvDetectorCenterHolder.INSTANCE; } @@ -105,7 +96,6 @@ public class EnvDetectorCenter { PROCESS.set(DetectorProcess.DESIGN_LAUNCH); launchOnce.run(); - listen(); } @@ -113,8 +103,10 @@ public class EnvDetectorCenter { * 销毁,一般用在模块关闭中 */ public void destroy() { + + // 清空 + pendingActions.clear(); - stopListen(); // 重置内容 DetectorBridge.getInstance().reset(); // 关闭逻辑 @@ -123,20 +115,6 @@ public class EnvDetectorCenter { PROCESS.set(null); } - /** - * 当前的流程符合预期 - * 什么情况下不符合? - * design.start -> server.start -> design.end -> server.end - * 这个时候 design.end 就不会触发,等待服务器启动后才触发 - * - * @param process 检测流程 - * @return 是 / 否 - */ - private boolean isSameProcess(DetectorProcess process) { - - return PROCESS.compareAndSet(process, null); - } - /** * 启动 */ @@ -153,13 +131,23 @@ public class EnvDetectorCenter { // 结束 DetectorBridge.getInstance().stop(); - // 30s后执行 - DelayHelper.delayCall(EnvDetectorCenter.class.getName(), () -> { + // id值 + String uuid = UUID.randomUUID().toString(); + // 确认当前的 action 是否有效 + Supplier validAction = () -> pendingActions.contains(uuid); + // 30s后执行 + Runnable detectorAction = () -> { + // 如果当前没开启,则直接返回 if (!EnvDetectorConfig.getInstance().isEnabled()) { return; } + + // 如果当前不包含这个 id, 就不执行 + if (!validAction.get()) { + return; + } Stream resultStream = DetectorBridge.getInstance().detect(); // 展示效果 @@ -174,10 +162,25 @@ public class EnvDetectorCenter { } UIUtil.invokeLaterIfNeeded(() -> { + + // 如果当前不包含这个 id, 就不执行 + if (!validAction.get()) { + return; + } NotificationDialog dialog = new NotificationDialog(properties, notificationModels); dialog.open(); }); - }, 30, TimeUnit.SECONDS); + + // 执行完移除 + pendingActions.remove(uuid); + + }; + + // 添加 + pendingActions.add(uuid); + + + DelayHelper.delayCall(EnvDetectorCenter.class.getName(), detectorAction, 30, TimeUnit.SECONDS); } @@ -208,18 +211,6 @@ public class EnvDetectorCenter { .collect(Collectors.toList()); } - private void listen() { - - // 内置服务器监听 - EventDispatcher.listen(EmbedServerEvent.BeforeStart, BEFORE_START_LISTENER); - EventDispatcher.listen(EmbedServerEvent.AfterStart, AFTER_START_LISTENER); - } - - private void stopListen() { - - EventDispatcher.stopListen(BEFORE_START_LISTENER); - EventDispatcher.stopListen(AFTER_START_LISTENER); - } private enum DetectorProcess { @@ -227,15 +218,5 @@ public class EnvDetectorCenter { * 设计器启动 */ DESIGN_LAUNCH, - - /** - * 服务器启动 - */ - SERVER_LAUNCH, - - /** - * 环境切换 - */ - ENV_SWITCH } } diff --git a/designer-base/src/main/java/com/fr/env/detect/impl/FineDbDirtyDetector.java b/designer-base/src/main/java/com/fr/env/detect/impl/FineDbDirtyDetector.java index 1b34597866..6e33eda43b 100644 --- a/designer-base/src/main/java/com/fr/env/detect/impl/FineDbDirtyDetector.java +++ b/designer-base/src/main/java/com/fr/env/detect/impl/FineDbDirtyDetector.java @@ -1,22 +1,106 @@ package com.fr.env.detect.impl; -import com.fr.env.detect.base.CatchExceptionDetector; +import com.fr.config.ConfigContext; +import com.fr.config.Configuration; +import com.fr.design.dialog.FineJOptionPane; +import com.fr.design.i18n.Toolkit; +import com.fr.env.detect.base.AbstractExceptionDetector; +import com.fr.env.detect.base.DetectorConstants; +import com.fr.env.detect.bean.DetectorResult; import com.fr.env.detect.bean.DetectorType; -import com.fr.env.detect.impl.converter.FineDbDirtyConverter; -import com.fr.env.detect.thowable.ThrowableStore; +import com.fr.env.detect.bean.ExceptionSolution; +import com.fr.env.detect.bean.Message; +import com.fr.env.detect.bean.SolutionAction; +import com.fr.io.utils.ResourceIOUtils; +import com.fr.log.FineLoggerFactory; +import com.fr.stable.StableUtils; +import com.fr.stable.project.ProjectConstants; +import com.fr.third.org.hibernate.exception.GenericJDBCException; + +import javax.swing.JOptionPane; +import java.util.Iterator; +import java.util.function.Function; /** * created by Harrison on 2022/05/25 **/ -public class FineDbDirtyDetector extends CatchExceptionDetector { +public class FineDbDirtyDetector extends AbstractExceptionDetector { public FineDbDirtyDetector() { - this(ThrowableStore.getInstance()); + + super(DetectorType.FINE_DB_DIRTY); } - public FineDbDirtyDetector(ThrowableStore throwableStore) { + @Override + public DetectorResult detect() { - super(DetectorType.FINE_DB_DIRTY, throwableStore, new FineDbDirtyConverter()); - } + Iterator tableNames = ConfigContext.getConfigNames(); + while (tableNames.hasNext()) { + String tableName = tableNames.next(); + Class configClass = ConfigContext.getConfigClass(tableName); + Configuration configuration = ConfigContext.getConfigInstance(configClass); + try { + + // 尝试获取每一个值 + configuration.mirror(); + } catch (Throwable e) { + + Function isDirtyExFunction = throwable -> { + while (throwable != null) { + if (throwable instanceof GenericJDBCException) { + return true; + } + throwable = throwable.getCause(); + } + return false; + }; + boolean isDirtyEx = isDirtyExFunction.apply(e); + + if (isDirtyEx) { + DetectorType detectorType = DetectorType.FINE_DB_DIRTY; + DetectorResult.DetectorResultBuilder builder = DetectorResult + .builder() + .withType(detectorType); + String tipsLocale = Toolkit.i18nText(detectorType.getTipsLocale(), tableName); + + String solutionLocale = detectorType.getSolutionLocale(); + ExceptionSolution exceptionSolution = new ExceptionSolution(new Message.Link(Toolkit.i18nText(solutionLocale, DetectorConstants.FINE_DB_HELP_LINK), DetectorConstants.FINE_DB_HELP_LINK), new SolutionAction() { + @Override + public String name() { + return Toolkit.i18nText("Fine-Design_Basic_Reset_Immediately"); + } + + @Override + public void run() { + boolean success = false; + try { + ResourceIOUtils.copy(StableUtils.pathJoin(ProjectConstants.EMBED_DB_DIRECTORY, ProjectConstants.FINE_DB_NAME), + StableUtils.pathJoin(ProjectConstants.EMBED_DB_DIRECTORY, ProjectConstants.FINE_DB_BAK_NAME)); + success = ResourceIOUtils.delete(StableUtils.pathJoin(ProjectConstants.EMBED_DB_DIRECTORY, ProjectConstants.FINE_DB_NAME)); + } catch (Exception e) { + FineLoggerFactory.getLogger().error(e.getMessage(), e); + } + // todo 最好的逻辑是,这里应该和 UI 隔离开的 + if (!success) { + FineJOptionPane.showMessageDialog(null, + Toolkit.i18nText("Fine-Design_Error_Finedb_Backup_Reset_Result", + ResourceIOUtils.getRealPath(StableUtils.pathJoin(ProjectConstants.EMBED_DB_DIRECTORY, ProjectConstants.FINE_DB_NAME))), + Toolkit.i18nText("Fine-Design_Basic_Error"), + JOptionPane.ERROR_MESSAGE); + } + } + }); + builder.withTips(tipsLocale) + .withSolution(exceptionSolution) + .withLog(Toolkit.i18nText(detectorType.getLogLocale(), tableName)); + + return builder.build(); + } + + } + } + + return DetectorResult.normal(DetectorType.FINE_DB_DIRTY); + } } diff --git a/designer-base/src/main/java/com/fr/env/detect/impl/converter/FineDbDirtyConverter.java b/designer-base/src/main/java/com/fr/env/detect/impl/converter/FineDbDirtyConverter.java deleted file mode 100644 index 4550cde4a2..0000000000 --- a/designer-base/src/main/java/com/fr/env/detect/impl/converter/FineDbDirtyConverter.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.fr.env.detect.impl.converter; - -import com.fr.config.ConfigContext; -import com.fr.config.Configuration; -import com.fr.design.dialog.FineJOptionPane; -import com.fr.design.i18n.Toolkit; -import com.fr.env.detect.base.DetectorConstants; -import com.fr.env.detect.bean.DetectorResult; -import com.fr.env.detect.bean.DetectorType; -import com.fr.env.detect.bean.ExceptionSolution; -import com.fr.env.detect.bean.Message; -import com.fr.env.detect.bean.SolutionAction; -import com.fr.env.detect.thowable.ThrowableConverter; -import com.fr.io.utils.ResourceIOUtils; -import com.fr.log.FineLoggerFactory; -import com.fr.stable.StableUtils; -import com.fr.stable.project.ProjectConstants; -import com.fr.third.org.hibernate.exception.GenericJDBCException; -import org.jetbrains.annotations.Nullable; - -import javax.swing.JOptionPane; -import java.util.Iterator; - -/** - * 脏数据检测 - * - * created by Harrison on 2022/05/25 - **/ -public class FineDbDirtyConverter implements ThrowableConverter { - - @Override - public boolean accept(Throwable throwable) { - - Throwable sign = throwable; - while (sign != null) { - if (sign.getClass() == GenericJDBCException.class) { - return true; - } - sign = sign.getCause(); - } - return false; - } - - /** - * 根据堆栈,确认是否是从配置中发出来的异常 - * 如果是,则找到对应的配置的表, - * 输出信息 - * - * @param throwable 异常 - * @return 检测结果 - */ - @Override - public @Nullable DetectorResult convert(Throwable throwable) { - - Iterator tableNames = ConfigContext.getConfigNames(); - while (tableNames.hasNext()) { - String tableName = tableNames.next(); - Class configClass = ConfigContext.getConfigClass(tableName); - Configuration configuration = ConfigContext.getConfigInstance(configClass); - try { - - // 尝试获取每一个值 - configuration.mirror(); - } catch (Throwable e) { - - DetectorType detectorType = DetectorType.FINE_DB_DIRTY; - DetectorResult.DetectorResultBuilder builder = DetectorResult.builder() - .withType(detectorType); - - String tipsLocale = Toolkit.i18nText(detectorType.getTipsLocale(), tableName); - - String solutionLocale = detectorType.getSolutionLocale(); - ExceptionSolution exceptionSolution = new ExceptionSolution(new Message.Link(Toolkit.i18nText(solutionLocale, DetectorConstants.FINE_DB_HELP_LINK), DetectorConstants.FINE_DB_HELP_LINK), new SolutionAction() { - @Override - public String name() { - return Toolkit.i18nText("Fine-Design_Basic_Reset_Immediately"); - } - - @Override - public void run() { - boolean success = false; - try { - ResourceIOUtils.copy(StableUtils.pathJoin(ProjectConstants.EMBED_DB_DIRECTORY, ProjectConstants.FINE_DB_NAME), - StableUtils.pathJoin(ProjectConstants.EMBED_DB_DIRECTORY, ProjectConstants.FINE_DB_BAK_NAME)); - success = ResourceIOUtils.delete(StableUtils.pathJoin(ProjectConstants.EMBED_DB_DIRECTORY, ProjectConstants.FINE_DB_NAME)); - } catch (Exception e) { - FineLoggerFactory.getLogger().error(e.getMessage(), e); - } - // todo 最好的逻辑是,这里应该和 UI 隔离开的 - if (!success) { - FineJOptionPane.showMessageDialog(null, - Toolkit.i18nText("Fine-Design_Error_Finedb_Backup_Reset_Result", - ResourceIOUtils.getRealPath(StableUtils.pathJoin(ProjectConstants.EMBED_DB_DIRECTORY, ProjectConstants.FINE_DB_NAME))), - Toolkit.i18nText("Fine-Design_Basic_Error"), - JOptionPane.ERROR_MESSAGE); - } - } - }); - builder.withTips(tipsLocale) - .withSolution(exceptionSolution) - .withLog(Toolkit.i18nText(detectorType.getLogLocale(), tableName)); - - return builder.build(); - } - } - - return DetectorResult.normal(DetectorType.FINE_DB_DIRTY); - } - -}