1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Optimizing and reusing code

This commit is contained in:
yidadi
2019-08-15 09:30:45 +08:00
parent c747d1f5e6
commit e2024a441d
3 changed files with 19 additions and 19 deletions

View File

@@ -16,11 +16,11 @@
package com.alibaba.cloud.nacos;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.cloud.nacos.client.NacosPropertySource;
import com.google.common.collect.Lists;
/**
* @author xiaojing
@@ -34,9 +34,7 @@ public class NacosPropertySourceRepository {
* @return all nacos properties from application context
*/
public static List<NacosPropertySource> getAll() {
List<NacosPropertySource> result = new ArrayList<>();
result.addAll(NACOS_PROPERTY_SOURCE_REPOSITORY.values());
return result;
return Lists.newArrayList(NACOS_PROPERTY_SOURCE_REPOSITORY.values());
}
public static void collectNacosPropertySources(

View File

@@ -83,23 +83,24 @@ public class NacosPropertySourceBuilder {
String data = null;
try {
data = configService.getConfig(dataId, group, timeout);
if (!StringUtils.isEmpty(data)) {
log.info(String.format("Loading nacos data, dataId: '%s', group: '%s'",
dataId, group));
if (StringUtils.isEmpty(data)) {
return EMPTY_PROPERTIES;
}
if (fileExtension.equalsIgnoreCase("properties")) {
Properties properties = new Properties();
log.info(String.format("Loading nacos data, dataId: '%s', group: '%s'",
dataId, group));
properties.load(new StringReader(data));
return properties;
}
else if (fileExtension.equalsIgnoreCase("yaml")
|| fileExtension.equalsIgnoreCase("yml")) {
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setResources(new ByteArrayResource(data.getBytes()));
return yamlFactory.getObject();
}
if (fileExtension.equalsIgnoreCase("properties")) {
Properties properties = new Properties();
properties.load(new StringReader(data));
return properties;
}
else if (fileExtension.equalsIgnoreCase("yaml")
|| fileExtension.equalsIgnoreCase("yml")) {
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
yamlFactory.setResources(new ByteArrayResource(data.getBytes()));
return yamlFactory.getObject();
}
}
catch (NacosException e) {