You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by da...@apache.org on 2018/04/27 09:53:36 UTC
[sling-org-apache-sling-feature-analyser] 20/28: Replace
SubstVarUtil with Apache Commons Lang StrSubstitutor
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-org-apache-sling-feature-analyser.git
commit 16743040af58970728e64adcd1e13006b7892538
Author: David Bosschaert <da...@gmail.com>
AuthorDate: Tue Apr 24 12:45:41 2018 +0100
Replace SubstVarUtil with Apache Commons Lang StrSubstitutor
---
pom.xml | 5 +++
.../scanner/impl/FelixFrameworkScanner.java | 21 +++++++--
.../scanner/impl/FelixFrameworkScannerTest.java | 49 +++++++++++++++++++++
src/test/resources/test-framework.jar | Bin 0 -> 19945 bytes
4 files changed, 72 insertions(+), 3 deletions(-)
diff --git a/pom.xml b/pom.xml
index 3179891..d600de8 100644
--- a/pom.xml
+++ b/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/src/main/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScanner.java b/src/main/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScanner.java
index c2faeac..14644d2 100644
--- a/src/main/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScanner.java
+++ b/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/src/test/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScannerTest.java b/src/test/java/org/apache/sling/feature/scanner/impl/FelixFrameworkScannerTest.java
new file mode 100644
index 0000000..7e0d02f
--- /dev/null
+++ b/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/src/test/resources/test-framework.jar b/src/test/resources/test-framework.jar
new file mode 100644
index 0000000..51fe8a1
Binary files /dev/null and b/src/test/resources/test-framework.jar differ
--
To stop receiving notification emails like this one, please contact
davidb@apache.org.