Skip to content

Commit

Permalink
Support reading files with customized charset in ConfigUtil (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
li-daqian authored and sczyh30 committed Aug 8, 2019
1 parent ebcf890 commit 56c7369
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,9 +69,10 @@ private static Properties loadPropertiesFromAbsoluteFile(String fileName) {
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();
Expand Down Expand Up @@ -107,20 +111,11 @@ private static Properties loadPropertiesFromClasspathFile(String fileName) {

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();
}
Expand All @@ -142,6 +137,12 @@ private static ClassLoader getClassLoader() {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

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;
Expand Down Expand Up @@ -79,6 +81,45 @@ public void testLoadProperties() throws IOException {

}

//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();
}
}

}

}

0 comments on commit 56c7369

Please sign in to comment.