Skip to content

Commit

Permalink
polish some code (apolloconfig#3249)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredTan95 authored Aug 22, 2020
1 parent 6ff6ddd commit 077b176
Show file tree
Hide file tree
Showing 34 changed files with 120 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
@Component
public class NamespaceUnlockAspect {

private Gson gson = new Gson();
private static final Gson GSON = new Gson();

private final NamespaceLockService namespaceLockService;
private final NamespaceService namespaceService;
Expand Down Expand Up @@ -109,7 +109,7 @@ boolean isModified(Namespace namespace) {
return hasNormalItems(items);
}

Map<String, String> releasedConfiguration = gson.fromJson(release.getConfigurations(), GsonType.CONFIG);
Map<String, String> releasedConfiguration = GSON.fromJson(release.getConfigurations(), GsonType.CONFIG);
Map<String, String> configurationFromItems = generateConfigurationFromItems(namespace, items);

MapDifference<String, String> difference = Maps.difference(releasedConfiguration, configurationFromItems);
Expand Down Expand Up @@ -139,7 +139,7 @@ private Map<String, String> generateConfigurationFromItems(Namespace namespace,
} else {//child namespace
Release parentRelease = releaseService.findLatestActiveRelease(parentNamespace);
if (parentRelease != null) {
configurationFromItems = gson.fromJson(parentRelease.getConfigurations(), GsonType.CONFIG);
configurationFromItems = GSON.fromJson(parentRelease.getConfigurations(), GsonType.CONFIG);
}
generateMapFromItems(namespaceItems, configurationFromItems);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
@RestController
public class ReleaseHistoryController {

private Gson gson = new Gson();
private static final Gson GSON = new Gson();

private Type configurationTypeReference = new TypeToken<Map<String, Object>>() {
}.getType();

Expand Down Expand Up @@ -87,7 +88,7 @@ private PageDTO<ReleaseHistoryDTO> transform2PageDTO(Page<ReleaseHistory> releas
private ReleaseHistoryDTO transformReleaseHistory2DTO(ReleaseHistory releaseHistory) {
ReleaseHistoryDTO dto = new ReleaseHistoryDTO();
BeanUtils.copyProperties(releaseHistory, dto, "operationContext");
dto.setOperationContext(gson.fromJson(releaseHistory.getOperationContext(),
dto.setOperationContext(GSON.fromJson(releaseHistory.getOperationContext(),
configurationTypeReference));

return dto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ControllerIntegrationExceptionTest extends AbstractControllerTest {
@Autowired
AppService appService;

Gson gson = new Gson();
private static final Gson GSON = new Gson();

@Before
public void setUp() {
Expand Down Expand Up @@ -68,7 +68,7 @@ public void testCreateFailed() {
restTemplate.postForEntity(getBaseAppUrl(), dto, AppDTO.class);
} catch (HttpStatusCodeException e) {
@SuppressWarnings("unchecked")
Map<String, String> attr = gson.fromJson(e.getResponseBodyAsString(), Map.class);
Map<String, String> attr = GSON.fromJson(e.getResponseBodyAsString(), Map.class);
Assert.assertEquals("save failed", attr.get("message"));
}
App savedApp = appService.findOne(dto.getAppId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

public class ReleaseControllerTest extends AbstractControllerTest {

private static final Gson GSON = new Gson();
@Autowired
ReleaseRepository releaseRepository;

Expand Down Expand Up @@ -88,8 +89,8 @@ public void testReleaseBuild() {
configurations.put("k1", "v1");
configurations.put("k2", "v2");
configurations.put("k3", "v3");
Gson gson = new Gson();
Assert.assertEquals(gson.toJson(configurations), release.getConfigurations());

Assert.assertEquals(GSON.toJson(configurations), release.getConfigurations());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class BizConfig extends RefreshableConfig {
private static final int DEFAULT_RELEASE_MESSAGE_NOTIFICATION_BATCH_INTERVAL_IN_MILLI = 100;//100ms
private static final int DEFAULT_LONG_POLLING_TIMEOUT = 60; //60s

private Gson gson = new Gson();
private static final Gson GSON = new Gson();

private static final Type namespaceValueLengthOverrideTypeReference =
new TypeToken<Map<Long, Integer>>() {
}.getType();
Expand Down Expand Up @@ -81,7 +82,7 @@ public Map<Long, Integer> namespaceValueLengthLimitOverride() {
Map<Long, Integer> namespaceValueLengthOverride = Maps.newHashMap();
if (!Strings.isNullOrEmpty(namespaceValueLengthOverrideString)) {
namespaceValueLengthOverride =
gson.fromJson(namespaceValueLengthOverrideString, namespaceValueLengthOverrideTypeReference);
GSON.fromJson(namespaceValueLengthOverrideString, namespaceValueLengthOverrideTypeReference);
}

return namespaceValueLengthOverride;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ public Instance findInstance(String appId, String clusterName, String dataCenter

public List<Instance> findInstancesByIds(Set<Long> instanceIds) {
Iterable<Instance> instances = instanceRepository.findAllById(instanceIds);
if (instances == null) {
return Collections.emptyList();
}
return Lists.newArrayList(instances);
}

Expand All @@ -66,10 +63,8 @@ public InstanceConfig findInstanceConfig(long instanceId, String configAppId, St

public Page<InstanceConfig> findActiveInstanceConfigsByReleaseKey(String releaseKey, Pageable
pageable) {
Page<InstanceConfig> instanceConfigs = instanceConfigRepository
.findByReleaseKeyAndDataChangeLastModifiedTimeAfter(releaseKey,
return instanceConfigRepository.findByReleaseKeyAndDataChangeLastModifiedTimeAfter(releaseKey,
getValidInstanceConfigDate(), pageable);
return instanceConfigs;
}

public Page<Instance> findInstancesByNamespace(String appId, String clusterName, String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public Item findOne(String appId, String clusterName, String namespaceName, Stri
throw new NotFoundException(
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
}
Item item = itemRepository.findByNamespaceIdAndKey(namespace.getId(), key);
return item;
return itemRepository.findByNamespaceIdAndKey(namespace.getId(), key);
}

public Item findLastOne(String appId, String clusterName, String namespaceName) {
Expand All @@ -84,8 +83,7 @@ public Item findLastOne(long namespaceId) {
}

public Item findOne(long itemId) {
Item item = itemRepository.findById(itemId).orElse(null);
return item;
return itemRepository.findById(itemId).orElse(null);
}

public List<Item> findItemsWithoutOrdered(Long namespaceId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@Service
public class NamespaceService {

private Gson gson = new Gson();
private static final Gson GSON = new Gson();

private final NamespaceRepository namespaceRepository;
private final AuditService auditService;
Expand Down Expand Up @@ -400,7 +400,7 @@ private boolean isNamespaceNotPublished(Namespace namespace) {
return false;
}

Map<String, String> publishedConfiguration = gson.fromJson(latestRelease.getConfigurations(), GsonType.CONFIG);
Map<String, String> publishedConfiguration = GSON.fromJson(latestRelease.getConfigurations(), GsonType.CONFIG);
for (Item item : itemsModifiedAfterLastPublish) {
if (!Objects.equals(item.getValue(), publishedConfiguration.get(item.getKey()))) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
@Service
public class ReleaseHistoryService {
private Gson gson = new Gson();
private static final Gson GSON = new Gson();

private final ReleaseHistoryRepository releaseHistoryRepository;
private final AuditService auditService;
Expand Down Expand Up @@ -65,7 +65,7 @@ public ReleaseHistory createReleaseHistory(String appId, String clusterName, Str
if (operationContext == null) {
releaseHistory.setOperationContext("{}"); //default empty object
} else {
releaseHistory.setOperationContext(gson.toJson(operationContext));
releaseHistory.setOperationContext(GSON.toJson(operationContext));
}
releaseHistory.setDataChangeCreatedTime(new Date());
releaseHistory.setDataChangeCreatedBy(operator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class ReleaseService {

private static final FastDateFormat TIMESTAMP_FORMAT = FastDateFormat.getInstance("yyyyMMddHHmmss");
private static final Gson gson = new Gson();
private static final Gson GSON = new Gson();
private static final Set<Integer> BRANCH_RELEASE_OPERATIONS = Sets
.newHashSet(ReleaseOperation.GRAY_RELEASE, ReleaseOperation.MASTER_NORMAL_RELEASE_MERGE_TO_GRAY,
ReleaseOperation.MATER_ROLLBACK_MERGE_TO_GRAY);
Expand Down Expand Up @@ -218,8 +218,8 @@ private Release publishBranchNamespace(Namespace parentNamespace, Namespace chil
String operator, boolean isEmergencyPublish, Set<String> grayDelKeys) {
Release parentLatestRelease = findLatestActiveRelease(parentNamespace);
Map<String, String> parentConfigurations = parentLatestRelease != null ?
gson.fromJson(parentLatestRelease.getConfigurations(),
GsonType.CONFIG) : new LinkedHashMap<>();
GSON.fromJson(parentLatestRelease.getConfigurations(),
GsonType.CONFIG) : new LinkedHashMap<>();
long baseReleaseId = parentLatestRelease == null ? 0 : parentLatestRelease.getId();

Map<String, String> configsToPublish = mergeConfiguration(parentConfigurations, childNamespaceItems);
Expand Down Expand Up @@ -274,16 +274,16 @@ private void mergeFromMasterAndPublishBranch(Namespace parentNamespace, Namespac
Map<String, String> childReleaseConfiguration;
Collection<String> branchReleaseKeys;
if (childNamespaceLatestActiveRelease != null) {
childReleaseConfiguration = gson.fromJson(childNamespaceLatestActiveRelease.getConfigurations(), GsonType.CONFIG);
childReleaseConfiguration = GSON.fromJson(childNamespaceLatestActiveRelease.getConfigurations(), GsonType.CONFIG);
branchReleaseKeys = getBranchReleaseKeys(childNamespaceLatestActiveRelease.getId());
} else {
childReleaseConfiguration = Collections.emptyMap();
branchReleaseKeys = null;
}

Map<String, String> parentNamespaceOldConfiguration = masterPreviousRelease == null ?
null : gson.fromJson(masterPreviousRelease.getConfigurations(),
GsonType.CONFIG);
null : GSON.fromJson(masterPreviousRelease.getConfigurations(),
GsonType.CONFIG);

Map<String, String> childNamespaceToPublishConfigs =
calculateChildNamespaceToPublishConfiguration(parentNamespaceOldConfiguration, parentNamespaceItems,
Expand All @@ -306,7 +306,7 @@ private Collection<String> getBranchReleaseKeys(long releaseId) {
return null;
}

Map<String, Object> operationContext = gson
Map<String, Object> operationContext = GSON
.fromJson(releaseHistories.getContent().get(0).getOperationContext(), OPERATION_CONTEXT_TYPE_REFERENCE);

if (operationContext == null || !operationContext.containsKey(ReleaseOperationContext.BRANCH_RELEASE_KEYS)) {
Expand Down Expand Up @@ -420,7 +420,7 @@ private Release createRelease(Namespace namespace, String name, String comment,
release.setAppId(namespace.getAppId());
release.setClusterName(namespace.getClusterName());
release.setNamespaceName(namespace.getNamespaceName());
release.setConfigurations(gson.toJson(configurations));
release.setConfigurations(GSON.toJson(configurations));
release = releaseRepository.save(release);

namespaceLockService.unlock(namespace.getId());
Expand Down Expand Up @@ -520,7 +520,7 @@ private void rollbackChildNamespace(String appId, String clusterName, String nam
Map<String, String> childReleaseConfiguration;
Collection<String> branchReleaseKeys;
if (childNamespaceLatestActiveRelease != null) {
childReleaseConfiguration = gson.fromJson(childNamespaceLatestActiveRelease.getConfigurations(), GsonType.CONFIG);
childReleaseConfiguration = GSON.fromJson(childNamespaceLatestActiveRelease.getConfigurations(), GsonType.CONFIG);
branchReleaseKeys = getBranchReleaseKeys(childNamespaceLatestActiveRelease.getId());
} else {
childReleaseConfiguration = Collections.emptyMap();
Expand All @@ -530,12 +530,12 @@ private void rollbackChildNamespace(String appId, String clusterName, String nam
Release abandonedRelease = parentNamespaceTwoLatestActiveRelease.get(0);
Release parentNamespaceNewLatestRelease = parentNamespaceTwoLatestActiveRelease.get(1);

Map<String, String> parentNamespaceAbandonedConfiguration = gson.fromJson(abandonedRelease.getConfigurations(),
Map<String, String> parentNamespaceAbandonedConfiguration = GSON.fromJson(abandonedRelease.getConfigurations(),
GsonType.CONFIG);

Map<String, String>
parentNamespaceNewLatestConfiguration =
gson.fromJson(parentNamespaceNewLatestRelease.getConfigurations(), GsonType.CONFIG);
GSON.fromJson(parentNamespaceNewLatestRelease.getConfigurations(), GsonType.CONFIG);

Map<String, String>
childNamespaceNewConfiguration =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class ConfigChangeContentBuilder {

private static final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
private static final Gson GSON = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

private List<Item> createItems = new LinkedList<>();
private List<ItemPair> updateItems = new LinkedList<>();
Expand Down Expand Up @@ -60,7 +60,7 @@ public String build() {
for (Item item : deleteItems) {
item.setDataChangeLastModifiedTime(now);
}
return gson.toJson(this);
return GSON.toJson(this);
}

static class ItemPair {
Expand All @@ -83,7 +83,7 @@ Item cloneItem(Item source) {
}

public static ConfigChangeContentBuilder convertJsonString(String content) {
return gson.fromJson(content, ConfigChangeContentBuilder.class);
return GSON.fromJson(content, ConfigChangeContentBuilder.class);
}

public List<Item> getCreateItems() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class GrayReleaseRulesHolderTest {
private BizConfig bizConfig;
@Mock
private GrayReleaseRuleRepository grayReleaseRuleRepository;
private Gson gson = new Gson();
private static final Gson GSON = new Gson();
private AtomicLong idCounter;

@Before
Expand Down Expand Up @@ -145,7 +145,7 @@ private GrayReleaseRule assembleGrayReleaseRule(String appId, String clusterName
rule.setClusterName(clusterName);
rule.setNamespaceName(namespaceName);
rule.setBranchName("someBranch");
rule.setRules(gson.toJson(ruleItems));
rule.setRules(GSON.toJson(ruleItems));
rule.setReleaseId(releaseId);
rule.setBranchStatus(branchStatus);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ctrip.framework.apollo.biz.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.ctrip.framework.apollo.biz.AbstractIntegrationTest;
import com.ctrip.framework.apollo.biz.entity.GrayReleaseRule;
Expand All @@ -19,12 +18,11 @@
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.jdbc.Sql;

import java.lang.reflect.Type;
import java.util.Map;

public class ReleaseCreationTest extends AbstractIntegrationTest {

private Gson gson = new Gson();
private static final Gson GSON = new Gson();

@Autowired
private ReleaseService releaseService;
Expand Down Expand Up @@ -494,7 +492,7 @@ private Namespace instanceNamespace(long id, String clusterName) {
}

private Map<String, String> parseConfiguration(String configuration) {
return gson.fromJson(configuration, GsonType.CONFIG);
return GSON.fromJson(configuration, GsonType.CONFIG);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class RemoteConfigLongPollService {
private final ConcurrentMap<String, Long> m_notifications;
private final Map<String, ApolloNotificationMessages> m_remoteNotificationMessages;//namespaceName -> watchedKey -> notificationId
private Type m_responseType;
private Gson gson;
private static final Gson GSON = new Gson();
private ConfigUtil m_configUtil;
private HttpUtil m_httpUtil;
private ConfigServiceLocator m_serviceLocator;
Expand All @@ -83,7 +83,6 @@ public RemoteConfigLongPollService() {
m_remoteNotificationMessages = Maps.newConcurrentMap();
m_responseType = new TypeToken<List<ApolloConfigNotification>>() {
}.getType();
gson = new Gson();
m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
m_httpUtil = ApolloInjector.getInstance(HttpUtil.class);
m_serviceLocator = ApolloInjector.getInstance(ConfigServiceLocator.class);
Expand Down Expand Up @@ -306,7 +305,7 @@ String assembleNotifications(Map<String, Long> notificationsMap) {
ApolloConfigNotification notification = new ApolloConfigNotification(entry.getKey(), entry.getValue());
notifications.add(notification);
}
return gson.toJson(notifications);
return GSON.toJson(notifications);
}

private List<ServiceDTO> getConfigServices() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class RemoteConfigRepository extends AbstractConfigRepository {
private final RateLimiter m_loadConfigRateLimiter;
private final AtomicBoolean m_configNeedForceRefresh;
private final SchedulePolicy m_loadConfigFailSchedulePolicy;
private final Gson gson;
private static final Gson GSON = new Gson();

static {
m_executorService = Executors.newScheduledThreadPool(1,
Expand All @@ -88,7 +88,6 @@ public RemoteConfigRepository(String namespace) {
m_configNeedForceRefresh = new AtomicBoolean(true);
m_loadConfigFailSchedulePolicy = new ExponentialSchedulePolicy(m_configUtil.getOnErrorRetryInterval(),
m_configUtil.getOnErrorRetryInterval() * 8);
gson = new Gson();
this.trySync();
this.schedulePeriodicRefresh();
this.scheduleLongPollingRefresh();
Expand Down Expand Up @@ -296,7 +295,7 @@ String assembleQueryConfigUrl(String uri, String appId, String cluster, String n
}

if (remoteMessages != null) {
queryParams.put("messages", queryParamEscaper.escape(gson.toJson(remoteMessages)));
queryParams.put("messages", queryParamEscaper.escape(GSON.toJson(remoteMessages)));
}

String pathExpanded = String.format(path, pathParams.toArray());
Expand Down
Loading

0 comments on commit 077b176

Please sign in to comment.