You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by si...@apache.org on 2019/06/18 21:42:27 UTC

[sling-whiteboard] 02/04: [r2f] fixed 'pom.properties' resource loading

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

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

commit 7cc0a3e133a744ac976115b16548130bb44c409e
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Tue Jun 18 23:40:41 2019 +0200

    [r2f] fixed 'pom.properties' resource loading
---
 .../feature/r2f/impl/Bundle2ArtifactMapper.java    | 74 ++++++++++++++--------
 1 file changed, 48 insertions(+), 26 deletions(-)

diff --git a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/Bundle2ArtifactMapper.java b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/Bundle2ArtifactMapper.java
index 6779c72..26f31ed 100644
--- a/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/Bundle2ArtifactMapper.java
+++ b/runtime2feature/src/main/java/org/apache/sling/feature/r2f/impl/Bundle2ArtifactMapper.java
@@ -16,12 +16,13 @@
  */
 package org.apache.sling.feature.r2f.impl;
 
-import java.io.IOException;
+import static org.osgi.framework.wiring.BundleWiring.LISTRESOURCES_RECURSE;
+
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
-import java.util.Enumeration;
+import java.util.Collection;
 import java.util.Properties;
 import java.util.function.Function;
 
@@ -30,12 +31,13 @@ import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Feature;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.startlevel.BundleStartLevel;
+import org.osgi.framework.wiring.BundleWiring;
 
 final class Bundle2ArtifactMapper extends AbstractFeatureElementConsumer<Artifact> implements Function<Bundle, Artifact> {
 
-    private static final String POM_PROPERTIES_RESOURCE_NAME = "pom.properties";
+    private static final String MAVEN_METADATA_PATH = "/META-INF/maven";
 
-    private static final String MAVEN_METADATA_PATH = "META-INF/maven";
+    private static final String POM_PROPERTIES_RESOURCE_NAME = "pom.properties";
 
     private static final String GROUP_ID = "groupId";
 
@@ -53,28 +55,34 @@ final class Bundle2ArtifactMapper extends AbstractFeatureElementConsumer<Artifac
     public Artifact apply(Bundle bundle) {
         Properties pomProperties = new Properties();
 
-        Enumeration<URL> pomPropertiesURLs = bundle.findEntries(MAVEN_METADATA_PATH, POM_PROPERTIES_RESOURCE_NAME, true);
-        if (pomPropertiesURLs.hasMoreElements()) {
-            URL pomPropertiesURL = pomPropertiesURLs.nextElement();
-
-            try {
-                URLConnection connection = pomPropertiesURL.openConnection();
-                connection.connect();
-
-                try (InputStream inStream = connection.getInputStream()) {
-                    pomProperties.load(inStream);
-                }
-
-                if (connection instanceof HttpURLConnection) {
-                    ((HttpURLConnection) connection).disconnect();
-                }
-            } catch (IOException e) {
-                throw new RuntimeException("An error occurred while reading "
-                                           + pomPropertiesURL
-                                           + " properties file from Bundle "
-                                           + bundle.getSymbolicName()
-                                           + " does not export valid Maven metadata", e);
+        BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);
+        Collection<String> pomPropertiesResources = bundleWiring.listResources(MAVEN_METADATA_PATH, POM_PROPERTIES_RESOURCE_NAME, LISTRESOURCES_RECURSE);
+
+        if (pomPropertiesResources == null || pomPropertiesResources.isEmpty()) {
+            return null;
+        }
+
+        URL pomPropertiesURL = getPomPropertiesURL(pomPropertiesResources, bundle);
+        if (pomPropertiesURL == null) {
+            return null;
+        }
+
+        try {
+            URLConnection connection = pomPropertiesURL.openConnection();
+            connection.connect();
+
+            try (InputStream inStream = connection.getInputStream()) {
+                pomProperties.load(inStream);
             }
+
+            if (connection instanceof HttpURLConnection) {
+                ((HttpURLConnection) connection).disconnect();
+            }
+        } catch (Throwable t) {
+            throw new RuntimeException("An error occurred while reading "
+                                       + pomPropertiesURL
+                                       + " properties file from Bundle "
+                                       + bundle.getSymbolicName(), t);
         }
 
         if (pomProperties.isEmpty()) {
@@ -97,9 +105,23 @@ final class Bundle2ArtifactMapper extends AbstractFeatureElementConsumer<Artifac
         return artifact;
     }
 
+    private static URL getPomPropertiesURL(Collection<String> pomPropertiesResources, Bundle bundle) {
+        for (String pomPropertiesResource : pomPropertiesResources) {
+            URL pomPropertiesURL = bundle.getEntry(pomPropertiesResource);
+
+            if (pomPropertiesURL != null) {
+                return pomPropertiesURL;
+            }
+        }
+
+        return null;
+    }
+
     @Override
     public void accept(Artifact artifact) {
-        getTargetFeature().getBundles().add(artifact);
+        if (artifact != null) {
+            getTargetFeature().getBundles().add(artifact);
+        }
     }
 
 }