Skip to content

Commit

Permalink
[ISSUE alibaba#8622] sub pr of 9356 - util module (alibaba#9453)
Browse files Browse the repository at this point in the history
- Replace system.getProperties and system.getEnv with NacosClientProperties

relate alibaba#9356
  • Loading branch information
onewe authored Nov 3, 2022
1 parent 645cd4a commit bd2b026
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.client.utils;

import com.alibaba.nacos.client.constant.Constants;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.common.utils.StringUtils;

import java.io.File;
Expand Down Expand Up @@ -63,17 +64,17 @@ public static String getAppName() {
}

private static String getAppNameByProjectName() {
return System.getProperty(Constants.SysEnv.PROJECT_NAME);
return NacosClientProperties.PROTOTYPE.getProperty(Constants.SysEnv.PROJECT_NAME);
}

private static String getAppNameByServerHome() {
String serverHome = null;
if (SERVER_JBOSS.equals(getServerType())) {
serverHome = System.getProperty(PARAM_MARKING_JBOSS);
serverHome = NacosClientProperties.PROTOTYPE.getProperty(PARAM_MARKING_JBOSS);
} else if (SERVER_JETTY.equals(getServerType())) {
serverHome = System.getProperty(PARAM_MARKING_JETTY);
serverHome = NacosClientProperties.PROTOTYPE.getProperty(PARAM_MARKING_JETTY);
} else if (SERVER_TOMCAT.equals(getServerType())) {
serverHome = System.getProperty(PARAM_MARKING_TOMCAT);
serverHome = NacosClientProperties.PROTOTYPE.getProperty(PARAM_MARKING_TOMCAT);
}

if (serverHome != null && serverHome.startsWith(LINUX_ADMIN_HOME)) {
Expand All @@ -85,11 +86,11 @@ private static String getAppNameByServerHome() {

private static String getServerType() {
String serverType;
if (System.getProperty(PARAM_MARKING_JBOSS) != null) {
if (NacosClientProperties.PROTOTYPE.getProperty(PARAM_MARKING_JBOSS) != null) {
serverType = SERVER_JBOSS;
} else if (System.getProperty(PARAM_MARKING_JETTY) != null) {
} else if (NacosClientProperties.PROTOTYPE.getProperty(PARAM_MARKING_JETTY) != null) {
serverType = SERVER_JETTY;
} else if (System.getProperty(PARAM_MARKING_TOMCAT) != null) {
} else if (NacosClientProperties.PROTOTYPE.getProperty(PARAM_MARKING_TOMCAT) != null) {
serverType = SERVER_TOMCAT;
} else {
serverType = SERVER_UNKNOWN;
Expand Down
21 changes: 11 additions & 10 deletions client/src/main/java/com/alibaba/nacos/client/utils/ParamUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.common.utils.VersionUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -77,18 +78,18 @@ public class ParamUtil {

static {
// Client identity information
appKey = System.getProperty(NACOS_CLIENT_APP_KEY, BLANK_STR);
appKey = NacosClientProperties.PROTOTYPE.getProperty(NACOS_CLIENT_APP_KEY, BLANK_STR);

defaultContextPath = System.getProperty(NACOS_CLIENT_CONTEXTPATH_KEY, DEFAULT_NACOS_CLIENT_CONTEXTPATH);
defaultContextPath = NacosClientProperties.PROTOTYPE.getProperty(NACOS_CLIENT_CONTEXTPATH_KEY, DEFAULT_NACOS_CLIENT_CONTEXTPATH);

appName = AppNameUtils.getAppName();

serverPort = System.getProperty(NACOS_SERVER_PORT_KEY, DEFAULT_SERVER_PORT);
serverPort = NacosClientProperties.PROTOTYPE.getProperty(NACOS_SERVER_PORT_KEY, DEFAULT_SERVER_PORT);
LOGGER.info("[settings] [req-serv] nacos-server port:{}", serverPort);

String tmp = "1000";
try {
tmp = System.getProperty(NACOS_CONNECT_TIMEOUT_KEY, DEFAULT_NACOS_CONNECT_TIMEOUT);
tmp = NacosClientProperties.PROTOTYPE.getProperty(NACOS_CONNECT_TIMEOUT_KEY, DEFAULT_NACOS_CONNECT_TIMEOUT);
connectTimeout = Integer.parseInt(tmp);
} catch (NumberFormatException e) {
final String msg = "[http-client] invalid connect timeout:" + tmp;
Expand All @@ -101,7 +102,7 @@ public class ParamUtil {

try {
perTaskConfigSize = Double
.parseDouble(System.getProperty(PER_TASK_CONFIG_SIZE_KEY, DEFAULT_PER_TASK_CONFIG_SIZE_KEY));
.parseDouble(NacosClientProperties.PROTOTYPE.getProperty(PER_TASK_CONFIG_SIZE_KEY, DEFAULT_PER_TASK_CONFIG_SIZE_KEY));
LOGGER.info("PER_TASK_CONFIG_SIZE: {}", perTaskConfigSize);
} catch (Throwable t) {
LOGGER.error("[PER_TASK_CONFIG_SIZE] PER_TASK_CONFIG_SIZE invalid", t);
Expand Down Expand Up @@ -178,14 +179,14 @@ public static String parseNamespace(Properties properties) {
String namespaceTmp = null;

String isUseCloudNamespaceParsing = properties.getProperty(PropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING,
System.getProperty(SystemPropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING,
NacosClientProperties.PROTOTYPE.getProperty(SystemPropertyKeyConst.IS_USE_CLOUD_NAMESPACE_PARSING,
String.valueOf(Constants.DEFAULT_USE_CLOUD_NAMESPACE_PARSING)));

if (Boolean.parseBoolean(isUseCloudNamespaceParsing)) {
namespaceTmp = TenantUtil.getUserTenantForAcm();

namespaceTmp = TemplateUtils.stringBlankAndThenExecute(namespaceTmp, () -> {
String namespace = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE);
String namespace = NacosClientProperties.PROTOTYPE.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE);
return StringUtils.isNotBlank(namespace) ? namespace : StringUtils.EMPTY;
});
}
Expand All @@ -206,7 +207,7 @@ public static String parsingEndpointRule(String endpointUrl) {
// If entered in the configuration file, the priority in ENV will be given priority.
if (endpointUrl == null || !PATTERN.matcher(endpointUrl).find()) {
// skip retrieve from system property and retrieve directly from system env
String endpointUrlSource = System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL);
String endpointUrlSource = NacosClientProperties.PROTOTYPE.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL);
if (StringUtils.isNotBlank(endpointUrlSource)) {
endpointUrl = endpointUrlSource;
}
Expand All @@ -223,8 +224,8 @@ public static String parsingEndpointRule(String endpointUrl) {
}

String endpointUrlSource = TemplateUtils
.stringBlankAndThenExecute(System.getProperty(endpointUrl, System.getenv(endpointUrl)),
() -> System.getenv(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL));
.stringBlankAndThenExecute(NacosClientProperties.PROTOTYPE.getProperty(endpointUrl, System.getenv(endpointUrl)),
() -> NacosClientProperties.PROTOTYPE.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_ENDPOINT_URL));

if (StringUtils.isBlank(endpointUrlSource)) {
if (StringUtils.isNotBlank(defaultEndpointUrl)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.client.utils;

import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.common.utils.StringUtils;

/**
Expand All @@ -35,7 +36,7 @@ public class TenantUtil {
private static final String ACM_NAMESPACE_PROPERTY = "acm.namespace";

static {
USER_TENANT = System.getProperty(TENANT_ID, "");
USER_TENANT = NacosClientProperties.PROTOTYPE.getProperty(TENANT_ID, "");
}

/**
Expand All @@ -51,7 +52,7 @@ public static String getUserTenantForAcm() {
String tmp = USER_TENANT;

if (StringUtils.isBlank(USER_TENANT)) {
tmp = System.getProperty(ACM_NAMESPACE_PROPERTY, DEFAULT_ACM_NAMESPACE);
tmp = NacosClientProperties.PROTOTYPE.getProperty(ACM_NAMESPACE_PROPERTY, DEFAULT_ACM_NAMESPACE);
}

return tmp;
Expand All @@ -66,7 +67,7 @@ public static String getUserTenantForAns() {
String tmp = USER_TENANT;

if (StringUtils.isBlank(USER_TENANT)) {
tmp = System.getProperty(SystemPropertyKeyConst.ANS_NAMESPACE);
tmp = NacosClientProperties.PROTOTYPE.getProperty(SystemPropertyKeyConst.ANS_NAMESPACE);
}
return tmp;
}
Expand Down

0 comments on commit bd2b026

Please sign in to comment.