This is an automated email from the ASF dual-hosted git repository. ebourg pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/tomcat-jakartaee-migration.git
commit 0a52c66b751e3245fad11faf85f7dbfa4343bac9 Author: Emmanuel Bourg <ebo...@apache.org> AuthorDate: Wed Apr 8 18:55:17 2020 +0200 Split the CLI handling from the main migration class --- pom.xml | 2 +- .../org/apache/tomcat/jakartaee/Migration.java | 63 --------------- .../org/apache/tomcat/jakartaee/MigrationCLI.java | 92 ++++++++++++++++++++++ src/main/scripts/migrate.sh | 2 +- .../org/apache/tomcat/jakartaee/MigrationTest.java | 8 +- 5 files changed, 98 insertions(+), 69 deletions(-) diff --git a/pom.xml b/pom.xml index 2d4ddd8..f6bd9d3 100644 --- a/pom.xml +++ b/pom.xml @@ -100,7 +100,7 @@ <archive> <manifest> <addClasspath>true</addClasspath> - <mainClass>org.apache.tomcat.jakartaee.Migration</mainClass> + <mainClass>org.apache.tomcat.jakartaee.MigrationCLI</mainClass> </manifest> </archive> </configuration> diff --git a/src/main/java/org/apache/tomcat/jakartaee/Migration.java b/src/main/java/org/apache/tomcat/jakartaee/Migration.java index 68a8fc2..44efc5a 100644 --- a/src/main/java/org/apache/tomcat/jakartaee/Migration.java +++ b/src/main/java/org/apache/tomcat/jakartaee/Migration.java @@ -24,7 +24,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -270,68 +269,6 @@ public class Migration { } } - private static final String PROFILE_ARG = "-profile="; - - public static void main(String[] args) { - System.setProperty("java.util.logging.SimpleFormatter.format", "%5$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"); - } - - Migration migration = new Migration(); - - 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; - try { - migration.setEESpecProfile(EESpecProfile.valueOf(arguments.get(0).substring(PROFILE_ARG.length()))); - } catch (IllegalArgumentException e) { - // Invalid profile value - valid = false; - } - } - } - if (arguments.size() == 2) { - source = arguments.get(0); - dest = arguments.get(1); - valid = true; - } - if (!valid) { - usage(); - System.exit(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); - } - } - - - private static void usage() { - System.out.println(sm.getString("migration.usage")); - } - - 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/MigrationCLI.java b/src/main/java/org/apache/tomcat/jakartaee/MigrationCLI.java new file mode 100644 index 0000000..1b6dc80 --- /dev/null +++ b/src/main/java/org/apache/tomcat/jakartaee/MigrationCLI.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tomcat.jakartaee; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +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 PROFILE_ARG = "-profile="; + + public static void main(String[] args) { + System.setProperty("java.util.logging.SimpleFormatter.format", "%5$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"); + } + + Migration migration = new Migration(); + + 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; + try { + migration.setEESpecProfile(EESpecProfile.valueOf(arguments.get(0).substring(PROFILE_ARG.length()))); + } catch (IllegalArgumentException e) { + // Invalid profile value + valid = false; + } + } + } + if (arguments.size() == 2) { + source = arguments.get(0); + dest = arguments.get(1); + valid = true; + } + if (!valid) { + usage(); + System.exit(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); + } + } + + private static void usage() { + System.out.println(sm.getString("migration.usage")); + } + +} diff --git a/src/main/scripts/migrate.sh b/src/main/scripts/migrate.sh index 4f8826b..c2b941c 100644 --- a/src/main/scripts/migrate.sh +++ b/src/main/scripts/migrate.sh @@ -1,4 +1,4 @@ #!/bin/sh # Assumes java is on the path -java -cp "../lib/*" org.apache.tomcat.jakartaee.Migration "$@" +java -cp "../lib/*" org.apache.tomcat.jakartaee.MigrationCLI "$@" diff --git a/src/test/java/org/apache/tomcat/jakartaee/MigrationTest.java b/src/test/java/org/apache/tomcat/jakartaee/MigrationTest.java index c41a735..e77925f 100644 --- a/src/test/java/org/apache/tomcat/jakartaee/MigrationTest.java +++ b/src/test/java/org/apache/tomcat/jakartaee/MigrationTest.java @@ -42,7 +42,7 @@ public class MigrationTest { @Test public void testMigrateSingleSourceFile() throws Exception { File migratedFile = new File("target/test-classes/HelloServlet.migrated.java"); - Migration.main(new String[] {"target/test-classes/HelloServlet.java", migratedFile.getAbsolutePath()}); + MigrationCLI.main(new String[] {"target/test-classes/HelloServlet.java", migratedFile.getAbsolutePath()}); assertTrue("Migrated file not found", migratedFile.exists()); @@ -54,7 +54,7 @@ public class MigrationTest { @Test public void testMigrateSingleSourceFileWithProfile() throws Exception { File migratedFile = new File("target/test-classes/HelloServlet.migrated.java"); - Migration.main(new String[] {"-verbose", "-profile=EE", "target/test-classes/HelloServlet.java", migratedFile.getAbsolutePath()}); + MigrationCLI.main(new String[] {"-verbose", "-profile=EE", "target/test-classes/HelloServlet.java", migratedFile.getAbsolutePath()}); assertTrue("Migrated file not found", migratedFile.exists()); @@ -69,7 +69,7 @@ public class MigrationTest { File migratedFile = new File("target/test-classes/HelloServlet.inplace.java"); FileUtils.copyFile(sourceFile, migratedFile); - Migration.main(new String[] {"-profile=EE", migratedFile.getAbsolutePath(), migratedFile.getAbsolutePath()}); + MigrationCLI.main(new String[] {"-profile=EE", migratedFile.getAbsolutePath(), migratedFile.getAbsolutePath()}); assertTrue("Migrated file not found", migratedFile.exists()); @@ -84,7 +84,7 @@ public class MigrationTest { File migratedFile = new File("target/test-classes/HelloServlet.migrated.java"); try { - Migration.main(new String[] {"-invalid", sourceFile.getAbsolutePath(), migratedFile.getAbsolutePath()}); + MigrationCLI.main(new String[] {"-invalid", sourceFile.getAbsolutePath(), migratedFile.getAbsolutePath()}); fail("No error code returned"); } catch (SecurityException e) { assertEquals("error code", "1", e.getMessage()); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org