You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by mi...@apache.org on 2022/12/13 03:21:41 UTC
[incubator-eventmesh] branch master updated: fix issue2541
This is an automated email from the ASF dual-hosted git repository.
mikexue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-eventmesh.git
The following commit(s) were added to refs/heads/master by this push:
new 262f42b55 fix issue2541
new fcba03685 Merge pull request #2575 from jonyangx/issue2541
262f42b55 is described below
commit 262f42b559146823178d7585f8f9ed94d1847e17
Author: jonyangx <jo...@gmail.com>
AuthorDate: Tue Dec 13 09:27:13 2022 +0800
fix issue2541
---
.../common/config/ConfigurationWrapper.java | 40 ++++++++++++++++++----
.../common/config/ConfigurationWrapperTest.java | 2 +-
2 files changed, 34 insertions(+), 8 deletions(-)
diff --git a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigurationWrapper.java b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigurationWrapper.java
index 26ce79818..eddf81c9e 100644
--- a/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigurationWrapper.java
+++ b/eventmesh-common/src/main/java/org/apache/eventmesh/common/config/ConfigurationWrapper.java
@@ -22,13 +22,15 @@ import org.apache.eventmesh.common.file.FileChangeListener;
import org.apache.eventmesh.common.file.WatchFileManager;
import org.apache.commons.lang3.StringUtils;
-import org.apache.logging.log4j.util.Strings;
import java.io.BufferedReader;
import java.io.File;
-import java.io.FileReader;
+import java.io.FileInputStream;
import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
import java.util.Map.Entry;
+import java.util.Objects;
import java.util.Properties;
import org.slf4j.Logger;
@@ -51,6 +53,7 @@ public class ConfigurationWrapper {
private final transient boolean reload;
+
private final transient FileChangeListener fileChangeListener = new FileChangeListener() {
@Override
public void onChanged(FileChangeContext changeContext) {
@@ -63,8 +66,12 @@ public class ConfigurationWrapper {
}
};
+ private static final ObjectMapper objectMapper = new ObjectMapper();
+
public ConfigurationWrapper(String directoryPath, String fileName, boolean reload) {
- Preconditions.checkArgument(Strings.isNotEmpty(directoryPath), "please configure environment variable 'confPath'");
+ Objects.requireNonNull(directoryPath, "please configure environment variable 'confPath'");
+ Objects.requireNonNull(fileName, "please configure environment variable 'fileName'");
+
this.directoryPath = directoryPath
.replace('/', File.separator.charAt(0))
.replace('\\', File.separator.charAt(0));
@@ -81,15 +88,22 @@ public class ConfigurationWrapper {
if (this.reload) {
WatchFileManager.registerFileChangeListener(directoryPath, fileChangeListener);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
- LOG.info("Configuration reload task closed");
+ if (LOG.isInfoEnabled()) {
+ LOG.info("Configuration reload task closed");
+ }
WatchFileManager.deregisterFileChangeListener(directoryPath);
}));
}
}
private void load() {
- try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
- LOG.info("loading config: {}", file);
+ Objects.requireNonNull(file, "properties can not be null");
+
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),
+ StandardCharsets.UTF_8))) {
+ if (LOG.isInfoEnabled()) {
+ LOG.info("loading config: {}", file);
+ }
properties.load(reader);
} catch (IOException e) {
LOG.error("loading properties [{}] error", file, e);
@@ -101,16 +115,21 @@ public class ConfigurationWrapper {
}
public int getIntProp(String configKey, int defaultValue) {
+ Objects.requireNonNull(configKey, "configKey can not be null");
+
String configValue = StringUtils.deleteWhitespace(getProp(configKey));
if (StringUtils.isEmpty(configValue)) {
return defaultValue;
}
+
Preconditions.checkState(StringUtils.isNumeric(configValue),
String.format("key:%s, value:%s error", configKey, configValue));
return Integer.parseInt(configValue);
}
public boolean getBoolProp(String configKey, boolean defaultValue) {
+ Objects.requireNonNull(configKey, "configKey can not be null");
+
String configValue = StringUtils.deleteWhitespace(getProp(configKey));
if (StringUtils.isEmpty(configValue)) {
return defaultValue;
@@ -119,11 +138,16 @@ public class ConfigurationWrapper {
}
private String removePrefix(String key, String prefix, boolean removePrefix) {
+ Objects.requireNonNull(key, "key can not be null");
+ Objects.requireNonNull(prefix, "prefix can not be null");
+
String newPrefix = prefix.endsWith(".") ? prefix : prefix + ".";
return removePrefix ? key.replace(newPrefix, "") : key;
}
public Properties getPropertiesByConfig(String prefix, boolean isRemovePrefix) {
+ Objects.requireNonNull(prefix, "prefix can not be null");
+
Properties properties = new Properties();
for (Entry<Object, Object> entry : this.properties.entrySet()) {
String key = (String) entry.getKey();
@@ -136,7 +160,9 @@ public class ConfigurationWrapper {
@SuppressWarnings("unchecked")
public <T> T getPropertiesByConfig(String prefix, Class<?> clazz, boolean removePrefix) {
- ObjectMapper objectMapper = new ObjectMapper();
+ Objects.requireNonNull(prefix, "prefix can not be null");
+ Objects.requireNonNull(clazz, "clazz can not be null");
+
return (T) objectMapper.convertValue(getPropertiesByConfig(prefix, removePrefix), clazz);
}
diff --git a/eventmesh-common/src/test/java/org/apache/eventmesh/common/config/ConfigurationWrapperTest.java b/eventmesh-common/src/test/java/org/apache/eventmesh/common/config/ConfigurationWrapperTest.java
index 24e0f0f3b..56ce9191c 100644
--- a/eventmesh-common/src/test/java/org/apache/eventmesh/common/config/ConfigurationWrapperTest.java
+++ b/eventmesh-common/src/test/java/org/apache/eventmesh/common/config/ConfigurationWrapperTest.java
@@ -40,7 +40,7 @@ public class ConfigurationWrapperTest {
Assert.assertEquals("value2", wraper.getProp("eventMesh.server.idc"));
}
- @Test(expected = IllegalArgumentException.class)
+ @Test(expected = NullPointerException.class)
public void construct() {
ConfigurationWrapper newWrapper = new ConfigurationWrapper(null, "eventmesh.properties", false);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org