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 749b6e6483a970d7afa5234a58ba7edcdadc07af Author: Mark Thomas <ma...@apache.org> AuthorDate: Mon Jan 13 12:49:49 2020 +0000 Initial import --- .gitignore | 4 + LICENSE | 177 +++++++++++++++++++++ NOTICE | 5 + .../apache/tomcat/jakartaee/ClassConverter.java | 48 ++++++ .../org/apache/tomcat/jakartaee/Converter.java | 12 ++ .../org/apache/tomcat/jakartaee/Migration.java | 147 +++++++++++++++++ .../org/apache/tomcat/jakartaee/NoOpConverter.java | 24 +++ .../tomcat/jakartaee/NonClosingInputStream.java | 68 ++++++++ .../tomcat/jakartaee/NonClosingOutputStream.java | 44 +++++ .../org/apache/tomcat/jakartaee/TextConverter.java | 74 +++++++++ .../java/org/apache/tomcat/jakartaee/Util.java | 31 ++++ 11 files changed, 634 insertions(+) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..107422a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.classpath +.project +.settings +bin \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f433b1a --- /dev/null +++ b/LICENSE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..2970a72 --- /dev/null +++ b/NOTICE @@ -0,0 +1,5 @@ +Apache Tomcat migration tool for Jakarta EE +Copyright 2020 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (https://www.apache.org/). \ No newline at end of file diff --git a/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java b/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java new file mode 100644 index 0000000..8a2eaf8 --- /dev/null +++ b/src/main/java/org/apache/tomcat/jakartaee/ClassConverter.java @@ -0,0 +1,48 @@ +package org.apache.tomcat.jakartaee; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.bcel.classfile.ClassParser; +import org.apache.bcel.classfile.Constant; +import org.apache.bcel.classfile.ConstantUtf8; +import org.apache.bcel.classfile.JavaClass; + +public class ClassConverter implements Converter { + + @Override + public boolean accpets(String filename) { + String extension = Util.getExtension(filename); + if (extension == null || extension.length() == 0) { + return false; + } + + if ("class".equals(extension)) { + return true; + } + + return false; + } + + + @Override + public void convert(InputStream src, OutputStream dest) throws IOException { + + ClassParser parser = new ClassParser(src, "unknown"); + JavaClass javaClass = parser.parse(); + + // 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(); + c = new ConstantUtf8(Util.convert(str)); + constantPool[i] = c; + } + } + + 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 new file mode 100644 index 0000000..8346b4c --- /dev/null +++ b/src/main/java/org/apache/tomcat/jakartaee/Converter.java @@ -0,0 +1,12 @@ +package org.apache.tomcat.jakartaee; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public interface Converter { + + boolean accpets(String filename); + + void convert(InputStream src, OutputStream dest) throws IOException; +} diff --git a/src/main/java/org/apache/tomcat/jakartaee/Migration.java b/src/main/java/org/apache/tomcat/jakartaee/Migration.java new file mode 100644 index 0000000..80b1721 --- /dev/null +++ b/src/main/java/org/apache/tomcat/jakartaee/Migration.java @@ -0,0 +1,147 @@ +package org.apache.tomcat.jakartaee; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; +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; + +public class Migration { + + private File source; + private File destination; + private final List<Converter> converters; + + public Migration() { + // Initialise the converters + converters = new ArrayList<>(); + + converters.add(new TextConverter()); + converters.add(new ClassConverter()); + + // Final converter is the NoOpConverter + converters.add(new NoOpConverter()); + } + + + public void setSource(File source) { + if (!source.canRead()) { + // TODO i18n + throw new IllegalArgumentException(); + } + this.source = source; + } + + + public void setDestination(File destination) { + // TODO validate + this.destination = destination; + } + + + public boolean execute() throws IOException { + // TODO validate arguments + + if (source.isDirectory()) { + migrateDirectory(source, destination); + } else { + // Single file + migrateFile(source, destination); + } + return false; + } + + + private void migrateDirectory(File src, File dest) throws IOException { + String[] files = src.list(); + for (String file : files) { + migrateFile(new File(src, file), new File(dest, file)); + } + } + + + private void migrateFile(File src, File dest) throws IOException { + try (InputStream is = new FileInputStream(src); + OutputStream os = new FileOutputStream(dest)) { + migrateStream(src.getName(), is, os); + } + } + + + private void migrateArchive(InputStream src, OutputStream dest) throws IOException { + try (JarInputStream jarIs = new JarInputStream(new NonClosingInputStream(src)); + JarOutputStream jarOs = new JarOutputStream(new NonClosingOutputStream(dest))) { + Manifest manifest = jarIs.getManifest(); + if (manifest != null) { + JarEntry manifestEntry = new JarEntry(JarFile.MANIFEST_NAME); + jarOs.putNextEntry(manifestEntry); + manifest.write(jarOs); + } + JarEntry jarEntry; + while ((jarEntry = jarIs.getNextJarEntry()) != null) { + String sourceName = jarEntry.getName(); + System.out.println("Migrating JarEntry [" + sourceName + "]"); + String destName = Util.convert(sourceName); + JarEntry destEntry = new JarEntry(destName); + jarOs.putNextEntry(destEntry); + migrateStream(destEntry.getName(), jarIs, jarOs); + } + } + } + + + private void migrateStream(String name, InputStream src, OutputStream dest) throws IOException { + System.out.println("Migrating stream [" + name + "]"); + if (isArchive(name)) { + migrateArchive(src, dest); + } else { + for (Converter converter : converters) { + if (converter.accpets(name)) { + converter.convert(src, dest); + break; + } + } + } + } + + + public static void main(String[] args) { + if (args.length != 2) { + usage(); + System.exit(1); + } + Migration migration = new Migration(); + migration.setSource(new File(args[0])); + migration.setDestination(new File(args[1])); + boolean result = false; + try { + result = migration.execute(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // Signal caller that migration failed + if (!result) { + System.exit(1); + } + } + + + private static void usage() { + System.out.println("Usage: Migration <source> <destination>"); + } + + + private static boolean isArchive(String fileName) { + return fileName.endsWith(".jar") || fileName.endsWith(".war") || fileName.endsWith(".zip"); + } +} diff --git a/src/main/java/org/apache/tomcat/jakartaee/NoOpConverter.java b/src/main/java/org/apache/tomcat/jakartaee/NoOpConverter.java new file mode 100644 index 0000000..1e25491 --- /dev/null +++ b/src/main/java/org/apache/tomcat/jakartaee/NoOpConverter.java @@ -0,0 +1,24 @@ +package org.apache.tomcat.jakartaee; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class NoOpConverter implements Converter { + + @Override + public boolean accpets(String filename) { + // Accepts everything + return true; + } + + @Override + public void convert(InputStream src, OutputStream dest) throws IOException { + // This simply copies the source to the destination + byte[] buf = new byte[8192]; + int numRead; + while ((numRead = src.read(buf)) >= 0) { + dest.write(buf, 0, numRead); + } + } +} diff --git a/src/main/java/org/apache/tomcat/jakartaee/NonClosingInputStream.java b/src/main/java/org/apache/tomcat/jakartaee/NonClosingInputStream.java new file mode 100644 index 0000000..1625c6c --- /dev/null +++ b/src/main/java/org/apache/tomcat/jakartaee/NonClosingInputStream.java @@ -0,0 +1,68 @@ +package org.apache.tomcat.jakartaee; + +import java.io.IOException; +import java.io.InputStream; + +public class NonClosingInputStream extends InputStream { + + private InputStream wrapped; + + + public NonClosingInputStream(InputStream wrapped) { + this.wrapped = wrapped; + } + + + @Override + public int read() throws IOException { + return wrapped.read(); + } + + + @Override + public int read(byte[] b) throws IOException { + return wrapped.read(b); + } + + + @Override + public int read(byte[] b, int off, int len) throws IOException { + return wrapped.read(b, off, len); + } + + + @Override + public long skip(long n) throws IOException { + return wrapped.skip(n); + } + + + @Override + public int available() throws IOException { + return wrapped.available(); + } + + + @Override + public void close() throws IOException { + // NO-OP + } + + + @Override + public synchronized void mark(int readlimit) { + wrapped.mark(readlimit); + } + + + @Override + public synchronized void reset() throws IOException { + wrapped.reset(); + } + + + @Override + public boolean markSupported() { + return wrapped.markSupported(); + } +} diff --git a/src/main/java/org/apache/tomcat/jakartaee/NonClosingOutputStream.java b/src/main/java/org/apache/tomcat/jakartaee/NonClosingOutputStream.java new file mode 100644 index 0000000..1d72659 --- /dev/null +++ b/src/main/java/org/apache/tomcat/jakartaee/NonClosingOutputStream.java @@ -0,0 +1,44 @@ +package org.apache.tomcat.jakartaee; + +import java.io.IOException; +import java.io.OutputStream; + +public class NonClosingOutputStream extends OutputStream { + + private OutputStream wrapped; + + + public NonClosingOutputStream(OutputStream wrapped) { + this.wrapped = wrapped; + } + + + @Override + public void write(int b) throws IOException { + wrapped.write(b); + } + + + @Override + public void write(byte[] b) throws IOException { + wrapped.write(b); + } + + + @Override + public void write(byte[] b, int off, int len) throws IOException { + wrapped.write(b, off, len); + } + + + @Override + public void flush() throws IOException { + wrapped.flush(); + } + + + @Override + public void close() throws IOException { + // NO-OP + } +} diff --git a/src/main/java/org/apache/tomcat/jakartaee/TextConverter.java b/src/main/java/org/apache/tomcat/jakartaee/TextConverter.java new file mode 100644 index 0000000..60695c2 --- /dev/null +++ b/src/main/java/org/apache/tomcat/jakartaee/TextConverter.java @@ -0,0 +1,74 @@ +package org.apache.tomcat.jakartaee; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +public class TextConverter implements Converter { + + private static final List<String> supportedExtensions; + + static { + supportedExtensions = new ArrayList<>(); + supportedExtensions.add("jsp"); + supportedExtensions.add("jspx"); + supportedExtensions.add("tag"); + supportedExtensions.add("tagx"); + supportedExtensions.add("tld"); + supportedExtensions.add("txt"); + supportedExtensions.add("xml"); + } + + + @Override + public boolean accpets(String filename) { + String extension = Util.getExtension(filename); + if (extension == null || extension.length() == 0) { + return false; + } + + if (supportedExtensions.contains(extension)) { + return true; + } + + return false; + } + + + /* + * This is a bit of a hack so the same Pattern can be used for text files as + * for Strings in class files. An approach that worked directly on the + * streams would be more efficient - but also require significantly more + * code. Since the conversion process is intended to be a one-time process, + * this implementation opts for simplicity of code over efficiency of + * execution. + */ + @Override + public void convert(InputStream src, OutputStream dest) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + flow(src, baos); + + String srcString = new String(baos.toByteArray(), StandardCharsets.ISO_8859_1); + + String destString = Util.convert(srcString); + + ByteArrayInputStream bais = new ByteArrayInputStream(destString.getBytes(StandardCharsets.ISO_8859_1)); + flow (bais, dest); + } + + + private static void flow(InputStream is, OutputStream os) throws IOException { + byte[] buf = new byte[8192]; + int numRead; + while ( (numRead = is.read(buf) ) >= 0) { + if (os != null) { + os.write(buf, 0, numRead); + } + } + } +} diff --git a/src/main/java/org/apache/tomcat/jakartaee/Util.java b/src/main/java/org/apache/tomcat/jakartaee/Util.java new file mode 100644 index 0000000..b8f86a0 --- /dev/null +++ b/src/main/java/org/apache/tomcat/jakartaee/Util.java @@ -0,0 +1,31 @@ +package org.apache.tomcat.jakartaee; + +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Util { + + private static Pattern PATTERN = Pattern.compile( + "javax([/\\.](annotation|ejb|el|mail|persistence|security[/\\.]auth[/\\.]message|servlet|transaction|websocket))"); + + public static String getExtension(String filename) { + // Extract the extension + int lastPeriod = filename.lastIndexOf("."); + if (lastPeriod == -1) { + return null; + } + return filename.substring(lastPeriod + 1).toLowerCase(Locale.ENGLISH); + } + + + public static String convert(String name) { + Matcher m = PATTERN.matcher(name); + return m.replaceAll("jakarta$1"); + } + + + private Util() { + // Hide default constructor. Utility class. + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org