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

pauls 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 64c76e6  SLING-9670: add a new unpack extension for the feature model.
64c76e6 is described below

commit 64c76e67eb3cb7570af03afdde8e6bff5ee83024
Author: Karl Pauls <[email protected]>
AuthorDate: Thu Aug 20 09:37:36 2020 +0200

    SLING-9670: add a new unpack extension for the feature model.
---
 .../sling/feature/extension/unpack/Unpack.java     | 83 +++++++---------------
 .../installer/UnpackArchiveExtensionHandler.java   |  3 +-
 .../installer/UnpackArchiveInstallerPlugin.java    |  2 +-
 .../impl/launcher/UnpackLauncherExtension.java     | 28 +++-----
 ...eature.launcher.spi.extensions.ExtensionHandler |  2 +-
 .../sling/feature/extension/unpack/TestUnpack.java | 18 ++---
 .../UnpackArchiveExtensionHandlerTest.java         |  2 +-
 .../UnpackArchiveInstallerPluginTest.java          |  2 +-
 .../impl/launcher/UnpackLauncherExtensionTest.java | 82 +++++++++++++++++++++
 9 files changed, 129 insertions(+), 93 deletions(-)

diff --git 
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/Unpack.java
 
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/Unpack.java
index ca420fd..5b9172e 100644
--- 
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/Unpack.java
+++ 
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/Unpack.java
@@ -47,8 +47,9 @@ import org.apache.sling.feature.Extension;
 import org.apache.sling.feature.ExtensionType;
 import org.apache.sling.feature.builder.ArtifactProvider;
 
-public class Unpack
-{
+public class Unpack {
+    public static final String UNPACK_EXTENSIONS_PROP = 
"org.apache.sling.feature.unpack.extensions";
+
     private final Map<String, Map<String, String>> registry;
     private final String defaultMapping;
 
@@ -96,34 +97,27 @@ public class Unpack
         String dir;
         String key;
         String value;
-        if (contextDir == null && this.defaultMapping != null){
+        if (contextDir == null && this.defaultMapping != null) {
             dir = this.registry.get(defaultMapping).get("dir");
             key = this.registry.get(defaultMapping).get("key");
             value = this.registry.get(defaultMapping).get("value");
-        }
-        else {
+        } else {
             dir = contextDir;
             key = (String) context.get("key");
             value = (String) context.get("value");
         }
         if (dir == null) {
             return false;
-        }
-        else if (key != null && value != null)
-        {
-            try (JarInputStream jarInputStream = new JarInputStream(stream))
-            {
+        } else if (key != null && value != null) {
+            try (JarInputStream jarInputStream = new JarInputStream(stream)) {
                 Manifest mf = jarInputStream.getManifest();
                 if (mf != null) {
                     return 
value.equalsIgnoreCase(mf.getMainAttributes().getValue(key));
                 }
-            }
-            catch (Exception ex)
-            {
+            } catch (Exception ex) {
                 return false;
             }
-        }
-        else if (contextDir != null) {
+        } else if (contextDir != null) {
             return true;
         }
 
@@ -132,19 +126,15 @@ public class Unpack
 
 
     private void unpack(URL url, Map<String, Object> context) {
-        try
-        {
+        try {
             unpack(url.openStream(), context);
-        }
-        catch (IOException e)
-        {
+        } catch (IOException e) {
             throw new UncheckedIOException(e);
         }
     }
 
     public void unpack(InputStream stream, Map<String, Object> context) {
-        try
-        {
+        try {
             String dir = (String) context.get("dir");
             boolean override;
             String index;
@@ -152,8 +142,7 @@ public class Unpack
                 dir = this.registry.get(defaultMapping).get("dir");
                 override = 
Boolean.parseBoolean(this.registry.get(defaultMapping).get("override"));
                 index = this.registry.get(defaultMapping).get("index");
-            }
-            else {
+            } else {
                 override = Boolean.parseBoolean((String) 
context.get("override"));
                 index = (String) context.get("index");
             }
@@ -167,8 +156,7 @@ public class Unpack
         }
     }
 
-    public static Unpack fromMapping(String mapping)
-    {
+    public static Unpack fromMapping(String mapping) {
         Map<String, Map<String, String>> registry = new HashMap<>();
 
         // Syntax: 
system-fonts;dir:=abc;overwrite:=true,customer-fonts;dir:=eft;default:=true;key:=foobar;value:=baz
@@ -193,48 +181,32 @@ public class Unpack
 
         try (JarInputStream jarInputStream = new JarInputStream(stream)) {
             String indexValue = null;
-            if (index != null)
-            {
+            if (index != null) {
                 Manifest mf = jarInputStream.getManifest();
-                if (mf != null)
-                {
+                if (mf != null) {
                     indexValue = mf.getMainAttributes().getValue(index);
                 }
             }
 
             List<String> roots = parseRoots(indexValue);
-            for (ZipEntry entry = jarInputStream.getNextEntry(); entry != 
null; entry = jarInputStream.getNextEntry())
-            {
-                if (!entry.isDirectory() && 
!entry.getName().toLowerCase().startsWith("meta-inf/") && isRoot(roots, 
entry.getName()))
-                {
+            for (ZipEntry entry = jarInputStream.getNextEntry(); entry != 
null; entry = jarInputStream.getNextEntry()) {
+                if (!entry.isDirectory() && 
!entry.getName().toLowerCase().startsWith("meta-inf/") && isRoot(roots, 
entry.getName())) {
                     File target = new File(base, relativize(roots, 
entry.getName()));
-                    if 
(target.getParentFile().toPath().startsWith(base.toPath()))
-                    {
-                        if (target.getParentFile().isDirectory() || 
target.getParentFile().mkdirs())
-                        {
-                            if (override)
-                            {
+                    if 
(target.getParentFile().toPath().startsWith(base.toPath())) {
+                        if (target.getParentFile().isDirectory() || 
target.getParentFile().mkdirs()) {
+                            if (override) {
                                 Files.copy(jarInputStream, target.toPath(), 
StandardCopyOption.REPLACE_EXISTING);
-                            }
-                            else if (!target.exists())
-                            {
-                                try
-                                {
+                            } else if (!target.exists()) {
+                                try {
                                     Files.copy(jarInputStream, 
target.toPath());
-                                }
-                                catch (FileAlreadyExistsException ex)
-                                {
+                                } catch (FileAlreadyExistsException ex) {
 
                                 }
                             }
-                        }
-                        else
-                        {
+                        } else {
                             throw new IOException("Can't create parent dir:" + 
target.getParentFile());
                         }
-                    }
-                    else
-                    {
+                    } else {
                         throw new IOException("Zip slip detected for: " + 
entry.getName());
                     }
                 }
@@ -251,8 +223,7 @@ public class Unpack
 
         if (index != null) {
             roots.addAll(Stream.of(Parser.parseDelimitedString(index, 
",")).map(root -> root.endsWith("/") ? root : root + "/").map(root -> 
root.startsWith("/") ? root : "/" + root).collect(Collectors.toList()));
-        }
-        else {
+        } else {
             roots.add("/");
         }
         return roots;
diff --git 
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveExtensionHandler.java
 
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveExtensionHandler.java
index 5007a24..312288c 100644
--- 
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveExtensionHandler.java
+++ 
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveExtensionHandler.java
@@ -30,13 +30,12 @@ import org.osgi.service.component.annotations.Component;
 
 @Component
 public class UnpackArchiveExtensionHandler implements ExtensionHandler {
-    static final String UNPACK_EXTENSIONS_PROP = 
"org.apache.sling.feature.unpack.extensions";
 
     final Unpack unpack;
 
     @Activate
     public UnpackArchiveExtensionHandler(BundleContext bc) {
-        this(Unpack.fromMapping(bc.getProperty(UNPACK_EXTENSIONS_PROP)));
+        
this(Unpack.fromMapping(bc.getProperty(Unpack.UNPACK_EXTENSIONS_PROP)));
     }
 
     UnpackArchiveExtensionHandler(Unpack unpack) {
diff --git 
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveInstallerPlugin.java
 
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveInstallerPlugin.java
index a667b37..de36ca8 100644
--- 
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveInstallerPlugin.java
+++ 
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveInstallerPlugin.java
@@ -53,7 +53,7 @@ public class UnpackArchiveInstallerPlugin implements 
InstallTaskFactory, Resourc
 
     @Activate
     public UnpackArchiveInstallerPlugin(BundleContext bc) {
-        
this(Unpack.fromMapping(bc.getProperty(UnpackArchiveExtensionHandler.UNPACK_EXTENSIONS_PROP)));
+        
this(Unpack.fromMapping(bc.getProperty(Unpack.UNPACK_EXTENSIONS_PROP)));
     }
 
     UnpackArchiveInstallerPlugin(Unpack unpack) {
diff --git 
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/launcher/UnpackLauncherExtension.java
 
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/launcher/UnpackLauncherExtension.java
index 9ac86c5..0cd272e 100644
--- 
a/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/launcher/UnpackLauncherExtension.java
+++ 
b/featuremodel-unpack-extension/src/main/java/org/apache/sling/feature/extension/unpack/impl/launcher/UnpackLauncherExtension.java
@@ -30,33 +30,23 @@ import org.apache.sling.feature.extension.unpack.Unpack;
 import org.apache.sling.feature.launcher.spi.extensions.ExtensionContext;
 import org.apache.sling.feature.launcher.spi.extensions.ExtensionHandler;
 
-public class UnpackLauncherExtension implements ExtensionHandler
-{
-    public static final String UNPACK_MAPPING_KEY = 
UnpackLauncherExtension.class.getPackage().getName() + ".mapping";
-
+public class UnpackLauncherExtension implements ExtensionHandler {
     @Override
-    public boolean handle(ExtensionContext extensionContext, Extension 
extension) throws Exception
-    {
-        if (extension.getType() == ExtensionType.ARTIFACTS)
-        {
-            String mapping = 
extensionContext.getFrameworkProperties().get(UNPACK_MAPPING_KEY);
+    public boolean handle(ExtensionContext extensionContext, Extension 
extension) throws Exception {
+        if (extension.getType() == ExtensionType.ARTIFACTS) {
+            String mapping = 
extensionContext.getFrameworkProperties().get(Unpack.UNPACK_EXTENSIONS_PROP);
 
             if (mapping == null || mapping.isEmpty()) {
-                mapping = System.getProperty(UNPACK_MAPPING_KEY);
+                mapping = System.getProperty(Unpack.UNPACK_EXTENSIONS_PROP);
             }
 
-            if (mapping != null && !mapping.isEmpty())
-            {
+            if (mapping != null && !mapping.isEmpty()) {
                 return Unpack.fromMapping(mapping).handle(extension, new 
ArtifactProvider() {
                     @Override
-                    public URL provide(ArtifactId artifactId)
-                    {
-                        try
-                        {
+                    public URL provide(ArtifactId artifactId) {
+                        try {
                             return 
extensionContext.getArtifactFile(artifactId);
-                        }
-                        catch (IOException e)
-                        {
+                        } catch (IOException e) {
                             throw new UncheckedIOException(e);
                         }
                     }
diff --git 
a/featuremodel-unpack-extension/src/main/resources/META-INF/services/org.apache.sling.feature.launcher.spi.extensions.ExtensionHandler
 
b/featuremodel-unpack-extension/src/main/resources/META-INF/services/org.apache.sling.feature.launcher.spi.extensions.ExtensionHandler
index 8cdb204..c7cf544 100644
--- 
a/featuremodel-unpack-extension/src/main/resources/META-INF/services/org.apache.sling.feature.launcher.spi.extensions.ExtensionHandler
+++ 
b/featuremodel-unpack-extension/src/main/resources/META-INF/services/org.apache.sling.feature.launcher.spi.extensions.ExtensionHandler
@@ -1 +1 @@
-org.apache.sling.feature.extension.unpack.impl.launcher.UnpackLauncherExtension
\ No newline at end of file
+org.apache.sling.feature.extension.unpack.impl.launcher.UnpackLauncherExtensionTest.UnpackLauncherExtension
\ No newline at end of file
diff --git 
a/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/TestUnpack.java
 
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/TestUnpack.java
index 484e2da..c66233b 100644
--- 
a/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/TestUnpack.java
+++ 
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/TestUnpack.java
@@ -37,11 +37,9 @@ import java.util.zip.ZipEntry;
 import org.junit.Assert;
 import org.junit.Test;
 
-public class TestUnpack
-{
+public class TestUnpack {
     @Test
-    public void testUnzip() throws IOException
-    {
+    public void testUnzip() throws IOException {
         File tmp = File.createTempFile("foo", "dir");
         tmp.delete();
         Unpack unpack = Unpack.fromMapping("test;default:=true;dir:=\"" + 
tmp.getPath() + "\";index:=\"Unpack-Index\";key:=binary;value:=1");
@@ -57,8 +55,7 @@ public class TestUnpack
         for (File child : file.listFiles()) {
             if (child.isDirectory()) {
                 result.addAll(childs(child, prefix + "/" + child.getName()));
-            }
-            else {
+            } else {
                 result.add(prefix + "/" + child.getName());
             }
         }
@@ -72,10 +69,8 @@ public class TestUnpack
         mf.getMainAttributes().putValue("binary", "1");
         mf.getMainAttributes().putValue("Unpack-Index", "sub1,sub2");
         try (JarOutputStream jarOutputStream = new JarOutputStream(new 
FileOutputStream(tmp), mf);
-             BufferedReader reader = new BufferedReader(new 
InputStreamReader(TestUnpack.class.getResourceAsStream(base + "/index.txt"), 
"UTF-8")))
-        {
-            for (String line = reader.readLine(); line != null; line = 
reader.readLine())
-            {
+             BufferedReader reader = new BufferedReader(new 
InputStreamReader(TestUnpack.class.getResourceAsStream(base + "/index.txt"), 
"UTF-8"))) {
+            for (String line = reader.readLine(); line != null; line = 
reader.readLine()) {
                 jarOutputStream.putNextEntry(new 
ZipEntry(line.contains("sub1") ? "sub1/" + line : line.contains("sub2") ? 
"sub2/" + line : line ));
                 try (InputStream inputStream = 
TestUnpack.class.getResourceAsStream(base + "/" + line)) {
                     byte[] buffer = new byte[64 * 1024];
@@ -92,8 +87,7 @@ public class TestUnpack
         try (BufferedReader reader = new BufferedReader(new 
InputStreamReader(TestUnpack.class.getResourceAsStream(base + "/index.txt"), 
"UTF-8"))) {
             Set<String> content = new HashSet<>();
             List<String> childs = childs(file, "");
-            for (String line = reader.readLine(); line != null; line = 
reader.readLine())
-            {
+            for (String line = reader.readLine(); line != null; line = 
reader.readLine()) {
                 if (!childs.contains("/" + line)) {
                     return false;
                 }
diff --git 
a/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveExtensionHandlerTest.java
 
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveExtensionHandlerTest.java
index 53d7e3f..643cfba 100644
--- 
a/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveExtensionHandlerTest.java
+++ 
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveExtensionHandlerTest.java
@@ -34,7 +34,7 @@ public class UnpackArchiveExtensionHandlerTest {
     @Test
     public void testCreateUnpacker() throws Exception {
         BundleContext bc = Mockito.mock(BundleContext.class);
-        
Mockito.when(bc.getProperty(UnpackArchiveExtensionHandler.UNPACK_EXTENSIONS_PROP))
+        Mockito.when(bc.getProperty(Unpack.UNPACK_EXTENSIONS_PROP))
             .thenReturn("foobar;dir:=/abc;default:=true");
 
         UnpackArchiveExtensionHandler uaeh = new 
UnpackArchiveExtensionHandler(bc);
diff --git 
a/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveInstallerPluginTest.java
 
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveInstallerPluginTest.java
index e5d4301..e64a9d5 100644
--- 
a/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveInstallerPluginTest.java
+++ 
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/installer/UnpackArchiveInstallerPluginTest.java
@@ -44,7 +44,7 @@ public class UnpackArchiveInstallerPluginTest {
     @Test
     public void testCreatePlugin() throws Exception {
         BundleContext bc = Mockito.mock(BundleContext.class);
-        
Mockito.when(bc.getProperty(UnpackArchiveExtensionHandler.UNPACK_EXTENSIONS_PROP))
+        Mockito.when(bc.getProperty(Unpack.UNPACK_EXTENSIONS_PROP))
             .thenReturn("foobar;dir:=/abc;default:=true");
 
         UnpackArchiveInstallerPlugin aeip = new 
UnpackArchiveInstallerPlugin(bc);
diff --git 
a/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/launcher/UnpackLauncherExtensionTest.java
 
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/launcher/UnpackLauncherExtensionTest.java
new file mode 100644
index 0000000..2847a31
--- /dev/null
+++ 
b/featuremodel-unpack-extension/src/test/java/org/apache/sling/feature/extension/unpack/impl/launcher/UnpackLauncherExtensionTest.java
@@ -0,0 +1,82 @@
+/*
+ * 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.extension.unpack.impl.launcher;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.jar.JarOutputStream;
+import java.util.zip.ZipEntry;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionState;
+import org.apache.sling.feature.ExtensionType;
+import org.apache.sling.feature.extension.unpack.Unpack;
+import org.apache.sling.feature.launcher.spi.extensions.ExtensionContext;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class UnpackLauncherExtensionTest {
+    @Test
+    public void testLauncherExtension() throws Exception {
+        UnpackLauncherExtension handler = new UnpackLauncherExtension();
+
+        ExtensionContext context = Mockito.mock(ExtensionContext.class);
+        Map<String, String> frameworkProperties = new HashMap<>();
+        File tmp = File.createTempFile("foo", "dir");
+        tmp.delete();
+        tmp.mkdirs();
+        tmp.deleteOnExit();
+
+        File target = new File(tmp, "target");
+        target.deleteOnExit();
+        frameworkProperties.put(Unpack.UNPACK_EXTENSIONS_PROP, "foo;dir:=\"" + 
target.getPath() + "\"");
+
+
+        
Mockito.when(context.getFrameworkProperties()).thenReturn(frameworkProperties);
+        Artifact artifact = new 
Artifact(ArtifactId.fromMvnId("org.apache.sling:foo:0.1.0-SNAPSHOT"));
+
+        File archiveJar = new File(tmp, "archive.jar");
+        archiveJar.deleteOnExit();
+        URL archive = createTestZip(archiveJar);
+
+        
Mockito.when(context.getArtifactFile(artifact.getId())).thenReturn(archive);
+
+        Extension extension = new Extension(ExtensionType.ARTIFACTS, "foo", 
ExtensionState.REQUIRED);
+
+        extension.getArtifacts().add(artifact);
+
+        Assert.assertTrue(handler.handle(context, extension));
+
+        Assert.assertTrue(new File(target, "foo/bar/baz.bin").isFile());
+    }
+
+    private URL createTestZip(File target) throws Exception {
+        try (JarOutputStream outputStream = new JarOutputStream(new 
FileOutputStream(target))) {
+            outputStream.putNextEntry(new ZipEntry("foo/bar/baz.bin"));
+            outputStream.write("this is a test".getBytes());
+        }
+        return target.toURI().toURL();
+    }
+}

Reply via email to