From d07862e68525a767fb000c96ba6e939b3a73500d Mon Sep 17 00:00:00 2001 From: jason <2353220944@qq.com> Date: Wed, 27 Nov 2024 21:24:06 +0800 Subject: [PATCH] fix npe --- .../internals/RemoteConfigRepository.java | 24 ++++++++++++------- .../apollo/core/dto/ApolloConfig.java | 1 - .../apollo/core/dto/ConfigurationChange.java | 11 ++------- .../apollo/core/enums/ConfigSyncType.java | 10 +++++--- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java index 7017db31..93413ab6 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java @@ -48,10 +48,8 @@ import com.google.common.net.UrlEscapers; import com.google.common.util.concurrent.RateLimiter; import com.google.gson.Gson; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Properties; + +import java.util.*; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -249,10 +247,12 @@ private ApolloConfig loadApolloConfig() { } ApolloConfig result = response.getBody(); - - ConfigSyncType configSyncType=ConfigSyncType.fromString(result.getConfigSyncType()); - if(configSyncType!=null&&configSyncType.equals(ConfigSyncType.INCREMENTALSYNC)){ - result.setConfigurations(mergeConfigurations(m_configCache.get().getConfigurations(),result.getConfigurationChanges())); + if(result!=null){ + ConfigSyncType configSyncType=ConfigSyncType.fromString(result.getConfigSyncType()); + if(configSyncType!=null&&configSyncType.equals(ConfigSyncType.INCREMENTALSYNC)){ + ApolloConfig apolloConfig = m_configCache.get()==null?new ApolloConfig():m_configCache.get(); + result.setConfigurations(mergeConfigurations(apolloConfig.getConfigurations(),result.getConfigurationChanges())); + } } logger.debug("Loaded config for {}: {}", m_namespace, result); @@ -363,7 +363,13 @@ private List getConfigServices() { } private Map mergeConfigurations(Map configurations,List configurationChanges) { - Map newConfigurations = Maps.newHashMap(configurations); + Map newConfigurations = new HashMap<>(); + if (configurationChanges == null) { + return newConfigurations; + } + if(configurations!=null){ + Maps.newHashMap(configurations); + } for (ConfigurationChange change : configurationChanges) { switch (change.getConfigurationChangeType()) { case ADDED: diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ApolloConfig.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ApolloConfig.java index 4525ff9a..b1d296be 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ApolloConfig.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ApolloConfig.java @@ -16,7 +16,6 @@ */ package com.ctrip.framework.apollo.core.dto; -import com.google.common.collect.Lists; import java.util.List; import java.util.Map; diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ConfigurationChange.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ConfigurationChange.java index 82004a3c..506ea5a1 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ConfigurationChange.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/dto/ConfigurationChange.java @@ -25,8 +25,8 @@ */ public class ConfigurationChange { private final String key; - private String newValue; - private ConfigurationChangeType configurationChangeType; + private final String newValue; + private final ConfigurationChangeType configurationChangeType; /** * Constructor. @@ -51,13 +51,6 @@ public ConfigurationChangeType getConfigurationChangeType() { return configurationChangeType; } - public void setNewValue(String newValue) { - this.newValue = newValue; - } - - public void setConfigurationChangeType(ConfigurationChangeType configurationChangeType) { - this.configurationChangeType = configurationChangeType; - } @Override public String toString() { diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigSyncType.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigSyncType.java index 38f2edd0..48881558 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigSyncType.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigSyncType.java @@ -48,9 +48,13 @@ public static ConfigSyncType fromString(String value) { if (StringUtils.isEmpty(value)) { return FULLSYNC; } - return Stream.of(ConfigSyncType.values()).filter(type -> type.value.equals(value)) - .findFirst() - .orElseThrow(() -> new IllegalArgumentException(value + " can not map enum")); + for (ConfigSyncType type : values()) { + if (type.value.equals(value)) { + return type; + } + } + throw new IllegalArgumentException("Invalid ConfigSyncType: " + value); + } /**