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:12 UTC

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

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));
+
+    }
+}