new GenerationTool().run(configuration);
}
@SuppressWarnings("unchecked")
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.");
// Some default values for optional elements to avoid NPE's
if (g.getStrategy() == null)
g.setStrategy(new Strategy());
if (g.getTarget() == null)
g.setTarget(new Target());
try {
// Initialise connection
// ---------------------
if (connection == null && j != null) {
Class<? extends Driver> driver = (Class<? extends Driver>) loadClass(driverClass(j));
Properties properties = properties(j.getProperties());
if (!properties.containsKey("user"))
properties.put("user", defaultString(j.getUser()));
if (!properties.containsKey("password"))
properties.put("password", defaultString(j.getPassword()));
connection = driver.newInstance().connect(defaultString(j.getUrl()), properties);
close = true;
}
else {
j = defaultIfNull(j, new Jdbc());
}
// Initialise generator
// --------------------
Class<Generator> generatorClass = (Class<Generator>) (!isBlank(g.getName())
? loadClass(trim(g.getName()))
: JavaGenerator.class);
Generator generator = generatorClass.newInstance();
GeneratorStrategy strategy;
Matchers matchers = g.getStrategy().getMatchers();
if (matchers != null) {
strategy = new MatcherStrategy(matchers);
if (g.getStrategy().getName() != null) {
log.warn("WARNING: Matchers take precedence over custom strategy. Strategy ignored: " +
g.getStrategy().getName());
}
}
else {
Class<GeneratorStrategy> strategyClass = (Class<GeneratorStrategy>) (!isBlank(g.getStrategy().getName())
? loadClass(trim(g.getStrategy().getName()))
: DefaultGeneratorStrategy.class);
strategy = strategyClass.newInstance();
}
generator.setStrategy(strategy);
org.jooq.util.jaxb.Database d = defaultIfNull(g.getDatabase(), new org.jooq.util.jaxb.Database());
String databaseName = trim(d.getName());
Class<? extends Database> databaseClass = isBlank(databaseName)
? databaseClass(j)
: (Class<? extends Database>) loadClass(databaseName);
Database database = databaseClass.newInstance();
database.setProperties(properties(d.getProperties()));
List<Schema> schemata = d.getSchemata();
// For convenience and backwards-compatibility, the schema configuration can be set also directly
// in the <database/> element
if (schemata.isEmpty()) {
Schema schema = new Schema();
schema.setInputSchema(trim(d.getInputSchema()));
schema.setOutputSchema(trim(d.getOutputSchema()));
schema.setOutputSchemaToDefault(d.isOutputSchemaToDefault());
schemata.add(schema);
}
else {
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())) {
log.warn("WARNING: Cannot combine configuration properties /configuration/generator/database/outputSchema and /configuration/generator/database/schemata");
}
}
for (Schema schema : schemata) {
if (StringUtils.isBlank(schema.getInputSchema())) {
if (!StringUtils.isBlank(j.getSchema())) {
log.warn("WARNING: The configuration property jdbc.Schema is deprecated and will be removed in the future. Use /configuration/generator/database/inputSchema instead");
}
schema.setInputSchema(trim(j.getSchema()));
}
// [#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.