Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit f9081d1

Browse files
committed
log4jdbc driver support for Jdbc module fix jooby-project#972
1 parent 1e6dff6 commit f9081d1

File tree

5 files changed

+175
-53
lines changed

5 files changed

+175
-53
lines changed

modules/jooby-jdbc/pom.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
45

56
<parent>
67
<groupId>org.jooby</groupId>
@@ -100,6 +101,21 @@
100101
<scope>test</scope>
101102
</dependency>
102103

104+
<!-- log4jdbc -->
105+
<dependency>
106+
<groupId>com.googlecode.log4jdbc</groupId>
107+
<artifactId>log4jdbc</artifactId>
108+
<version>1.2</version>
109+
<scope>test</scope>
110+
</dependency>
111+
112+
<!-- ASM -->
113+
<dependency>
114+
<groupId>org.ow2.asm</groupId>
115+
<artifactId>asm</artifactId>
116+
<scope>test</scope>
117+
</dependency>
118+
103119
</dependencies>
104120

105121
</project>

modules/jooby-jdbc/src/main/java/org/jooby/jdbc/Jdbc.java

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,21 @@
204204
package org.jooby.jdbc;
205205

206206
import com.google.common.base.CharMatcher;
207-
208207
import static com.google.common.base.Preconditions.checkArgument;
209-
210208
import com.google.common.base.Splitter;
211-
import com.google.common.base.Strings;
212209
import com.google.inject.Binder;
213210
import com.google.inject.Key;
214211
import com.google.inject.name.Names;
215212
import com.typesafe.config.Config;
213+
import com.typesafe.config.ConfigException;
216214
import com.typesafe.config.ConfigFactory;
217215
import com.typesafe.config.ConfigObject;
218216
import com.typesafe.config.ConfigValue;
219217
import com.typesafe.config.ConfigValueFactory;
220218
import com.typesafe.config.ConfigValueType;
221219
import com.zaxxer.hikari.HikariConfig;
222220
import com.zaxxer.hikari.HikariDataSource;
223-
224221
import static java.util.Objects.requireNonNull;
225-
226222
import org.jooby.Env;
227223
import org.jooby.Jooby;
228224
import org.jooby.funzy.Throwing;
@@ -240,6 +236,7 @@
240236
import java.util.function.BiConsumer;
241237
import java.util.function.Consumer;
242238
import java.util.function.Function;
239+
import java.util.stream.Stream;
243240

244241
/**
245242
* <h1>jdbc</h1>
@@ -578,6 +575,7 @@ private Config dbConfig(final String key, final Config source) {
578575
return Try.apply(() -> source.getConfig("databases." + db))
579576
.map(it -> {
580577
// Rewrite embedded db
578+
it = it.getConfig("dataSource");
581579
Config dbtree = it.withValue("url", ConfigValueFactory.fromAnyRef(
582580
it.getString("url").replace("{mem.seed}", System.currentTimeMillis() + "")));
583581
// write embedded with current key
@@ -605,16 +603,15 @@ private HikariConfig hikariConfig(final String url, final String key, final Stri
605603
props.setProperty(propertyName, propertyValue);
606604
};
607605

608-
Throwing.Function<String, Config> dbconf = Throwing.<String, Config>throwingFunction(
609-
path -> conf.getConfig(path))
606+
Throwing.Function<String, Config> dbconf = Throwing.<String, Config>throwingFunction(path -> conf.getConfig(path))
610607
.orElse(ConfigFactory.empty());
611608

612609
Config $hikari = dbconf.apply(key + ".hikari")
613610
.withFallback(dbconf.apply("db." + db + ".hikari"))
614611
.withFallback(dbconf.apply("hikari"));
615612

616613
// figure it out db type.
617-
dbtype = dbtype(url, conf);
614+
dbtype = dbtype(url);
618615

619616
/**
620617
* dump properties from less to higher precedence
@@ -623,24 +620,20 @@ private HikariConfig hikariConfig(final String url, final String key, final Stri
623620
* # db.* -> dataSource.*
624621
* # hikari.* -> * (no prefix)
625622
*/
626-
dbtype.ifPresent(type -> dbconf(conf, type)
627-
.entrySet().forEach(entry -> dumper.accept("dataSource.", entry)));
623+
Stream.of(url.split(":"))
624+
.forEach(type -> dbconf(conf, type)
625+
.entrySet().forEach(entry -> dumper.accept("", entry)));
628626

629627
dbconf.apply(key)
630628
.withoutPath("hikari")
631629
.entrySet().forEach(entry -> dumper.accept("dataSource.", entry));
632630

633631
$hikari.entrySet().forEach(entry -> dumper.accept("", entry));
634632

635-
String dataSourceClassName = props.getProperty("dataSourceClassName");
636-
if (Strings.isNullOrEmpty(dataSourceClassName)) {
637-
// adjust dataSourceClassName when missing
638-
dataSourceClassName = props.getProperty("dataSource.dataSourceClassName");
639-
props.setProperty("dataSourceClassName", dataSourceClassName);
633+
if (props.containsKey("driverClassName")) {
634+
props.remove("dataSourceClassName");
635+
props.setProperty("jdbcUrl", url);
640636
}
641-
642-
// remove dataSourceClassName under dataSource
643-
props.remove("dataSource.dataSourceClassName");
644637
// set pool name
645638
props.setProperty("poolName", dbtype.map(type -> type + "." + db).orElse(db));
646639

@@ -653,17 +646,21 @@ private HikariConfig hikariConfig(final String url, final String key, final Stri
653646

654647
@SuppressWarnings("unchecked")
655648
private Config dbconf(final Config conf, final String type) {
656-
String dbtype = "databases." + type;
657-
ConfigValue value = conf.getValue(dbtype);
658-
if (value.valueType() == ConfigValueType.OBJECT) {
659-
return ((ConfigObject) value).toConfig();
649+
try {
650+
String dbtype = "databases." + type;
651+
ConfigValue value = conf.getValue(dbtype);
652+
if (value.valueType() == ConfigValueType.OBJECT) {
653+
return ((ConfigObject) value).toConfig();
654+
}
655+
List<Config> list = (List<Config>) conf.getConfigList(dbtype);
656+
ClassLoader loader = getClass().getClassLoader();
657+
return list.stream()
658+
.filter(it -> dataSourcePresent(loader, it.getString("dataSourceClassName")))
659+
.findFirst()
660+
.orElse(list.get(0));
661+
} catch (ConfigException.Missing | ConfigException.BadPath x) {
662+
return ConfigFactory.empty();
660663
}
661-
List<Config> list = (List<Config>) conf.getConfigList(dbtype);
662-
ClassLoader loader = getClass().getClassLoader();
663-
return list.stream()
664-
.filter(it -> dataSourcePresent(loader, it.getString("dataSourceClassName")))
665-
.findFirst()
666-
.orElse(list.get(0));
667664
}
668665

669666
private boolean dataSourcePresent(final ClassLoader loader, final String className) {
@@ -684,9 +681,9 @@ protected void callback(final Object value, final Config conf) {
684681
}).recover(CCE::apply).get());
685682
}
686683

687-
private Optional<String> dbtype(final String url, final Config config) {
684+
private Optional<String> dbtype(final String url) {
688685
String type = Arrays.stream(url.toLowerCase().split(":"))
689-
.filter(token -> !(token.equals("jdbc") || token.equals("jtds")))
686+
.filter(token -> !(token.equals("jdbc") || token.equals("jtds")|| token.equals("log4jdbc")))
690687
.findFirst()
691688
.get();
692689

modules/jooby-jdbc/src/main/resources/org/jooby/jdbc/jdbc.conf

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ databases {
55
# connection templates
66
###############################################################################################
77
mem {
8-
url = "jdbc:h2:mem:{mem.seed};DB_CLOSE_DELAY=-1"
9-
user = sa
10-
password = ""
8+
dataSource.url = "jdbc:h2:mem:{mem.seed};DB_CLOSE_DELAY=-1"
9+
dataSource.user = sa
10+
dataSource.password = ""
1111
}
1212

1313
fs {
14-
url = "jdbc:h2:"${application.tmpdir}/${application.name}
15-
user = sa
16-
password = ""
14+
dataSource.url = "jdbc:h2:"${application.tmpdir}/${application.name}
15+
dataSource.user = sa
16+
dataSource.password = ""
1717
}
1818

1919
###############################################################################################
@@ -66,11 +66,11 @@ databases {
6666
}, {
6767
# v5.x
6868
dataSourceClassName = com.mysql.jdbc.jdbc2.optional.MysqlDataSource
69-
encoding = ${application.charset}
70-
cachePrepStmts = true
71-
prepStmtCacheSize = 250
72-
prepStmtCacheSqlLimit = 2048
73-
useServerPrepStmts = true
69+
dataSource.encoding = ${application.charset}
70+
dataSource.cachePrepStmts = true
71+
dataSource.prepStmtCacheSize = 250
72+
dataSource.prepStmtCacheSqlLimit = 2048
73+
dataSource.useServerPrepStmts = true
7474
}]
7575

7676
###############################################################################################
@@ -128,6 +128,14 @@ databases {
128128
sqlite {
129129
dataSourceClassName = org.sqlite.SQLiteDataSource
130130
}
131+
132+
###############################################################################################
133+
# log4jdbc
134+
# jdbc:log4jdbc:${dbtype}:${db}
135+
###############################################################################################
136+
log4jdbc {
137+
driverClassName = net.sf.log4jdbc.DriverSpy
138+
}
131139
}
132140

133141
##
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package jdbc;
2+
3+
import com.typesafe.config.ConfigFactory;
4+
import com.typesafe.config.ConfigValueFactory;
5+
import org.jooby.Jooby;
6+
import org.jooby.jdbc.Jdbc;
7+
8+
public class Log4jdbcApp extends Jooby {
9+
10+
{
11+
use(ConfigFactory.empty()
12+
.withValue("db.url", ConfigValueFactory
13+
.fromAnyRef("jdbc:log4jdbc:mysql://localhost/log4jdbc"))
14+
.withValue("db.user", ConfigValueFactory.fromAnyRef("root"))
15+
.withValue("db.password", ConfigValueFactory.fromAnyRef("")));
16+
17+
use(new Jdbc());
18+
}
19+
20+
public static void main(final String[] args) {
21+
run(Log4jdbcApp::new, args);
22+
}
23+
24+
}

0 commit comments

Comments
 (0)