From 455d06000b0469d4673f83bceed950fff3cc83d5 Mon Sep 17 00:00:00 2001 From: Harrison Date: Thu, 16 Jun 2022 19:25:46 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-73800=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=91windows?= =?UTF-8?q?=E4=B8=8B=E6=B2=A1=E6=9C=89finedb=E6=9D=83=E9=99=90=EF=BC=8C?= =?UTF-8?q?=E8=AE=BE=E8=AE=A1=E5=99=A8=E6=8A=A5=E9=94=99=E8=84=8F=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=20=E6=8A=8A=E8=84=8F=E6=95=B0=E6=8D=AE=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9=E5=90=8E=EF=BC=8C=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E5=8D=B3=E4=BE=BF=E5=8F=AA=E6=98=AF=E9=94=81=E4=BD=8F?= =?UTF-8?q?=E3=80=81=E6=B2=A1=E6=9D=83=E9=99=90=E4=B9=9F=E4=BC=9A=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E8=BF=99=E4=B8=AA=E9=97=AE=E9=A2=98=E3=80=82=20?= =?UTF-8?q?=E6=89=80=E4=BB=A5=EF=BC=8C=E8=BF=99=E9=87=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=A3=80=E6=B5=8B=E9=93=BE=EF=BC=8C=E5=89=8D?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=A3=80=E6=B5=8B=E5=90=8E=EF=BC=8C=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E6=89=8D=E8=83=BD=E7=BB=A7=E7=BB=AD=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E3=80=82=20=E5=B9=B6=E5=9C=A8=E5=AF=B9=E5=85=B7?= =?UTF-8?q?=E4=BD=93=E7=9A=84=E7=B1=BB=E5=9E=8B=E6=A3=80=E6=B5=8B=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E4=BF=9D=E6=8C=81=E5=8E=9F=E6=9D=A5=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fr/env/detect/base/DetectorBridge.java | 11 +++-- .../fr/env/detect/base/DetectorManager.java | 33 ++++++++++++- .../com/fr/env/detect/impl/DetectorChain.java | 46 +++++++++++++++++++ .../impl/converter/FineDbLockedConverter.java | 12 ++++- .../converter/FineDbPermissionConverter.java | 45 ++++++++++++++---- 5 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 designer-base/src/main/java/com/fr/env/detect/impl/DetectorChain.java diff --git a/designer-base/src/main/java/com/fr/env/detect/base/DetectorBridge.java b/designer-base/src/main/java/com/fr/env/detect/base/DetectorBridge.java index 798e0ae047..d5291bb7f6 100644 --- a/designer-base/src/main/java/com/fr/env/detect/base/DetectorBridge.java +++ b/designer-base/src/main/java/com/fr/env/detect/base/DetectorBridge.java @@ -2,6 +2,7 @@ package com.fr.env.detect.base; import com.fr.env.detect.bean.DetectorResult; import com.fr.env.detect.bean.DetectorType; +import com.fr.env.detect.impl.DetectorChain; import com.fr.env.detect.impl.FineDbDirtyDetector; import com.fr.env.detect.impl.FineDbLockedDetector; import com.fr.env.detect.impl.FineDbPermissionDetector; @@ -41,10 +42,12 @@ public class DetectorBridge { protected @NotNull DetectorManager compute() { DetectorManager manager = new DetectorManager(); - - manager.register(new FineDbDirtyDetector()); - manager.register(new FineDbPermissionDetector()); - manager.register(new FineDbLockedDetector()); + + // 按照顺序构造检测链 + manager.register(DetectorChain.construct( + new FineDbLockedDetector(), + new FineDbPermissionDetector(), + new FineDbDirtyDetector())); manager.register(new JarInconsistentDetector()); manager.register(new JarLackDetector()); diff --git a/designer-base/src/main/java/com/fr/env/detect/base/DetectorManager.java b/designer-base/src/main/java/com/fr/env/detect/base/DetectorManager.java index bc65870da7..2ebbf331ea 100644 --- a/designer-base/src/main/java/com/fr/env/detect/base/DetectorManager.java +++ b/designer-base/src/main/java/com/fr/env/detect/base/DetectorManager.java @@ -2,8 +2,10 @@ package com.fr.env.detect.base; import com.fr.env.detect.bean.DetectorResult; import com.fr.env.detect.bean.DetectorType; +import com.fr.env.detect.impl.DetectorChain; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -17,8 +19,23 @@ import java.util.stream.Stream; **/ class DetectorManager { + /** + * 检测链的列表 + */ + private List chains = new ArrayList<>(); + + /** + * 单纯的检测器的列表 + */ private List detectors = new ArrayList<>(); + public void register(DetectorChain chain) { + + if (chain != null) { + chains.add(chain); + } + } + public void register(ExceptionDetector detector) { if (detector != null) { @@ -32,7 +49,13 @@ class DetectorManager { .map(ExceptionDetector::detect) .filter(Objects::nonNull); - List resultList = results.collect(Collectors.toList()); + Stream chainResults = chains.stream() + .map(DetectorChain::detect) + .filter(Objects::nonNull); + + List resultList = Stream.concat(results, chainResults) + .collect(Collectors.toList()); + resultList.forEach(DetectorResult::log); return resultList.stream(); @@ -40,7 +63,13 @@ class DetectorManager { public DetectorResult detect(DetectorType type) { - Optional result = detectors.stream() + Stream chainDetectors = chains.stream() + .map(DetectorChain::getDetectors) + .flatMap(Collection::stream); + + Stream allDetectors = Stream.concat(detectors.stream(), chainDetectors); + + Optional result = allDetectors .filter((detector -> type == detector.type())) .findFirst() .map(ExceptionDetector::detect); diff --git a/designer-base/src/main/java/com/fr/env/detect/impl/DetectorChain.java b/designer-base/src/main/java/com/fr/env/detect/impl/DetectorChain.java new file mode 100644 index 0000000000..352e72c6b8 --- /dev/null +++ b/designer-base/src/main/java/com/fr/env/detect/impl/DetectorChain.java @@ -0,0 +1,46 @@ +package com.fr.env.detect.impl; + +import com.fr.env.detect.base.ExceptionDetector; +import com.fr.env.detect.bean.DetectorResult; +import com.fr.env.detect.bean.DetectorStatus; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * 检测链 + * 责任链模式, 有先后顺序,哪一个先获得,哪一个先终止。 + * + * created by Harrison on 2022/06/16 + **/ +public class DetectorChain { + + private List detectors = new ArrayList<>(); + + public static DetectorChain construct(ExceptionDetector... detectors) { + + DetectorChain detectorChain = new DetectorChain(); + detectorChain.detectors = Arrays.stream(detectors).collect(Collectors.toList()); + return detectorChain; + } + + @Nullable + public DetectorResult detect() { + + for (ExceptionDetector detector : detectors) { + DetectorResult result = detector.detect(); + if (result != null && result.getStatus() == DetectorStatus.EXCEPTION) { + return result; + } + } + return null; + } + + public List getDetectors() { + + return this.detectors; + } +} diff --git a/designer-base/src/main/java/com/fr/env/detect/impl/converter/FineDbLockedConverter.java b/designer-base/src/main/java/com/fr/env/detect/impl/converter/FineDbLockedConverter.java index 8a5e74f0b5..2b627a7b22 100644 --- a/designer-base/src/main/java/com/fr/env/detect/impl/converter/FineDbLockedConverter.java +++ b/designer-base/src/main/java/com/fr/env/detect/impl/converter/FineDbLockedConverter.java @@ -7,6 +7,7 @@ import com.fr.env.detect.bean.DetectorType; import com.fr.env.detect.thowable.ThrowableConverter; import com.fr.third.org.apache.commons.lang3.StringUtils; import com.fr.third.org.hsqldb.HsqlException; +import com.fr.workspace.WorkContext; /** * created by Harrison on 2022/05/24 @@ -36,12 +37,21 @@ public class FineDbLockedConverter implements ThrowableConverter { */ @Override public DetectorResult convert(Throwable throwable) { + + // 远程不执行 + if (!WorkContext.getCurrent().isLocal()) { + return null; + } Throwable sign = throwable; - while (sign.getClass() != HsqlException.class) { + while (sign != null && sign.getClass() != HsqlException.class) { sign = sign.getCause(); } + if (sign == null) { + return null; + } + DetectorType type = DetectorType.FINE_DB_LOCKED; String message = sign.getMessage(); diff --git a/designer-base/src/main/java/com/fr/env/detect/impl/converter/FineDbPermissionConverter.java b/designer-base/src/main/java/com/fr/env/detect/impl/converter/FineDbPermissionConverter.java index dd350abdf4..d34e7c245d 100644 --- a/designer-base/src/main/java/com/fr/env/detect/impl/converter/FineDbPermissionConverter.java +++ b/designer-base/src/main/java/com/fr/env/detect/impl/converter/FineDbPermissionConverter.java @@ -5,17 +5,24 @@ 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.thowable.ThrowableConverter; -import com.fr.third.org.apache.commons.lang3.StringUtils; +import com.fr.stable.project.ProjectConstants; +import com.fr.third.org.apache.commons.io.FileUtils; import com.fr.third.org.hsqldb.HsqlException; +import com.fr.workspace.WorkContext; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.RandomAccessFile; +import java.util.Collection; /** * HSQL 下的权限检测 - * + *

* created by Harrison on 2022/05/24 **/ public class FineDbPermissionConverter implements ThrowableConverter { - public static final String PERMISSION = "permission"; + public static final String EMBED_DB_NAME = "finedb"; @Override public boolean accept(Throwable throwable) { @@ -33,15 +40,37 @@ public class FineDbPermissionConverter implements ThrowableConverter { @Override public DetectorResult convert(Throwable throwable) { + // 远程不执行 + if (!WorkContext.getCurrent().isLocal()) { + return null; + } + Throwable sign = throwable; - while (sign.getClass() != HsqlException.class) { + while (sign != null && sign.getClass() != HsqlException.class) { sign = sign.getCause(); } + + if (sign == null) { + return null; + } - DetectorType type = DetectorType.FINE_DB_PERMISSION; - String message = sign.getMessage(); - if (StringUtils.containsIgnoreCase(message, PERMISSION)) { - + String fineDbDirectory = WorkContext.getCurrent().getPath() + File.separator + ProjectConstants.EMBED_DB_DIRECTORY + File.separator + EMBED_DB_NAME; + Collection files = FileUtils.listFiles(new File(fineDbDirectory), null, true); + Boolean isPermitted = files.stream() + .map((file -> { + try { + // 进行权限判断 + new RandomAccessFile(file, "rw"); + return true; + } catch (FileNotFoundException e) { + return false; + } + })) + .reduce((a, b) -> a & b) + .orElse(Boolean.FALSE); + + if (!isPermitted) { + DetectorType type = DetectorType.FINE_DB_PERMISSION; return DetectorResult.builder() .withType(type) .withTips(Toolkit.i18nText(type.getTipsLocale()))