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

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


The following commit(s) were added to refs/heads/master by this push:
     new 018a7d8  Migrate all the Manifest attributes
018a7d8 is described below

commit 018a7d8a22ecc4bab5d584bdf2eea9713c582a19
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Mon Feb 8 16:21:13 2021 +0000

    Migrate all the Manifest attributes
---
 CHANGES.md                                         |   1 +
 .../apache/tomcat/jakartaee/ManifestConverter.java | 127 +++++++++++++++++++++
 .../org/apache/tomcat/jakartaee/Migration.java     | 114 ++++--------------
 3 files changed, 148 insertions(+), 94 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 876d46d..ee98a7c 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -19,6 +19,7 @@
 - Make `migrate.sh` work from any path (mgrigorov)
 - Add a new option `-zipInMemory` that processes archives (ZIP, JAR, WAR, 
etc.) in memory rather via a streaming approach. While less efficient, it 
allows archives to be processed when their structure means that a streaming 
approach will fail. (markt)
 - Include the Maven Wrapper source files in the source distribution. (markt)
+- Ensure that all the Manifest attributes are processed during the migration 
process. (markt)
 
 ## 0.1.0
 
diff --git a/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java 
b/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java
new file mode 100644
index 0000000..667a00e
--- /dev/null
+++ b/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java
@@ -0,0 +1,127 @@
+/*
+ * 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.tomcat.jakartaee;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.jar.Attributes;
+import java.util.jar.JarFile;
+import java.util.jar.Manifest;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Updates Manifests.
+ */
+public class ManifestConverter implements Converter {
+
+    private static final Logger logger = 
Logger.getLogger(ManifestConverter.class.getCanonicalName());
+    private static final StringManager sm = 
StringManager.getManager(ManifestConverter.class);
+
+    @Override
+    public boolean accepts(String filename) {
+        if (JarFile.MANIFEST_NAME.equals(filename)) {
+            return true;
+        }
+
+        // Reject everything else
+        return false;
+    }
+
+    @Override
+    public void convert(InputStream src, OutputStream dest, EESpecProfile 
profile) throws IOException {
+        Manifest srcManifest = new Manifest(src);
+        Manifest destManifest = new Manifest(srcManifest);
+
+        removeSignatures(destManifest);
+        updateVersion(destManifest);
+        updateValues(destManifest, profile);
+
+        destManifest.write(dest);
+    }
+
+
+    private boolean removeSignatures(Manifest manifest) {
+        boolean removedSignatures = 
manifest.getMainAttributes().remove(Attributes.Name.SIGNATURE_VERSION) != null;
+        List<String> signatureEntries = new ArrayList<>();
+        Map<String, Attributes> manifestAttributeEntries = 
manifest.getEntries();
+        for (Entry<String, Attributes> entry : 
manifestAttributeEntries.entrySet()) {
+            if (isCryptoSignatureEntry(entry.getValue())) {
+                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;
+    }
+
+
+    private boolean isCryptoSignatureEntry(Attributes attributes) {
+        for (Object attributeKey : attributes.keySet()) {
+            if (attributeKey.toString().endsWith("-Digest")) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+
+    private void updateVersion(Manifest manifest) {
+        updateVersion(manifest.getMainAttributes());
+        for (Attributes attributes : manifest.getEntries().values()) {
+            updateVersion(attributes);
+        }
+    }
+
+
+    private void updateVersion(Attributes attributes) {
+        if (attributes.containsKey(Attributes.Name.IMPLEMENTATION_VERSION)) {
+            String newValue = 
attributes.get(Attributes.Name.IMPLEMENTATION_VERSION) + "-" + 
Info.getVersion();
+            attributes.put(Attributes.Name.IMPLEMENTATION_VERSION, newValue);
+        }
+    }
+
+
+    private void updateValues(Manifest manifest, EESpecProfile profile) {
+        updateValues(manifest.getMainAttributes(), profile);
+        for (Attributes attributes : manifest.getEntries().values()) {
+            updateValues(attributes, profile);
+        }
+    }
+
+
+    private void updateValues(Attributes attributes, EESpecProfile profile) {
+        for (Entry<Object,Object> entry : attributes.entrySet()) {
+            entry.setValue(profile.convert((String) entry.getValue()));
+        }
+        if (attributes.containsKey(Attributes.Name.IMPLEMENTATION_VERSION)) {
+            String newValue = 
attributes.get(Attributes.Name.IMPLEMENTATION_VERSION) + "-" + 
Info.getVersion();
+            attributes.put(Attributes.Name.IMPLEMENTATION_VERSION, newValue);
+        }
+    }
+}
diff --git a/src/main/java/org/apache/tomcat/jakartaee/Migration.java 
b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
index 2489844..7a4cbe9 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/Migration.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
@@ -27,18 +27,14 @@ import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
 import java.util.concurrent.TimeUnit;
-import java.util.jar.Attributes;
 import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
-import java.util.jar.JarInputStream;
-import java.util.jar.JarOutputStream;
-import java.util.jar.Manifest;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.zip.ZipEntry;
 import java.util.zip.ZipException;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
 
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
@@ -65,6 +61,7 @@ public class Migration {
 
         converters.add(new TextConverter());
         converters.add(new ClassConverter());
+        converters.add(new ManifestConverter());
 
         // Final converter is the pass-through converter
         converters.add(new PassThroughConverter());
@@ -184,24 +181,11 @@ public class Migration {
 
     private boolean migrateArchiveStreaming(String name, InputStream src, 
OutputStream dest) throws IOException {
         boolean result = true;
-        try (JarInputStream jarIs = new JarInputStream(new 
CloseShieldInputStream(src));
-                JarOutputStream jarOs = new JarOutputStream(new 
CloseShieldOutputStream(dest))) {
-            Manifest manifest = jarIs.getManifest();
-            if (manifest != null) {
-                // Make a safe copy to leave original manifest untouched.
-                // Otherwise messing with signatures will fail
-                manifest = new Manifest(manifest);
-                updateVersion(manifest);
-                if (removeSignatures(manifest)) {
-                    logger.log(Level.WARNING, 
sm.getString("migration.warnSignatureRemoval"));
-                }
-                JarEntry manifestEntry = new JarEntry(JarFile.MANIFEST_NAME);
-                jarOs.putNextEntry(manifestEntry);
-                manifest.write(jarOs);
-            }
-            JarEntry jarEntry;
-            while ((jarEntry = jarIs.getNextJarEntry()) != null) {
-                String sourceName = jarEntry.getName();
+        try (ZipInputStream zipIs = new ZipInputStream(new 
CloseShieldInputStream(src));
+                ZipOutputStream zipOs = new ZipOutputStream(new 
CloseShieldOutputStream(dest))) {
+            ZipEntry zipEntry;
+            while ((zipEntry = zipIs.getNextEntry()) != null) {
+                String sourceName = zipEntry.getName();
                 logger.log(Level.FINE, sm.getString("migration.entry", 
sourceName));
                 if (isSignatureFile(sourceName)) {
                     logger.log(Level.FINE, 
sm.getString("migration.skipSignatureFile", sourceName));
@@ -209,8 +193,8 @@ public class Migration {
                 }
                 String destName = profile.convert(sourceName);
                 JarEntry destEntry = new JarEntry(destName);
-                jarOs.putNextEntry(destEntry);
-                result = result && migrateStream(destEntry.getName(), jarIs, 
jarOs);
+                zipOs.putNextEntry(destEntry);
+                result = result && migrateStream(destEntry.getName(), zipIs, 
zipOs);
             }
         } catch (ZipException ze) {
             logger.log(Level.SEVERE, sm.getString("migration.archiveFailed", 
name), ze);
@@ -236,28 +220,16 @@ public class Migration {
                 ZipArchiveEntry srcZipEntry = entries.nextElement();
                 String srcName = srcZipEntry.getName();
                 logger.log(Level.FINE, sm.getString("migration.entry", 
srcName));
-                if (srcZipEntry.getName().equals(JarFile.MANIFEST_NAME)) {
-                    // Special handling for manifest
-                    Manifest manifest = new 
Manifest(srcZipFile.getInputStream(srcZipEntry));
-                    updateVersion(manifest);
-                    if (removeSignatures(manifest)) {
-                        logger.log(Level.WARNING, 
sm.getString("migration.warnSignatureRemoval"));
-                    }
-                    destZipStream.putArchiveEntry(srcZipEntry);
-                    manifest.write(destZipStream);
-                    destZipStream.closeArchiveEntry();
-                } else {
-                    if (isSignatureFile(srcName)) {
-                        logger.log(Level.FINE, 
sm.getString("migration.skipSignatureFile", srcName));
-                        continue;
-                    }
-                    String destName = profile.convert(srcName);
-                    RenamableZipArchiveEntry destZipEntry = new 
RenamableZipArchiveEntry(srcZipEntry);
-                    destZipEntry.setName(destName);
-                    destZipStream.putArchiveEntry(destZipEntry);
-                    result = result && migrateStream(srcName, 
srcZipFile.getInputStream(srcZipEntry), destZipStream);
-                    destZipStream.closeArchiveEntry();
+                if (isSignatureFile(srcName)) {
+                    logger.log(Level.FINE, 
sm.getString("migration.skipSignatureFile", srcName));
+                    continue;
                 }
+                String destName = profile.convert(srcName);
+                RenamableZipArchiveEntry destZipEntry = new 
RenamableZipArchiveEntry(srcZipEntry);
+                destZipEntry.setName(destName);
+                destZipStream.putArchiveEntry(destZipEntry);
+                result = result && migrateStream(srcName, 
srcZipFile.getInputStream(srcZipEntry), destZipStream);
+                destZipStream.closeArchiveEntry();
             }
         }
 
@@ -296,52 +268,6 @@ public class Migration {
     }
 
 
-    private boolean removeSignatures(Manifest manifest) {
-        boolean removedSignatures = 
manifest.getMainAttributes().remove(Attributes.Name.SIGNATURE_VERSION) != null;
-        List<String> signatureEntries = new ArrayList<>();
-        Map<String, Attributes> manifestAttributeEntries = 
manifest.getEntries();
-        for (Entry<String, Attributes> entry : 
manifestAttributeEntries.entrySet()) {
-            if (isCryptoSignatureEntry(entry.getValue())) {
-                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;
-    }
-
-
-    private boolean isCryptoSignatureEntry(Attributes attributes) {
-        for (Object attributeKey : attributes.keySet()) {
-            if (attributeKey.toString().endsWith("-Digest")) {
-                return true;
-            }
-        }
-        return false;
-    }
-
-
-    private void updateVersion(Manifest manifest) {
-        updateVersion(manifest.getMainAttributes());
-        for (Attributes attributes : manifest.getEntries().values()) {
-            updateVersion(attributes);
-        }
-    }
-
-
-    private void updateVersion(Attributes attributes) {
-        if (attributes.containsKey(Attributes.Name.IMPLEMENTATION_VERSION)) {
-            String newValue = 
attributes.get(Attributes.Name.IMPLEMENTATION_VERSION) + "-" + 
Info.getVersion();
-            attributes.put(Attributes.Name.IMPLEMENTATION_VERSION, newValue);
-        }
-    }
-
     private static boolean isArchive(String fileName) {
         return fileName.endsWith(".jar") || fileName.endsWith(".war") || 
fileName.endsWith(".zip");
     }


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

Reply via email to