Skip to content

Commit

Permalink
[#5524] Don't System.exit(-1) from within GenerationTool. Throw an ex…
Browse files Browse the repository at this point in the history
…ception instead
  • Loading branch information
lukaseder committed Sep 6, 2016
1 parent cae2a72 commit 5976d70
Showing 1 changed file with 29 additions and 49 deletions.
78 changes: 29 additions & 49 deletions jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java
Expand Up @@ -135,11 +135,15 @@ public void setDataSource(DataSource dataSource) {
}

public static void main(String[] args) throws Exception {
if (args.length < 1)
error();
if (args.length < 1) {
log.error("Usage : GenerationTool <configuration-file>");
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
Expand All @@ -158,23 +162,22 @@ public static void main(String[] args) throws Exception {
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();
}
}
}
}
Expand Down Expand Up @@ -222,7 +225,8 @@ public void run(Configuration configuration) throws Exception {

Jdbc j = configuration.getJdbc();
org.jooq.util.jaxb.Generator g = configuration.getGenerator();
errorIfNull(g, "The <generator/> tag is mandatory.");
if (g == null)
throw new GeneratorException("The <generator/> tag is mandatory. For details, see " + Constants.NS_CODEGEN);

// Some default values for optional elements to avoid NPE's
if (g.getStrategy() == null)
Expand Down Expand Up @@ -305,12 +309,10 @@ else if (j != null) {
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) {
Expand All @@ -325,18 +327,14 @@ else if (j != null) {
// [#3018] Prior to <outputSchemaToDefault/>, empty <outputSchema/> 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 <outputSchema/> should no longer be used to model default outputSchemas. Use <outputSchemaToDefault>true</outputSchemaToDefault>, instead. See also: https://github.com/jOOQ/jOOQ/issues/3018");
}

// [#3018] If users want the output schema to be "" then, ignore the actual <outputSchema/> 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()));
}



Expand All @@ -350,11 +348,9 @@ else if (schema.getOutputSchema() == null) {

}

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 <inputSchema/> was provided. Generating ALL available schemata instead!");
}
}

database.setConnection(connection);
database.setConfiguredSchemata(schemata);
Expand Down Expand Up @@ -550,24 +546,20 @@ else if (schema.getOutputSchema() == null) {


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<Property> properties) {
Properties result = new Properties();

for (Property p : properties) {
for (Property p : properties)
result.put(p.getKey(), p.getValue());
}

return result;
}
Expand All @@ -592,13 +584,13 @@ private Class<? extends Database> databaseClass(Connection c) {
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<? extends Database> databaseClass(String url) {
if (isBlank(url))
throw new RuntimeException("No JDBC URL configured.");
throw new GeneratorException("No JDBC URL configured.");

Class<? extends Database> result = Databases.databaseClass(JDBCUtils.dialect(url));
log.info("Database", "Inferring database " + result.getName() + " from URL " + url);
Expand Down Expand Up @@ -642,18 +634,6 @@ private static String trim(String string) {
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 <configuration-file>");
System.exit(-1);
}

/**
* Copy bytes from a large (over 2GB) <code>InputStream</code> to an
* <code>OutputStream</code>.
Expand Down Expand Up @@ -716,7 +696,7 @@ public boolean handleEvent(ValidationEvent event) {
return (Configuration) unmarshaller.unmarshal(new StringReader(xml));
}
catch (Exception e) {
throw new RuntimeException(e);
throw new GeneratorException("Error while reading XML configuration", e);
}
}
}

0 comments on commit 5976d70

Please sign in to comment.