You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
3.8 KiB
139 lines
3.8 KiB
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.FineDbDirtyDetector; |
|
import com.fr.env.detect.impl.FineDbLockedDetector; |
|
import com.fr.env.detect.impl.FineDbPermissionDetector; |
|
import com.fr.env.detect.impl.JarConflictDetector; |
|
import com.fr.env.detect.impl.JarInconsistentDetector; |
|
import com.fr.env.detect.impl.JarLackDetector; |
|
import com.fr.env.detect.thowable.ThrowableLogAppender; |
|
import com.fr.env.detect.thowable.ThrowableStore; |
|
import com.fr.value.NotNullLazyValue; |
|
import org.jetbrains.annotations.NotNull; |
|
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
import java.util.stream.Stream; |
|
|
|
/** |
|
* 检测器桥接逻辑 |
|
* [detect-core] - bridge - ui |
|
* |
|
* created by Harrison on 2022/05/13 |
|
**/ |
|
public class DetectorBridge { |
|
|
|
public static DetectorBridge getInstance() { |
|
|
|
return DetectorBridgeHolder.INSTANCE; |
|
} |
|
|
|
private static class DetectorBridgeHolder { |
|
|
|
private static final DetectorBridge INSTANCE = new DetectorBridge(); |
|
} |
|
|
|
private final NotNullLazyValue<DetectorManager> detectorManager = new NotNullLazyValue<DetectorManager>() { |
|
|
|
@Override |
|
protected @NotNull DetectorManager compute() { |
|
|
|
DetectorManager manager = new DetectorManager(); |
|
|
|
manager.register(new FineDbDirtyDetector()); |
|
manager.register(new FineDbPermissionDetector()); |
|
manager.register(new FineDbLockedDetector()); |
|
|
|
manager.register(new JarInconsistentDetector()); |
|
manager.register(new JarLackDetector()); |
|
manager.register(new JarConflictDetector()); |
|
return manager; |
|
} |
|
}; |
|
|
|
private final AtomicBoolean hasStarted = new AtomicBoolean(false); |
|
|
|
public void start() { |
|
|
|
if (!hasStarted.get() && EnvDetectorConfig.getInstance().isEnabled()) { |
|
// 开始注册异常处理 |
|
ThrowableLogAppender.getInstance().enable(); |
|
hasStarted.set(true); |
|
} |
|
} |
|
|
|
public void stop() { |
|
|
|
if (hasStarted.compareAndSet(true, false)) { |
|
// 关闭异常处理 |
|
ThrowableLogAppender.getInstance().disable(); |
|
} |
|
} |
|
|
|
public void reset() { |
|
|
|
ThrowableStore.getInstance().reset(); |
|
} |
|
|
|
/** |
|
* 针对某一项进行检测 |
|
* 主要用于手动检测时 |
|
* |
|
* @param type 检测类型 |
|
* @return 检测结果 |
|
*/ |
|
@NotNull |
|
public DetectorResult detect(DetectorType type) { |
|
|
|
return detectorManager.getValue().detect(type); |
|
} |
|
|
|
/** |
|
* 针对某一项 \ 某一个异常进行检测 |
|
* |
|
* @param type 类型 |
|
* @param throwable 异常 |
|
* @return 结果 |
|
*/ |
|
@NotNull |
|
public DetectorResult detect(DetectorType type, Throwable throwable) { |
|
|
|
ThrowableStore.getInstance().add(throwable); |
|
|
|
DetectorResult result = detect(type); |
|
|
|
ThrowableStore.getInstance().reset(); |
|
|
|
return result; |
|
|
|
} |
|
|
|
/** |
|
* 异常检测 |
|
* 对异常统一检测 |
|
* |
|
* @return 能够检测出的异常情况 |
|
*/ |
|
public Stream<DetectorResult> detect() { |
|
|
|
return detectorManager.getValue().detect(); |
|
} |
|
|
|
/** |
|
* 异常检测 |
|
* 当遇到异常时,且异常难以处理,直接导致服务器启动失败时,调用 |
|
* 将异常添加进来,统一检测 |
|
* |
|
* @param throwable 异常 |
|
* @return 检测结果 |
|
*/ |
|
public Stream<DetectorResult> detect(Throwable throwable) { |
|
|
|
ThrowableStore.getInstance().add(throwable); |
|
Stream<DetectorResult> result = detect(); |
|
ThrowableStore.getInstance().reset(); |
|
|
|
return result; |
|
} |
|
}
|
|
|