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 2021/12/19 15:53:10 UTC

[logging-log4j2] branch release-2.x updated (632569d -> 65a264e)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git.


    from 632569d  Fix NPE when input is null in StrSubstitutor.replace(String, Properties).
     new 544db52  Oops, fix wrong issue reference.
     new 6bd519e  Reuse StrSubstitutor.
     new 65a264e  No need to nest.

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../logging/log4j/core/util/OptionConverter.java   | 58 ++--------------------
 ...NanoClockTest.java => OptionConverterTest.java} | 22 ++++----
 src/changes/changes.xml                            |  2 +-
 3 files changed, 15 insertions(+), 67 deletions(-)
 copy log4j-core/src/test/java/org/apache/logging/log4j/core/util/{DummyNanoClockTest.java => OptionConverterTest.java} (70%)

[logging-log4j2] 02/03: Reuse StrSubstitutor.

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 6bd519e26d53317ef8597fe439272a26e063149d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Dec 19 10:33:42 2021 -0500

    Reuse StrSubstitutor.
---
 .../logging/log4j/core/util/OptionConverter.java   | 53 +---------------------
 .../log4j/core/util/OptionConverterTest.java       | 37 +++++++++++++++
 2 files changed, 39 insertions(+), 51 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/OptionConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/OptionConverter.java
index 7b9bc2b..025cedc 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/OptionConverter.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/OptionConverter.java
@@ -22,9 +22,8 @@ import java.util.Properties;
 
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.lookup.StrSubstitutor;
 import org.apache.logging.log4j.status.StatusLogger;
-import org.apache.logging.log4j.util.PropertiesUtil;
-import org.apache.logging.log4j.util.Strings;
 
 /**
  * A convenience class to convert property values to specific types.
@@ -33,10 +32,6 @@ public final class OptionConverter {
 
     private static final Logger LOGGER = StatusLogger.getLogger();
 
-    private static final String DELIM_START = "${";
-    private static final char DELIM_STOP = '}';
-    private static final int DELIM_START_LEN = 2;
-    private static final int DELIM_STOP_LEN = 1;
     private static final int ONE_K = 1024;
 
     /**
@@ -347,50 +342,6 @@ public final class OptionConverter {
      */
     public static String substVars(final String val, final Properties props) throws
         IllegalArgumentException {
-
-        final StringBuilder sbuf = new StringBuilder();
-
-        int i = 0;
-        int j;
-        int k;
-
-        while (true) {
-            j = val.indexOf(DELIM_START, i);
-            if (j == -1) {
-                // no more variables
-                if (i == 0) { // this is a simple string
-                    return val;
-                }
-                // add the tail string which contails no variables and return the result.
-                sbuf.append(val.substring(i, val.length()));
-                return sbuf.toString();
-            }
-            sbuf.append(val.substring(i, j));
-            k = val.indexOf(DELIM_STOP, j);
-            if (k == -1) {
-                throw new IllegalArgumentException(Strings.dquote(val)
-                    + " has no closing brace. Opening brace at position " + j
-                    + '.');
-            }
-            j += DELIM_START_LEN;
-            final String key = val.substring(j, k);
-            // first try in System properties
-            String replacement = PropertiesUtil.getProperties().getStringProperty(key, null);
-            // then try props parameter
-            if (replacement == null && props != null) {
-                replacement = props.getProperty(key);
-            }
-
-            if (replacement != null) {
-                // Do variable substitution on the replacement string
-                // such that we can solve "Hello ${x2}" as "Hello p1"
-                // the where the properties are
-                // x1=p1
-                // x2=${x1}
-                final String recursiveReplacement = substVars(replacement, props);
-                sbuf.append(recursiveReplacement);
-            }
-            i = k + DELIM_STOP_LEN;
-        }
+        return StrSubstitutor.replace(val, props);
     }
 }
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/OptionConverterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/OptionConverterTest.java
new file mode 100644
index 0000000..5c0a294
--- /dev/null
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/OptionConverterTest.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+package org.apache.logging.log4j.core.util;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.Properties;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link OptionConverter}.
+ */
+public class OptionConverterTest {
+
+    @Test
+    public void testReplace() {
+        Properties props = new Properties();
+        props.setProperty("key", "${key}");
+        assertEquals("Value of key is ${key}.", OptionConverter.substVars("Value of key is ${key}.", props));
+
+    }
+}

[logging-log4j2] 01/03: Oops, fix wrong issue reference.

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 544db529ed770f716ef3e2ffaa42ea57803020d2
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Dec 19 10:32:10 2021 -0500

    Oops, fix wrong issue reference.
---
 src/changes/changes.xml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 895bd75..ba71ad9 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -30,7 +30,7 @@
          - "remove" - Removed
     -->
     <release version="2.17.1" date="2021-MM-dd" description="GA Release 2.17.1">
-      <action issue="LOG4J2-3230" dev="ggregory" type="fix">
+      <action dev="ggregory" type="fix">
         Fix NPE when input is null in StrSubstitutor.replace(String, Properties).
       </action>
     </release>

[logging-log4j2] 03/03: No need to nest.

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 65a264ea73f617c2f0a0f3232efb34b07e7bae64
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Dec 19 10:46:48 2021 -0500

    No need to nest.
---
 .../java/org/apache/logging/log4j/core/util/OptionConverter.java     | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/OptionConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/OptionConverter.java
index 025cedc..c1f0b13 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/OptionConverter.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/OptionConverter.java
@@ -162,10 +162,9 @@ public final class OptionConverter {
         if (hashIndex == -1) {
             if("NULL".equalsIgnoreCase(value)) {
                 return null;
-            } else {
-                // no class name specified : use standard Level class
-                return Level.toLevel(value, defaultValue);
             }
+            // no class name specified : use standard Level class
+            return Level.toLevel(value, defaultValue);
         }
 
         Level result = defaultValue;