net.leanix.dropkit.persistence.PersistenceModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leanix-dropkit-persistence Show documentation
Show all versions of leanix-dropkit-persistence Show documentation
Base functionality for persistence in leanIX dropwizard-based services
package net.leanix.dropkit.persistence;
import com.google.common.collect.ImmutableList;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import io.dropwizard.Configuration;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.db.DatabaseConfiguration;
import io.dropwizard.hibernate.HibernateBundle;
import io.dropwizard.hibernate.ScanningHibernateBundle;
import io.dropwizard.hibernate.SessionFactoryFactory;
import io.dropwizard.migrations.DbCommand;
import io.dropwizard.setup.Environment;
import org.hibernate.SessionFactory;
/**
* Guice module for persistence.
*
*
* @param
*/
public class PersistenceModule extends AbstractModule implements DatabaseConfiguration {
private HibernateBundle hibernateBundle;
private final Class configClass;
/**
* Pass the hibernate bundle to the
* constructor.
*
* new PersistenceModule(A.class, B.class, C.class)
*
* @param configClass
*/
public PersistenceModule(HibernateBundle hibernateBundle, Class configClass) {
super();
this.hibernateBundle = hibernateBundle;
this.configClass = configClass;
}
/**
* Provides the session factory for DAO objects.
*
* @param configuration
* @param environment
* @return
* @throws java.lang.Exception
*/
@Provides
public SessionFactory provideSessionFactory(
Configuration configuration,
Environment environment
) throws Exception {
SessionFactory sf = hibernateBundle.getSessionFactory();
if (sf == null) {
hibernateBundle.run(configuration, environment);
return hibernateBundle.getSessionFactory();
}
return sf;
}
//@Provides
public DbCommand getMigrationsCommand() {
return new DbCommand<>("db", this, configClass, null);
}
@Override
protected void configure() {
//bind(DataSourceFactoryProvider.class).to(configClass);
}
/**
* This method is required for the migration db command (module implements
* DatabaseConfiguration).
*
* @param t
* @return
*/
@Override
public DataSourceFactory getDataSourceFactory(T t) {
return t.getDataSourceFactory();
}
public Class getConfigClass() {
return configClass;
}
public void run(Environment environment, SessionFactory factory) {
environment.healthChecks().register("database", new DatabaseHealthCheck(factory));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy