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/03/13 13:38:28 UTC

[sling-whiteboard] branch master updated: [cp2fm] always make sure that missing pom file is supplied with an on-the-fly generated one

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


The following commit(s) were added to refs/heads/master by this push:
     new da4b1d4  [cp2fm] always make sure that missing pom file is supplied with an on-the-fly generated one
da4b1d4 is described below

commit da4b1d4b737b7e4b4fa2c42c6b5a7d619f928594
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Wed Mar 13 14:38:17 2019 +0100

    [cp2fm] always make sure that missing pom file is supplied with an
    on-the-fly generated one
---
 .../ContentPackage2FeatureModelConverter.java      |  32 +++++------
 .../AbstractConfigurationEntryHandler.java         |   6 +++
 .../sling/cp2fm/handlers/BundleEntryHandler.java   |  32 +++++++----
 .../apache/sling/cp2fm/utils/MavenPomSupplier.java |  60 +++++++++++++++++++++
 .../org/apache/sling/cp2fm/utils/package-info.java |  17 ++++++
 .../cp2fm/handlers/BundleEntryHandlerTest.java     |   1 +
 .../apps/asd/install/test-framework-no-pom.jar     | Bin 0 -> 10769 bytes
 7 files changed, 119 insertions(+), 29 deletions(-)

diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
index 56b54c5..bb4bd16 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/ContentPackage2FeatureModelConverter.java
@@ -23,7 +23,6 @@ import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.StringWriter;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Dictionary;
@@ -49,10 +48,9 @@ import org.apache.jackrabbit.vault.packaging.PackageProperties;
 import org.apache.jackrabbit.vault.packaging.PackageType;
 import org.apache.jackrabbit.vault.packaging.VaultPackage;
 import org.apache.jackrabbit.vault.packaging.impl.PackageManagerImpl;
-import org.apache.maven.model.Model;
-import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
 import org.apache.sling.cp2fm.handlers.DefaultEntryHandler;
 import org.apache.sling.cp2fm.spi.EntryHandler;
+import org.apache.sling.cp2fm.utils.MavenPomSupplier;
 import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Configuration;
@@ -264,23 +262,17 @@ public class ContentPackage2FeatureModelConverter {
                     destFile.delete();
                 }
 
-                Model model = new Model();
-                model.setGroupId(targetFeature.getId().getGroupId());
-                model.setArtifactId(targetFeature.getId().getArtifactId());
-                model.setVersion(targetFeature.getId().getVersion());
-                model.setPackaging(ZIP_TYPE);
-
-                try (StringWriter stringWriter = new StringWriter()) {
-                    new MavenXpp3Writer().write(stringWriter, model);
-
-                    try (InputStream input = new ByteArrayInputStream(stringWriter.toString().getBytes())) {
-                        deployLocally(input,
-                                      targetFeature.getId().getGroupId(),
-                                      targetFeature.getId().getArtifactId(),
-                                      targetFeature.getId().getVersion(),
-                                      null,
-                                      POM_TYPE);
-                    }
+                String pomModel = MavenPomSupplier.generatePom(targetFeature.getId().getGroupId(),
+                                                               targetFeature.getId().getArtifactId(),
+                                                               targetFeature.getId().getVersion(),
+                                                               ZIP_TYPE);
+                try (InputStream input = new ByteArrayInputStream(pomModel.getBytes())) {
+                    deployLocally(input,
+                                  targetFeature.getId().getGroupId(),
+                                  targetFeature.getId().getArtifactId(),
+                                  targetFeature.getId().getVersion(),
+                                  null,
+                                  POM_TYPE);
                 }
             } else {
                 logger.info("No resources to be repackaged.");
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/AbstractConfigurationEntryHandler.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/AbstractConfigurationEntryHandler.java
index 9e891b3..58f3b3e 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/AbstractConfigurationEntryHandler.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/AbstractConfigurationEntryHandler.java
@@ -52,6 +52,12 @@ abstract class AbstractConfigurationEntryHandler extends AbstractRegexEntryHandl
         if (matcher.matches()) {
             // there is a specified RunMode
             runMode = matcher.group(3);
+        } else {
+            throw new IllegalStateException("Something went terribly wrong: pattern '"
+                                            + getPattern().pattern()
+                                            + "' should have matched already with path '"
+                                            + path
+                                            + "' but it does not, currently");
         }
 
         converter.addConfiguration(runMode, pid, configurationProperties);
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/BundleEntryHandler.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/BundleEntryHandler.java
index e1cab62..92843ad 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/BundleEntryHandler.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/BundleEntryHandler.java
@@ -25,6 +25,7 @@ import static org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter.POM_TY
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.InputStream;
+import java.util.Objects;
 import java.util.Properties;
 import java.util.jar.JarEntry;
 import java.util.jar.JarInputStream;
@@ -35,6 +36,7 @@ import org.apache.commons.io.IOUtils;
 import org.apache.jackrabbit.vault.fs.io.Archive;
 import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
 import org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter;
+import org.apache.sling.cp2fm.utils.MavenPomSupplier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -78,9 +80,9 @@ public final class BundleEntryHandler extends AbstractRegexEntryHandler {
             }
         }
 
-        String groupId = getTrimmedProperty(properties, NAME_GROUP_ID);
-        String artifactId = getTrimmedProperty(properties, NAME_ARTIFACT_ID);
-        String version = getTrimmedProperty(properties, NAME_VERSION);
+        String groupId = getCheckedProperty(properties, NAME_GROUP_ID);
+        String artifactId = getCheckedProperty(properties, NAME_ARTIFACT_ID);
+        String version = getCheckedProperty(properties, NAME_VERSION);
 
         Matcher matcher = getPattern().matcher(path);
         String runMode = null;
@@ -88,21 +90,33 @@ public final class BundleEntryHandler extends AbstractRegexEntryHandler {
         if (matcher.matches()) {
             // there is a specified RunMode
             runMode = matcher.group(3);
+        } else {
+            throw new IllegalStateException("Something went terribly wrong: pattern '"
+                                            + getPattern().pattern()
+                                            + "' should have matched already with path '"
+                                            + path
+                                            + "' but it does not, currently");
         }
 
         try (InputStream input = archive.openInputStream(entry)) {
             converter.deployLocallyAndAttach(runMode, input, groupId, artifactId, version, null, JAR_TYPE);
         }
 
-        if (pomXml != null) {
-            try (ByteArrayInputStream input = new ByteArrayInputStream(pomXml)) {
-                converter.deployLocally(input, groupId, artifactId, version, null, POM_TYPE);
-            }
+        if (pomXml == null) {
+            pomXml = MavenPomSupplier.generatePom(groupId, artifactId, version, JAR_TYPE).getBytes();
+        }
+
+        try (ByteArrayInputStream input = new ByteArrayInputStream(pomXml)) {
+            converter.deployLocally(input, groupId, artifactId, version, null, POM_TYPE);
         }
     }
 
-    private static String getTrimmedProperty(Properties properties, String name) {
-        return properties.getProperty(name).trim();
+    private static String getCheckedProperty(Properties properties, String name) {
+        String property = properties.getProperty(name).trim();
+        Objects.requireNonNull(property, "Bundle can not be defined as a valid Maven artifact without specifying a valid '"
+                                         + name
+                                         + "' property.");
+        return property;
     }
 
 }
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/utils/MavenPomSupplier.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/utils/MavenPomSupplier.java
new file mode 100644
index 0000000..f8b2cbc
--- /dev/null
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/utils/MavenPomSupplier.java
@@ -0,0 +1,60 @@
+/*
+ * 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.cp2fm.utils;
+
+import java.io.IOException;
+import java.io.StringWriter;
+
+import org.apache.maven.model.Model;
+import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class MavenPomSupplier {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(MavenPomSupplier.class);
+
+    /**
+     * Hidden constructor, this class can not be directly instantiated.
+     */
+    private MavenPomSupplier() {
+        // do nothing
+    }
+
+    public static String generatePom(String groupId,
+                                     String artifactId,
+                                     String version,
+                                     String type) throws IOException {
+        LOGGER.info("Creating synthetic POM file for bundle [{}:{}:{}:{}]",
+                    groupId,
+                    artifactId,
+                    version,
+                    type);
+
+        Model model = new Model();
+        model.setGroupId(groupId);
+        model.setArtifactId(artifactId);
+        model.setVersion(version);
+        model.setPackaging(type);
+
+        try (StringWriter stringWriter = new StringWriter()) {
+            new MavenXpp3Writer().write(stringWriter, model);
+            return stringWriter.toString();
+        }
+    }
+
+}
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/utils/package-info.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/utils/package-info.java
new file mode 100644
index 0000000..76fa3b1
--- /dev/null
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/utils/package-info.java
@@ -0,0 +1,17 @@
+/*
+ * 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.cp2fm.utils;
diff --git a/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/BundleEntryHandlerTest.java b/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/BundleEntryHandlerTest.java
index 57613a0..754a108 100644
--- a/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/BundleEntryHandlerTest.java
+++ b/content-package-2-feature-model/src/test/java/org/apache/sling/cp2fm/handlers/BundleEntryHandlerTest.java
@@ -109,6 +109,7 @@ public final class BundleEntryHandlerTest {
         final BundleEntryHandler bundleEntryHandler = new BundleEntryHandler();
 
         return Arrays.asList(new Object[][] {
+            { "jcr_root/apps/asd/install/test-framework-no-pom.jar", bundleEntryHandler },
             { "jcr_root/apps/asd/install/test-framework.jar", bundleEntryHandler },
             { "jcr_root/apps/asd/install.author/test-framework.jar", bundleEntryHandler },
             { "jcr_root/apps/asd/install.publish/test-framework.jar", bundleEntryHandler }
diff --git a/content-package-2-feature-model/src/test/resources/org/apache/sling/cp2fm/handlers/jcr_root/apps/asd/install/test-framework-no-pom.jar b/content-package-2-feature-model/src/test/resources/org/apache/sling/cp2fm/handlers/jcr_root/apps/asd/install/test-framework-no-pom.jar
new file mode 100644
index 0000000..cb3116e
Binary files /dev/null and b/content-package-2-feature-model/src/test/resources/org/apache/sling/cp2fm/handlers/jcr_root/apps/asd/install/test-framework-no-pom.jar differ