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

Commit 0782ea5

Browse files
author
edgar
committed
decouple jdbc module fix jooby-project#868
1 parent a7061ac commit 0782ea5

File tree

4 files changed

+107
-13
lines changed

4 files changed

+107
-13
lines changed

jooby/src/main/java/org/jooby/Env.java

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,20 @@ default Env build(final Config config) {
523523

524524
private Map<String, Function<String, String>> xss = new HashMap<>();
525525

526+
private Map<Object, Object> globals = new HashMap<>();
527+
526528
private ServiceKey key = new ServiceKey();
527529

530+
public <T> Env set(Key<T> key, T value) {
531+
globals.put(key, value);
532+
return this;
533+
}
534+
535+
public <T> Optional<T> get(Key<T> key) {
536+
T value = (T) globals.get(key);
537+
return Optional.ofNullable(value);
538+
}
539+
528540
@Override
529541
public String name() {
530542
return name;
@@ -652,11 +664,10 @@ default String resolve(final String text) {
652664
/**
653665
* Creates a new environment {@link Resolver}.
654666
*
655-
* @return
667+
* @return A resolver object.
656668
*/
657669
default Resolver resolver() {
658-
return new Resolver()
659-
.source(config());
670+
return new Resolver().source(config());
660671
}
661672

662673
/**
@@ -719,4 +730,45 @@ default Function<String, String> xss(final String... xss) {
719730
*/
720731
List<Throwing.Consumer<Registry>> stopTasks();
721732

733+
/**
734+
* Add a global object.
735+
*
736+
* @param key Object key.
737+
* @param value Object value.
738+
* @param <T> Object type.
739+
* @return This environment.
740+
*/
741+
<T> Env set(Key<T> key, T value);
742+
743+
/**
744+
* Add a global object.
745+
*
746+
* @param key Object key.
747+
* @param value Object value.
748+
* @param <T> Object type.
749+
* @return This environment.
750+
*/
751+
default <T> Env set(Class<T> key, T value) {
752+
return set(Key.get(key), value);
753+
}
754+
755+
/**
756+
* Get an object by key or empty when missing.
757+
*
758+
* @param key Object key.
759+
* @param <T> Object type.
760+
* @return Object valur or empty.
761+
*/
762+
<T> Optional<T> get(Key<T> key);
763+
764+
/**
765+
* Get an object by key or empty when missing.
766+
*
767+
* @param key Object key.
768+
* @param <T> Object type.
769+
* @return Object valur or empty.
770+
*/
771+
default <T> Optional<T> get(Class<T> key) {
772+
return get(Key.get(key));
773+
}
722774
}

jooby/src/test/java/org/jooby/EnvTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jooby;
22

33
import com.google.common.collect.ImmutableMap;
4+
import com.google.inject.Key;
45
import com.typesafe.config.Config;
56
import com.typesafe.config.ConfigFactory;
67
import com.typesafe.config.ConfigValueFactory;
@@ -113,6 +114,17 @@ public void novars() {
113114
assertEquals("var", env.resolve("var"));
114115
}
115116

117+
@Test
118+
public void globalObject() {
119+
Config config = ConfigFactory.empty()
120+
.withValue("var", ConfigValueFactory.fromAnyRef("foo.bar"));
121+
122+
Env env = Env.DEFAULT.build(config);
123+
Object value = new Object();
124+
env.set(Object.class, value);
125+
assertEquals(value, env.get(Object.class));
126+
}
127+
116128
@Test
117129
public void serviceKey() {
118130
Config config = ConfigFactory.empty()
@@ -122,6 +134,13 @@ public void serviceKey() {
122134
assertNotNull(env.serviceKey());
123135

124136
assertNotNull(new Env() {
137+
@Override public <T> Env set(Key<T> key, T value) {
138+
throw new UnsupportedOperationException();
139+
}
140+
141+
@Override public <T> Optional<T> get(Key<T> key) {
142+
throw new UnsupportedOperationException();
143+
}
125144

126145
@Override
127146
public LifeCycle onStart(final Throwing.Consumer<Registry> task) {

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@
204204
package org.jooby.jdbc;
205205

206206
import com.google.common.base.CharMatcher;
207+
207208
import static com.google.common.base.Preconditions.checkArgument;
209+
208210
import com.google.common.base.Splitter;
209211
import com.google.common.base.Strings;
210212
import com.google.inject.Binder;
@@ -216,7 +218,9 @@
216218
import com.typesafe.config.ConfigValueType;
217219
import com.zaxxer.hikari.HikariConfig;
218220
import com.zaxxer.hikari.HikariDataSource;
221+
219222
import static java.util.Objects.requireNonNull;
223+
220224
import org.jooby.Env;
221225
import org.jooby.Jooby;
222226
import org.jooby.funzy.Throwing;
@@ -512,12 +516,6 @@ public <T> Jdbc doWith(final Consumer<T> configurer) {
512516

513517
@Override
514518
public void configure(final Env env, final Config config, final Binder binder) {
515-
configure(env, config, binder, (name, ds) -> {
516-
});
517-
}
518-
519-
protected void configure(final Env env, final Config config, final Binder binder,
520-
final BiConsumer<String, HikariDataSource> extensions) {
521519
Config dbconf;
522520
String url, dbname, dbkey;
523521
boolean seturl = false;
@@ -544,10 +542,16 @@ protected void configure(final Env env, final Config config, final Binder binder
544542
callback(hikariConf, config);
545543
HikariDataSource ds = new HikariDataSource(hikariConf);
546544

547-
extensions.accept(dbname, ds);
548-
549-
env.serviceKey()
550-
.generate(DataSource.class, dbname, k -> binder.bind(k).toInstance(ds));
545+
if (!dbkey.equals(dbname)) {
546+
env.serviceKey().generate(DataSource.class, dbkey, k -> {
547+
binder.bind(k).toInstance(ds);
548+
env.set(k, ds);
549+
});
550+
}
551+
env.serviceKey().generate(DataSource.class, dbname, k -> {
552+
binder.bind(k).toInstance(ds);
553+
env.set(k, ds);
554+
});
551555

552556
env.onStop(ds::close);
553557
}

modules/jooby-jdbc/src/test/java/org/jooby/jdbc/JdbcTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void memdb() throws Exception {
7070
"sa", "", false))
7171
.expect(hikariConfig())
7272
.expect(hikariDataSource())
73+
.expect(serviceKey("db"))
7374
.expect(serviceKey("123"))
7475
.expect(onStop)
7576
.run(unit -> {
@@ -92,6 +93,7 @@ public void fsdb() throws Exception {
9293
"sa", "", false))
9394
.expect(hikariConfig())
9495
.expect(hikariDataSource())
96+
.expect(serviceKey("db"))
9597
.expect(serviceKey("jdbctest"))
9698
.expect(onStop)
9799
.expect(unit -> {
@@ -136,6 +138,7 @@ public void dbWithCallback() throws Exception {
136138
.expect(hikariConfig())
137139
.expect(hikariDataSource())
138140
.expect(serviceKey("jdbctest"))
141+
.expect(serviceKey("db"))
139142
.expect(onStop)
140143
.expect(unit -> {
141144
HikariConfig h = unit.get(HikariConfig.class);
@@ -192,6 +195,7 @@ public void derby() throws Exception {
192195
.expect(hikariConfig())
193196
.expect(hikariDataSource())
194197
.expect(serviceKey("testdb"))
198+
.expect(serviceKey("db"))
195199
.expect(onStop)
196200
.run(unit -> {
197201
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -244,6 +248,7 @@ public void db2() throws Exception {
244248
.expect(hikariConfig())
245249
.expect(hikariDataSource())
246250
.expect(serviceKey("SAMPLE"))
251+
.expect(serviceKey("db"))
247252
.expect(onStop)
248253
.run(unit -> {
249254
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -267,6 +272,7 @@ public void hsql() throws Exception {
267272
.expect(hikariConfig())
268273
.expect(hikariDataSource())
269274
.expect(serviceKey("file"))
275+
.expect(serviceKey("db"))
270276
.expect(onStop)
271277
.run(unit -> {
272278
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -384,6 +390,7 @@ public void setHikariOptions() throws Exception {
384390
.expect(hikariConfig())
385391
.expect(hikariDataSource())
386392
.expect(serviceKey("jdbctest"))
393+
.expect(serviceKey("db"))
387394
.expect(onStop)
388395
.run(unit -> {
389396
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -412,6 +419,7 @@ public void overrideDataSource() throws Exception {
412419
.expect(hikariConfig())
413420
.expect(hikariDataSource())
414421
.expect(serviceKey("jdbctest"))
422+
.expect(serviceKey("db"))
415423
.expect(onStop)
416424
.run(unit -> {
417425
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -444,6 +452,7 @@ public void twoDatabases() throws Exception {
444452
.expect(hikariConfig())
445453
.expect(hikariDataSource())
446454
.expect(serviceKey("audit"))
455+
.expect(serviceKey("db.audit"))
447456
.expect(onStop)
448457
.run(unit -> {
449458
new Jdbc("db.audit").configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -470,6 +479,7 @@ public void sqlserver() throws Exception {
470479
.expect(hikariConfig())
471480
.expect(hikariDataSource())
472481
.expect(serviceKey("AdventureWorks"))
482+
.expect(serviceKey("db"))
473483
.expect(onStop)
474484
.run(unit -> {
475485
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -494,6 +504,7 @@ public void oracle() throws Exception {
494504
.expect(hikariConfig())
495505
.expect(hikariDataSource())
496506
.expect(serviceKey("orcl"))
507+
.expect(serviceKey("db"))
497508
.expect(onStop)
498509
.run(unit -> {
499510
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -518,6 +529,7 @@ public void pgsql() throws Exception {
518529
.expect(hikariConfig())
519530
.expect(hikariDataSource())
520531
.expect(serviceKey("database"))
532+
.expect(serviceKey("db"))
521533
.expect(onStop)
522534
.run(unit -> {
523535
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -541,6 +553,7 @@ public void postgresql() throws Exception {
541553
.expect(hikariConfig())
542554
.expect(hikariDataSource())
543555
.expect(serviceKey("database"))
556+
.expect(serviceKey("db"))
544557
.expect(onStop)
545558
.run(unit -> {
546559
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -564,6 +577,7 @@ public void sybase() throws Exception {
564577
.expect(hikariConfig())
565578
.expect(hikariDataSource())
566579
.expect(serviceKey("database"))
580+
.expect(serviceKey("db"))
567581
.expect(onStop)
568582
.run(unit -> {
569583
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -587,6 +601,7 @@ public void firebirdsql() throws Exception {
587601
.expect(hikariConfig())
588602
.expect(hikariDataSource())
589603
.expect(serviceKey("mydb"))
604+
.expect(serviceKey("db"))
590605
.expect(onStop)
591606
.run(unit -> {
592607
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -610,6 +625,7 @@ public void sqlite() throws Exception {
610625
.expect(hikariConfig())
611626
.expect(hikariDataSource())
612627
.expect(serviceKey("testdb"))
628+
.expect(serviceKey("db"))
613629
.expect(onStop)
614630
.run(unit -> {
615631
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -635,6 +651,7 @@ public void unknownDb() throws Exception {
635651
.expect(hikariConfig())
636652
.expect(hikariDataSource())
637653
.expect(serviceKey("testdb"))
654+
.expect(serviceKey("db"))
638655
.expect(onStop)
639656
.run(unit -> {
640657
new Jdbc().configure(unit.get(Env.class), dbconf, unit.get(Binder.class));
@@ -654,6 +671,8 @@ private Block serviceKey(final String db) {
654671
Binder binder = unit.get(Binder.class);
655672
expect(binder.bind(Key.get(DataSource.class))).andReturn(binding);
656673
expect(binder.bind(Key.get(DataSource.class, Names.named(db)))).andReturn(binding);
674+
expect(env.set(Key.get(DataSource.class), unit.get(HikariDataSource.class))).andReturn(env);
675+
expect(env.set(Key.get(DataSource.class, Names.named(db)), unit.get(HikariDataSource.class))).andReturn(env);
657676
};
658677
}
659678

0 commit comments

Comments
 (0)