You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2021/04/26 09:03:08 UTC
[sling-org-apache-sling-feature-cpconverter] branch master updated:
SLING-10330 : Remove dependency to Plexus Archiver
This is an automated email from the ASF dual-hosted git repository.
cziegeler 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 b701d02 SLING-10330 : Remove dependency to Plexus Archiver
b701d02 is described below
commit b701d025e4a6b03e7b3129df3a143a9e9c183b72
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Mon Apr 26 11:02:53 2021 +0200
SLING-10330 : Remove dependency to Plexus Archiver
---
pom.xml | 35 -------
.../cpconverter/vltpkg/VaultPackageAssembler.java | 103 ++++++++++++++++++---
.../feature/cpconverter/handlers/RepoInitTest.java | 4 +-
.../vltpkg/VaultPackageAssemblerTest.java | 2 +
.../cpconverter/test-with-install-hooks.zip | Bin 17655 -> 15433 bytes
5 files changed, 94 insertions(+), 50 deletions(-)
diff --git a/pom.xml b/pom.xml
index 8d09648..182f6b0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -38,10 +38,6 @@
<org.apache.jackrabbit.vault.version>3.2.8</org.apache.jackrabbit.vault.version>
<jackrabbit-api.version>2.18.4</jackrabbit-api.version>
<jackrabbit-spi-commons.version>2.18.4</jackrabbit-spi-commons.version>
- <plexus-archiver.version>4.1.0</plexus-archiver.version>
- <plexus-utils.version>3.2.1</plexus-utils.version>
- <plexus-io.version>3.1.1</plexus-io.version>
- <plexus-container-default.version>2.0.0</plexus-container-default.version>
<maven-model.version>3.6.3</maven-model.version>
<mockito-core.version>3.2.0</mockito-core.version>
<license-maven-plugin.version>1.16</license-maven-plugin.version>
@@ -193,37 +189,6 @@
</dependency>
<!--
- | repackage
- -->
- <dependency>
- <groupId>org.codehaus.plexus</groupId>
- <artifactId>plexus-archiver</artifactId>
- <version>${plexus-archiver.version}</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.codehaus.plexus</groupId>
- <artifactId>plexus-utils</artifactId>
- <version>${plexus-utils.version}</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.codehaus.plexus</groupId>
- <artifactId>plexus-io</artifactId>
- <version>${plexus-io.version}</version>
- <scope>provided</scope>
- </dependency>
- <!--
- | avoid classloading issue
- -->
- <dependency>
- <groupId>org.codehaus.plexus</groupId>
- <artifactId>plexus-container-default</artifactId>
- <version>${plexus-container-default.version}</version>
- <scope>provided</scope>
- </dependency>
-
- <!--
| POM model
-->
<dependency>
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java b/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
index 58a1aa7..979a1f5 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
@@ -31,6 +31,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@@ -39,8 +40,13 @@ import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Predicate;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarOutputStream;
+import java.util.jar.Manifest;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
+import java.util.zip.Deflater;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
@@ -58,9 +64,6 @@ import org.apache.jackrabbit.vault.packaging.VaultPackage;
import org.apache.jackrabbit.vault.util.Constants;
import org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
import org.apache.sling.feature.cpconverter.handlers.EntryHandler;
-import org.codehaus.plexus.archiver.Archiver;
-import org.codehaus.plexus.archiver.util.DefaultFileSet;
-import org.codehaus.plexus.archiver.zip.ZipArchiver;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
@@ -270,20 +273,94 @@ public class VaultPackageAssembler implements EntryHandler {
}
// create the target archiver
-
- Archiver archiver = new ZipArchiver();
- archiver.setIncludeEmptyDirs(true);
-
- String destFileName = storingDirectory.getName().substring(0, storingDirectory.getName().lastIndexOf('-'));
- File destFile = new File(this.tmpDir, destFileName);
-
- archiver.setDestFile(destFile);
- archiver.addFileSet(new DefaultFileSet(storingDirectory));
- archiver.createArchive();
+ final String destFileName = storingDirectory.getName().substring(0, storingDirectory.getName().lastIndexOf('-'));
+ final File destFile = new File(this.tmpDir, destFileName);
+
+ final File manifestFile = new File(storingDirectory, JarFile.MANIFEST_NAME.replace('/', File.separatorChar));
+ Manifest manifest = null;
+ if ( manifestFile.exists() ) {
+ try ( final InputStream r = new FileInputStream(manifestFile)) {
+ manifest = new Manifest(r);
+ }
+ }
+ try ( final JarOutputStream jos = manifest == null ? new JarOutputStream(new FileOutputStream(destFile))
+ : new JarOutputStream(new FileOutputStream(destFile), manifest)) {
+ jos.setLevel(Deflater.DEFAULT_COMPRESSION);
+ final byte buffer[] = new byte[1024 * 8];
+ addDirectory(jos, storingDirectory, storingDirectory.getAbsolutePath().length() + 1, buffer);
+ }
return destFile;
}
+ private void addDirectory(final JarOutputStream jos, final File dir, final int prefixLength, final byte buffer[]) throws IOException {
+ if ( dir.getAbsolutePath().length() > prefixLength && dir.listFiles().length == 0 ) {
+ final String dirName = dir.getAbsolutePath().substring(prefixLength).replace(File.separatorChar, '/');
+ final JarEntry entry = new JarEntry(dirName);
+ entry.setTime(dir.lastModified());
+ entry.setSize(0);
+ jos.putNextEntry(entry);
+ jos.closeEntry();
+ }
+ for(final File f : dir.listFiles()) {
+ final String name = f.getAbsolutePath().substring(prefixLength).replace(File.separatorChar, '/');
+ if ( f.isFile() && !JarFile.MANIFEST_NAME.equals(name) ) {
+ final JarEntry entry = new JarEntry(name);
+ entry.setTime(f.lastModified());
+ jos.putNextEntry(entry);
+
+ try ( final FileInputStream in = new FileInputStream(f)) {
+ int l = 0;
+ while (( l = in.read(buffer)) > 0 ) {
+ jos.write(buffer, 0, l);
+ }
+ }
+ jos.closeEntry();
+ } else if ( f.isDirectory() ) {
+ addDirectory(jos, f, prefixLength, buffer);
+ }
+ }
+ }
+
+ public static int BUFFER_SIZE = 10240;
+ protected void createJarArchive(File archiveFile, File[] tobeJared) {
+ try {
+ byte buffer[] = new byte[BUFFER_SIZE];
+ // Open archive file
+ FileOutputStream stream = new FileOutputStream(archiveFile);
+ JarOutputStream out = new JarOutputStream(stream, new Manifest());
+
+ for (int i = 0; i < tobeJared.length; i++) {
+ if (tobeJared[i] == null || !tobeJared[i].exists()
+ || tobeJared[i].isDirectory())
+ continue; // Just in case...
+ System.out.println("Adding " + tobeJared[i].getName());
+
+ // Add archive entry
+ JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
+ jarAdd.setTime(tobeJared[i].lastModified());
+ out.putNextEntry(jarAdd);
+
+ // Write file to archive
+ FileInputStream in = new FileInputStream(tobeJared[i]);
+ while (true) {
+ int nRead = in.read(buffer, 0, buffer.length);
+ if (nRead <= 0)
+ break;
+ out.write(buffer, 0, nRead);
+ }
+ in.close();
+ }
+
+ out.close();
+ stream.close();
+ System.out.println("Adding completed OK");
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ System.out.println("Error: " + ex.getMessage());
+ }
+ }
+
static @Nullable PackageType recalculatePackageType(PackageType sourcePackageType, @NotNull File outputDirectory) {
if (sourcePackageType != null && sourcePackageType != PackageType.MIXED) {
return null;
diff --git a/src/test/java/org/apache/sling/feature/cpconverter/handlers/RepoInitTest.java b/src/test/java/org/apache/sling/feature/cpconverter/handlers/RepoInitTest.java
index 257abae..bdbe741 100644
--- a/src/test/java/org/apache/sling/feature/cpconverter/handlers/RepoInitTest.java
+++ b/src/test/java/org/apache/sling/feature/cpconverter/handlers/RepoInitTest.java
@@ -16,7 +16,6 @@
*/
package org.apache.sling.feature.cpconverter.handlers;
-import com.google.common.collect.Lists;
import org.apache.jackrabbit.vault.fs.io.Archive;
import org.apache.sling.feature.ArtifactId;
import org.apache.sling.feature.Extension;
@@ -34,6 +33,7 @@ import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.StringReader;
+import java.util.Arrays;
import java.util.Collection;
import static org.apache.sling.feature.cpconverter.Util.normalize;
@@ -63,7 +63,7 @@ public class RepoInitTest {
@Parameterized.Parameters(name = "name={1}")
public static Collection<Object[]> parameters() {
- return Lists.newArrayList(
+ return Arrays.asList(
new Object[] { true, "Enforce principal-based ac setup" },
new Object[] { false, "Don't enforce principal-based ac setup" });
};
diff --git a/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerTest.java b/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerTest.java
index 1528fee..6b67d6f 100644
--- a/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerTest.java
+++ b/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerTest.java
@@ -20,12 +20,14 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.vault.packaging.PackageId;
diff --git a/src/test/resources/org/apache/sling/feature/cpconverter/test-with-install-hooks.zip b/src/test/resources/org/apache/sling/feature/cpconverter/test-with-install-hooks.zip
index af3a859..cde06c6 100644
Binary files a/src/test/resources/org/apache/sling/feature/cpconverter/test-with-install-hooks.zip and b/src/test/resources/org/apache/sling/feature/cpconverter/test-with-install-hooks.zip differ