You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by rm...@apache.org on 2018/05/29 13:08:59 UTC

svn commit: r1832446 - in /geronimo/components/config/trunk/impl/src: main/java/org/apache/geronimo/config/Placeholders.java test/java/org/apache/geronimo/config/PlaceholdersTest.java

Author: rmannibucau
Date: Tue May 29 13:08:59 2018
New Revision: 1832446

URL: http://svn.apache.org/viewvc?rev=1832446&view=rev
Log:
few more tolerance in placeholders

Added:
    geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/PlaceholdersTest.java
Modified:
    geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/Placeholders.java

Modified: geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/Placeholders.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/Placeholders.java?rev=1832446&r1=1832445&r2=1832446&view=diff
==============================================================================
--- geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/Placeholders.java (original)
+++ geronimo/components/config/trunk/impl/src/main/java/org/apache/geronimo/config/Placeholders.java Tue May 29 13:08:59 2018
@@ -30,21 +30,50 @@ public final class Placeholders {
         while ((startVar = value.indexOf("${", startVar)) >= 0)
         {
             int endVar = value.indexOf("}", startVar);
-            if (endVar <= 0)
+            if (endVar < startVar)
             {
-                break;
+                return value;
             }
-            String varName = value.substring(startVar + 2, endVar);
+            int endStart = startVar;
+            int nestedPlaceholder;
+            do {
+                nestedPlaceholder = value.indexOf("${", endStart + 1);
+                if (nestedPlaceholder > 0 && nestedPlaceholder < endVar) { // then we grabbed the end of this placeholder
+                    endVar = value.indexOf("}", nestedPlaceholder + 1); // end nested placeholder
+                    if (endVar < startVar) {
+                        return value;
+                    }
+                    endVar = value.indexOf("}", endVar + 1); // end of the enclosing placeholder
+                    if (endVar < startVar) { // broken value pby
+                        return value;
+                    }
+                    endStart = endVar;
+                } else {
+                    break;
+                }
+            } while (true);
+
+            int defaultSep = value.indexOf(':', startVar);
+            boolean hasDefault = defaultSep > 0 && defaultSep < endVar;
+            String varName = value.substring(startVar + 2, hasDefault ? defaultSep : endVar);
             if (varName.isEmpty())
             {
-                break;
+                return value;
             }
+
             String variableValue = config.getOptionalValue(varName, String.class).orElse(null);
+            if (variableValue == null && hasDefault) {
+                variableValue = value.substring(defaultSep + 1, endVar);
+            }
+
             if (variableValue != null)
             {
-                value = value.replace("${" + varName + "}", variableValue);
+                final String old = value;
+                value = value.substring(0, startVar) + variableValue + value.substring(endVar + 1);
+                if (value.equals(old)) { // avoid loops
+                    return value;
+                }
             }
-            startVar++;
         }
         return value;
     }

Added: geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/PlaceholdersTest.java
URL: http://svn.apache.org/viewvc/geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/PlaceholdersTest.java?rev=1832446&view=auto
==============================================================================
--- geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/PlaceholdersTest.java (added)
+++ geronimo/components/config/trunk/impl/src/test/java/org/apache/geronimo/config/PlaceholdersTest.java Tue May 29 13:08:59 2018
@@ -0,0 +1,71 @@
+/*
+ * 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.geronimo.config;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.geronimo.config.configsource.BaseConfigSource;
+import org.eclipse.microprofile.config.Config;
+import org.eclipse.microprofile.config.ConfigProvider;
+import org.eclipse.microprofile.config.spi.ConfigBuilder;
+import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
+import org.junit.Test;
+
+public class PlaceholdersTest {
+    private final Config config = ConfigProviderResolver.instance()
+        .getBuilder()
+        .withSources(new BaseConfigSource() {
+            private final Map<String, String> properties = new HashMap<String, String>() {{
+                put("p1", "v1");
+                put("p2", "v2");
+            }};
+
+            @Override
+            public Map<String, String> getProperties() {
+                return properties;
+            }
+
+            @Override
+            public String getValue(final String s) {
+                return getProperties().get(s);
+            }
+
+            @Override
+            public String getName() {
+                return "geronimo-config-test";
+            }
+        })
+        .build();
+
+    @Test
+    public void simplePlaceholder() {
+        assertEquals("v1", Placeholders.replace(config, "${p1}"));
+    }
+
+    @Test
+    public void defaultPlaceholder() {
+        assertEquals("d1", Placeholders.replace(config, "${missing:d1}"));
+    }
+
+    @Test
+    public void nestedPlaceholder() {
+        assertEquals("v2/foo", Placeholders.replace(config, "${missing:${p2}/foo}"));
+    }
+}