[tomcat-jakartaee-migration] branch master updated (1a8f20a -> e963951)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[tomcat-jakartaee-migration] branch master updated (1a8f20a -> e963951)

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

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


    from 1a8f20a  Update chnagelog
     new c09f8b4  Replace `-verbose` with `-logLevel=` to provide more control
     new d8373f2  Add Javadoc
     new 4a0b09d  Reduce object creation during conversion
     new 9acbf5e  Simplify error handling
     new 0a09f52  Track whether source was modified or not.
     new 066f71d  Fix #13 refactor logging
     new e963951  Fix #3 Add support for excluding files from conversion

The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 CHANGES.md                                         |   3 +
 .../apache/tomcat/jakartaee/ClassConverter.java    |  26 ++-
 .../org/apache/tomcat/jakartaee/Converter.java     |  13 +-
 .../org/apache/tomcat/jakartaee/GlobMatcher.java   | 243 +++++++++++++++++++++
 .../apache/tomcat/jakartaee/ManifestConverter.java |  34 ++-
 .../org/apache/tomcat/jakartaee/Migration.java     | 136 ++++++++----
 .../org/apache/tomcat/jakartaee/MigrationCLI.java  | 108 ++++-----
 .../org/apache/tomcat/jakartaee/MigrationTask.java |  24 +-
 .../tomcat/jakartaee/PassThroughConverter.java     |  10 +-
 .../org/apache/tomcat/jakartaee/TextConverter.java |  18 +-
 .../tomcat/jakartaee/LocalStrings.properties       |  35 ++-
 .../org/apache/tomcat/jakartaee/MigrationTest.java |   3 +-
 .../tomcat/jakartaee/PassThroughConverterTest.java |   6 +-
 .../apache/tomcat/jakartaee/TextConverterTest.java |   4 +-
 14 files changed, 536 insertions(+), 127 deletions(-)
 create mode 100644 src/main/java/org/apache/tomcat/jakartaee/GlobMatcher.java


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[tomcat-jakartaee-migration] 01/07: Replace `-verbose` with `-logLevel=` to provide more control

markt
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

commit c09f8b4a1620643c40018cb54730cc06176c35c2
Author: Mark Thomas <[hidden email]>
AuthorDate: Tue Feb 9 12:00:36 2021 +0000

    Replace `-verbose` with `-logLevel=` to provide more control
---
 CHANGES.md                                         |   1 +
 .../org/apache/tomcat/jakartaee/MigrationCLI.java  | 103 +++++++++++----------
 .../tomcat/jakartaee/LocalStrings.properties       |  16 +++-
 3 files changed, 70 insertions(+), 50 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index af74bb7..99d84e1 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -21,6 +21,7 @@
 - Include the Maven Wrapper source files in the source distribution. (markt)
 - Ensure that all the Manifest attributes are processed during the migration process. (markt)
 - Include `.properties` and `.json` files in the conversion process. (markt)
+- Replace `-verbose` with `-logLevel=` to provide more control over logging level. (markt)
 
 ## 0.1.0
 
diff --git a/src/main/java/org/apache/tomcat/jakartaee/MigrationCLI.java b/src/main/java/org/apache/tomcat/jakartaee/MigrationCLI.java
index 5c3bf4f..64af9b9 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/MigrationCLI.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/MigrationCLI.java
@@ -20,82 +20,87 @@ import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 public class MigrationCLI {
 
-    private static final Logger logger = Logger.getLogger(MigrationCLI.class.getCanonicalName());
     private static final StringManager sm = StringManager.getManager(MigrationCLI.class);
 
+    private static final String LOGLEVEL_ARG = "-logLevel=";
     private static final String PROFILE_ARG = "-profile=";
-
+    // Will be removed for 1.0.0
+    @Deprecated
+    private static final String VERBOSE_ARG = "-verbose";
     private static final String ZIPINMEMORY_ARG = "-zipInMemory";
 
-    public static void main(String[] args) {
-        System.setProperty("java.util.logging.SimpleFormatter.format", "%5$s%n %6$s%n");
-
-        List<String> arguments = new ArrayList<>(Arrays.asList(args));
-        if (arguments.contains("-verbose")) {
-            Logger.getGlobal().getParent().getHandlers()[0].setLevel(Level.FINE);
-            Logger.getGlobal().getParent().setLevel(Level.FINE);
-            arguments.remove("-verbose");
-        }
-
-        boolean zipInMemory = false;
-        if (arguments.contains(ZIPINMEMORY_ARG)) {
-            zipInMemory = true;
-            arguments.remove(ZIPINMEMORY_ARG);
-        }
+    public static void main(String[] args) throws IOException {
 
+        // Defaults
+        System.setProperty("java.util.logging.SimpleFormatter.format", "%5$s%n");
         Migration migration = new Migration();
-        migration.setZipInMemory(zipInMemory);
 
-        boolean valid = false;
-        String source = null;
-        String dest = null;
-        if (arguments.size() == 3) {
-            if (arguments.get(0).startsWith(PROFILE_ARG)) {
-                source = arguments.get(1);
-                dest = arguments.get(2);
-                valid = true;
+        // Process argumnets
+        List<String> arguments = new ArrayList<>(Arrays.asList(args));
+
+        // Process the custom log level if present
+        // Use an iterator so we can remove the log level argument if found
+        Iterator<String> iter = arguments.iterator();
+        while (iter.hasNext()) {
+            String argument = iter.next();
+            if (argument.startsWith(LOGLEVEL_ARG)) {
+                iter.remove();
+                String logLevelName = argument.substring(LOGLEVEL_ARG.length());
+                Level level = null;
                 try {
-                    migration.setEESpecProfile(EESpecProfile.valueOf(arguments.get(0).substring(PROFILE_ARG.length())));
+                    level = Level.parse(logLevelName.toUpperCase(Locale.ENGLISH));
+                } catch (IllegalArgumentException iae) {
+                    invalidArguments();
+                }
+                // Configure the explicit level
+                Logger.getGlobal().getParent().getHandlers()[0].setLevel(level);
+                Logger.getGlobal().getParent().setLevel(level);
+            } else if (argument.startsWith(PROFILE_ARG)) {
+                iter.remove();
+                String profileName = argument.substring(PROFILE_ARG.length());
+                try {
+                    EESpecProfile profile = EESpecProfile.valueOf(profileName.toUpperCase(Locale.ENGLISH));
+                    migration.setEESpecProfile(profile);
                 } catch (IllegalArgumentException e) {
                     // Invalid profile value
-                    valid = false;
+                    invalidArguments();
+                }
+            } else if (argument.equals(ZIPINMEMORY_ARG)) {
+                iter.remove();
+                migration.setZipInMemory(true);
+            } else if (argument.equals(VERBOSE_ARG)) {
+                iter.remove();
+                // Ignore if LOGLEVEL_ARG has set something different
+                if (Logger.getGlobal().getParent().getLevel().equals(Level.INFO)) {
+                    Logger.getGlobal().getParent().getHandlers()[0].setLevel(Level.FINE);
+                    Logger.getGlobal().getParent().setLevel(Level.FINE);
                 }
             }
         }
-        if (arguments.size() == 2) {
-            source = arguments.get(0);
-            dest = arguments.get(1);
-            valid = true;
-        }
-        if (!valid) {
-            usage();
-            System.exit(1);
+
+        if (arguments.size() != 2) {
+            invalidArguments();
         }
 
+        String source = arguments.get(0);
+        String dest = arguments.get(1);
+
         migration.setSource(new File(source));
         migration.setDestination(new File(dest));
-        boolean result = false;
-        try {
-            result = migration.execute();
-        } catch (IOException e) {
-            logger.log(Level.SEVERE, sm.getString("migration.error"), e);
-            result = false;
-        }
 
-        // Signal caller that migration failed
-        if (!result) {
-            System.exit(1);
-        }
+        migration.execute();
     }
 
-    private static void usage() {
+    private static void invalidArguments() {
         System.out.println(sm.getString("migration.usage"));
+        System.exit(1);
     }
-
 }
diff --git a/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties b/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
index cf62ca3..d3274e4 100644
--- a/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
+++ b/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
@@ -24,5 +24,19 @@ migration.mkdirError=Error creating destination directory [{0}]
 migration.removeSignature=Remove cryptographic signature for [{0}]
 migration.skipSignatureFile=Drop cryptographic signature file [{0}]
 migration.stream=Migrating stream [{0}]
-migration.usage=Usage: Migration [-profile=TOMCAT|-profile=EE] [-zipInMemory] [-verbose] <source> <destination>
+migration.usage=Usage: Migration [options] <source> <destination>\n\
+where options includes:\n\
+\    -logLevel=<name of java.util.logging.level enum value>\n\
+\                Useful values are INFO (default), FINE or FINEST\n\
+\    -profile=<profile name>\n\
+\                TOMCAT (default) to convert Java EE APIs provided by Tomcat\n\
+\                EE to convert all Java EE APIs\n\
+\    -zipInMemory\n\
+\                By default zip format archives (.zip, jar, .war, .ear, etc.)\n\
+\                are processed in memory. This is more efficient but is not\n\
+\                compatible with some zip archive structures. If you see an\n\
+\                exception while processing a zip file, enabling this option\n\
+\                may workaround the issue.
+
+
 migration.warnSignatureRemoval=Removed cryptographic signature from JAR file


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[tomcat-jakartaee-migration] 02/07: Add Javadoc

markt
In reply to this post by markt
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

commit d8373f2e132434e4e8b1aec9fed30f599e6d0eb8
Author: Mark Thomas <[hidden email]>
AuthorDate: Tue Feb 9 14:24:54 2021 +0000

    Add Javadoc
---
 src/main/java/org/apache/tomcat/jakartaee/Converter.java | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/main/java/org/apache/tomcat/jakartaee/Converter.java b/src/main/java/org/apache/tomcat/jakartaee/Converter.java
index 92ef1dd..5a62b5c 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/Converter.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/Converter.java
@@ -24,5 +24,15 @@ public interface Converter {
 
     boolean accepts(String filename);
 
+    /**
+     * Copies the source to the destination, converting it if necessary,
+     * according to the requirements of the given profile.
+     *
+     * @param src       The source data to convert
+     * @param dest      The destination to write the converted data
+     * @param profile   The profile that defines the conversion required
+     *
+     * @throws IOException  If the conversion fails
+     */
     void convert(InputStream src, OutputStream dest, EESpecProfile profile) throws IOException;
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[tomcat-jakartaee-migration] 03/07: Reduce object creation during conversion

markt
In reply to this post by markt
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

commit 4a0b09d3e43dfcb35c5a7488a222b1c7ea941669
Author: Mark Thomas <[hidden email]>
AuthorDate: Tue Feb 9 14:25:17 2021 +0000

    Reduce object creation during conversion
---
 src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java b/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
index 9d398d7..cc06bde 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
@@ -46,8 +46,12 @@ public class ClassConverter implements Converter {
             if (constantPool[i] instanceof ConstantUtf8) {
                 ConstantUtf8 c = (ConstantUtf8) constantPool[i];
                 String str = c.getBytes();
-                c = new ConstantUtf8(profile.convert(str));
-                constantPool[i] = c;
+                String converted = profile.convert(str);
+                // Object comparison is deliberate
+                if (converted != str) {
+                    c = new ConstantUtf8(profile.convert(str));
+                    constantPool[i] = c;
+                }
             }
         }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[tomcat-jakartaee-migration] 04/07: Simplify error handling

markt
In reply to this post by markt
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

commit 9acbf5e8089114a8ea3a5118ca4b64beeb5d5b14
Author: Mark Thomas <[hidden email]>
AuthorDate: Tue Feb 9 14:34:49 2021 +0000

    Simplify error handling
---
 .../java/org/apache/tomcat/jakartaee/Migration.java    | 18 +++++++-----------
 .../org/apache/tomcat/jakartaee/MigrationTask.java     |  7 +------
 .../apache/tomcat/jakartaee/LocalStrings.properties    |  2 +-
 3 files changed, 9 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/apache/tomcat/jakartaee/Migration.java b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
index c6a6371..e970a08 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/Migration.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
@@ -103,32 +103,28 @@ public class Migration {
     }
 
 
-    public boolean execute() throws IOException {
+    public void execute() throws IOException {
         logger.log(Level.INFO, sm.getString("migration.execute", source.getAbsolutePath(),
                 destination.getAbsolutePath(), profile.toString()));
-        boolean result = true;
+
         long t1 = System.nanoTime();
         if (source.isDirectory()) {
             if ((destination.exists() && destination.isDirectory()) || destination.mkdirs()) {
-                result = result && migrateDirectory(source, destination);
+                migrateDirectory(source, destination);
             } else {
-                logger.log(Level.WARNING, sm.getString("migration.mkdirError", destination.getAbsolutePath()));
-                result = false;
+                throw new IOException(sm.getString("migration.mkdirError", destination.getAbsolutePath()));
             }
         } else {
             // Single file
             File parentDestination = destination.getAbsoluteFile().getParentFile();
             if (parentDestination.exists() || parentDestination.mkdirs()) {
-                result = result && migrateFile(source, destination);
+                migrateFile(source, destination);
             } else {
-                logger.log(Level.WARNING, sm.getString("migration.mkdirError", parentDestination.getAbsolutePath()));
-                result = false;
+                throw new IOException(sm.getString("migration.mkdirError", parentDestination.getAbsolutePath()));
             }
         }
         logger.log(Level.INFO, sm.getString("migration.done",
-                Long.valueOf(TimeUnit.MILLISECONDS.convert(System.nanoTime() - t1, TimeUnit.NANOSECONDS)),
-                Boolean.valueOf(result)));
-        return result;
+                Long.valueOf(TimeUnit.MILLISECONDS.convert(System.nanoTime() - t1, TimeUnit.NANOSECONDS))));
     }
 
 
diff --git a/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java b/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java
index 6bdda1d..c19599e 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java
@@ -74,15 +74,10 @@ public class MigrationTask extends Task {
         migration.setEESpecProfile(profile);
         migration.setZipInMemory(zipInMemory);
 
-        boolean success = false;
         try {
-            success = migration.execute();
+            migration.execute();
         } catch (IOException e) {
             throw new BuildException(e, getLocation());
         }
-
-        if (!success) {
-            throw new BuildException("Migration failed", getLocation());
-        }
     }
 }
diff --git a/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties b/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
index d3274e4..d8150c0 100644
--- a/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
+++ b/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
@@ -16,7 +16,7 @@
 migration.archive=Migrating archive [{0}]
 migration.archiveFailed=Failed to migrate archive [{0}]. Using the "-zipInMemory" option may help.
 migration.cannotReadSource=Cannot read source location [{0}]
-migration.done=Migration completed successfully [{1}] in [{0}] milliseconds
+migration.done=Migration completed successfully in [{0}] milliseconds
 migration.entry=Migrating Jar entry [{0}]
 migration.error=Error performing migration
 migration.execute=Performing migration from source [{0}] to destination [{1}] with Jakarta EE specification profile [{2}]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[tomcat-jakartaee-migration] 05/07: Track whether source was modified or not.

markt
In reply to this post by markt
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

commit 0a09f522ac65a4ae587e0c69e273fddd77ebdfdb
Author: Mark Thomas <[hidden email]>
AuthorDate: Tue Feb 9 14:35:08 2021 +0000

    Track whether source was modified or not.
---
 .../apache/tomcat/jakartaee/ManifestConverter.java | 32 ++++++++++++++++------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java b/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java
index 3a89ae2..e177217 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java
@@ -52,10 +52,16 @@ public class ManifestConverter implements Converter {
         Manifest srcManifest = new Manifest(src);
         Manifest destManifest = new Manifest(srcManifest);
 
-        removeSignatures(destManifest);
-        updateValues(destManifest, profile);
+        boolean result = false;
 
-        destManifest.write(dest);
+        result = result | removeSignatures(destManifest);
+        result = result | updateValues(destManifest, profile);
+
+        if (result) {
+            destManifest.write(dest);
+        } else {
+            srcManifest.write(dest);
+        }
     }
 
 
@@ -90,23 +96,33 @@ public class ManifestConverter implements Converter {
     }
 
 
-    private void updateValues(Manifest manifest, EESpecProfile profile) {
-        updateValues(manifest.getMainAttributes(), profile);
+    private boolean updateValues(Manifest manifest, EESpecProfile profile) {
+        boolean result = false;
+        result = result | updateValues(manifest.getMainAttributes(), profile);
         for (Attributes attributes : manifest.getEntries().values()) {
-            updateValues(attributes, profile);
+            result = result | updateValues(attributes, profile);
         }
+        return result;
     }
 
 
-    private void updateValues(Attributes attributes, EESpecProfile profile) {
+    private boolean updateValues(Attributes attributes, EESpecProfile profile) {
+        boolean result = false;
         // Update version info
         if (attributes.containsKey(Attributes.Name.IMPLEMENTATION_VERSION)) {
             String newValue = attributes.get(Attributes.Name.IMPLEMENTATION_VERSION) + "-" + Info.getVersion();
             attributes.put(Attributes.Name.IMPLEMENTATION_VERSION, newValue);
+            result = true;
         }
         // Update package names in values
         for (Entry<Object,Object> entry : attributes.entrySet()) {
-            entry.setValue(profile.convert((String) entry.getValue()));
+            String newValue = profile.convert((String) entry.getValue());
+            // Object comparison is deliberate
+            if (newValue != entry.getValue()) {
+                entry.setValue(newValue);
+                result = true;
+            }
         }
+        return result;
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[tomcat-jakartaee-migration] 06/07: Fix #13 refactor logging

markt
In reply to this post by markt
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

commit 066f71df6bcd647b26af96079235861a035032cb
Author: Mark Thomas <[hidden email]>
AuthorDate: Tue Feb 9 15:18:48 2021 +0000

    Fix #13 refactor logging
   
    INFO - Lists archives and dropped signature files
    FINE - INFO plus modified files and dropped manifets entries
    FINEST - Every file
---
 CHANGES.md                                         |  1 +
 .../apache/tomcat/jakartaee/ClassConverter.java    | 22 +++++++--
 .../org/apache/tomcat/jakartaee/Converter.java     |  3 +-
 .../apache/tomcat/jakartaee/ManifestConverter.java |  2 +-
 .../org/apache/tomcat/jakartaee/Migration.java     | 55 +++++++++-------------
 .../tomcat/jakartaee/PassThroughConverter.java     | 10 +++-
 .../org/apache/tomcat/jakartaee/TextConverter.java | 18 ++++++-
 .../tomcat/jakartaee/LocalStrings.properties       | 13 +++--
 .../org/apache/tomcat/jakartaee/MigrationTest.java |  3 +-
 .../tomcat/jakartaee/PassThroughConverterTest.java |  6 ++-
 .../apache/tomcat/jakartaee/TextConverterTest.java |  4 +-
 11 files changed, 88 insertions(+), 49 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 99d84e1..9b23746 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -22,6 +22,7 @@
 - Ensure that all the Manifest attributes are processed during the migration process. (markt)
 - Include `.properties` and `.json` files in the conversion process. (markt)
 - Replace `-verbose` with `-logLevel=` to provide more control over logging level. (markt)
+- Fix [#13](https://github.com/apache/tomcat-jakartaee-migration/issues/13). Refactor mapping of log messages to log levels. (markt)
 
 ## 0.1.0
 
diff --git a/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java b/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
index cc06bde..173ea8e 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
@@ -19,6 +19,8 @@ package org.apache.tomcat.jakartaee;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import org.apache.bcel.classfile.ClassParser;
 import org.apache.bcel.classfile.Constant;
@@ -27,6 +29,9 @@ import org.apache.bcel.classfile.JavaClass;
 
 public class ClassConverter implements Converter {
 
+    private static final Logger logger = Logger.getLogger(ClassConverter.class.getCanonicalName());
+    private static final StringManager sm = StringManager.getManager(ClassConverter.class);
+
     @Override
     public boolean accepts(String filename) {
         String extension = Util.getExtension(filename);
@@ -35,26 +40,37 @@ public class ClassConverter implements Converter {
 
 
     @Override
-    public void convert(InputStream src, OutputStream dest, EESpecProfile profile) throws IOException {
+    public void convert(String path, InputStream src, OutputStream dest, EESpecProfile profile) throws IOException {
 
         ClassParser parser = new ClassParser(src, "unknown");
         JavaClass javaClass = parser.parse();
 
+        boolean converted = false;
+
         // Loop through constant pool
         Constant[] constantPool = javaClass.getConstantPool().getConstantPool();
         for (short i = 0; i < constantPool.length; i++) {
             if (constantPool[i] instanceof ConstantUtf8) {
                 ConstantUtf8 c = (ConstantUtf8) constantPool[i];
                 String str = c.getBytes();
-                String converted = profile.convert(str);
+                String newString = profile.convert(str);
                 // Object comparison is deliberate
-                if (converted != str) {
+                if (newString != str) {
                     c = new ConstantUtf8(profile.convert(str));
                     constantPool[i] = c;
+                    converted = true;
                 }
             }
         }
 
+        if (logger.isLoggable(Level.FINE)) {
+            if (converted) {
+                logger.log(Level.FINE, sm.getString("classConverter.converted", path));
+            } else if (logger.isLoggable(Level.FINEST)) {
+                logger.log(Level.FINEST, sm.getString("classConverter.noConversion", path));
+            }
+        }
+
         javaClass.dump(dest);
     }
 }
diff --git a/src/main/java/org/apache/tomcat/jakartaee/Converter.java b/src/main/java/org/apache/tomcat/jakartaee/Converter.java
index 5a62b5c..e05c232 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/Converter.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/Converter.java
@@ -28,11 +28,12 @@ public interface Converter {
      * Copies the source to the destination, converting it if necessary,
      * according to the requirements of the given profile.
      *
+     * @param path      The path to the data being converted
      * @param src       The source data to convert
      * @param dest      The destination to write the converted data
      * @param profile   The profile that defines the conversion required
      *
      * @throws IOException  If the conversion fails
      */
-    void convert(InputStream src, OutputStream dest, EESpecProfile profile) throws IOException;
+    void convert(String path, InputStream src, OutputStream dest, EESpecProfile profile) throws IOException;
 }
diff --git a/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java b/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java
index e177217..30e4ec0 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/ManifestConverter.java
@@ -48,7 +48,7 @@ public class ManifestConverter implements Converter {
     }
 
     @Override
-    public void convert(InputStream src, OutputStream dest, EESpecProfile profile) throws IOException {
+    public void convert(String path, InputStream src, OutputStream dest, EESpecProfile profile) throws IOException {
         Manifest srcManifest = new Manifest(src);
         Manifest destManifest = new Manifest(srcManifest);
 
diff --git a/src/main/java/org/apache/tomcat/jakartaee/Migration.java b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
index e970a08..153345a 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/Migration.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
@@ -128,8 +128,7 @@ public class Migration {
     }
 
 
-    private boolean migrateDirectory(File src, File dest) throws IOException {
-        boolean result = true;
+    private void migrateDirectory(File src, File dest) throws IOException {
         // Won't return null because src is known to be a directory
         String[] files = src.list();
         for (String file : files) {
@@ -137,71 +136,61 @@ public class Migration {
             File destFile = new File(dest, file);
             if (srcFile.isDirectory()) {
                 if ((destFile.exists() && destFile.isDirectory()) || destFile.mkdir()) {
-                    result = result && migrateDirectory(srcFile, destFile);
+                    migrateDirectory(srcFile, destFile);
                 } else {
-                    logger.log(Level.WARNING, sm.getString("migration.mkdirError", destFile.getAbsolutePath()));
-                    result = false;
+                    throw new IOException(sm.getString("migration.mkdirError", destFile.getAbsolutePath()));
                 }
             } else {
-                result = result && migrateFile(srcFile, destFile);
+                migrateFile(srcFile, destFile);
             }
         }
-        return result;
     }
 
 
-    private boolean migrateFile(File src, File dest) throws IOException {
-        boolean result = false;
-
+    private void migrateFile(File src, File dest) throws IOException {
         boolean inplace = src.equals(dest);
         if (!inplace) {
             try (InputStream is = new FileInputStream(src);
                  OutputStream os = new FileOutputStream(dest)) {
-                result = migrateStream(src.getName(), is, os);
+                migrateStream(src.getName(), is, os);
             }
         } else {
             ByteArrayOutputStream buffer = new ByteArrayOutputStream((int) (src.length() * 1.05));
 
             try (InputStream is = new FileInputStream(src)) {
-                result = migrateStream(src.getName(), is, buffer);
+                migrateStream(src.getName(), is, buffer);
             }
 
             try (OutputStream os = new FileOutputStream(dest)) {
                 os.write(buffer.toByteArray());
             }
         }
-
-        return result;
     }
 
 
-    private boolean migrateArchiveStreaming(String name, InputStream src, OutputStream dest) throws IOException {
-        boolean result = true;
+    private void migrateArchiveStreaming(String name, InputStream src, OutputStream dest) throws IOException {
         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));
+                    logger.log(Level.WARNING, sm.getString("migration.skipSignatureFile", sourceName));
                     continue;
                 }
                 String destName = profile.convert(sourceName);
                 JarEntry destEntry = new JarEntry(destName);
                 zipOs.putNextEntry(destEntry);
-                result = result && migrateStream(destEntry.getName(), zipIs, zipOs);
+                migrateStream(destEntry.getName(), zipIs, zipOs);
             }
         } catch (ZipException ze) {
             logger.log(Level.SEVERE, sm.getString("migration.archiveFailed", name), ze);
             throw ze;
         }
-        return result;
     }
 
 
-    private boolean migrateArchiveInMemory(InputStream src, OutputStream dest) throws IOException {
-        boolean result = true;
+    private void migrateArchiveInMemory(InputStream src, OutputStream dest) throws IOException {
         // Read the source into memory
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         IOUtils.copy(src, baos);
@@ -216,16 +205,15 @@ public class Migration {
             while (entries.hasMoreElements()) {
                 ZipArchiveEntry srcZipEntry = entries.nextElement();
                 String srcName = srcZipEntry.getName();
-                logger.log(Level.FINE, sm.getString("migration.entry", srcName));
                 if (isSignatureFile(srcName)) {
-                    logger.log(Level.FINE, sm.getString("migration.skipSignatureFile", srcName));
+                    logger.log(Level.WARNING, 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);
+                migrateStream(srcName, srcZipFile.getInputStream(srcZipEntry), destZipStream);
                 destZipStream.closeArchiveEntry();
             }
         }
@@ -233,8 +221,6 @@ public class Migration {
         // Write the destination back to the stream
         ByteArrayInputStream bais = new ByteArrayInputStream(destByteChannel.array(), 0, (int) destByteChannel.size());
         IOUtils.copy(bais, dest);
-
-        return result;
     }
 
 
@@ -244,23 +230,24 @@ public class Migration {
     }
 
 
-    private boolean migrateStream(String name, InputStream src, OutputStream dest) throws IOException {
+    private void migrateStream(String name, InputStream src, OutputStream dest) throws IOException {
         if (isArchive(name)) {
-            logger.log(Level.INFO, sm.getString("migration.archive", name));
             if (zipInMemory) {
-                return migrateArchiveInMemory(src, dest);
+                logger.log(Level.INFO, sm.getString("migration.archive.memory", name));
+                migrateArchiveInMemory(src, dest);
+                logger.log(Level.INFO, sm.getString("migration.archive.complete", name));
             } else {
-                return migrateArchiveStreaming(name, src, dest);
+                logger.log(Level.INFO, sm.getString("migration.archive.stream", name));
+                migrateArchiveStreaming(name, src, dest);
+                logger.log(Level.INFO, sm.getString("migration.archive.complete", name));
             }
         } else {
-            logger.log(Level.FINE, sm.getString("migration.stream", name));
             for (Converter converter : converters) {
                 if (converter.accepts(name)) {
-                    converter.convert(src, dest, profile);
+                    converter.convert(name, src, dest, profile);
                     break;
                 }
             }
-            return true;
         }
     }
 
diff --git a/src/main/java/org/apache/tomcat/jakartaee/PassThroughConverter.java b/src/main/java/org/apache/tomcat/jakartaee/PassThroughConverter.java
index 24c01f9..8da2d9b 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/PassThroughConverter.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/PassThroughConverter.java
@@ -19,9 +19,14 @@ package org.apache.tomcat.jakartaee;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 public class PassThroughConverter implements Converter {
 
+    private static final Logger logger = Logger.getLogger(PassThroughConverter.class.getCanonicalName());
+    private static final StringManager sm = StringManager.getManager(PassThroughConverter.class);
+
     @Override
     public boolean accepts(String filename) {
         // Accepts everything
@@ -29,8 +34,11 @@ public class PassThroughConverter implements Converter {
     }
 
     @Override
-    public void convert(InputStream src, OutputStream dest, EESpecProfile profile) throws IOException {
+    public void convert(String path, InputStream src, OutputStream dest, EESpecProfile profile) throws IOException {
         // This simply copies the source to the destination
         Util.copy(src, dest);
+        if (logger.isLoggable(Level.FINEST)) {
+            logger.log(Level.FINEST, sm.getString("passThroughConverter.noConversion", path));
+        }
     }
 }
diff --git a/src/main/java/org/apache/tomcat/jakartaee/TextConverter.java b/src/main/java/org/apache/tomcat/jakartaee/TextConverter.java
index 57cf1ae..b3c7857 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/TextConverter.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/TextConverter.java
@@ -23,9 +23,14 @@ import java.io.OutputStream;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 public class TextConverter implements Converter {
 
+    private static final Logger logger = Logger.getLogger(TextConverter.class.getCanonicalName());
+    private static final StringManager sm = StringManager.getManager(TextConverter.class);
+
     private static final List<String> supportedExtensions;
 
     static {
@@ -60,10 +65,21 @@ public class TextConverter implements Converter {
      * execution.
      */
     @Override
-    public void convert(InputStream src, OutputStream dest, EESpecProfile profile) throws IOException {
+    public void convert(String path, InputStream src, OutputStream dest, EESpecProfile profile) throws IOException {
         String srcString = Util.toString(src, StandardCharsets.ISO_8859_1);
         String destString = profile.convert(srcString);
 
+        // Object comparison is deliberate here
+        if (srcString == destString) {
+            if (logger.isLoggable(Level.FINEST)) {
+                logger.log(Level.FINEST, sm.getString("classConverter.noConversion", path));
+            }
+        } else {
+            if (logger.isLoggable(Level.FINE)) {
+                logger.log(Level.FINE, sm.getString("textConverter.converted", path));
+            }
+        }
+
         ByteArrayInputStream bais = new ByteArrayInputStream(destString.getBytes(StandardCharsets.ISO_8859_1));
         Util.copy(bais, dest);
     }
diff --git a/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties b/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
index d8150c0..5515874 100644
--- a/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
+++ b/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
@@ -13,7 +13,12 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-migration.archive=Migrating archive [{0}]
+classConverter.converted=Migrated class [{0}]
+classConverter.noConversion=No conversion necessary for [{0}]
+
+migration.archive.complete=Migration of archive [{0}] is complete
+migration.archive.memory=Migrating archive [{0}] using in memory copy
+migration.archive.stream=Migrating archive [{0}] using streaming
 migration.archiveFailed=Failed to migrate archive [{0}]. Using the "-zipInMemory" option may help.
 migration.cannotReadSource=Cannot read source location [{0}]
 migration.done=Migration completed successfully in [{0}] milliseconds
@@ -23,7 +28,6 @@ migration.execute=Performing migration from source [{0}] to destination [{1}] wi
 migration.mkdirError=Error creating destination directory [{0}]
 migration.removeSignature=Remove cryptographic signature for [{0}]
 migration.skipSignatureFile=Drop cryptographic signature file [{0}]
-migration.stream=Migrating stream [{0}]
 migration.usage=Usage: Migration [options] <source> <destination>\n\
 where options includes:\n\
 \    -logLevel=<name of java.util.logging.level enum value>\n\
@@ -37,6 +41,9 @@ where options includes:\n\
 \                compatible with some zip archive structures. If you see an\n\
 \                exception while processing a zip file, enabling this option\n\
 \                may workaround the issue.
+migration.warnSignatureRemoval=Removed cryptographic signature from JAR file
 
+passThroughConverter.noConversion=No conversion necessary for [{0}]
 
-migration.warnSignatureRemoval=Removed cryptographic signature from JAR file
+textConverter.converted=Migrated text file [{0}]
+textConverter.noConversion=No conversion necessary for [{0}]
diff --git a/src/test/java/org/apache/tomcat/jakartaee/MigrationTest.java b/src/test/java/org/apache/tomcat/jakartaee/MigrationTest.java
index 0e0ba2a..e4384be 100644
--- a/src/test/java/org/apache/tomcat/jakartaee/MigrationTest.java
+++ b/src/test/java/org/apache/tomcat/jakartaee/MigrationTest.java
@@ -115,9 +115,8 @@ public class MigrationTest {
         Migration migration = new Migration();
         migration.setSource(sourceDirectory);
         migration.setDestination(destinationDirectory);
-        boolean success = migration.execute();
+        migration.execute();
 
-        assertTrue("Migration failed", success);
         assertTrue("Destination directory not found", destinationDirectory.exists());
 
         File migratedFile = new File("target/test-classes/migration/HelloServlet.java");
diff --git a/src/test/java/org/apache/tomcat/jakartaee/PassThroughConverterTest.java b/src/test/java/org/apache/tomcat/jakartaee/PassThroughConverterTest.java
index 5df0687..fb8fa91 100644
--- a/src/test/java/org/apache/tomcat/jakartaee/PassThroughConverterTest.java
+++ b/src/test/java/org/apache/tomcat/jakartaee/PassThroughConverterTest.java
@@ -26,6 +26,8 @@ import static org.junit.Assert.*;
 
 public class PassThroughConverterTest {
 
+    private static final String TEST_FILENAME = "project.properties";
+
     @Test
     public void testConverter() throws Exception {
         String content = "javax.servlet";
@@ -35,9 +37,9 @@ public class PassThroughConverterTest {
 
         Converter converter = new PassThroughConverter();
 
-        assertTrue(converter.accepts("project.properties"));
+        assertTrue(converter.accepts(TEST_FILENAME));
 
-        converter.convert(in, out, null);
+        converter.convert(TEST_FILENAME, in, out, null);
 
         assertArrayEquals(content.getBytes(), out.toByteArray());
     }
diff --git a/src/test/java/org/apache/tomcat/jakartaee/TextConverterTest.java b/src/test/java/org/apache/tomcat/jakartaee/TextConverterTest.java
index 2d015f3..f8d029b 100644
--- a/src/test/java/org/apache/tomcat/jakartaee/TextConverterTest.java
+++ b/src/test/java/org/apache/tomcat/jakartaee/TextConverterTest.java
@@ -10,6 +10,8 @@ import org.junit.Test;
 
 public class TextConverterTest {
 
+    private static final String TEST_FILENAME = "text.txt";
+
  private static final String INPUT = "javax.servlet.http.HttpServletRequest";
  private static final String OUTPUT = "jakarta.servlet.http.HttpServletRequest";
 
@@ -23,7 +25,7 @@ public class TextConverterTest {
  EESpecProfile profile = EESpecProfile.EE;
 
  // test
- converter.convert(in, out, profile);
+ converter.convert(TEST_FILENAME, in, out, profile);
 
  // assert
  String result = new String(out.toByteArray(), StandardCharsets.ISO_8859_1);


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[tomcat-jakartaee-migration] 07/07: Fix #3 Add support for excluding files from conversion

markt
In reply to this post by markt
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

commit e963951d3f3a7ac0785815dfe44bd008425ccd43
Author: Mark Thomas <[hidden email]>
AuthorDate: Tue Feb 9 17:00:03 2021 +0000

    Fix #3 Add support for excluding files from conversion
---
 CHANGES.md                                         |   3 +-
 .../org/apache/tomcat/jakartaee/GlobMatcher.java   | 243 +++++++++++++++++++++
 .../org/apache/tomcat/jakartaee/Migration.java     |  65 +++++-
 .../org/apache/tomcat/jakartaee/MigrationCLI.java  |   7 +-
 .../org/apache/tomcat/jakartaee/MigrationTask.java |  17 ++
 .../tomcat/jakartaee/LocalStrings.properties       |  12 +-
 6 files changed, 338 insertions(+), 9 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 9b23746..4c6b15d 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -22,7 +22,8 @@
 - Ensure that all the Manifest attributes are processed during the migration process. (markt)
 - Include `.properties` and `.json` files in the conversion process. (markt)
 - Replace `-verbose` with `-logLevel=` to provide more control over logging level. (markt)
-- Fix [#13](https://github.com/apache/tomcat-jakartaee-migration/issues/13). Refactor mapping of log messages to log levels. (markt)
+- Fix [#13](https://github.com/apache/tomcat-jakartaee-migration/issues/13). Refactor mapping of log messages to log levels. (markt)
+- Fix [#3](https://github.com/apache/tomcat-jakartaee-migration/issues/3). Add support for excluding files from conversion. (markt)
 
 ## 0.1.0
 
diff --git a/src/main/java/org/apache/tomcat/jakartaee/GlobMatcher.java b/src/main/java/org/apache/tomcat/jakartaee/GlobMatcher.java
new file mode 100644
index 0000000..33dc338
--- /dev/null
+++ b/src/main/java/org/apache/tomcat/jakartaee/GlobMatcher.java
@@ -0,0 +1,243 @@
+/*
+ *  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.util.Set;
+
+/**
+ * <p>This is a utility class to match file globs.
+ * The class has been derived from
+ * org.apache.tools.ant.types.selectors.SelectorUtils.
+ * </p>
+ * <p>All methods are static.</p>
+ */
+public final class GlobMatcher {
+
+    /**
+     * Tests whether or not a given file name matches any file name pattern in
+     * the given set. The match is performed case-sensitively.
+     *
+     * @see #match(String, String, boolean)
+     *
+     * @param patternSet The pattern set to match against. Must not be
+     *                <code>null</code>.
+     * @param fileName The file name to match, as a String. Must not be
+     *                <code>null</code>. It must be just a file name, without
+     *                a path.
+     * @param caseSensitive Whether or not matching should be performed
+     *                        case sensitively.
+     *
+     * @return <code>true</code> if any pattern in the set matches against the
+     *         file name, or <code>false</code> otherwise.
+     */
+    public static boolean matchName(Set<String> patternSet, String fileName, boolean caseSensitive) {
+        char[] fileNameArray = fileName.toCharArray();
+        for (String pattern: patternSet) {
+            if (match(pattern, fileNameArray, caseSensitive)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+
+    /**
+     * Tests whether or not a string matches against a pattern.
+     * The pattern may contain two special characters:<br>
+     * '*' means zero or more characters<br>
+     * '?' means one and only one character
+     *
+     * @param pattern The pattern to match against.
+     *                Must not be <code>null</code>.
+     * @param str     The string which must be matched against the
+     *                pattern. Must not be <code>null</code>.
+     * @param caseSensitive Whether or not matching should be performed
+     *                        case sensitively.
+     *
+     *
+     * @return <code>true</code> if the string matches against the pattern,
+     *         or <code>false</code> otherwise.
+     */
+    public static boolean match(String pattern, String str,
+            boolean caseSensitive) {
+
+        return match(pattern, str.toCharArray(), caseSensitive);
+    }
+
+
+    /**
+     * Tests whether or not a string matches against a pattern.
+     * The pattern may contain two special characters:<br>
+     * '*' means zero or more characters<br>
+     * '?' means one and only one character
+     *
+     * @param pattern The pattern to match against.
+     *                Must not be <code>null</code>.
+     * @param strArr  The character array which must be matched against the
+     *                pattern. Must not be <code>null</code>.
+     * @param caseSensitive Whether or not matching should be performed
+     *                        case sensitively.
+     *
+     *
+     * @return <code>true</code> if the string matches against the pattern,
+     *         or <code>false</code> otherwise.
+     */
+    private static boolean match(String pattern, char[] strArr,
+                                boolean caseSensitive) {
+        char[] patArr = pattern.toCharArray();
+        int patIdxStart = 0;
+        int patIdxEnd = patArr.length - 1;
+        int strIdxStart = 0;
+        int strIdxEnd = strArr.length - 1;
+        char ch;
+
+        boolean containsStar = false;
+        for (char c : patArr) {
+            if (c == '*') {
+                containsStar = true;
+                break;
+            }
+        }
+
+        if (!containsStar) {
+            // No '*'s, so we make a shortcut
+            if (patIdxEnd != strIdxEnd) {
+                return false; // Pattern and string do not have the same size
+            }
+            for (int i = 0; i <= patIdxEnd; i++) {
+                ch = patArr[i];
+                if (ch != '?') {
+                    if (different(caseSensitive, ch, strArr[i])) {
+                        return false; // Character mismatch
+                    }
+                }
+            }
+            return true; // String matches against pattern
+        }
+
+        if (patIdxEnd == 0) {
+            return true; // Pattern contains only '*', which matches anything
+        }
+
+        // Process characters before first star
+        while (true) {
+            ch = patArr[patIdxStart];
+            if (ch == '*' || strIdxStart > strIdxEnd) {
+                break;
+            }
+            if (ch != '?') {
+                if (different(caseSensitive, ch, strArr[strIdxStart])) {
+                    return false; // Character mismatch
+                }
+            }
+            patIdxStart++;
+            strIdxStart++;
+        }
+        if (strIdxStart > strIdxEnd) {
+            // All characters in the string are used. Check if only '*'s are
+            // left in the pattern. If so, we succeeded. Otherwise failure.
+            return allStars(patArr, patIdxStart, patIdxEnd);
+        }
+
+        // Process characters after last star
+        while (true) {
+            ch = patArr[patIdxEnd];
+            if (ch == '*' || strIdxStart > strIdxEnd) {
+                break;
+            }
+            if (ch != '?') {
+                if (different(caseSensitive, ch, strArr[strIdxEnd])) {
+                    return false; // Character mismatch
+                }
+            }
+            patIdxEnd--;
+            strIdxEnd--;
+        }
+        if (strIdxStart > strIdxEnd) {
+            // All characters in the string are used. Check if only '*'s are
+            // left in the pattern. If so, we succeeded. Otherwise failure.
+            return allStars(patArr, patIdxStart, patIdxEnd);
+        }
+
+        // process pattern between stars. padIdxStart and patIdxEnd point
+        // always to a '*'.
+        while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd) {
+            int patIdxTmp = -1;
+            for (int i = patIdxStart + 1; i <= patIdxEnd; i++) {
+                if (patArr[i] == '*') {
+                    patIdxTmp = i;
+                    break;
+                }
+            }
+            if (patIdxTmp == patIdxStart + 1) {
+                // Two stars next to each other, skip the first one.
+                patIdxStart++;
+                continue;
+            }
+            // Find the pattern between padIdxStart & padIdxTmp in str between
+            // strIdxStart & strIdxEnd
+            int patLength = (patIdxTmp - patIdxStart - 1);
+            int strLength = (strIdxEnd - strIdxStart + 1);
+            int foundIdx = -1;
+            strLoop:
+            for (int i = 0; i <= strLength - patLength; i++) {
+                for (int j = 0; j < patLength; j++) {
+                    ch = patArr[patIdxStart + j + 1];
+                    if (ch != '?') {
+                        if (different(caseSensitive, ch,
+                                      strArr[strIdxStart + i + j])) {
+                            continue strLoop;
+                        }
+                    }
+                }
+
+                foundIdx = strIdxStart + i;
+                break;
+            }
+
+            if (foundIdx == -1) {
+                return false;
+            }
+
+            patIdxStart = patIdxTmp;
+            strIdxStart = foundIdx + patLength;
+        }
+
+        // All characters in the string are used. Check if only '*'s are left
+        // in the pattern. If so, we succeeded. Otherwise failure.
+        return allStars(patArr, patIdxStart, patIdxEnd);
+    }
+
+    private static boolean allStars(char[] chars, int start, int end) {
+        for (int i = start; i <= end; ++i) {
+            if (chars[i] != '*') {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private static boolean different(
+        boolean caseSensitive, char ch, char other) {
+        return caseSensitive
+            ? ch != other
+            : Character.toUpperCase(ch) != Character.toUpperCase(other);
+    }
+
+}
diff --git a/src/main/java/org/apache/tomcat/jakartaee/Migration.java b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
index 153345a..e8c39b6 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/Migration.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
@@ -26,7 +26,9 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import java.util.jar.JarEntry;
 import java.util.logging.Level;
@@ -49,11 +51,46 @@ public class Migration {
     private static final Logger logger = Logger.getLogger(Migration.class.getCanonicalName());
     private static final StringManager sm = StringManager.getManager(Migration.class);
 
+    private static final Set<String> DEFAULT_EXCLUDES = new HashSet<>();
+
+    static {
+        // Apache Commons
+        DEFAULT_EXCLUDES.add("commons-codec-*.jar");
+        DEFAULT_EXCLUDES.add("commons-lang-*.jar");
+        // Apache HTTP Components
+        DEFAULT_EXCLUDES.add("httpclient-*.jar");
+        DEFAULT_EXCLUDES.add("httpcore-*.jar");
+        // ASM
+        DEFAULT_EXCLUDES.add("asm-*.jar");
+        // AspectJ
+        DEFAULT_EXCLUDES.add("aspectjweaver-*.jar");
+        // Bouncy Castle JCE provider
+        DEFAULT_EXCLUDES.add("bcprov*.jar");
+        DEFAULT_EXCLUDES.add("bcpkix*.jar");
+        // Hystrix
+        DEFAULT_EXCLUDES.add("hystrix-core-*.jar");
+        DEFAULT_EXCLUDES.add("hystrix-serialization-*.jar");
+        // Jackson
+        DEFAULT_EXCLUDES.add("jackson-annotations-*.jar");
+        DEFAULT_EXCLUDES.add("jackson-core-*.jar");
+        DEFAULT_EXCLUDES.add("jackson-module-afterburner-*.jar");
+        // Logging
+        DEFAULT_EXCLUDES.add("jul-to-slf4j-*.jar");
+        DEFAULT_EXCLUDES.add("log4j-to-slf4j-*.jar");
+        DEFAULT_EXCLUDES.add("slf4j-api-*.jar");
+        // Spring
+        DEFAULT_EXCLUDES.add("spring-aop-*.jar");
+        DEFAULT_EXCLUDES.add("spring-expression-*.jar");
+        DEFAULT_EXCLUDES.add("spring-security-crypto-*.jar");
+        DEFAULT_EXCLUDES.add("spring-security-rsa-*.jar");
+    }
+
     private EESpecProfile profile = EESpecProfile.TOMCAT;
     private boolean zipInMemory;
     private File source;
     private File destination;
     private final List<Converter> converters;
+    private final Set<String> excludes = new HashSet<>();
 
     public Migration() {
         // Initialise the converters
@@ -89,6 +126,10 @@ public class Migration {
         this.zipInMemory = zipInMemory;
     }
 
+    public void addExclude(String exclude) {
+        this.excludes.add(exclude);
+    }
+
     public void setSource(File source) {
         if (!source.canRead()) {
             throw new IllegalArgumentException(sm.getString("migration.cannotReadSource",
@@ -181,7 +222,7 @@ public class Migration {
                 String destName = profile.convert(sourceName);
                 JarEntry destEntry = new JarEntry(destName);
                 zipOs.putNextEntry(destEntry);
-                migrateStream(destEntry.getName(), zipIs, zipOs);
+                migrateStream(sourceName, zipIs, zipOs);
             }
         } catch (ZipException ze) {
             logger.log(Level.SEVERE, sm.getString("migration.archiveFailed", name), ze);
@@ -231,7 +272,10 @@ public class Migration {
 
 
     private void migrateStream(String name, InputStream src, OutputStream dest) throws IOException {
-        if (isArchive(name)) {
+        if (isExcluded(name)) {
+            Util.copy(src, dest);
+            logger.log(Level.INFO, sm.getString("migration.skip", name));
+        } else if (isArchive(name)) {
             if (zipInMemory) {
                 logger.log(Level.INFO, sm.getString("migration.archive.memory", name));
                 migrateArchiveInMemory(src, dest);
@@ -252,11 +296,26 @@ public class Migration {
     }
 
 
-    private static boolean isArchive(String fileName) {
+    private boolean isArchive(String fileName) {
         return fileName.endsWith(".jar") || fileName.endsWith(".war") || fileName.endsWith(".zip");
     }
 
 
+    private boolean isExcluded(String name) {
+        File f = new File(name);
+        String filename = f.getName();
+
+        if (GlobMatcher.matchName(DEFAULT_EXCLUDES, filename, true)) {
+            return true;
+        }
+
+        if (GlobMatcher.matchName(excludes, filename, true)) {
+            return true;
+        }
+
+        return false;
+    }
+
     private static class RenamableZipArchiveEntry extends ZipArchiveEntry {
 
         public RenamableZipArchiveEntry(ZipArchiveEntry entry) throws ZipException {
diff --git a/src/main/java/org/apache/tomcat/jakartaee/MigrationCLI.java b/src/main/java/org/apache/tomcat/jakartaee/MigrationCLI.java
index 64af9b9..36e9dc5 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/MigrationCLI.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/MigrationCLI.java
@@ -30,6 +30,7 @@ public class MigrationCLI {
 
     private static final StringManager sm = StringManager.getManager(MigrationCLI.class);
 
+    private static final String EXCLUDE_ARG = "-exclude=";
     private static final String LOGLEVEL_ARG = "-logLevel=";
     private static final String PROFILE_ARG = "-profile=";
     // Will be removed for 1.0.0
@@ -51,7 +52,11 @@ public class MigrationCLI {
         Iterator<String> iter = arguments.iterator();
         while (iter.hasNext()) {
             String argument = iter.next();
-            if (argument.startsWith(LOGLEVEL_ARG)) {
+            if (argument.startsWith(EXCLUDE_ARG)) {
+                iter.remove();
+                String exclude = argument.substring(EXCLUDE_ARG.length());
+                migration.addExclude(exclude);
+            } else if (argument.startsWith(LOGLEVEL_ARG)) {
                 iter.remove();
                 String logLevelName = argument.substring(LOGLEVEL_ARG.length());
                 Level level = null;
diff --git a/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java b/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java
index c19599e..39f31dd 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java
@@ -33,6 +33,7 @@ public class MigrationTask extends Task {
     private File dest;
     private String profile = EESpecProfile.TOMCAT.toString();
     private boolean zipInMemory = false;
+    private String excludes;
 
     public void setSrc(File src) {
         this.src = src;
@@ -50,6 +51,16 @@ public class MigrationTask extends Task {
         this.zipInMemory = zipInMemory;
     }
 
+    /**
+     * Set exclusion patterns.
+     *
+     * @param excludes  Comma separated, case sensitive list of glob patterns
+     *                  for files to exclude
+     */
+    public void setExcludes(String excludes) {
+        this.excludes = excludes;
+    }
+
     @Override
     public void execute() throws BuildException {
         // redirect the log messages to Ant
@@ -73,6 +84,12 @@ public class MigrationTask extends Task {
         migration.setDestination(dest);
         migration.setEESpecProfile(profile);
         migration.setZipInMemory(zipInMemory);
+        if (this.excludes != null) {
+            String[] excludes= this.excludes.split(",");
+            for (String exclude : excludes) {
+                migration.addExclude(exclude);
+            }
+        }
 
         try {
             migration.execute();
diff --git a/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties b/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
index 5515874..655bc10 100644
--- a/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
+++ b/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
@@ -16,20 +16,24 @@
 classConverter.converted=Migrated class [{0}]
 classConverter.noConversion=No conversion necessary for [{0}]
 
-migration.archive.complete=Migration of archive [{0}] is complete
-migration.archive.memory=Migrating archive [{0}] using in memory copy
-migration.archive.stream=Migrating archive [{0}] using streaming
+migration.archive.complete=Migration finished for archive [{0}]
+migration.archive.memory=Migration starting for archive [{0}] using in memory copy
+migration.skip=Migration skipped for archive [{0}] because it is excluded (the archive was copied unchanged)
+migration.archive.stream=Migration starting for archive [{0}] using streaming
 migration.archiveFailed=Failed to migrate archive [{0}]. Using the "-zipInMemory" option may help.
 migration.cannotReadSource=Cannot read source location [{0}]
 migration.done=Migration completed successfully in [{0}] milliseconds
-migration.entry=Migrating Jar entry [{0}]
 migration.error=Error performing migration
 migration.execute=Performing migration from source [{0}] to destination [{1}] with Jakarta EE specification profile [{2}]
 migration.mkdirError=Error creating destination directory [{0}]
 migration.removeSignature=Remove cryptographic signature for [{0}]
+migration.skip=Migration skipped for archive [{0}] because it is excluded (the archive was copied unchanged)
 migration.skipSignatureFile=Drop cryptographic signature file [{0}]
 migration.usage=Usage: Migration [options] <source> <destination>\n\
 where options includes:\n\
+\    -exclude=<glob pattern to exclude>\n\
+\                This option may be used multiple times. Wild cards '*'\n\
+\                and '?' are supported. Matching is case sensitive.\n\
 \    -logLevel=<name of java.util.logging.level enum value>\n\
 \                Useful values are INFO (default), FINE or FINEST\n\
 \    -profile=<profile name>\n\


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [tomcat-jakartaee-migration] 03/07: Reduce object creation during conversion

Martin Grigorov
In reply to this post by markt
On Tue, Feb 9, 2021 at 7:18 PM <[hidden email]> wrote:

> 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
>
> commit 4a0b09d3e43dfcb35c5a7488a222b1c7ea941669
> Author: Mark Thomas <[hidden email]>
> AuthorDate: Tue Feb 9 14:25:17 2021 +0000
>
>     Reduce object creation during conversion
> ---
>  src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
> b/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
> index 9d398d7..cc06bde 100644
> --- a/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
> +++ b/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
> @@ -46,8 +46,12 @@ public class ClassConverter implements Converter {
>              if (constantPool[i] instanceof ConstantUtf8) {
>                  ConstantUtf8 c = (ConstantUtf8) constantPool[i];
>                  String str = c.getBytes();
> -                c = new ConstantUtf8(profile.convert(str));
> -                constantPool[i] = c;
> +                String converted = profile.convert(str);
> +                // Object comparison is deliberate
> +                if (converted != str) {
> +                    c = new ConstantUtf8(profile.convert(str));
>

Does it need to convert the second time ?
 c = new ConstantUtf8(converted) should work too, no ?


> +                    constantPool[i] = c;
> +                }
>              }
>          }
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [tomcat-jakartaee-migration] 03/07: Reduce object creation during conversion

markt
On 17/02/2021 11:53, Martin Grigorov wrote:

> On Tue, Feb 9, 2021 at 7:18 PM <[hidden email]> wrote:
>
>> 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
>>
>> commit 4a0b09d3e43dfcb35c5a7488a222b1c7ea941669
>> Author: Mark Thomas <[hidden email]>
>> AuthorDate: Tue Feb 9 14:25:17 2021 +0000
>>
>>     Reduce object creation during conversion
>> ---
>>  src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java | 8 ++++++--
>>  1 file changed, 6 insertions(+), 2 deletions(-)
>>
>> diff --git a/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
>> b/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
>> index 9d398d7..cc06bde 100644
>> --- a/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
>> +++ b/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java
>> @@ -46,8 +46,12 @@ public class ClassConverter implements Converter {
>>              if (constantPool[i] instanceof ConstantUtf8) {
>>                  ConstantUtf8 c = (ConstantUtf8) constantPool[i];
>>                  String str = c.getBytes();
>> -                c = new ConstantUtf8(profile.convert(str));
>> -                constantPool[i] = c;
>> +                String converted = profile.convert(str);
>> +                // Object comparison is deliberate
>> +                if (converted != str) {
>> +                    c = new ConstantUtf8(profile.convert(str));
>>
>
> Does it need to convert the second time ?
>  c = new ConstantUtf8(converted) should work too, no ?

Correct. Good catch. I'll get that fixed.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]