From 5976d700960b6d6992de933e072bed04dd1b64a5 Mon Sep 17 00:00:00 2001 From: lukaseder Date: Tue, 6 Sep 2016 15:19:01 +0200 Subject: [PATCH] [#5524] Don't System.exit(-1) from within GenerationTool. Throw an exception instead --- .../java/org/jooq/util/GenerationTool.java | 78 +++++++------------ 1 file changed, 29 insertions(+), 49 deletions(-) diff --git a/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java b/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java index 1a2c3a9211..2faeae510f 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java +++ b/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java @@ -135,11 +135,15 @@ public class GenerationTool { } public static void main(String[] args) throws Exception { - if (args.length < 1) - error(); + if (args.length < 1) { + log.error("Usage : GenerationTool "); + System.exit(-1); + return; + } - argsLoop: for (String arg : args) { + for (String arg : args) { InputStream in = GenerationTool.class.getResourceAsStream(arg); + try { // [#2932] Retry loading the file, if it wasn't found. This may be helpful @@ -158,23 +162,22 @@ public class GenerationTool { log.error(" - on the classpath and qualified as a classpath location."); log.error(" - in the local directory or at a global path in the file system."); - continue argsLoop; + System.exit(-1); + return; } log.info("Initialising properties", arg); - generate(load(in)); } catch (Exception e) { - log.error("Cannot read " + arg + ". Error : " + e.getMessage()); - e.printStackTrace(); + log.error("Cannot read " + arg + ". Error : " + e.getMessage(), e); - continue argsLoop; + System.exit(-1); + return; } finally { - if (in != null) { + if (in != null) in.close(); - } } } } @@ -222,7 +225,8 @@ public class GenerationTool { Jdbc j = configuration.getJdbc(); org.jooq.util.jaxb.Generator g = configuration.getGenerator(); - errorIfNull(g, "The tag is mandatory."); + if (g == null) + throw new GeneratorException("The tag is mandatory. For details, see " + Constants.NS_CODEGEN); // Some default values for optional elements to avoid NPE's if (g.getStrategy() == null) @@ -305,12 +309,10 @@ public class GenerationTool { schemata.add(schema); } else { - if (!StringUtils.isBlank(d.getInputSchema())) { + if (!StringUtils.isBlank(d.getInputSchema())) log.warn("WARNING: Cannot combine configuration properties /configuration/generator/database/inputSchema and /configuration/generator/database/schemata"); - } - if (!StringUtils.isBlank(d.getOutputSchema())) { + if (!StringUtils.isBlank(d.getOutputSchema())) log.warn("WARNING: Cannot combine configuration properties /configuration/generator/database/outputSchema and /configuration/generator/database/schemata"); - } } for (Schema schema : schemata) { @@ -325,18 +327,14 @@ public class GenerationTool { // [#3018] Prior to , empty elements meant that // the outputSchema should be the default schema. This is a bit too clever, and doesn't // work when Maven parses the XML configurations. - if ("".equals(schema.getOutputSchema())) { + if ("".equals(schema.getOutputSchema())) log.warn("WARNING: Empty should no longer be used to model default outputSchemas. Use true, instead. See also: https://github.com/jOOQ/jOOQ/issues/3018"); - } // [#3018] If users want the output schema to be "" then, ignore the actual configuration - if (TRUE.equals(schema.isOutputSchemaToDefault())) { + if (TRUE.equals(schema.isOutputSchemaToDefault())) schema.setOutputSchema(""); - } - - else if (schema.getOutputSchema() == null) { + else if (schema.getOutputSchema() == null) schema.setOutputSchema(trim(schema.getInputSchema())); - } @@ -350,11 +348,9 @@ public class GenerationTool { } - if (schemata.size() == 1) { - if (StringUtils.isBlank(schemata.get(0).getInputSchema())) { + if (schemata.size() == 1) + if (StringUtils.isBlank(schemata.get(0).getInputSchema())) log.info("No was provided. Generating ALL available schemata instead!"); - } - } database.setConnection(connection); database.setConfiguredSchemata(schemata); @@ -550,24 +546,20 @@ public class GenerationTool { generator.generate(database); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } finally { + } + finally { // Close connection only if it was created by the GenerationTool - if (close && connection != null) { + if (close && connection != null) connection.close(); - } } } private Properties properties(List properties) { Properties result = new Properties(); - for (Property p : properties) { + for (Property p : properties) result.put(p.getKey(), p.getValue()); - } return result; } @@ -592,13 +584,13 @@ public class GenerationTool { return databaseClass(c.getMetaData().getURL()); } catch (SQLException e) { - throw new RuntimeException(e); + throw new GeneratorException("Error when reading URL from JDBC connection", e); } } private Class databaseClass(String url) { if (isBlank(url)) - throw new RuntimeException("No JDBC URL configured."); + throw new GeneratorException("No JDBC URL configured."); Class result = Databases.databaseClass(JDBCUtils.dialect(url)); log.info("Database", "Inferring database " + result.getName() + " from URL " + url); @@ -642,18 +634,6 @@ public class GenerationTool { return (string == null ? null : string.trim()); } - private static void errorIfNull(Object o, String message) { - if (o == null) { - log.error(message + " For details, see " + Constants.NS_CODEGEN); - System.exit(-1); - } - } - - private static void error() { - log.error("Usage : GenerationTool "); - System.exit(-1); - } - /** * Copy bytes from a large (over 2GB) InputStream to an * OutputStream. @@ -716,7 +696,7 @@ public class GenerationTool { return (Configuration) unmarshaller.unmarshal(new StringReader(xml)); } catch (Exception e) { - throw new RuntimeException(e); + throw new GeneratorException("Error while reading XML configuration", e); } } }