Skip to content
This repository has been archived by the owner on Sep 13, 2021. It is now read-only.

Commit

Permalink
Update bundle and JDBI interfaces, update gradlew, add example applic…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
alex-shpak committed Aug 3, 2018
1 parent 537519d commit b325372
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 62 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.gradle
.idea
build
out

*.iml
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ dependencies {
compileOnly "io.dropwizard:dropwizard-jdbi:$dropwizardVersion"

testCompile "junit:junit:$junitVersion"
testCompile "com.h2database:h2:1.4.197"
testCompile "io.dropwizard:dropwizard-jdbi:$dropwizardVersion"
}

compileJava {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ database:
driverClass: org.h2.Driver
user: test
password: test
url: jdbc:h2:mem
url: jdbc:h2:mem:db
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.winterly.dropwizard.hk2bundle.jdbi;

import org.glassfish.hk2.api.Factory;
import org.glassfish.jersey.process.internal.RequestScoped;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

@Singleton
public class HandleFactory implements Factory<Handle> {

private final Provider<DBI> dbi;

@Inject
public HandleFactory(Provider<DBI> dbi) {
this.dbi = dbi;
}

@Override
@RequestScoped
public Handle provide() {
return dbi.get().open();
}

@Override
public void dispose(Handle instance) {
instance.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.dropwizard.db.DatabaseConfiguration;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.hk2.utilities.binding.ServiceBindingBuilder;
import org.skife.jdbi.v2.DBI;

import javax.inject.Singleton;
Expand All @@ -13,41 +12,69 @@

import static java.util.Objects.requireNonNull;

/**
* HK2 binder used to configure sql objects injection
*
* @param <T> application configuration type that contains {@link io.dropwizard.db.PooledDataSourceFactory}
*/
public class JDBIBinder<T extends Configuration> extends AbstractBinder {

private Class<? extends Factory<DBI>> dbiFactory = JDBIFactory.class;
private Class<? extends Factory<Object>> sqlObjectFactory = SqlObjectFactory.class;

private DatabaseConfiguration<T> databaseConfiguration;
private DatabaseConfiguration databaseConfiguration;
private HashSet<Class<?>> sqlInterfaces = new HashSet<>();

public JDBIBinder<T> setDatabaseConfiguration(DatabaseConfiguration<T> databaseConfiguration) {
/**
* @param databaseConfiguration database configuration provider
*/
public JDBIBinder(DatabaseConfiguration<T> databaseConfiguration) {
this.databaseConfiguration = requireNonNull(databaseConfiguration);
return this;
}

/**
* Set factory that creates DBI instance
*
* @param dbiFactory dbi factory type
* @return self
*/
public JDBIBinder<T> setDBIFactory(Class<? extends Factory<DBI>> dbiFactory) {
this.dbiFactory = requireNonNull(dbiFactory);
return this;
}

/**
* Set SQL object proxy factory
*
* @param sqlObjectFactory proxy factory type
* @return self
*/
public JDBIBinder<T> setSqlObjectFactory(Class<? extends Factory<Object>> sqlObjectFactory) {
this.sqlObjectFactory = requireNonNull(sqlObjectFactory);
return this;
}

public JDBIBinder<T> addSqlObjects(Class<?>... interfaces) {
/**
* Register list of JDBI interfaces
*
* @param interfaces interfaces
* @return self
*/
public JDBIBinder<T> register(Class<?>... interfaces) {
Collections.addAll(sqlInterfaces, interfaces);
return this;
}

@Override
protected void configure() {
bind(databaseConfiguration).to(DatabaseConfiguration.class);
bindFactory(dbiFactory).to(DBI.class).in(Singleton.class);

ServiceBindingBuilder<?> binding = bindFactory(sqlObjectFactory);
sqlInterfaces.forEach(binding::to);
binding.in(Singleton.class);
addActiveFactoryDescriptor(dbiFactory);
addActiveFactoryDescriptor(HandleFactory.class);

sqlInterfaces.forEach(type -> bindFactory(sqlObjectFactory)
.to(type)
.in(Singleton.class)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,38 @@

import io.dropwizard.Configuration;
import io.dropwizard.db.DatabaseConfiguration;
import io.dropwizard.db.PooledDataSourceFactory;
import io.dropwizard.jdbi.DBIFactory;
import io.dropwizard.setup.Environment;
import org.glassfish.hk2.api.Factory;
import org.skife.jdbi.v2.DBI;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class JDBIFactory implements Factory<DBI> {

private final DBIFactory factory = new DBIFactory();
private final String name = getClass().getSimpleName();

@Inject
private Environment environment;

@Inject
private Configuration configuration;
private final Environment environment;
private final Configuration configuration;
private final DatabaseConfiguration<Configuration> databaseConfiguration;

@Inject
private DatabaseConfiguration<Configuration> databaseConfiguration;
public JDBIFactory(Environment environment, Configuration configuration,
DatabaseConfiguration<Configuration> databaseConfiguration) {
this.environment = environment;
this.configuration = configuration;
this.databaseConfiguration = databaseConfiguration;
}

@Override
@Singleton
public DBI provide() {
return factory.build(
environment,
databaseConfiguration.getDataSourceFactory(configuration),
name
);
PooledDataSourceFactory dataSourceFactory = databaseConfiguration.getDataSourceFactory(configuration);
return factory.build(environment, dataSourceFactory, name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,31 @@
import org.skife.jdbi.v2.DBI;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

@Singleton
public class SqlObjectFactory implements Factory<Object> {

@Inject
private InstantiationService instantiationService;
private final InstantiationService instantiationService;
private final Provider<DBI> dbi;

@Inject
private DBI dbi;
public SqlObjectFactory(InstantiationService instantiationService, Provider<DBI> dbi) {
this.instantiationService = instantiationService;
this.dbi = dbi;
}

@Override
public Object provide() {
Injectee injectee = instantiationService.getInstantiationData().getParentInjectee();
Class<?> daoInterface = (Class) injectee.getRequiredType();
return dbi.onDemand(daoInterface);

return dbi.get().onDemand(daoInterface);
}

@Override
public void dispose(Object instance) {
dbi.close(instance);
dbi.get().close(instance);
}
}
13 changes: 7 additions & 6 deletions src/test/java/net/winterly/dropwizard/hk2bundle/ExampleApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import net.winterly.dropwizard.hk2bundle.jdbi.ExampleDAO;
import net.winterly.dropwizard.hk2bundle.jdbi.JDBIBinder;
import net.winterly.dropwizard.hk2bundle.jdbi.JDBIFactory;
import net.winterly.dropwizard.hk2bundle.jdbi.SqlObjectFactory;
Expand All @@ -12,15 +13,15 @@ public class ExampleApp extends Application<ExampleAppConfiguration> {

@Override
public void initialize(Bootstrap<ExampleAppConfiguration> bootstrap) {
JDBIBinder jdbiBinder = new JDBIBinder<ExampleAppConfiguration>()
ExampleAppBinder appBinder = new ExampleAppBinder();
JDBIBinder jdbiBinder = new JDBIBinder<ExampleAppConfiguration>(configuration -> configuration.database)
.setDBIFactory(JDBIFactory.class)
.setSqlObjectFactory(SqlObjectFactory.class)
.setDatabaseConfiguration(configuration -> configuration.database);

HK2Bundle hk2Bundle = new HK2Bundle(
new ExampleAppBinder(),
jdbiBinder
);
.register(ExampleDAO.class);


HK2Bundle hk2Bundle = new HK2Bundle(appBinder, jdbiBinder);
bootstrap.addBundle(hk2Bundle);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package net.winterly.dropwizard.hk2bundle;

import net.winterly.dropwizard.hk2bundle.health.ExampleHealthCheck;
import net.winterly.dropwizard.hk2bundle.jdbi.ExampleDAO;
import net.winterly.dropwizard.hk2bundle.jdbi.SqlObjectFactory;
import net.winterly.dropwizard.hk2bundle.metric.ExampleGauge;
import net.winterly.dropwizard.hk2bundle.spi.DropwizardBinder;
import net.winterly.dropwizard.hk2bundle.validation.InjectValidatorBundle;

import javax.inject.Singleton;

public class ExampleAppBinder extends DropwizardBinder {
@Override
protected void configure() {
bundle(InjectValidatorBundle.class);

metric(ExampleGauge.class);
healthCheck(ExampleHealthCheck.class);

bindFactory(SqlObjectFactory.class)
.to(ExampleDAO.class)
.in(Singleton.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package net.winterly.dropwizard.hk2bundle.jdbi;

import org.glassfish.hk2.api.UseProxy;
import org.skife.jdbi.v2.sqlobject.Bind;
import org.skife.jdbi.v2.sqlobject.SqlQuery;

@UseProxy
public interface ExampleDAO {

@SqlQuery("SELECT :i")
int selectNumber(int i);
int selectNumber(@Bind("i") int i);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import net.winterly.dropwizard.hk2bundle.jdbi.ExampleDAO;

import javax.inject.Inject;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -18,21 +14,9 @@ public class ExampleResource {
@Inject
private ExampleDAO dao;

@POST
@GET
@Path("ping")
public Pong ping(@Valid Ping ping) {
return new Pong(ping);
}

public static class Ping {
public int value;
}

public static class Pong {
public final int value;

public Pong(Ping ping) {
this.value = ping.value;
}
public int ping(@QueryParam("value") int value) {
return dao.selectNumber(value);
}
}

0 comments on commit b325372

Please sign in to comment.