From 7b310988b03e07febe3bc8ba75397a0c1eab29e8 Mon Sep 17 00:00:00 2001 From: hanbingleixue Date: Tue, 6 Aug 2024 11:26:43 +0800 Subject: [PATCH] Fixed the issue where a NullPointerException would occur when the data type of the configuration item is a collection and the configuration content is empty. Signed-off-by: hanbingleixue --- .../core/config/utils/ConfigValueUtil.java | 6 ++++++ .../io/sermant/core/event/EventCollector.java | 21 ++++++++++++++++--- .../collector/FrameworkEventCollector.java | 4 ---- .../event/collector/LogEventCollector.java | 8 ++----- .../common/event/RouterEventCollector.java | 4 ---- .../SpringBootRegistryEventCollector.java | 4 ---- 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/config/utils/ConfigValueUtil.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/config/utils/ConfigValueUtil.java index c6b810425b..d4709594c3 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/config/utils/ConfigValueUtil.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/config/utils/ConfigValueUtil.java @@ -207,6 +207,9 @@ public static Set toSetType(String configStr, Class type) { } private static void parseConfigToCollection(String configStr, Class type, Collection result) { + if (StringUtils.isBlank(configStr)) { + return; + } for (String configSlice : configStr.split(CONFIG_SEPARATOR)) { final R obj = toBaseType(configSlice.trim(), type); if (obj == null) { @@ -240,6 +243,9 @@ private static String buildTransformErrMsg(String configSlice, String typeName) */ public static Map toMapType(String configStr, Class keyType, Class valueType) { final Map result = new HashMap(); + if (StringUtils.isBlank(configStr)) { + return result; + } for (String kvSlice : configStr.split(CONFIG_SEPARATOR)) { final String[] kvEntry = kvSlice.trim().split(":"); if (kvEntry.length != MAP_KV_LEN) { diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/EventCollector.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/EventCollector.java index 6a36afe4fd..12ce27d89b 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/EventCollector.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/EventCollector.java @@ -37,12 +37,15 @@ public class EventCollector { private static final Logger LOGGER = LoggerFactory.getLogger(); + /** + * Event configuration. Set as protected for easy use by subclasses + */ + protected EventConfig eventConfig = ConfigManager.getConfig(EventConfig.class); + // BlockingQueue for event cache. If the queue is not full, event is reported periodically. If // the queue is full, event is reported automatically private final BlockingQueue eventQueue = new ArrayBlockingQueue<>(100); - private final EventConfig eventConfig = ConfigManager.getConfig(EventConfig.class); - private final ConcurrentHashMap eventInfoOfferTimeCache = new ConcurrentHashMap<>(); /** @@ -69,7 +72,7 @@ public final BlockingQueue collect() { * @return result */ public boolean offerEvent(Event event) { - if (!eventConfig.isEnable()) { + if (!isEnableEvent()) { return false; } if (event.getEventInfo() != null) { @@ -126,4 +129,16 @@ protected void cleanOfferTimeCacheMap() { } } } + + /** + * Check if the event is enabled + * + * @return Verification results + */ + protected boolean isEnableEvent() { + if (eventConfig == null) { + eventConfig = ConfigManager.getConfig(EventConfig.class); + } + return eventConfig != null && eventConfig.isEnable(); + } } diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/collector/FrameworkEventCollector.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/collector/FrameworkEventCollector.java index f6c50fecbc..01e74f6941 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/collector/FrameworkEventCollector.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/collector/FrameworkEventCollector.java @@ -16,11 +16,9 @@ package io.sermant.core.event.collector; -import io.sermant.core.config.ConfigManager; import io.sermant.core.event.Event; import io.sermant.core.event.EventCollector; import io.sermant.core.event.EventInfo; -import io.sermant.core.event.config.EventConfig; /** * Framework event collector @@ -31,8 +29,6 @@ public class FrameworkEventCollector extends EventCollector { private static FrameworkEventCollector frameworkEventCollector; - private final EventConfig eventConfig = ConfigManager.getConfig(EventConfig.class); - private FrameworkEventCollector() { } diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/collector/LogEventCollector.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/collector/LogEventCollector.java index 4ad42901d7..392c3763de 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/collector/LogEventCollector.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/collector/LogEventCollector.java @@ -16,13 +16,11 @@ package io.sermant.core.event.collector; -import io.sermant.core.config.ConfigManager; import io.sermant.core.event.Event; import io.sermant.core.event.EventCollector; import io.sermant.core.event.EventLevel; import io.sermant.core.event.EventType; import io.sermant.core.event.LogInfo; -import io.sermant.core.event.config.EventConfig; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.LogRecord; @@ -36,8 +34,6 @@ public class LogEventCollector extends EventCollector { private static LogEventCollector logEventCollector; - private final EventConfig eventConfig = ConfigManager.getConfig(EventConfig.class); - private final ConcurrentHashMap logInfoOfferTimeCache = new ConcurrentHashMap<>(); private LogEventCollector() { @@ -61,7 +57,7 @@ public static synchronized LogEventCollector getInstance() { * @param record log record */ public void offerWarning(LogRecord record) { - if (!eventConfig.isEnable() || !eventConfig.isOfferWarnLog()) { + if (!isEnableEvent() || !eventConfig.isOfferWarnLog()) { return; } LogInfo logInfo = new LogInfo(record); @@ -77,7 +73,7 @@ public void offerWarning(LogRecord record) { * @param record log record */ public void offerError(LogRecord record) { - if (!eventConfig.isEnable() || !eventConfig.isOfferErrorLog()) { + if (!isEnableEvent() || !eventConfig.isOfferErrorLog()) { return; } LogInfo logInfo = new LogInfo(record); diff --git a/sermant-plugins/sermant-router/router-common/src/main/java/io/sermant/router/common/event/RouterEventCollector.java b/sermant-plugins/sermant-router/router-common/src/main/java/io/sermant/router/common/event/RouterEventCollector.java index 56cdc3337d..345ba5f86f 100644 --- a/sermant-plugins/sermant-router/router-common/src/main/java/io/sermant/router/common/event/RouterEventCollector.java +++ b/sermant-plugins/sermant-router/router-common/src/main/java/io/sermant/router/common/event/RouterEventCollector.java @@ -16,12 +16,10 @@ package io.sermant.router.common.event; -import io.sermant.core.config.ConfigManager; import io.sermant.core.event.Event; import io.sermant.core.event.EventCollector; import io.sermant.core.event.EventInfo; import io.sermant.core.event.EventManager; -import io.sermant.core.event.config.EventConfig; /** * Routing plugin event collector @@ -32,8 +30,6 @@ public class RouterEventCollector extends EventCollector { private static volatile RouterEventCollector routerEventCollector; - private final EventConfig eventConfig = ConfigManager.getConfig(EventConfig.class); - private RouterEventCollector() { } diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/io/sermant/discovery/event/SpringBootRegistryEventCollector.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/io/sermant/discovery/event/SpringBootRegistryEventCollector.java index 999b43483c..1d5e43f67b 100644 --- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/io/sermant/discovery/event/SpringBootRegistryEventCollector.java +++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/io/sermant/discovery/event/SpringBootRegistryEventCollector.java @@ -16,12 +16,10 @@ package io.sermant.discovery.event; -import io.sermant.core.config.ConfigManager; import io.sermant.core.event.Event; import io.sermant.core.event.EventCollector; import io.sermant.core.event.EventInfo; import io.sermant.core.event.EventManager; -import io.sermant.core.event.config.EventConfig; import io.sermant.discovery.entity.DefaultServiceInstance; /** @@ -33,8 +31,6 @@ public class SpringBootRegistryEventCollector extends EventCollector { private static volatile SpringBootRegistryEventCollector collector; - private final EventConfig eventConfig = ConfigManager.getConfig(EventConfig.class); - private SpringBootRegistryEventCollector() { }