From f96869e1d26699ba60d21a95c7a5497bfe7962fa Mon Sep 17 00:00:00 2001 From: flystar32 Date: Thu, 21 Feb 2019 11:37:17 +0800 Subject: [PATCH] format code and change logger to log --- .../acm/bootstrap/AcmPropertySource.java | 38 +++--- .../bootstrap/AcmPropertySourceBuilder.java | 118 +++++++++--------- .../alicloud/acm/endpoint/AcmEndpoint.java | 56 ++++----- .../acm/endpoint/AcmHealthIndicator.java | 61 ++++----- .../acm/refresh/AcmContextRefresher.java | 4 +- 5 files changed, 141 insertions(+), 136 deletions(-) diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySource.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySource.java index 3ebae2773..fb7b58ad2 100644 --- a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySource.java +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySource.java @@ -27,29 +27,29 @@ import java.util.Map; */ public class AcmPropertySource extends MapPropertySource { - private final String dataId; + private final String dataId; - private final Date timestamp; + private final Date timestamp; - private final boolean groupLevel; + private final boolean groupLevel; - AcmPropertySource(String dataId, Map source, Date timestamp, - boolean groupLevel) { - super(dataId, source); - this.dataId = dataId; - this.timestamp = timestamp; - this.groupLevel = groupLevel; - } + AcmPropertySource(String dataId, Map source, Date timestamp, + boolean groupLevel) { + super(dataId, source); + this.dataId = dataId; + this.timestamp = timestamp; + this.groupLevel = groupLevel; + } - public String getDataId() { - return dataId; - } + public String getDataId() { + return dataId; + } - public Date getTimestamp() { - return timestamp; - } + public Date getTimestamp() { + return timestamp; + } - public boolean isGroupLevel() { - return groupLevel; - } + public boolean isGroupLevel() { + return groupLevel; + } } diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceBuilder.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceBuilder.java index 2bf9cbba3..7afabd83a 100644 --- a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceBuilder.java +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceBuilder.java @@ -33,64 +33,68 @@ import java.util.*; */ class AcmPropertySourceBuilder { - private Logger logger = LoggerFactory.getLogger(AcmPropertySourceBuilder.class); + private Logger log = LoggerFactory.getLogger(AcmPropertySourceBuilder.class); - /** - * 传入 ACM 的 DataId 和 groupID,获取到解析后的 AcmProperty 对象 - * - * @param dataId - * @param diamondGroup - * @param groupLevel - * @return - */ - AcmPropertySource build(String dataId, String diamondGroup, boolean groupLevel) { - Properties properties = loadDiamondData(dataId, diamondGroup); - if (properties == null) { - return null; - } - return new AcmPropertySource(dataId, toMap(properties), new Date(), groupLevel); - } + /** + * 传入 ACM 的 DataId 和 groupID,获取到解析后的 AcmProperty 对象 + * + * @param dataId + * @param diamondGroup + * @param groupLevel + * @return + */ + AcmPropertySource build(String dataId, String diamondGroup, boolean groupLevel) { + Properties properties = loadDiamondData(dataId, diamondGroup); + if (properties == null) { + return null; + } + return new AcmPropertySource(dataId, toMap(properties), new Date(), groupLevel); + } - private Properties loadDiamondData(String dataId, String diamondGroup) { - try { - String data = ConfigService.getConfig(dataId, diamondGroup, 3000L); - if (StringUtils.isEmpty(data)) { - return null; - } - if (dataId.endsWith(".properties")) { - Properties properties = new Properties(); - logger.info(String.format("Loading acm data, dataId: '%s', group: '%s'", - dataId, diamondGroup)); - properties.load(new StringReader(data)); - return properties; - } else if (dataId.endsWith(".yaml") || dataId.endsWith(".yml")) { - YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); - yamlFactory.setResources(new ByteArrayResource(data.getBytes())); - return yamlFactory.getObject(); - } - } catch (Exception e) { - if (e instanceof ConfigException) { - logger.error("DIAMOND-100500:" + dataId + ", " + e.toString(), e); - } else { - logger.error("DIAMOND-100500:" + dataId, e); - } - } - return null; - } + private Properties loadDiamondData(String dataId, String diamondGroup) { + try { + String data = ConfigService.getConfig(dataId, diamondGroup, 3000L); + if (StringUtils.isEmpty(data)) { + return null; + } + if (dataId.endsWith(".properties")) { + Properties properties = new Properties(); + log.info(String.format("Loading acm data, dataId: '%s', group: '%s'", + dataId, diamondGroup)); + properties.load(new StringReader(data)); + return properties; + } + else if (dataId.endsWith(".yaml") || dataId.endsWith(".yml")) { + YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); + yamlFactory.setResources(new ByteArrayResource(data.getBytes())); + return yamlFactory.getObject(); + } + } + catch (Exception e) { + if (e instanceof ConfigException) { + log.error("DIAMOND-100500:" + dataId + ", " + e.toString(), e); + } + else { + log.error("DIAMOND-100500:" + dataId, e); + } + } + return null; + } - @SuppressWarnings("unchecked") - private Map toMap(Properties properties) { - Map result = new HashMap<>(); - Enumeration keys = (Enumeration)properties.propertyNames(); - while (keys.hasMoreElements()) { - String key = keys.nextElement(); - Object value = properties.getProperty(key); - if (value != null) { - result.put(key, ((String)value).trim()); - } else { - result.put(key, null); - } - } - return result; - } + @SuppressWarnings("unchecked") + private Map toMap(Properties properties) { + Map result = new HashMap<>(); + Enumeration keys = (Enumeration) properties.propertyNames(); + while (keys.hasMoreElements()) { + String key = keys.nextElement(); + Object value = properties.getProperty(key); + if (value != null) { + result.put(key, ((String) value).trim()); + } + else { + result.put(key, null); + } + } + return result; + } } diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpoint.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpoint.java index 6c5518d76..0133fe241 100644 --- a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpoint.java +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpoint.java @@ -38,40 +38,40 @@ import java.util.Map; @Endpoint(id = "acm") public class AcmEndpoint { - private final AcmProperties properties; + private final AcmProperties properties; - private final AcmRefreshHistory refreshHistory; + private final AcmRefreshHistory refreshHistory; - private final AcmPropertySourceRepository propertySourceRepository; + private final AcmPropertySourceRepository propertySourceRepository; - private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - public AcmEndpoint(AcmProperties properties, AcmRefreshHistory refreshHistory, - AcmPropertySourceRepository propertySourceRepository) { - this.properties = properties; - this.refreshHistory = refreshHistory; - this.propertySourceRepository = propertySourceRepository; - } + public AcmEndpoint(AcmProperties properties, AcmRefreshHistory refreshHistory, + AcmPropertySourceRepository propertySourceRepository) { + this.properties = properties; + this.refreshHistory = refreshHistory; + this.propertySourceRepository = propertySourceRepository; + } - @ReadOperation - public Map invoke() { - Map result = new HashMap<>(); - result.put("config", properties); + @ReadOperation + public Map invoke() { + Map result = new HashMap<>(); + result.put("config", properties); - Map runtime = new HashMap<>(); - List all = propertySourceRepository.getAll(); + Map runtime = new HashMap<>(); + List all = propertySourceRepository.getAll(); - List> sources = new ArrayList<>(); - for (AcmPropertySource ps : all) { - Map source = new HashMap<>(); - source.put("dataId", ps.getDataId()); - source.put("lastSynced", dateFormat.format(ps.getTimestamp())); - sources.add(source); - } - runtime.put("sources", sources); - runtime.put("refreshHistory", refreshHistory.getRecords()); + List> sources = new ArrayList<>(); + for (AcmPropertySource ps : all) { + Map source = new HashMap<>(); + source.put("dataId", ps.getDataId()); + source.put("lastSynced", dateFormat.format(ps.getTimestamp())); + sources.add(source); + } + runtime.put("sources", sources); + runtime.put("refreshHistory", refreshHistory.getRecords()); - result.put("runtime", runtime); - return result; - } + result.put("runtime", runtime); + return result; + } } diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmHealthIndicator.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmHealthIndicator.java index 27569ec1a..c6052ae17 100644 --- a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmHealthIndicator.java +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmHealthIndicator.java @@ -33,39 +33,40 @@ import java.util.List; */ public class AcmHealthIndicator extends AbstractHealthIndicator { - private final AcmProperties acmProperties; + private final AcmProperties acmProperties; - private final AcmPropertySourceRepository acmPropertySourceRepository; + private final AcmPropertySourceRepository acmPropertySourceRepository; - private final List dataIds; + private final List dataIds; - public AcmHealthIndicator(AcmProperties acmProperties, - AcmPropertySourceRepository acmPropertySourceRepository) { - this.acmProperties = acmProperties; - this.acmPropertySourceRepository = acmPropertySourceRepository; + public AcmHealthIndicator(AcmProperties acmProperties, + AcmPropertySourceRepository acmPropertySourceRepository) { + this.acmProperties = acmProperties; + this.acmPropertySourceRepository = acmPropertySourceRepository; - this.dataIds = new ArrayList<>(); - for (AcmPropertySource acmPropertySource : this.acmPropertySourceRepository - .getAll()) { - this.dataIds.add(acmPropertySource.getDataId()); - } - } + this.dataIds = new ArrayList<>(); + for (AcmPropertySource acmPropertySource : this.acmPropertySourceRepository + .getAll()) { + this.dataIds.add(acmPropertySource.getDataId()); + } + } - @Override - protected void doHealthCheck(Health.Builder builder) throws Exception { - for (String dataId : dataIds) { - try { - String config = ConfigService.getConfig(dataId, acmProperties.getGroup(), - acmProperties.getTimeout()); - if (StringUtils.isEmpty(config)) { - builder.down().withDetail(String.format("dataId: '%s', group: '%s'", - dataId, acmProperties.getGroup()), "config is empty"); - } - } catch (Exception e) { - builder.down().withDetail(String.format("dataId: '%s', group: '%s'", - dataId, acmProperties.getGroup()), e.getMessage()); - } - } - builder.up().withDetail("dataIds", dataIds); - } + @Override + protected void doHealthCheck(Health.Builder builder) throws Exception { + for (String dataId : dataIds) { + try { + String config = ConfigService.getConfig(dataId, acmProperties.getGroup(), + acmProperties.getTimeout()); + if (StringUtils.isEmpty(config)) { + builder.down().withDetail(String.format("dataId: '%s', group: '%s'", + dataId, acmProperties.getGroup()), "config is empty"); + } + } + catch (Exception e) { + builder.down().withDetail(String.format("dataId: '%s', group: '%s'", + dataId, acmProperties.getGroup()), e.getMessage()); + } + } + builder.up().withDetail("dataIds", dataIds); + } } diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java index 617c88063..29021bba2 100644 --- a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java @@ -49,7 +49,7 @@ import com.alibaba.edas.acm.listener.ConfigChangeListener; public class AcmContextRefresher implements ApplicationListener, ApplicationContextAware { - private Logger logger = LoggerFactory.getLogger(AcmContextRefresher.class); + private Logger log = LoggerFactory.getLogger(AcmContextRefresher.class); private final ContextRefresher contextRefresher; @@ -103,7 +103,7 @@ public class AcmContextRefresher } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { - logger.warn("unable to get md5 for dataId: " + dataId, e); + log.warn("unable to get md5 for dataId: " + dataId, e); } } refreshHistory.add(dataId, md5);