This is an automated email from the ASF dual-hosted git repository.

lihan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat-jakartaee-migration.git

commit 4d83b8d04d28cd4a711942c6cdd3659802a0dbb2
Author: Danny Thomas <dan...@netflix.com>
AuthorDate: Wed Oct 26 13:54:57 2022 +1100

    Improve manifest handling
    
    - Write the original bytes for unchanged manifests to retain the key 
ordering (Attributes is a HashMap and changes key order for manifests in many 
cases)
    - Avoid treating signature removal and Implementation-Version as 
conversions, otherwise jars with these fields in their manifests, but otherwise 
no interesting changes return true for Migration.hasConverted
---
 .../apache/tomcat/jakartaee/ManifestConverter.java | 39 +++++++++++++---------
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java 
b/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java
index 7d344cf..324bfa5 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java
@@ -16,6 +16,7 @@
  */
 package org.apache.tomcat.jakartaee;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -29,6 +30,8 @@ import java.util.jar.Manifest;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import org.apache.commons.io.IOUtils;
+
 /**
  * Updates Manifests.
  */
@@ -49,20 +52,27 @@ public class ManifestConverter implements Converter {
 
     @Override
     public boolean convert(String path, InputStream src, OutputStream dest, 
EESpecProfile profile) throws IOException {
-        Manifest srcManifest = new Manifest(src);
+        byte[] srcBytes = IOUtils.toByteArray(src);
+        Manifest srcManifest = new Manifest(new 
ByteArrayInputStream(srcBytes));
         Manifest destManifest = new Manifest(srcManifest);
 
-        boolean result = removeSignatures(destManifest);
-        result = result | updateValues(destManifest, profile);
+        // Only consider profile conversions, allowing Migration.hasConverted 
to be true only when there are actual
+        // conversions made
+        boolean converted = updateValues(destManifest, profile);
+        removeSignatures(destManifest);
 
-        destManifest.write(dest);
+        if (srcManifest.equals(destManifest)) {
+            IOUtils.writeChunked(srcBytes, dest);
+        } else {
+            destManifest.write(dest);
+        }
 
-        return result;
+        return converted;
     }
 
 
-    private boolean removeSignatures(Manifest manifest) {
-        boolean removedSignatures = 
manifest.getMainAttributes().remove(Attributes.Name.SIGNATURE_VERSION) != null;
+    private void removeSignatures(Manifest manifest) {
+        manifest.getMainAttributes().remove(Attributes.Name.SIGNATURE_VERSION);
         List<String> signatureEntries = new ArrayList<>();
         Map<String, Attributes> manifestAttributeEntries = 
manifest.getEntries();
         for (Entry<String, Attributes> entry : 
manifestAttributeEntries.entrySet()) {
@@ -70,15 +80,12 @@ public class ManifestConverter implements Converter {
                 String entryName = entry.getKey();
                 signatureEntries.add(entryName);
                 logger.log(Level.FINE, 
sm.getString("migration.removeSignature", entryName));
-                removedSignatures = true;
             }
         }
 
         for (String entry : signatureEntries) {
             manifestAttributeEntries.remove(entry);
         }
-
-        return removedSignatures;
     }
 
 
@@ -93,16 +100,16 @@ public class ManifestConverter implements Converter {
 
 
     private boolean updateValues(Manifest manifest, EESpecProfile profile) {
-        boolean result = updateValues(manifest.getMainAttributes(), profile);
+        boolean converted = updateValues(manifest.getMainAttributes(), 
profile);
         for (Attributes attributes : manifest.getEntries().values()) {
-            result = result | updateValues(attributes, profile);
+            converted = converted | updateValues(attributes, profile);
         }
-        return result;
+        return converted;
     }
 
 
     private boolean updateValues(Attributes attributes, EESpecProfile profile) 
{
-        boolean result = false;
+        boolean converted = false;
         // Update version info
         if (attributes.containsKey(Attributes.Name.IMPLEMENTATION_VERSION)) {
             String newValue = 
attributes.get(Attributes.Name.IMPLEMENTATION_VERSION) + "-" + 
Info.getVersion();
@@ -115,9 +122,9 @@ public class ManifestConverter implements Converter {
             // Object comparison is deliberate
             if (newValue != entry.getValue()) {
                 entry.setValue(newValue);
-                result = true;
+                converted = true;
             }
         }
-        return result;
+        return converted;
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to