Blame SOURCES/0002-Add-duplicated-ZIP-entry-hack-for-OpenJDK.patch

e981c5
From df720cdeb86e671ac336d337dfedda1cf2d1f711 Mon Sep 17 00:00:00 2001
e981c5
From: Mikolaj Izdebski <mizdebsk@redhat.com>
e981c5
Date: Wed, 28 Oct 2015 13:08:23 +0100
082d69
Subject: [PATCH 2/5] Add duplicated ZIP entry hack for OpenJDK
e981c5
e981c5
---
e981c5
 .../xmvn/tools/install/impl/JarUtils.java          | 29 ++++++++++++++++++++++
e981c5
 1 file changed, 29 insertions(+)
e981c5
e981c5
diff --git a/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/JarUtils.java b/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/JarUtils.java
e981c5
index 5dd09ea..ae2a5a6 100644
e981c5
--- a/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/JarUtils.java
e981c5
+++ b/xmvn-tools/xmvn-install/src/main/java/org/fedoraproject/xmvn/tools/install/impl/JarUtils.java
e981c5
@@ -16,8 +16,10 @@
e981c5
 package org.fedoraproject.xmvn.tools.install.impl;
e981c5
 
e981c5
 import java.io.IOException;
e981c5
+import java.lang.reflect.Field;
e981c5
 import java.nio.file.Files;
e981c5
 import java.nio.file.Path;
e981c5
+import java.util.Collection;
e981c5
 import java.util.jar.Attributes;
e981c5
 import java.util.jar.JarEntry;
e981c5
 import java.util.jar.JarInputStream;
e981c5
@@ -25,6 +27,7 @@ import java.util.jar.JarOutputStream;
e981c5
 import java.util.jar.Manifest;
e981c5
 import java.util.zip.ZipEntry;
e981c5
 import java.util.zip.ZipInputStream;
e981c5
+import java.util.zip.ZipOutputStream;
e981c5
 
e981c5
 import org.objectweb.asm.ClassReader;
e981c5
 import org.objectweb.asm.ClassVisitor;
e981c5
@@ -165,6 +168,31 @@ class JarUtils
e981c5
     }
e981c5
 
e981c5
     /**
e981c5
+     * OpenJDK has a sanity check that prevents adding duplicate entries to ZIP streams. The problem is that some of
e981c5
+     * JARs we try to inject manifests to (especially the ones created by Gradle) already contain duplicate entries, so
e981c5
+     * manifest injection would always fail for them with "ZipException: duplicate entry".
e981c5
+     * 

e981c5
+     * This function tries to work around this OpenJDK sanity check, effectively allowing creating ZIP files with
e981c5
+     * duplicated entries. It should be called on particular ZIP output stream before adding each duplicate entry.
e981c5
+     * 
e981c5
+     * @param zipOutputStream ZIP stream to hack
e981c5
+     */
e981c5
+    private static void openJdkAvoidDuplicateEntryHack( ZipOutputStream zipOutputStream )
e981c5
+    {
e981c5
+        try
e981c5
+        {
e981c5
+            Field namesField = ZipOutputStream.class.getDeclaredField( "names" );
e981c5
+            namesField.setAccessible( true );
e981c5
+            Collection names = (Collection) namesField.get( zipOutputStream );
e981c5
+            names.clear();
e981c5
+        }
e981c5
+        catch ( ReflectiveOperationException e )
e981c5
+        {
e981c5
+            // This hack relies on OpenJDK internals and therefore is not ugarranteed to work. Ignore failures.
e981c5
+        }
e981c5
+    }
e981c5
+
e981c5
+    /**
e981c5
      * Inject artifact coordinates into manifest of specified JAR (or WAR, EAR, ...) file. The file is modified
e981c5
      * in-place.
e981c5
      * 
e981c5
@@ -200,6 +228,7 @@ class JarUtils
e981c5
                 JarEntry entry;
e981c5
                 while ( ( entry = jis.getNextJarEntry() ) != null )
e981c5
                 {
e981c5
+                    openJdkAvoidDuplicateEntryHack( jos );
e981c5
                     jos.putNextEntry( entry );
e981c5
 
e981c5
                     int sz;
e981c5
-- 
082d69
2.7.4
e981c5