Skip to content

Commit

Permalink
Let ebean module support multiple named dataSources fix #771
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed May 24, 2017
1 parent 74e5823 commit 965c72e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
33 changes: 22 additions & 11 deletions jooby-ebean/src/main/java/org/jooby/ebean/Ebeanby.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,26 @@
*/
package org.jooby.ebean;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.function.Consumer;

import org.jooby.Env;
import org.jooby.Env.ServiceKey;
import org.jooby.internal.ebean.EbeanEnhancer;
import org.jooby.internal.ebean.EbeanManaged;
import org.jooby.jdbc.Jdbc;

import com.google.inject.Binder;
import com.google.inject.Key;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

import io.ebean.EbeanServer;
import io.ebean.config.ContainerConfig;
import io.ebean.config.ServerConfig;
import org.jooby.Env;
import org.jooby.internal.ebean.EbeanEnhancer;
import org.jooby.internal.ebean.EbeanManaged;
import org.jooby.jdbc.Jdbc;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

/**
* <h1>ebean module</h1>
Expand Down Expand Up @@ -218,8 +223,14 @@ public void configure(final Env env, final Config conf, final Binder binder) {
EbeanManaged server = new EbeanManaged(conf, config);
env.onStart(server::start);
env.onStop(server::stop);
env.serviceKey().generate(EbeanServer.class, name,
k -> binder.bind(k).toProvider(server).asEagerSingleton());
/** Bind db key: */
Consumer<Key<EbeanServer>> provider = k -> binder.bind(k).toProvider(server)
.asEagerSingleton();
ServiceKey keys = env.serviceKey();
if (!name.equals(dbref)) {
keys.generate(EbeanServer.class, dbref, provider);
}
keys.generate(EbeanServer.class, name, provider);
});
}

Expand Down
49 changes: 49 additions & 0 deletions jooby-ebean/src/test/java/org/jooby/ebean/EbeanbyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,55 @@ public void configure() throws Exception {
});
}

@SuppressWarnings("unchecked")
@Test
public void configureWith2Names() throws Exception {
new MockUnit(Env.class, Binder.class)
.expect(props("com.ibm.db2.jcc.DB2SimpleDataSource", "jdbc:db2://127.0.0.1/mydb",
"db2.mydb", null, "", false))
.expect(hikariConfig())
.expect(hikariDataSource())
.expect(serviceKey("mydb"))
.expect(containerConfig)
.expect(unit -> {
ServerConfig serverConfig = unit.mockConstructor(ServerConfig.class);

serverConfig.setName("mydb");
serverConfig.addPackage("my.model");
serverConfig.setContainerConfig(unit.get(ContainerConfig.class));
serverConfig.setDataSource(isA(DataSource.class));
serverConfig.loadFromProperties(isA(Properties.class));
serverConfig.setDefaultServer(true);
serverConfig.setRegister(true);

unit.registerMock(ServerConfig.class, serverConfig);
})
.expect(enhancer("my.model"))
.expect(ebeanProperties())
.expect(unit -> {
Binder binder = unit.get(Binder.class);

ScopedBindingBuilder sbbES = unit.mock(ScopedBindingBuilder.class);
sbbES.asEagerSingleton();
sbbES.asEagerSingleton();
sbbES.asEagerSingleton();

LinkedBindingBuilder<EbeanServer> lbbES = unit.mock(LinkedBindingBuilder.class);
expect(lbbES.toProvider(isA(EbeanManaged.class))).andReturn(sbbES).times(3);

expect(binder.bind(Key.get(EbeanServer.class))).andReturn(lbbES);
expect(binder.bind(Key.get(EbeanServer.class, Names.named("db")))).andReturn(lbbES);
expect(binder.bind(Key.get(EbeanServer.class, Names.named("mydb")))).andReturn(lbbES);
})
.expect(onStop)
.run(unit -> {
new Ebeanby("db")
.configure(unit.get(Env.class), config()
.withValue("db", ConfigValueFactory.fromAnyRef("jdbc:db2://127.0.0.1/mydb")),
unit.get(Binder.class));
});
}

@Test
public void configureWithPackages() throws Exception {
new MockUnit(Env.class, Binder.class)
Expand Down
2 changes: 1 addition & 1 deletion jooby-jdbc/src/main/java/org/jooby/jdbc/Jdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public class Jdbc implements Jooby.Module {
@SuppressWarnings("rawtypes")
private final List<BiConsumer> callback = new ArrayList<>();

private final String dbref;
protected final String dbref;

protected Optional<String> dbtype;

Expand Down

0 comments on commit 965c72e

Please sign in to comment.