Support reading files with customized charset in ConfigUtil (#961)
This commit is contained in:
parent
ebcf89024c
commit
56c73698cb
|
|
@ -15,10 +15,13 @@
|
||||||
*/
|
*/
|
||||||
package com.alibaba.csp.sentinel.util;
|
package com.alibaba.csp.sentinel.util;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -66,9 +69,10 @@ public final class ConfigUtil {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try (FileInputStream input = new FileInputStream(file)) {
|
try (BufferedReader bufferedReader =
|
||||||
|
new BufferedReader(new InputStreamReader(new FileInputStream(file), getCharset()))) {
|
||||||
properties = new Properties();
|
properties = new Properties();
|
||||||
properties.load(input);
|
properties.load(bufferedReader);
|
||||||
}
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
@ -107,20 +111,11 @@ public final class ConfigUtil {
|
||||||
|
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
for (URL url : list) {
|
for (URL url : list) {
|
||||||
try {
|
try (BufferedReader bufferedReader =
|
||||||
|
new BufferedReader(new InputStreamReader(url.openStream(), getCharset()))) {
|
||||||
Properties p = new Properties();
|
Properties p = new Properties();
|
||||||
InputStream input = url.openStream();
|
p.load(bufferedReader);
|
||||||
if (input != null) {
|
|
||||||
try {
|
|
||||||
p.load(input);
|
|
||||||
properties.putAll(p);
|
properties.putAll(p);
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
input.close();
|
|
||||||
} catch (Throwable t) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
@ -142,6 +137,12 @@ public final class ConfigUtil {
|
||||||
return classLoader;
|
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) {
|
public static String addSeparator(String dir) {
|
||||||
if (!dir.endsWith(File.separator)) {
|
if (!dir.endsWith(File.separator)) {
|
||||||
dir += File.separator;
|
dir += File.separator;
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,10 @@ import org.junit.Test;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import static com.alibaba.csp.sentinel.log.LogBase.LOG_DIR;
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue