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/05/06 23:05:50 UTC

[sling-org-apache-sling-feature-cpconverter] branch master updated: SLING-8396 - Sling Content Package to Feature Model fails with embedded Bundles

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-org-apache-sling-feature-cpconverter.git


The following commit(s) were added to refs/heads/master by this push:
     new 82e4b67  SLING-8396 - Sling Content Package to Feature Model fails with embedded Bundles
82e4b67 is described below

commit 82e4b670f7d03088662f6f44421c6207f5fd4f62
Author: stripodi <st...@simos-mbp>
AuthorDate: Tue May 7 01:05:43 2019 +0200

    SLING-8396 - Sling Content Package to Feature Model fails with embedded
    Bundles
    
    guess the best pom.properties candidate via bundle file name
    additionally guess the artifact classifier
---
 .../cpconverter/handlers/BundleEntryHandler.java   |  82 +++++++++++++++++----
 .../handlers/GavDeclarationsInBundleTest.java      |  66 +++++++++++++++++
 .../handlers/core-1.0.0-SNAPSHOT-classified.jar    | Bin 0 -> 295107 bytes
 .../cpconverter/handlers/core-1.0.0-SNAPSHOT.jar   | Bin 0 -> 295107 bytes
 4 files changed, 134 insertions(+), 14 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandler.java b/src/main/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandler.java
index 8e39cfa..97735f2 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandler.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandler.java
@@ -19,6 +19,7 @@ package org.apache.sling.feature.cpconverter.handlers;
 import static java.util.Objects.requireNonNull;
 import static org.apache.jackrabbit.vault.packaging.PackageProperties.NAME_VERSION;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.Properties;
 import java.util.jar.JarEntry;
@@ -40,6 +41,8 @@ public final class BundleEntryHandler extends AbstractRegexEntryHandler {
 
     private static final String NAME_ARTIFACT_ID = "artifactId";
 
+    private static final String NAME_CLASSIFIER = "classifier";
+
     private static final String BUNDLE_SYMBOLIC_NAME = "Bundle-SymbolicName";
 
     private static final String BUNDLE_NAME = "Bundle-Name";
@@ -63,26 +66,17 @@ public final class BundleEntryHandler extends AbstractRegexEntryHandler {
         String groupId;
         String artifactId;
         String version;
+        String classifier = null;
 
         try (JarInputStream jarInput = new JarInputStream(archive.openInputStream(entry))) {
-            Properties properties = new Properties();
+            Properties properties = readGav(entry.getName(), jarInput);
             Manifest manifest = jarInput.getManifest();
 
-            JarEntry jarEntry;
-            while ((jarEntry = jarInput.getNextJarEntry()) != null) {
-                String entryName = jarEntry.getName();
-
-                if (pomPropertiesPattern.matcher(entryName).matches()) {
-                    logger.info("Reading '{}' bundle GAV from {}...", entry.getName(), entryName);
-
-                    properties.load(jarInput);
-                }
-            }
-
             if (!properties.isEmpty()) {
                 groupId = getCheckedProperty(properties, NAME_GROUP_ID);
                 artifactId = getCheckedProperty(properties, NAME_ARTIFACT_ID);
                 version = getCheckedProperty(properties, NAME_VERSION);
+                classifier = properties.getProperty(NAME_CLASSIFIER);
             } else { // maybe the included jar is just an OSGi bundle but not a valid Maven artifact
                 groupId = getCheckedProperty(manifest, BUNDLE_SYMBOLIC_NAME);
                 artifactId = getCheckedProperty(manifest, BUNDLE_NAME);
@@ -109,18 +103,78 @@ public final class BundleEntryHandler extends AbstractRegexEntryHandler {
                                                    groupId,
                                                    artifactId,
                                                    version,
-                                                   null,
+                                                   classifier,
                                                    JAR_TYPE);
 
             converter.attach(runMode,
                              groupId,
                              artifactId,
                              version,
-                             null,
+                             classifier,
                              JAR_TYPE);
         }
     }
 
+    // method visibility set to 'protected' fot testing purposes
+    protected Properties readGav(String bundleName, JarInputStream jarInput) throws IOException {
+        Properties properties = new Properties();
+
+        JarEntry jarEntry;
+        dance : while ((jarEntry = jarInput.getNextJarEntry()) != null) {
+            String entryName = jarEntry.getName();
+
+            if (pomPropertiesPattern.matcher(entryName).matches()) {
+                logger.info("Reading '{}' bundle GAV from {}...", bundleName, entryName);
+
+                properties.load(jarInput);
+
+                String artifactId = properties.getProperty(NAME_ARTIFACT_ID);
+                String version = properties.getProperty(NAME_VERSION);
+
+                if (artifactId == null || version == null) {
+                    continue;
+                }
+
+                int idx = bundleName.lastIndexOf('/');
+                if (idx >= 0) {
+                    bundleName = bundleName.substring(idx + 1);
+                }
+
+                int edx = bundleName.lastIndexOf('.');
+                if (edx > 0) {
+                    bundleName = bundleName.substring(0, edx);
+                }
+
+                // bundleName is now the bare name without extension
+                String synthesized = artifactId + "-" + version;
+
+                // it was the pom.properties  we were looking for
+                if (bundleName.startsWith(synthesized)) {
+
+                    // check the artifact has a classifier in the bundle file name
+                    if (synthesized.length() < bundleName.length()) {
+                        String suffix = bundleName.substring(synthesized.length());
+                        if (suffix.length() > 1 && suffix.startsWith("-")) {
+                            String classifier = suffix.substring(1);
+                            logger.info("Inferred classifier of '"
+                                        + artifactId
+                                        + ":"
+                                        + version
+                                        + "' to be '"
+                                        + classifier
+                                        + "'");
+                            properties.setProperty(NAME_CLASSIFIER, classifier);
+                        }
+                    }
+
+                    break dance;
+                }
+            }
+        }
+
+        return properties;
+    }
+
     private static String getCheckedProperty(Manifest manifest, String name) {
         String property = manifest.getMainAttributes().getValue(name).trim();
         return requireNonNull(property, "Jar file can not be defined as a valid OSGi bundle without specifying a valid '"
diff --git a/src/test/java/org/apache/sling/feature/cpconverter/handlers/GavDeclarationsInBundleTest.java b/src/test/java/org/apache/sling/feature/cpconverter/handlers/GavDeclarationsInBundleTest.java
new file mode 100644
index 0000000..7d766fc
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/cpconverter/handlers/GavDeclarationsInBundleTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.cpconverter.handlers;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import java.util.Properties;
+import java.util.jar.JarInputStream;
+
+import org.junit.Test;
+
+public class GavDeclarationsInBundleTest {
+
+    @Test
+    public void guessRightGavsWhenMultiplePomPropertiesDeclarationsAreIncluded() throws Exception {
+        verifyBundle("core-1.0.0-SNAPSHOT.jar",
+                     "com.madplanet.sling.cp2sf",
+                     "core",
+                     "1.0.0-SNAPSHOT",
+                     null);
+        
+    }
+
+    @Test
+    public void guessTheClassifierFromBundleName() throws Exception {
+        verifyBundle("core-1.0.0-SNAPSHOT-classified.jar",
+                     "com.madplanet.sling.cp2sf",
+                     "core",
+                     "1.0.0-SNAPSHOT",
+                    "classified");
+    }
+
+    private void verifyBundle(String bundleName,
+                              String expectedGroupId,
+                              String expectedArtifactId,
+                              String expectedVersion,
+                              String expectedClassifier) throws Exception {
+        BundleEntryHandler bundleEntryHandler = new BundleEntryHandler();
+
+        try (JarInputStream jarInput = new JarInputStream(getClass().getResourceAsStream(bundleName))) {
+            Properties actual = bundleEntryHandler.readGav(bundleName, jarInput);
+
+            assertFalse(actual.isEmpty());
+            assertEquals(expectedGroupId, actual.getProperty("groupId"));
+            assertEquals(expectedArtifactId, actual.getProperty("artifactId"));
+            assertEquals(expectedVersion, actual.getProperty("version"));
+            assertEquals(expectedClassifier, actual.getProperty("classifier"));
+        }
+    }
+
+}
diff --git a/src/test/resources/org/apache/sling/feature/cpconverter/handlers/core-1.0.0-SNAPSHOT-classified.jar b/src/test/resources/org/apache/sling/feature/cpconverter/handlers/core-1.0.0-SNAPSHOT-classified.jar
new file mode 100644
index 0000000..3991329
Binary files /dev/null and b/src/test/resources/org/apache/sling/feature/cpconverter/handlers/core-1.0.0-SNAPSHOT-classified.jar differ
diff --git a/src/test/resources/org/apache/sling/feature/cpconverter/handlers/core-1.0.0-SNAPSHOT.jar b/src/test/resources/org/apache/sling/feature/cpconverter/handlers/core-1.0.0-SNAPSHOT.jar
new file mode 100644
index 0000000..3991329
Binary files /dev/null and b/src/test/resources/org/apache/sling/feature/cpconverter/handlers/core-1.0.0-SNAPSHOT.jar differ