Bladeren bron

Support reading files with customized charset in ConfigUtil (#961)

master
aq0706 Eric Zhao 5 jaren geleden
bovenliggende
commit
56c73698cb
2 gewijzigde bestanden met toevoegingen van 58 en 16 verwijderingen
  1. +17
    -16
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java
  2. +41
    -0
      sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java

+ 17
- 16
sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java Bestand weergeven

@@ -15,10 +15,13 @@
*/
package com.alibaba.csp.sentinel.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
@@ -66,9 +69,10 @@ public final class ConfigUtil {
return null;
}

try (FileInputStream input = new FileInputStream(file)) {
try (BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(new FileInputStream(file), getCharset()))) {
properties = new Properties();
properties.load(input);
properties.load(bufferedReader);
}
} catch (Throwable e) {
e.printStackTrace();
@@ -107,20 +111,11 @@ public final class ConfigUtil {

Properties properties = new Properties();
for (URL url : list) {
try {
try (BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(url.openStream(), getCharset()))) {
Properties p = new Properties();
InputStream input = url.openStream();
if (input != null) {
try {
p.load(input);
properties.putAll(p);
} finally {
try {
input.close();
} catch (Throwable t) {
}
}
}
p.load(bufferedReader);
properties.putAll(p);
} catch (Throwable e) {
e.printStackTrace();
}
@@ -142,6 +137,12 @@ public final class ConfigUtil {
return classLoader;
}

private static Charset getCharset() {
// avoid static loop dependencies: SentinelConfig -> SentinelConfigLoader -> ConfigUtil -> SentinelConfig
// so not use SentinelConfig.charset()
return Charset.forName(System.getProperty("csp.sentinel.charset", StandardCharsets.UTF_8.name()));
}

public static String addSeparator(String dir) {
if (!dir.endsWith(File.separator)) {
dir += File.separator;


+ 41
- 0
sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java Bestand weergeven

@@ -20,8 +20,10 @@ import org.junit.Test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Properties;

import static com.alibaba.csp.sentinel.log.LogBase.LOG_DIR;
@@ -79,6 +81,45 @@ public class ConfigUtilTest {

}

//add Jvm parameter
//-Dcsp.sentinel.charset="UTF-16"
//@Test
public void testLoadPropertiesWithCustomizedCharset() throws IOException {

String charset = "UTF-16";

File file = null;
String dir = "/data/logs/",
fileName = "propertiesTest.properties";
try {
String userDir = System.getProperty("user.dir");
file = new File(addSeparator(userDir) + "target/classes/" + fileName);
if (!file.exists()) {
file.createNewFile();
}
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), charset);
out.write(LOG_DIR + "=" + dir);
out.flush();
out.close();

//Load from absolutePath
Properties properties = ConfigUtil.loadProperties(file.getAbsolutePath());
Assert.assertTrue(dir.equals(properties.getProperty(LOG_DIR)));

//Load from classPath
properties = ConfigUtil.loadProperties(ConfigUtil.CLASSPATH_FILE_FLAG + fileName);
Assert.assertTrue(dir.equals(properties.getProperty(LOG_DIR)));

//Load from relativePath
properties = ConfigUtil.loadProperties("target/classes/" + fileName);
Assert.assertTrue(dir.equals(properties.getProperty(LOG_DIR)));

} finally {
if (file != null) {
file.delete();
}
}

}

}

Laden…
Annuleren
Opslaan