sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject [sling-whiteboard] branch master updated: Replace SubstVarUtil with Apache Commons Lang StrSubstitutor
Date Tue, 24 Apr 2018 11:45:58 GMT
This is an automated email from the ASF dual-hosted git repository.

davidb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new a94b8a4  Replace SubstVarUtil with Apache Commons Lang StrSubstitutor
a94b8a4 is described below

commit a94b8a40c515cb8d201206a4119216b84be4f538
Author: David Bosschaert <david.bosschaert@gmail.com>
AuthorDate: Tue Apr 24 12:45:41 2018 +0100

    Replace SubstVarUtil with Apache Commons Lang StrSubstitutor
---
 featuremodel/feature-analyser/pom.xml              |   5 +
 .../scanner/impl/FelixFrameworkScanner.java        |  21 ++-
 .../scanner/impl/FelixFrameworkScannerTest.java    |  49 +++++++
 .../src/test/resources/test-framework.jar          | Bin 0 -> 19945 bytes
 featuremodel/feature-launcher/pom.xml              |   5 +
 .../launcher/impl/launchers/FrameworkLauncher.java |  18 ++-
 .../sling/feature/support/util/SubstVarUtil.java   | 148 ---------------------
 7 files changed, 93 insertions(+), 153 deletions(-)

diff --git a/featuremodel/feature-analyser/pom.xml b/featuremodel/feature-analyser/pom.xml
index 3179891..d600de8 100644
--- a/featuremodel/feature-analyser/pom.xml
+++ b/featuremodel/feature-analyser/pom.xml
@@ -87,6 +87,11 @@
     </build>
     <dependencies>
         <dependency>
+            <groupId>commons-lang</groupId>
+            <artifactId>commons-lang</artifactId>
+            <version>2.6</version>
+        </dependency>
+        <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.annotation.versioning</artifactId>
             <version>1.0.0</version>
diff --git a/featuremodel/feature-analyser/src/main/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScanner.java
b/featuremodel/feature-analyser/src/main/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScanner.java
index c2faeac..14644d2 100644
--- a/featuremodel/feature-analyser/src/main/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScanner.java
+++ b/featuremodel/feature-analyser/src/main/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScanner.java
@@ -35,6 +35,8 @@ import java.util.stream.Stream;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
+import org.apache.commons.lang.text.StrLookup;
+import org.apache.commons.lang.text.StrSubstitutor;
 import org.apache.sling.commons.osgi.ManifestHeader;
 import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.ArtifactId;
@@ -42,7 +44,6 @@ import org.apache.sling.feature.KeyValueMap;
 import org.apache.sling.feature.scanner.BundleDescriptor;
 import org.apache.sling.feature.scanner.spi.FrameworkScanner;
 import org.apache.sling.feature.support.util.PackageInfo;
-import org.apache.sling.feature.support.util.SubstVarUtil;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
 import org.osgi.resource.Capability;
@@ -142,7 +143,7 @@ public class FelixFrameworkScanner implements FrameworkScanner {
 
     private static final String DEFAULT_PROPERTIES = "default.properties";
 
-    private KeyValueMap getFrameworkProperties(final KeyValueMap appProps, final File framework)
+    KeyValueMap getFrameworkProperties(final KeyValueMap appProps, final File framework)
     throws IOException {
         final Map<String, Properties> propsMap = new HashMap<>();
         try (final ZipInputStream zis = new ZipInputStream(new FileInputStream(framework))
) {
@@ -175,10 +176,24 @@ public class FelixFrameworkScanner implements FrameworkScanner {
         // replace variables
         defaultMap.put("java.specification.version",
                 System.getProperty("java.specification.version", "1.8"));
+
+        StrSubstitutor ss = new StrSubstitutor(new StrLookup() {
+            @Override
+            public String lookup(String key) {
+                // Normally if a variable cannot be found, StrSubstitutor will
+                // leave the raw variable in place. We need to replace it with
+                // nothing in that case.
+
+                String val = defaultMap.getProperty(key);
+                return val != null ? val : "";
+            }
+        });
+        ss.setEnableSubstitutionInVariables(true);
+
         for(final Object name : defaultMap.keySet()) {
             if ( frameworkProps.get(name.toString()) == null ) {
                 final String value = (String)defaultMap.get(name);
-                final String substValue = SubstVarUtil.substVars(value, name.toString(),
null, (Map) defaultMap);
+                final String substValue = ss.replace(value);
                 frameworkProps.put(name.toString(), substValue);
             }
         }
diff --git a/featuremodel/feature-analyser/src/test/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScannerTest.java
b/featuremodel/feature-analyser/src/test/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScannerTest.java
new file mode 100644
index 0000000..7e0d02f
--- /dev/null
+++ b/featuremodel/feature-analyser/src/test/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScannerTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.sling.feature.scanner.impl;
+
+import org.apache.sling.feature.KeyValueMap;
+import org.junit.Test;
+
+import java.io.File;
+import java.net.URL;
+
+import static org.junit.Assert.assertEquals;
+
+public class FelixFrameworkScannerTest {
+    @Test
+    public void testGetFrameworkProperties() throws Exception {
+        URL url = getClass().getResource("/test-framework.jar");
+        File fwFile = new File(url.toURI());
+
+        FelixFrameworkScanner ffs = new FelixFrameworkScanner();
+
+        KeyValueMap kvmap = new KeyValueMap();
+        KeyValueMap props = ffs.getFrameworkProperties(kvmap, fwFile);
+        assertEquals("osgi.service; objectClass:List<String>=org.osgi.service.resolver.Resolver;
"
+                    + "uses:=org.osgi.service.resolver, "
+                + "osgi.service; objectClass:List<String>=org.osgi.service.startlevel.StartLevel;
"
+                    + "uses:=org.osgi.service.startlevel, "
+                + "osgi.service; objectClass:List<String>=org.osgi.service.packageadmin.PackageAdmin;
"
+                    + "uses:=org.osgi.service.packageadmin , "
+                + "osgi.ee; osgi.ee=\"OSGi/Minimum\"; version:List<Version>=\"1.0,1.1,1.2\",
"
+                + "osgi.ee; osgi.ee=\"JavaSE\"; version:List<Version>=\"1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8\",
"
+                + "osgi.ee; osgi.ee=\"JavaSE/compact1\"; version:List<Version>=\"1.8\",
"
+                + "osgi.ee; osgi.ee=\"JavaSE/compact2\"; version:List<Version>=\"1.8\",
"
+                + "osgi.ee; osgi.ee=\"JavaSE/compact3\"; version:List<Version>=\"1.8\"
", props.get("org.osgi.framework.system.capabilities"));
+    }
+}
diff --git a/featuremodel/feature-analyser/src/test/resources/test-framework.jar b/featuremodel/feature-analyser/src/test/resources/test-framework.jar
new file mode 100644
index 0000000..51fe8a1
Binary files /dev/null and b/featuremodel/feature-analyser/src/test/resources/test-framework.jar
differ
diff --git a/featuremodel/feature-launcher/pom.xml b/featuremodel/feature-launcher/pom.xml
index 0c73499..fb1574b 100644
--- a/featuremodel/feature-launcher/pom.xml
+++ b/featuremodel/feature-launcher/pom.xml
@@ -76,6 +76,11 @@
 
     <dependencies>
         <dependency>
+            <groupId>commons-lang</groupId>
+            <artifactId>commons-lang</artifactId>
+            <version>2.6</version>
+        </dependency>
+        <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>osgi.core</artifactId>
             <version>6.0.0</version>
diff --git a/featuremodel/feature-launcher/src/main/java/org/apache/sling/feature/launcher/impl/launchers/FrameworkLauncher.java
b/featuremodel/feature-launcher/src/main/java/org/apache/sling/feature/launcher/impl/launchers/FrameworkLauncher.java
index 8bb5fdc..ded2239 100644
--- a/featuremodel/feature-launcher/src/main/java/org/apache/sling/feature/launcher/impl/launchers/FrameworkLauncher.java
+++ b/featuremodel/feature-launcher/src/main/java/org/apache/sling/feature/launcher/impl/launchers/FrameworkLauncher.java
@@ -16,12 +16,13 @@
  */
 package org.apache.sling.feature.launcher.impl.launchers;
 
+import org.apache.commons.lang.text.StrLookup;
+import org.apache.commons.lang.text.StrSubstitutor;
 import org.apache.sling.feature.Application;
 import org.apache.sling.feature.launcher.impl.Main;
 import org.apache.sling.feature.launcher.spi.Launcher;
 import org.apache.sling.feature.launcher.spi.LauncherPrepareContext;
 import org.apache.sling.feature.launcher.spi.LauncherRunContext;
-import org.apache.sling.feature.support.util.SubstVarUtil;
 
 import java.io.File;
 import java.lang.reflect.Constructor;
@@ -46,9 +47,22 @@ public class FrameworkLauncher implements Launcher {
      */
     @Override
     public void run(final LauncherRunContext context, final ClassLoader cl) throws Exception
{
+        StrSubstitutor ss = new StrSubstitutor(new StrLookup() {
+            @Override
+            public String lookup(String key) {
+                // Normally if a variable cannot be found, StrSubstitutor will
+                // leave the raw variable in place. We need to replace it with
+                // nothing in that case.
+
+                String val = context.getFrameworkProperties().get(key);
+                return val != null ? val : "";
+            }
+        });
+        ss.setEnableSubstitutionInVariables(true);
+
         Map<String, String> properties = new HashMap<>();
         context.getFrameworkProperties().forEach((key, value) -> {
-            properties.put(key, SubstVarUtil.substVars(value, key,null, context.getFrameworkProperties()));
+            properties.put(key, ss.replace(value));
         });
         if ( Main.LOG().isDebugEnabled() ) {
             Main.LOG().debug("Bundles:");
diff --git a/featuremodel/feature-support/src/main/java/org/apache/sling/feature/support/util/SubstVarUtil.java
b/featuremodel/feature-support/src/main/java/org/apache/sling/feature/support/util/SubstVarUtil.java
deleted file mode 100644
index ec77483..0000000
--- a/featuremodel/feature-support/src/main/java/org/apache/sling/feature/support/util/SubstVarUtil.java
+++ /dev/null
@@ -1,148 +0,0 @@
-/*
- * 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.sling.feature.support.util;
-
-import java.util.HashMap;
-import java.util.Map;
-
-public class SubstVarUtil {
-    private static final String DELIM_START = "${";
-    private static final String DELIM_STOP  = "}";
-
-    /**
-     * <p>
-     * This method performs property variable substitution on the
-     * specified value. If the specified value contains the syntax
-     * <tt>${&lt;prop-name&gt;}</tt>, where <tt>&lt;prop-name&gt;</tt>
-     * refers to either a configuration property or a system property,
-     * then the corresponding property value is substituted for the variable
-     * placeholder. Multiple variable place holders may exist in the
-     * specified value as well as nested variable place holders, which
-     * are substituted from inner most to outer most. Configuration
-     * properties override system properties.
-     * </p>
-     * @param val The string on which to perform property substitution.
-     * @param currentKey The key of the property being evaluated used to
-     *        detect cycles.
-     * @param cycleMap Map of variable references used to detect nested cycles - may be null.
-     * @param configProps Set of configuration properties.
-     * @return The value of the specified string after system property substitution.
-     * @throws IllegalArgumentException If there was a syntax error in the
-     *         property placeholder syntax or a recursive variable reference.
-     **/
-    public static String substVars(String val, String currentKey,
-                                    Map<String, String> cycleMap, Map<String, String>
configProps)
-            throws IllegalArgumentException
-    {
-        // If there is currently no cycle map, then create
-        // one for detecting cycles for this invocation.
-        if (cycleMap == null)
-        {
-            cycleMap = new HashMap<>();
-        }
-
-        // Put the current key in the cycle map.
-        cycleMap.put(currentKey, currentKey);
-
-        // Assume we have a value that is something like:
-        // "leading ${foo.${bar}} middle ${baz} trailing"
-
-        // Find the first ending '}' variable delimiter, which
-        // will correspond to the first deepest nested variable
-        // placeholder.
-        int stopDelim = -1;
-        int startDelim = -1;
-
-        do
-        {
-            stopDelim = val.indexOf(DELIM_STOP, stopDelim + 1);
-            // If there is no stopping delimiter, then just return
-            // the value since there is no variable declared.
-            if (stopDelim < 0)
-            {
-                return val;
-            }
-            // Try to find the matching start delimiter by
-            // looping until we find a start delimiter that is
-            // greater than the stop delimiter we have found.
-            startDelim = val.indexOf(DELIM_START);
-            // If there is no starting delimiter, then just return
-            // the value since there is no variable declared.
-            if (startDelim < 0)
-            {
-                return val;
-            }
-            while (stopDelim >= 0)
-            {
-                int idx = val.indexOf(DELIM_START, startDelim + DELIM_START.length());
-                if ((idx < 0) || (idx > stopDelim))
-                {
-                    break;
-                }
-                else if (idx < stopDelim)
-                {
-                    startDelim = idx;
-                }
-            }
-        }
-        while ((startDelim > stopDelim) && (stopDelim >= 0));
-
-        // At this point, we have found a variable placeholder so
-        // we must perform a variable substitution on it.
-        // Using the start and stop delimiter indices, extract
-        // the first, deepest nested variable placeholder.
-        String variable =
-                val.substring(startDelim + DELIM_START.length(), stopDelim);
-
-        // Verify that this is not a recursive variable reference.
-        if (cycleMap.get(variable) != null)
-        {
-            throw new IllegalArgumentException(
-                    "recursive variable reference: " + variable);
-        }
-
-        // Get the value of the deepest nested variable placeholder.
-        // Try to configuration properties first.
-        String substValue = (configProps != null)
-                ? configProps.get(variable)
-                : null;
-        if (substValue == null)
-        {
-            // Ignore unknown property values.
-            substValue = System.getProperty(variable, "");
-        }
-
-        // Remove the found variable from the cycle map, since
-        // it may appear more than once in the value and we don't
-        // want such situations to appear as a recursive reference.
-        cycleMap.remove(variable);
-
-        // Append the leading characters, the substituted value of
-        // the variable, and the trailing characters to get the new
-        // value.
-        val = val.substring(0, startDelim)
-                + substValue
-                + val.substring(stopDelim + DELIM_STOP.length(), val.length());
-
-        // Now perform substitution again, since there could still
-        // be substitutions to make.
-        val = substVars(val, currentKey, cycleMap, configProps);
-
-        // Return the value.
-        return val;
-    }
-}

-- 
To stop receiving notification emails like this one, please contact
davidb@apache.org.

Mime
View raw message