You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by en...@apache.org on 2021/03/03 19:08:08 UTC

[sling-org-apache-sling-feature-analyser] branch master updated: SLING-10182 Analyze fails to evaluate when the aggregate feature contains the bundle being built

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 642a147  SLING-10182 Analyze fails to evaluate when the aggregate feature contains the bundle being built
642a147 is described below

commit 642a14742f1b940ab658979dc4828248d092610b
Author: Eric Norman <en...@apache.org>
AuthorDate: Wed Mar 3 11:07:50 2021 -0800

    SLING-10182 Analyze fails to evaluate when the aggregate feature
    contains the bundle being built
---
 .../feature/scanner/impl/BundleDescriptorImpl.java | 26 ++++++++++++-
 .../scanner/impl/BundleDescriptorImplTest.java     | 44 ++++++++++++++++++++--
 2 files changed, 65 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/scanner/impl/BundleDescriptorImpl.java b/src/main/java/org/apache/sling/feature/scanner/impl/BundleDescriptorImpl.java
index 7620946..a6ec6cb 100644
--- a/src/main/java/org/apache/sling/feature/scanner/impl/BundleDescriptorImpl.java
+++ b/src/main/java/org/apache/sling/feature/scanner/impl/BundleDescriptorImpl.java
@@ -29,8 +29,14 @@ import org.osgi.framework.Constants;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.net.URISyntaxException;
 import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
@@ -72,9 +78,25 @@ public class BundleDescriptorImpl
     private final Artifact artifact;
 
     private static Manifest getManifest(URL file) throws IOException {
-        try (JarFile jarFile = IOUtils.getJarFileFromURL(file, true, null)) {
-            return jarFile.getManifest();
+        Manifest manifest = null;
+        try {
+            if ("file".equals(file.getProtocol()) && new File(file.toURI()).isDirectory()) {
+                // it's a directory
+                Path manifestPath = Paths.get(file.toURI()).resolve("META-INF/MANIFEST.MF");
+                if (Files.exists(manifestPath)) {
+                    try (FileInputStream is = new FileInputStream(manifestPath.toFile())) {
+                        manifest = new Manifest(is);
+                    }
+                }
+            } else {
+                try (JarFile jarFile = IOUtils.getJarFileFromURL(file, true, null)) {
+                    manifest = jarFile.getManifest();
+                }
+            }
+        } catch (URISyntaxException e) {
+            throw new IOException("Failed to determine if the file is a directory", e);
         }
+        return manifest;
     }
 
     public BundleDescriptorImpl(final Artifact a,
diff --git a/src/test/java/org/apache/sling/feature/scanner/impl/BundleDescriptorImplTest.java b/src/test/java/org/apache/sling/feature/scanner/impl/BundleDescriptorImplTest.java
index c56d9c3..e1b3fc5 100644
--- a/src/test/java/org/apache/sling/feature/scanner/impl/BundleDescriptorImplTest.java
+++ b/src/test/java/org/apache/sling/feature/scanner/impl/BundleDescriptorImplTest.java
@@ -16,8 +16,17 @@
  */
 package org.apache.sling.feature.scanner.impl;
 
-import java.io.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
 import java.net.URL;
+import java.nio.file.Files;
 import java.util.Set;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Manifest;
@@ -32,8 +41,6 @@ import org.apache.sling.feature.scanner.Scanner;
 import org.junit.Test;
 import org.osgi.framework.Version;
 
-import static org.junit.Assert.*;
-
 public class BundleDescriptorImplTest
 {
 
@@ -73,6 +80,23 @@ public class BundleDescriptorImplTest
                 .getManifest().getMainAttributes().getValue("Manifest-Version"));
     }
 
+    /**
+     * for SLING-10182 
+     */
+    @Test
+    public void testBundleManifestForFolder() throws Exception {
+        String bmf = "Bundle-SymbolicName: pkg.bundle\n"
+                + "Bundle-Version: 1\n"
+                + "Bundle-ManifestVersion: 2\n"
+                + "Export-Package: org.apache.sling;version=1.0,org.apache.felix;version=2.0\n";
+        URL f = createBundleFolder(bmf).toURI().toURL();
+        BundleDescriptorImpl bdf = new BundleDescriptorImpl(new Artifact(new ArtifactId("foo", "bar", "1.0", "bla", "bundle")), f, 1);
+        final Set<PackageInfo> infos = bdf.getExportedPackages();
+        assertEquals(2, infos.size());
+        assertPackageInfo(infos ,"org.apache.sling", Version.parseVersion("1.0"));
+        assertPackageInfo(infos,"org.apache.felix", Version.parseVersion("2.0"));
+    }
+
     private File createBundle(String manifest) throws IOException
     {
         File f = File.createTempFile("bundle", ".jar");
@@ -83,4 +107,18 @@ public class BundleDescriptorImplTest
         os.close();
         return f;
     }
+
+    private File createBundleFolder(String manifest) throws IOException
+    {
+        File f = Files.createTempDirectory("bundle.folder").toFile();
+        f.deleteOnExit();
+        Manifest mf = new Manifest(new ByteArrayInputStream(manifest.getBytes("UTF-8")));
+        mf.getMainAttributes().putValue("Manifest-Version", "1.0");
+        File manifestFile = new File(f, "META-INF/MANIFEST.MF");
+        manifestFile.getParentFile().mkdirs();
+        try (FileOutputStream out = new FileOutputStream(manifestFile)) {
+            mf.write(out);
+        }
+        return f;
+    }
 }