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/05 17:08:33 UTC

[sling-whiteboard] branch master updated: [cp2fm] repackaged unhandled resources in a proper artifact and added in the resulting Feature Model

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 17c9def  [cp2fm] repackaged unhandled resources in a proper artifact and added in the resulting Feature Model
17c9def is described below

commit 17c9def804fd0ccf2a7ac9669880e0b7c00676e0
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Tue Mar 5 18:08:26 2019 +0100

    [cp2fm] repackaged unhandled resources in a proper artifact and added in
    the resulting Feature Model
---
 content-package-2-feature-model/pom.xml            |  41 +++++++
 .../ContentPackage2FeatureModelConverter.java      | 121 ++++++++++++++++++++-
 .../sling/cp2fm/handlers/BundleEntryHandler.java   |  52 ++-------
 .../sling/cp2fm/handlers/DefaultEntryHandler.java  |   6 +-
 4 files changed, 174 insertions(+), 46 deletions(-)

diff --git a/content-package-2-feature-model/pom.xml b/content-package-2-feature-model/pom.xml
index 38cf1eb..95fb311 100644
--- a/content-package-2-feature-model/pom.xml
+++ b/content-package-2-feature-model/pom.xml
@@ -160,6 +160,47 @@
       <version>2.19.1</version>
       <scope>provided</scope>
     </dependency>
+
+    <!--
+     | repackage
+    -->
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-archiver</artifactId>
+      <version>4.1.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-utils</artifactId>
+      <version>3.1.1</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-io</artifactId>
+      <version>3.1.1</version>
+      <scope>provided</scope>
+    </dependency>
+    <!--
+     | avoid classloading issue
+    -->
+    <dependency>
+      <groupId>org.codehaus.plexus</groupId>
+      <artifactId>plexus-container-default</artifactId>
+      <version>2.0.0</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <!--
+     | POM model
+    -->
+    <dependency>
+      <groupId>org.apache.maven</groupId>
+      <artifactId>maven-model</artifactId>
+      <version>3.6.0</version>
+      <scope>provided</scope>
+    </dependency>
   </dependencies>
 
   <build>
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 38764f0..69b694f 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
@@ -17,26 +17,48 @@
 package org.apache.sling.cp2fm;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Iterator;
 import java.util.ServiceLoader;
+import java.util.StringTokenizer;
 
+import org.apache.commons.io.IOUtils;
+import org.apache.jackrabbit.vault.fs.config.MetaInf;
 import org.apache.jackrabbit.vault.fs.io.Archive;
 import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
 import org.apache.jackrabbit.vault.packaging.PackageManager;
 import org.apache.jackrabbit.vault.packaging.PackageProperties;
 import org.apache.jackrabbit.vault.packaging.VaultPackage;
 import org.apache.jackrabbit.vault.packaging.impl.PackageManagerImpl;
+import org.apache.maven.model.Model;
 import org.apache.sling.cp2fm.handlers.DefaultEntryHandler;
 import org.apache.sling.cp2fm.spi.EntryHandler;
+import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.Feature;
 import org.apache.sling.feature.io.json.FeatureJSONWriter;
+import org.codehaus.plexus.archiver.Archiver;
+import org.codehaus.plexus.archiver.jar.JarArchiver;
+import org.codehaus.plexus.archiver.util.DefaultFileSet;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public final class ContentPackage2FeatureModelConverter {
 
+    public static final String POM_TYPE = "pom";
+
+    private static final String ZIP_TYPE = "zip";
+
+    public static final String GROUP_ID = "groupId";
+
+    public static final String ARTIFACT_ID = "artifactId";
+
+    public static final String VERSION = "version";
+
     private static final String FEATURE_CLASSIFIER = "cp2fm-converted-feature";
 
     private static final String SLING_OSGI_FEATURE_TILE_TYPE = "slingosgifeature";
@@ -77,10 +99,6 @@ public final class ContentPackage2FeatureModelConverter {
         return this;
     }
 
-    public int getBundlesStartOrder() {
-        return bundlesStartOrder;
-    }
-
     public ContentPackage2FeatureModelConverter setContentPackage(File contentPackage) {
         this.contentPackage = contentPackage;
         return this;
@@ -148,6 +166,43 @@ public final class ContentPackage2FeatureModelConverter {
             Archive archive = vaultPackage.getArchive();
             process(archive);
 
+            // attach all un matched resources as new content-packge
+
+            File deflatedDir = new File(outputDirectory, DefaultEntryHandler.TMP_DEFLATED);
+
+            if (deflatedDir.listFiles().length > 0) {
+                Archiver archiver = new JarArchiver();
+
+                File destFile = File.createTempFile(targetFeature.getId().getArtifactId(), '.' + ZIP_TYPE);
+
+                archiver.setDestFile(destFile);
+                archiver.addFileSet(new DefaultFileSet(deflatedDir));
+                archiver.createArchive();
+
+                try (InputStream input = new FileInputStream(destFile)) {
+                    deployLocallyAndAttach(input,
+                                           targetFeature.getId().getGroupId(),
+                                           targetFeature.getId().getArtifactId(),
+                                           targetFeature.getId().getVersion(),
+                                           FEATURE_CLASSIFIER,
+                                           ZIP_TYPE);
+                } finally {
+                    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);
+
+                // TODO deploy the pom in the local dir
+            } else {
+                logger.info("No resources to be repackaged.");
+            }
+
+            // finally serialize the Feature Model file
+
             File targetFile = new File(outputDirectory, targetFeature.getId().getArtifactId() + JSON_FILE_EXTENSION);
 
             logger.info("Conversion complete!", targetFile);
@@ -169,6 +224,9 @@ public final class ContentPackage2FeatureModelConverter {
         try {
             archive.open(strictValidation);
 
+            MetaInf metaInf = archive.getMetaInf();
+            logger.debug("Meta Inf: {}, l'altra metà invece non so cosa sia", metaInf);
+
             Entry jcrRoot = archive.getJcrRoot();
             traverse(null, archive, jcrRoot);
         } finally {
@@ -215,4 +273,59 @@ public final class ContentPackage2FeatureModelConverter {
         return defaultEntryHandler;
     }
 
+    public void deployLocallyAndAttach(InputStream input,
+                                       String groupId,
+                                       String artifactId,
+                                       String version,
+                                       String classifier,
+                                       String type) throws IOException {
+        deployLocally(input, groupId, artifactId, version, classifier, type);
+
+        Artifact artifact = new Artifact(new ArtifactId(groupId, artifactId, version, classifier, type));
+        artifact.setStartOrder(bundlesStartOrder);
+        targetFeature.getBundles().add(artifact);
+    }
+
+    public void deployLocally(InputStream input,
+                              String groupId,
+                              String artifactId,
+                              String version,
+                              String classifier,
+                              String type) throws IOException {
+        File targetDir = new File(outputDirectory, "bundles");
+
+        StringTokenizer tokenizer = new StringTokenizer(groupId, ".");
+        while (tokenizer.hasMoreTokens()) {
+            String current = tokenizer.nextToken();
+            targetDir = new File(targetDir, current);
+        }
+
+        targetDir = new File(targetDir, artifactId);
+
+        targetDir = new File(targetDir, version);
+
+        targetDir.mkdirs();
+
+        StringBuilder nameBuilder = new StringBuilder()
+                                    .append(artifactId)
+                                    .append('-')
+                                    .append(version);
+
+        if (classifier != null) {
+            nameBuilder.append('-').append(classifier);
+        }
+
+        nameBuilder.append('.').append(type);
+
+        File targetFile = new File(targetDir, nameBuilder.toString());
+
+        logger.info("Writing data to {}...", targetFile);
+
+        try (FileOutputStream targetStream = new FileOutputStream(targetFile)) {
+            IOUtils.copy(input, targetStream);
+        }
+
+        logger.info("Data successfully written to {}.", targetFile);
+    }
+
 }
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 2666de7..f07c836 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
@@ -16,14 +16,15 @@
  */
 package org.apache.sling.cp2fm.handlers;
 
+import static org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter.ARTIFACT_ID;
+import static org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter.GROUP_ID;
+import static org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter.POM_TYPE;
+import static org.apache.sling.cp2fm.ContentPackage2FeatureModelConverter.VERSION;
+
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
 import java.io.InputStream;
 import java.util.Properties;
-import java.util.StringTokenizer;
 import java.util.jar.JarEntry;
 import java.util.jar.JarInputStream;
 import java.util.regex.Pattern;
@@ -32,13 +33,13 @@ 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.feature.Artifact;
-import org.apache.sling.feature.ArtifactId;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public final class BundleEntryHandler extends AbstractRegexEntryHandler {
 
+    private static final String JAR_TYPE = "jar";
+
     private final Logger logger = LoggerFactory.getLogger(getClass());
 
     private final Pattern pomPropertiesPattern = Pattern.compile("META-INF/maven/[^/]+/[^/]+/pom.properties");
@@ -75,52 +76,23 @@ public final class BundleEntryHandler extends AbstractRegexEntryHandler {
             }
         }
 
-        File targetDir = new File(converter.getOutputDirectory(), "bundles");
-
-        String groupId = getTrimmedProperty(properties, "groupId");
-        StringTokenizer tokenizer = new StringTokenizer(groupId, ".");
-        while (tokenizer.hasMoreTokens()) {
-            String current = tokenizer.nextToken();
-            targetDir = new File(targetDir, current);
-        }
-
-        String artifactId = getTrimmedProperty(properties, "artifactId");
-        targetDir = new File(targetDir, artifactId);
-
-        String version = getTrimmedProperty(properties, "version");
-        targetDir = new File(targetDir, version);
-
-        targetDir.mkdirs();
+        String groupId = getTrimmedProperty(properties, GROUP_ID);
+        String artifactId = getTrimmedProperty(properties, ARTIFACT_ID);
+        String version = getTrimmedProperty(properties, VERSION);
 
         try (InputStream input = archive.openInputStream(entry)) {
-            write(input, targetDir, artifactId, version, "jar");
+            converter.deployLocallyAndAttach(input, groupId, artifactId, version, null, JAR_TYPE);
         }
 
         if (pomXml != null) {
             try (ByteArrayInputStream input = new ByteArrayInputStream(pomXml)) {
-                write(input, targetDir, artifactId, version, "pom");
+                converter.deployLocally(input, groupId, artifactId, version, null, POM_TYPE);
             }
         }
-
-        Artifact artifact = new Artifact(new ArtifactId(groupId, artifactId, version, null, null));
-        artifact.setStartOrder(converter.getBundlesStartOrder());
-        converter.getTargetFeature().getBundles().add(artifact);
     }
 
     private static String getTrimmedProperty(Properties properties, String name) {
         return properties.getProperty(name).trim();
     }
 
-    private void write(InputStream input, File targetDir, String artifactId, String version, String type) throws IOException {
-        File targetFile = new File(targetDir, String.format("%s-%s.%s", artifactId, version, type));
-
-        logger.info("Writing data to {}...", targetFile);
-
-        try (FileOutputStream targetStream = new FileOutputStream(targetFile)) {
-            IOUtils.copy(input, targetStream);
-        }
-
-        logger.info("Data successfully written to {}.", targetFile);
-    }
-
 }
diff --git a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/DefaultEntryHandler.java b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/DefaultEntryHandler.java
index 1706071..9eece3f 100644
--- a/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/DefaultEntryHandler.java
+++ b/content-package-2-feature-model/src/main/java/org/apache/sling/cp2fm/handlers/DefaultEntryHandler.java
@@ -29,14 +29,16 @@ import org.apache.sling.cp2fm.spi.EntryHandler;
 
 public final class DefaultEntryHandler implements EntryHandler {
 
+    public static final String TMP_DEFLATED = "tmp-deflated";
+
     @Override
-    public boolean matches(String sourceSystemId) {
+    public boolean matches(String path) {
         return true;
     }
 
     @Override
     public void handle(String path, Archive archive, Entry entry, ContentPackage2FeatureModelConverter converter) throws Exception {
-        File deflatedDir = new File(converter.getOutputDirectory(), "tmp-deflated");
+        File deflatedDir = new File(converter.getOutputDirectory(), TMP_DEFLATED);
         File target = new File(deflatedDir, path);
 
         target.getParentFile().mkdirs();