4545import com .google .inject .Binder ;
4646import com .typesafe .config .Config ;
4747import com .typesafe .config .ConfigFactory ;
48+ import com .typesafe .config .ConfigObject ;
4849import com .typesafe .config .ConfigValue ;
4950import com .typesafe .config .ConfigValueFactory ;
51+ import com .typesafe .config .ConfigValueType ;
5052import com .zaxxer .hikari .HikariConfig ;
5153import com .zaxxer .hikari .HikariDataSource ;
5254
@@ -247,7 +249,7 @@ public class Jdbc implements Jooby.Module {
247249 Map <String , String > params = new HashMap <>(result ._2 );
248250 result = indexOf .apply (result ._1 , ";" , ";" );
249251 params .putAll (result ._2 );
250- List <String > parts = Splitter .on (CharMatcher .JAVA_LETTER_OR_DIGIT .negate ())
252+ List <String > parts = Splitter .on (CharMatcher .javaLetterOrDigit () .negate ())
251253 .trimResults ()
252254 .omitEmptyStrings ()
253255 .splitToList (result ._1 );
@@ -429,7 +431,7 @@ private HikariConfig hikariConfig(final String url, final String key, final Stri
429431 * # db.* -> dataSource.*
430432 * # hikari.* -> * (no prefix)
431433 */
432- dbtype .ifPresent (type -> config . getConfig ( "databases." + type )
434+ dbtype .ifPresent (type -> dbconf ( config , type )
433435 .entrySet ().forEach (entry -> dumper .accept ("dataSource." , entry )));
434436
435437 dbconf .apply (key )
@@ -453,6 +455,30 @@ private HikariConfig hikariConfig(final String url, final String key, final Stri
453455 return new HikariConfig (props );
454456 }
455457
458+ @ SuppressWarnings ("unchecked" )
459+ private Config dbconf (final Config conf , final String type ) {
460+ String dbtype = "databases." + type ;
461+ ConfigValue value = conf .getValue (dbtype );
462+ if (value .valueType () == ConfigValueType .OBJECT ) {
463+ return ((ConfigObject ) value ).toConfig ();
464+ }
465+ List <Config > list = (List <Config >) conf .getConfigList (dbtype );
466+ ClassLoader loader = getClass ().getClassLoader ();
467+ return list .stream ()
468+ .filter (it -> dataSourcePresent (loader , it .getString ("dataSourceClassName" )))
469+ .findFirst ()
470+ .orElse (list .get (0 ));
471+ }
472+
473+ private boolean dataSourcePresent (final ClassLoader loader , final String className ) {
474+ try {
475+ loader .loadClass (className .trim ());
476+ return true ;
477+ } catch (ClassNotFoundException e ) {
478+ return false ;
479+ }
480+ }
481+
456482 @ SuppressWarnings ("unchecked" )
457483 protected void callback (final Object value , final Config conf ) {
458484 this .callback .forEach (it -> Try .run (() -> it .accept (value , conf ))
0 commit comments