diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/context/AbstractConfigManager.java b/dubbo-common/src/main/java/org/apache/dubbo/config/context/AbstractConfigManager.java index 95c1ea6c39f..cab26b6829f 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/config/context/AbstractConfigManager.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/config/context/AbstractConfigManager.java @@ -161,8 +161,10 @@ public final T addConfig(AbstractConfig config) { config.setScopeModel(scopeModel); } + Class targetConfigType = getTargetConfigType(config.getClass()); + Map configsMap = - configsCache.computeIfAbsent(getTagName(config.getClass()), type -> new ConcurrentHashMap<>()); + configsCache.computeIfAbsent(getTagName(targetConfigType), type -> new ConcurrentHashMap<>()); // fast check duplicated equivalent config before write lock if (!(config instanceof ReferenceConfigBase || config instanceof ServiceConfigBase)) { @@ -175,17 +177,21 @@ public final T addConfig(AbstractConfig config) { // lock by config type synchronized (configsMap) { - return (T) addIfAbsent(config, configsMap); + return (T) addIfAbsent(config, configsMap, targetConfigType); } } protected boolean isSupportConfigType(Class type) { + return getTargetConfigType(type) != null; + } + + protected Class getTargetConfigType(Class type) { for (Class supportedConfigType : supportedConfigTypes) { if (supportedConfigType.isAssignableFrom(type)) { - return true; + return supportedConfigType; } } - return false; + return null; } /** @@ -196,7 +202,9 @@ protected boolean isSupportConfigType(Class type) { * @return the existing equivalent config or the new adding config * @throws IllegalStateException */ - private C addIfAbsent(C config, Map configsMap) throws IllegalStateException { + private C addIfAbsent( + C config, Map configsMap, Class targetConfigType) + throws IllegalStateException { if (config == null || configsMap == null) { return config; @@ -218,7 +226,7 @@ private C addIfAbsent(C config, Map config C existedConfig = configsMap.get(key); if (existedConfig != null && !isEquals(existedConfig, config)) { - String type = config.getClass().getSimpleName(); + String type = targetConfigType.getSimpleName(); logger.warn( COMMON_UNEXPECTED_EXCEPTION, "", diff --git a/dubbo-common/src/test/java/org/apache/dubbo/config/context/ConfigManagerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/config/context/ConfigManagerTest.java index 9b2966a05b4..d51d43a1e93 100644 --- a/dubbo-common/src/test/java/org/apache/dubbo/config/context/ConfigManagerTest.java +++ b/dubbo-common/src/test/java/org/apache/dubbo/config/context/ConfigManagerTest.java @@ -251,6 +251,21 @@ void testAddConfig() { assertFalse(moduleConfigManager.getProviders().isEmpty()); } + @Test + void testAddCustomConfig() { + configManager.addConfig(new CustomRegistryConfig("CustomConfigManagerTest")); + + assertTrue(configManager.getRegistry("CustomConfigManagerTest").isPresent()); + } + + static class CustomRegistryConfig extends RegistryConfig { + + CustomRegistryConfig(String id) { + super(); + this.setId(id); + } + } + @Test void testRefreshAll() { configManager.refreshAll();