You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2017/10/06 22:47:49 UTC

logging-log4j2 git commit: [LOG4J2-2053] Exception java.nio.charset.UnsupportedCharsetException: cp65001 in 2.9.0. Add a mapping layer when a charset is not found. Used map legacy charset names when you say "chcp 65001" in a console on Windows for exampl

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 307257b3d -> 324606e11


[LOG4J2-2053] Exception java.nio.charset.UnsupportedCharsetException:
cp65001 in 2.9.0. Add a mapping layer when a charset is not found. Used
map legacy charset names when you say "chcp 65001" in a console on
Windows for example.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/324606e1
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/324606e1
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/324606e1

Branch: refs/heads/master
Commit: 324606e113edb96f21fe6ae6d32690f1fb6f633f
Parents: 307257b
Author: Gary Gregory <gg...@apache.org>
Authored: Fri Oct 6 16:47:47 2017 -0600
Committer: Gary Gregory <gg...@apache.org>
Committed: Fri Oct 6 16:47:47 2017 -0600

----------------------------------------------------------------------
 .../logging/log4j/util/PropertiesUtil.java      | 112 +++++++++++++------
 .../main/resources/Log4j-charsets.properties    |   1 +
 .../logging/log4j/util/PropertiesUtilTest.java  |   7 ++
 3 files changed, 84 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/324606e1/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
index 658ebe2..6dae543 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
@@ -25,6 +25,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.ResourceBundle;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
@@ -43,7 +44,8 @@ public final class PropertiesUtil {
     /**
      * Constructs a PropertiesUtil using a given Properties object as its source of defined properties.
      *
-     * @param props the Properties to use by default
+     * @param props
+     *            the Properties to use by default
      */
     public PropertiesUtil(final Properties props) {
         this.props = props;
@@ -53,7 +55,8 @@ public final class PropertiesUtil {
      * Constructs a PropertiesUtil for a given properties file name on the classpath. The properties specified in this
      * file are used by default. If a property is not defined in this file, then the equivalent system property is used.
      *
-     * @param propertiesFileName the location of properties file to load
+     * @param propertiesFileName
+     *            the location of properties file to load
      */
     public PropertiesUtil(final String propertiesFileName) {
         final Properties properties = new Properties();
@@ -70,8 +73,10 @@ public final class PropertiesUtil {
     /**
      * Loads and closes the given property input stream. If an error occurs, log to the status logger.
      *
-     * @param in a property input stream.
-     * @param source a source object describing the source, like a resource string or a URL.
+     * @param in
+     *            a property input stream.
+     * @param source
+     *            a source object describing the source, like a resource string or a URL.
      * @return a new Properties object
      */
     static Properties loadClose(final InputStream in, final Object source) {
@@ -103,7 +108,9 @@ public final class PropertiesUtil {
 
     /**
      * Returns {@code true} if the specified property is defined, regardless of its value (it may not have a value).
-     * @param name the name of the property to verify
+     * 
+     * @param name
+     *            the name of the property to verify
      * @return {@code true} if the specified property is defined, regardless of its value
      */
     public boolean hasProperty(final String name) {
@@ -115,7 +122,8 @@ public final class PropertiesUtil {
      * then it is returned as the boolean value {@code true}. Any other non-{@code null} text in the property is
      * considered {@code false}.
      *
-     * @param name the name of the property to look up
+     * @param name
+     *            the name of the property to look up
      * @return the boolean value of the property or {@code false} if undefined.
      */
     public boolean getBooleanProperty(final String name) {
@@ -125,8 +133,10 @@ public final class PropertiesUtil {
     /**
      * Gets the named property as a boolean value.
      *
-     * @param name the name of the property to look up
-     * @param defaultValue the default value to use if the property is undefined
+     * @param name
+     *            the name of the property to look up
+     * @param defaultValue
+     *            the default value to use if the property is undefined
      * @return the boolean value of the property or {@code defaultValue} if undefined.
      */
     public boolean getBooleanProperty(final String name, final boolean defaultValue) {
@@ -137,9 +147,12 @@ public final class PropertiesUtil {
     /**
      * Gets the named property as a boolean value.
      *
-     * @param name the name of the property to look up
-     * @param defaultValueIfAbsent the default value to use if the property is undefined
-     * @param defaultValueIfPresent the default value to use if the property is defined but not assigned
+     * @param name
+     *            the name of the property to look up
+     * @param defaultValueIfAbsent
+     *            the default value to use if the property is undefined
+     * @param defaultValueIfPresent
+     *            the default value to use if the property is defined but not assigned
      * @return the boolean value of the property or {@code defaultValue} if undefined.
      */
     public boolean getBooleanProperty(final String name, final boolean defaultValueIfAbsent,
@@ -152,7 +165,8 @@ public final class PropertiesUtil {
     /**
      * Gets the named property as a Charset value.
      *
-     * @param name the name of the property to look up
+     * @param name
+     *            the name of the property to look up
      * @return the Charset value of the property or {@link Charset#defaultCharset()} if undefined.
      */
     public Charset getCharsetProperty(final String name) {
@@ -160,29 +174,44 @@ public final class PropertiesUtil {
     }
 
     /**
-     * Gets the named property as a Charset value.
+     * Gets the named property as a Charset value. If we cannot find the named Charset, see if it is mapped in
+     * file {@code Log4j-charsets.properties} on the class path.
      *
-     * @param name the name of the property to look up
-     * @param defaultValue the default value to use if the property is undefined
+     * @param name
+     *            the name of the property to look up
+     * @param defaultValue
+     *            the default value to use if the property is undefined
      * @return the Charset value of the property or {@code defaultValue} if undefined.
      */
     public Charset getCharsetProperty(final String name, final Charset defaultValue) {
-        final String prop = getStringProperty(name);
-        try {
-            return prop == null ? defaultValue : Charset.forName(prop);
-        } catch (UnsupportedCharsetException e) {
-            LowLevelLogUtil.logException(
-                    "Unable to get Charset '" + prop + "' for property '" + name + "', using default " + defaultValue + " and continuing.", e);
+        final String charsetName = getStringProperty(name);
+        if (charsetName == null) {
             return defaultValue;
         }
+        if (Charset.isSupported(charsetName)) {
+            return Charset.forName(charsetName);
+        }
+        ResourceBundle bundle = ResourceBundle.getBundle("Log4j-charsets");
+        if (bundle.containsKey(name)) {
+            String mapped = bundle.getString(name);
+            if (Charset.isSupported(mapped)) {
+                return Charset.forName(mapped);
+            }
+        }
+        LowLevelLogUtil.log("Unable to get Charset '" + charsetName + "' for property '" + name + "', using default "
+                + defaultValue + " and continuing.");
+        return defaultValue;
     }
 
     /**
      * Gets the named property as a double.
      *
-     * @param name the name of the property to look up
-     * @param defaultValue the default value to use if the property is undefined
-     * @return the parsed double value of the property or {@code defaultValue} if it was undefined or could not be parsed.
+     * @param name
+     *            the name of the property to look up
+     * @param defaultValue
+     *            the default value to use if the property is undefined
+     * @return the parsed double value of the property or {@code defaultValue} if it was undefined or could not be
+     *         parsed.
      */
     public double getDoubleProperty(final String name, final double defaultValue) {
         final String prop = getStringProperty(name);
@@ -199,8 +228,10 @@ public final class PropertiesUtil {
     /**
      * Gets the named property as an integer.
      *
-     * @param name the name of the property to look up
-     * @param defaultValue the default value to use if the property is undefined
+     * @param name
+     *            the name of the property to look up
+     * @param defaultValue
+     *            the default value to use if the property is undefined
      * @return the parsed integer value of the property or {@code defaultValue} if it was undefined or could not be
      *         parsed.
      */
@@ -219,8 +250,10 @@ public final class PropertiesUtil {
     /**
      * Gets the named property as a long.
      *
-     * @param name the name of the property to look up
-     * @param defaultValue the default value to use if the property is undefined
+     * @param name
+     *            the name of the property to look up
+     * @param defaultValue
+     *            the default value to use if the property is undefined
      * @return the parsed long value of the property or {@code defaultValue} if it was undefined or could not be parsed.
      */
     public long getLongProperty(final String name, final long defaultValue) {
@@ -238,7 +271,8 @@ public final class PropertiesUtil {
     /**
      * Gets the named property as a String.
      *
-     * @param name the name of the property to look up
+     * @param name
+     *            the name of the property to look up
      * @return the String value of the property or {@code null} if undefined.
      */
     public String getStringProperty(final String name) {
@@ -254,8 +288,10 @@ public final class PropertiesUtil {
     /**
      * Gets the named property as a String.
      *
-     * @param name the name of the property to look up
-     * @param defaultValue the default value to use if the property is undefined
+     * @param name
+     *            the name of the property to look up
+     * @param defaultValue
+     *            the default value to use if the property is undefined
      * @return the String value of the property or {@code defaultValue} if undefined.
      */
     public String getStringProperty(final String name, final String defaultValue) {
@@ -282,8 +318,10 @@ public final class PropertiesUtil {
      * Extracts properties that start with or are equals to the specific prefix and returns them in a new Properties
      * object with the prefix removed.
      *
-     * @param properties The Properties to evaluate.
-     * @param prefix The prefix to extract.
+     * @param properties
+     *            The Properties to evaluate.
+     * @param prefix
+     *            The prefix to extract.
      * @return The subset of properties.
      */
     public static Properties extractSubset(final Properties properties, final String prefix) {
@@ -313,9 +351,10 @@ public final class PropertiesUtil {
     /**
      * Partitions a properties map based on common key prefixes up to the first period.
      *
-     * @param properties properties to partition
-     * @return the partitioned properties where each key is the common prefix (minus the period) and the values are
-     * new property maps without the prefix and period in the key
+     * @param properties
+     *            properties to partition
+     * @return the partitioned properties where each key is the common prefix (minus the period) and the values are new
+     *         property maps without the prefix and period in the key
      * @since 2.6
      */
     public static Map<String, Properties> partitionOnCommonPrefixes(final Properties properties) {
@@ -332,6 +371,7 @@ public final class PropertiesUtil {
 
     /**
      * Returns true if system properties tell us we are running on Windows.
+     * 
      * @return true if system properties tell us we are running on Windows.
      */
     public boolean isOsWindows() {

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/324606e1/log4j-api/src/main/resources/Log4j-charsets.properties
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/resources/Log4j-charsets.properties b/log4j-api/src/main/resources/Log4j-charsets.properties
new file mode 100644
index 0000000..344ac7d
--- /dev/null
+++ b/log4j-api/src/main/resources/Log4j-charsets.properties
@@ -0,0 +1 @@
+cp65001 = UTF-8

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/324606e1/log4j-api/src/test/java/org/apache/logging/log4j/util/PropertiesUtilTest.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/util/PropertiesUtilTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/util/PropertiesUtilTest.java
index 4dd5e7e..84c36ba 100644
--- a/log4j-api/src/test/java/org/apache/logging/log4j/util/PropertiesUtilTest.java
+++ b/log4j-api/src/test/java/org/apache/logging/log4j/util/PropertiesUtilTest.java
@@ -77,4 +77,11 @@ public class PropertiesUtilTest {
         assertEquals(StandardCharsets.US_ASCII, pu.getCharsetProperty("e.1"));
         assertEquals(Charset.defaultCharset(), pu.getCharsetProperty("e.2"));
     }
+    
+    @Test
+    public void testGetMappedProperty() {
+        final Properties p = new Properties();
+        final PropertiesUtil pu = new PropertiesUtil(p);
+        assertEquals(StandardCharsets.UTF_8, pu.getCharsetProperty("cp65001"));
+    }
 }