From 07672a1432932ec4edfbebcd3718ae9822708d2f Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Thu, 28 Feb 2013 12:04:52 +0100 Subject: [PATCH] [#2283] Class loading issues in GenerationTool when called by Gradle - Allow for setting ClassLoader and/or Connection externally --- .../java/org/jooq/util/GenerationTool.java | 54 +++++++++++++------ 1 file changed, 37 insertions(+), 17 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 22ce0317e7..93b96e8639 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java +++ b/jOOQ-codegen/src/main/java/org/jooq/util/GenerationTool.java @@ -82,6 +82,28 @@ public class GenerationTool { private static final JooqLogger log = JooqLogger.getLogger(GenerationTool.class); + private static ClassLoader loader; + private static Connection connection; + + /** + * The class loader to use with this generation tool. + *

+ * If set, all classes are loaded with this class loader + */ + public static void setClassLoader(ClassLoader loader) { + GenerationTool.loader = loader; + } + + /** + * The JDBC connection to use with this generation tool. + *

+ * If set, the configuration XML's <jdbc/> configuration is + * ignored, and this connection is used for meta data inspection, instead. + */ + public static void setConnection(Connection connection) { + GenerationTool.connection = connection; + } + public static void main(String[] args) throws Exception { if (args.length < 1) { error(); @@ -112,12 +134,8 @@ public class GenerationTool { } } + @SuppressWarnings("unchecked") public static void main(Configuration configuration) throws Exception { - main(configuration, null); - } - - @SuppressWarnings("unchecked") - public static void main(Configuration configuration, ClassLoader loader) throws Exception { Jdbc j = configuration.getJdbc(); org.jooq.util.jaxb.Generator g = configuration.getGenerator(); @@ -127,24 +145,26 @@ public class GenerationTool { if (g.getTarget() == null) g.setTarget(new Target()); - loadClass(j.getDriver(), loader); - Connection connection = null; - try { // Initialise connection // --------------------- - Properties properties = new Properties(); - for (Property p : j.getProperties()) { - properties.put(p.getKey(), p.getValue()); + if (connection == null) { + loadClass(j.getDriver(), loader); + + Properties properties = new Properties(); + for (Property p : j.getProperties()) { + properties.put(p.getKey(), p.getValue()); + } + + if (!properties.containsKey("user")) + properties.put("user", defaultString(j.getUser())); + if (!properties.containsKey("password")) + properties.put("password", defaultString(j.getPassword())); + + connection = DriverManager.getConnection(defaultString(j.getUrl()), properties); } - if (!properties.containsKey("user")) - properties.put("user", defaultString(j.getUser())); - if (!properties.containsKey("password")) - properties.put("password", defaultString(j.getPassword())); - - connection = DriverManager.getConnection(defaultString(j.getUrl()), properties); // Initialise generator // --------------------