From 56c73698cbb9f3cc4c04e46f4b82450e769a5cb1 Mon Sep 17 00:00:00 2001 From: aq0706 Date: Thu, 8 Aug 2019 15:14:36 +0800 Subject: [PATCH] Support reading files with customized charset in ConfigUtil (#961) --- .../alibaba/csp/sentinel/util/ConfigUtil.java | 33 +++++++-------- .../csp/sentinel/util/ConfigUtilTest.java | 41 +++++++++++++++++++ 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java index 80d7a94d..a5182b36 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/ConfigUtil.java @@ -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; diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java index 65a24e20..43094006 100644 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/ConfigUtilTest.java @@ -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(); + } + } + + } }