From e4f7beddd26a8665caeb1ff95e5debe116f08f03 Mon Sep 17 00:00:00 2001 From: chenglu <1285823170@qq.com> Date: Tue, 8 Jun 2021 14:36:05 +0800 Subject: [PATCH] [code quality] [nacos-client] [identity] simple the judge logic, constant export, chinese javadoc translate --- .../client/identify/CredentialService.java | 15 ++++++++------- .../client/identify/CredentialWatcher.java | 9 ++++----- .../client/identify/IdentifyConstants.java | 11 +++++++++++ .../nacos/client/identify/StsConfig.java | 19 ++++++++++--------- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/identify/CredentialService.java b/client/src/main/java/com/alibaba/nacos/client/identify/CredentialService.java index ae68d796f47..b0736dbf9c9 100644 --- a/client/src/main/java/com/alibaba/nacos/client/identify/CredentialService.java +++ b/client/src/main/java/com/alibaba/nacos/client/identify/CredentialService.java @@ -43,7 +43,7 @@ public final class CredentialService implements SpasCredentialLoader { private CredentialService(String appName) { if (appName == null) { - String value = System.getProperty("project.name"); + String value = System.getProperty(IdentifyConstants.PROJECT_NAME_PROPERTY); if (StringUtils.isNotEmpty(value)) { appName = value; } @@ -59,12 +59,13 @@ public static CredentialService getInstance() { public static CredentialService getInstance(String appName) { String key = appName != null ? appName : IdentifyConstants.NO_APP_NAME; CredentialService instance = INSTANCES.get(key); - if (instance == null) { - instance = new CredentialService(appName); - CredentialService previous = INSTANCES.putIfAbsent(key, instance); - if (previous != null) { - instance = previous; - } + if (instance != null) { + return instance; + } + instance = new CredentialService(appName); + CredentialService previous = INSTANCES.putIfAbsent(key, instance); + if (previous != null) { + instance = previous; } return instance; } diff --git a/client/src/main/java/com/alibaba/nacos/client/identify/CredentialWatcher.java b/client/src/main/java/com/alibaba/nacos/client/identify/CredentialWatcher.java index fad15300a63..88e729fd9b2 100644 --- a/client/src/main/java/com/alibaba/nacos/client/identify/CredentialWatcher.java +++ b/client/src/main/java/com/alibaba/nacos/client/identify/CredentialWatcher.java @@ -106,7 +106,6 @@ public void stop() { } private void loadCredential(boolean init) { - boolean logWarn = init; if (propertyPath == null) { URL url = ClassLoader.getSystemResource(IdentifyConstants.PROPERTIES_FILENAME); if (url != null) { @@ -123,12 +122,12 @@ private void loadCredential(boolean init) { IdentifyConstants.CREDENTIAL_PATH + (appName == null ? IdentifyConstants.CREDENTIAL_DEFAULT : appName); } else { - if (logWarn) { + if (init) { SPAS_LOGGER.info("[{}] Defined credential file: -Dspas.identity={}", appName, propertyPath); } } } else { - if (logWarn) { + if (init) { SPAS_LOGGER.info("[{}] Load credential file from classpath: {}", appName, IdentifyConstants.PROPERTIES_FILENAME); } @@ -161,7 +160,7 @@ private void loadCredential(boolean init) { accessKey = System.getenv(IdentifyConstants.ENV_ACCESS_KEY); secretKey = System.getenv(IdentifyConstants.ENV_SECRET_KEY); if (accessKey == null && secretKey == null) { - if (logWarn) { + if (init) { SPAS_LOGGER.info("{} No credential found", appName); } return; @@ -184,7 +183,7 @@ private void loadCredential(boolean init) { } } - if (logWarn) { + if (init) { SPAS_LOGGER.info("[{}] Load credential file {}", appName, propertyPath); } diff --git a/client/src/main/java/com/alibaba/nacos/client/identify/IdentifyConstants.java b/client/src/main/java/com/alibaba/nacos/client/identify/IdentifyConstants.java index d453bec3d29..c38cecfe262 100644 --- a/client/src/main/java/com/alibaba/nacos/client/identify/IdentifyConstants.java +++ b/client/src/main/java/com/alibaba/nacos/client/identify/IdentifyConstants.java @@ -51,4 +51,15 @@ public class IdentifyConstants { public static final String NO_APP_NAME = ""; + public static final String PROJECT_NAME_PROPERTY = "project.name"; + + public static final String RAM_ROLE_NAME_PROPERTY = "ram.role.name"; + + public static final String REFRESH_TIME_PROPERTY = "time.to.refresh.in.millisecond"; + + public static final String SECURITY_PROPERTY = "security.credentials"; + + public static final String SECURITY_URL_PROPERTY = "security.credentials.url"; + + public static final String SECURITY_CACHE_PROPERTY = "cache.security.credentials"; } diff --git a/client/src/main/java/com/alibaba/nacos/client/identify/StsConfig.java b/client/src/main/java/com/alibaba/nacos/client/identify/StsConfig.java index 92789dc079a..1ef2ffe9a6e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/identify/StsConfig.java +++ b/client/src/main/java/com/alibaba/nacos/client/identify/StsConfig.java @@ -30,22 +30,23 @@ public class StsConfig { private String ramRoleName; /** - * STS 临时凭证有效期剩余多少时开始刷新(允许本地时间比 STS 服务时间最多慢多久). + * The STS temporary certificate will be refreshed when the validity period of + * the temporary certificate is left (allow the local time to be at most slower than the STS service time). */ private int timeToRefreshInMillisecond = 3 * 60 * 1000; /** - * 获取 STS 临时凭证的元数据接口(包含角色名称). + * Metadata interface for obtaining STS temporary credentials (including role name). */ private String securityCredentialsUrl; /** - * 设定 STS 临时凭证,不再通过元数据接口获取. + * Set the STS temporary certificate and no longer obtain it through the metadata interface. */ private String securityCredentials; /** - * 是否缓存. + * Whether to cache. */ private boolean cacheSecurityCredentials = true; @@ -55,27 +56,27 @@ private static class Singleton { } private StsConfig() { - String ramRoleName = System.getProperty("ram.role.name"); + String ramRoleName = System.getProperty(IdentifyConstants.RAM_ROLE_NAME_PROPERTY); if (!StringUtils.isBlank(ramRoleName)) { setRamRoleName(ramRoleName); } - String timeToRefreshInMillisecond = System.getProperty("time.to.refresh.in.millisecond"); + String timeToRefreshInMillisecond = System.getProperty(IdentifyConstants.REFRESH_TIME_PROPERTY); if (!StringUtils.isBlank(timeToRefreshInMillisecond)) { setTimeToRefreshInMillisecond(Integer.parseInt(timeToRefreshInMillisecond)); } - String securityCredentials = System.getProperty("security.credentials"); + String securityCredentials = System.getProperty(IdentifyConstants.SECURITY_PROPERTY); if (!StringUtils.isBlank(securityCredentials)) { setSecurityCredentials(securityCredentials); } - String securityCredentialsUrl = System.getProperty("security.credentials.url"); + String securityCredentialsUrl = System.getProperty(IdentifyConstants.SECURITY_URL_PROPERTY); if (!StringUtils.isBlank(securityCredentialsUrl)) { setSecurityCredentialsUrl(securityCredentialsUrl); } - String cacheSecurityCredentials = System.getProperty("cache.security.credentials"); + String cacheSecurityCredentials = System.getProperty(IdentifyConstants.SECURITY_CACHE_PROPERTY); if (!StringUtils.isBlank(cacheSecurityCredentials)) { setCacheSecurityCredentials(Boolean.parseBoolean(cacheSecurityCredentials)); }