Skip to content

Commit

Permalink
[ISSUES #10014] clear confused logic about namespace properties (#10023)
Browse files Browse the repository at this point in the history
* [ISSUES #10014] clear confused logic about namespace properties

- add getPropertyFrom method
- optimize some code logic

Close #10014

* fix ci error
  • Loading branch information
onewe authored Mar 9, 2023
1 parent 62ef024 commit f969595
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public class LocalConfigInfoProcessor {

private static final Logger LOGGER = LogUtils.logger(LocalConfigInfoProcessor.class);

public static final String LOCAL_FILEROOT_PATH;

public static final String LOCAL_SNAPSHOT_PATH;

private static final String SUFFIX = "_nacos";
Expand All @@ -59,12 +57,9 @@ public class LocalConfigInfoProcessor {
private static final String SNAPSHOT_FILE_CHILD_2 = "snapshot-tenant";

static {
LOCAL_FILEROOT_PATH = NacosClientProperties.PROTOTYPE.getProperty("JM.LOG.PATH",
NacosClientProperties.PROTOTYPE.getProperty("user.home")) + File.separator + "nacos" + File.separator
+ "config";
LOCAL_SNAPSHOT_PATH = NacosClientProperties.PROTOTYPE.getProperty("JM.SNAPSHOT.PATH",
NacosClientProperties.PROTOTYPE.getProperty("user.home")) + File.separator + "nacos" + File.separator
+ "config";
LOCAL_SNAPSHOT_PATH = NacosClientProperties.PROTOTYPE.getProperty(com.alibaba.nacos.client.constant.Constants.SysEnv.JM_SNAPSHOT_PATH,
NacosClientProperties.PROTOTYPE.getProperty(com.alibaba.nacos.client.constant.Constants.SysEnv.USER_HOME)) + File.separator
+ "nacos" + File.separator + "config";
LOGGER.info("LOCAL_SNAPSHOT_PATH:{}", LOCAL_SNAPSHOT_PATH);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public interface NacosClientProperties {
*/
String getProperty(String key, String defaultValue);

/**
* get property from special property source.
* @param source source type
* @see SourceType
* @param key special key
* @return string value or null.
*/
String getPropertyFrom(SourceType source, String key);

/**
* get boolean, if the value can not be got by the special key, the null will be returned.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,25 @@ public String getProperty(String key, String defaultValue) {
return this.search(key, String.class).orElse(defaultValue);
}

@Override
public String getPropertyFrom(SourceType source, String key) {
if (source == null) {
return this.getProperty(key);
}
switch (source) {
case JVM:
return JVM_ARGS_PROPERTY_SOURCE.getProperty(key);
case ENV:
return SYSTEM_ENV_PROPERTY_SOURCE.getProperty(key);
case PROPERTIES:
return this.propertiesPropertySource.getProperty(key);
case DEFAULT_SETTING:
return DEFAULT_SETTING_PROPERTY_SOURCE.getProperty(key);
default:
return this.getProperty(key);
}
}

@Override
public Boolean getBoolean(String key) {
return getBoolean(key, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package com.alibaba.nacos.client.env;

enum SourceType {
/**
* properties source type enum.
* @author onewe
*/
public enum SourceType {
/**
* get value from properties.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,7 @@ private void init(Properties properties) throws NacosException {
}

private void initLogName(NacosClientProperties properties) {
logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME);
if (StringUtils.isEmpty(logName)) {

if (StringUtils
.isNotEmpty(properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME))) {
logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME);
} else {
logName = DEFAULT_NAMING_LOG_FILE_PATH;
}
}
logName = properties.getProperty(UtilAndComs.NACOS_NAMING_LOG_NAME, DEFAULT_NAMING_LOG_FILE_PATH);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.alibaba.nacos.api.selector.NoneSelector;
import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.client.env.NacosClientProperties;
import com.alibaba.nacos.client.env.SourceType;
import com.alibaba.nacos.client.utils.ContextPathUtil;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
Expand Down Expand Up @@ -58,21 +59,21 @@ public static String initNamespaceForNaming(NacosClientProperties properties) {
if (Boolean.parseBoolean(isUseCloudNamespaceParsing)) {

tmpNamespace = TenantUtil.getUserTenantForAns();
LogUtils.NAMING_LOGGER.info("initializer namespace from System Property : {}", tmpNamespace);
LogUtils.NAMING_LOGGER.info("initializer namespace from ans.namespace attribute : {}", tmpNamespace);

tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, () -> {
String namespace = properties.getProperty(PropertyKeyConst.SystemEnv.ALIBABA_ALIWARE_NAMESPACE);
LogUtils.NAMING_LOGGER.info("initializer namespace from System Environment :" + namespace);
LogUtils.NAMING_LOGGER.info("initializer namespace from ALIBABA_ALIWARE_NAMESPACE attribute :" + namespace);
return namespace;
});
}

tmpNamespace = TemplateUtils.stringEmptyAndThenExecute(tmpNamespace, () -> {
String namespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
LogUtils.NAMING_LOGGER.info("initializer namespace from System Property :" + namespace);
String namespace = properties.getPropertyFrom(SourceType.JVM, PropertyKeyConst.NAMESPACE);
LogUtils.NAMING_LOGGER.info("initializer namespace from namespace attribute :" + namespace);
return namespace;
});

if (StringUtils.isEmpty(tmpNamespace)) {
tmpNamespace = properties.getProperty(PropertyKeyConst.NAMESPACE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public static String parsingEndpointRule(String endpointUrl) {
}

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

if (StringUtils.isBlank(endpointUrlSource)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,22 @@ public void testGetPropertyWithScope() {

}

@Test
public void testGetPropertyFrom() {
System.setProperty("nacos.home.default.test", "/home/jvm_args");
NacosClientProperties.PROTOTYPE.setProperty("nacos.home.default.test", "/home/properties_args");

Assert.assertEquals(NacosClientProperties.PROTOTYPE.getPropertyFrom(SourceType.JVM, "nacos.home.default.test"),
"/home/jvm_args");
Assert.assertEquals(
NacosClientProperties.PROTOTYPE.getPropertyFrom(SourceType.DEFAULT_SETTING, "nacos.home.default.test"),
"/home/default_setting");
Assert.assertEquals(
NacosClientProperties.PROTOTYPE.getPropertyFrom(SourceType.PROPERTIES, "nacos.home.default.test"),
"/home/properties_args");
Assert.assertEquals(
NacosClientProperties.PROTOTYPE.getPropertyFrom(null, "nacos.home.default.test"),
"/home/jvm_args");
}

}

0 comments on commit f969595

Please sign in to comment.