com.guicedee.guicedpersistence.services.PersistenceServicesModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of guiced-persistence Show documentation
Show all versions of guiced-persistence Show documentation
A complete JPA 2.1 implementation for Standalone or EE Implementation. Enables Multiple Persistence units
with full JTA Support using BTM. Perfect for Guice implementations, test suites, and Guice enabled Web Applications or EAR Projects.
Requires JDK 8
package com.guicedee.guicedpersistence.services;
import com.google.inject.AbstractModule;
import com.google.inject.Key;
import com.google.inject.Module;
import com.guicedee.guicedinjection.interfaces.IGuiceModule;
import com.guicedee.guicedpersistence.db.ConnectionBaseInfo;
import com.guicedee.logger.LogFactory;
import javax.sql.DataSource;
import java.lang.annotation.Annotation;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@SuppressWarnings("unused")
public class PersistenceServicesModule
extends AbstractModule
implements IGuiceModule {
private static final Logger log = LogFactory.getLog(PersistenceServicesModule.class);
private static final Map, Module> modules = new LinkedHashMap<>();
private static final Map, ConnectionBaseInfo> jtaConnectionBaseInfo = new LinkedHashMap<>();
private static final Map jtaDataSources = new LinkedHashMap<>();
private static final Map> jtaPersistenceUnits = new LinkedHashMap<>();
@Override
protected void configure() {
log.config("Building Persistence Services Module");
jtaConnectionBaseInfo.forEach((key, value) ->
{
DataSource ds;
try {
if (!jtaDataSources.containsKey(value.getJndiName())) {
log.config("Starting datasource - " + value.getJndiName());
ds = value.toPooledDatasource();
if (ds != null) {
jtaDataSources.put(value.getJndiName(), ds);
bind(Key.get(DataSource.class, key)).toInstance(ds);
}
if (!jtaPersistenceUnits.containsKey(value.getJndiName())) {
jtaPersistenceUnits.put(value.getJndiName(), new LinkedHashSet<>());
}
jtaPersistenceUnits.get(value.getJndiName())
.add(jtaConnectionBaseInfo.get(key).getPersistenceUnitName());
} else {
ds = jtaDataSources.get(value.getJndiName());
if (ds != null)
bind(Key.get(DataSource.class, key)).toInstance(ds);
}
} catch (Exception t) {
log.log(Level.SEVERE, "Cannot start datasource!", t);
}
});
modules.forEach((key, value) -> install(value));
}
public static Map, Module> getModules() {
return modules;
}
public static Map, ConnectionBaseInfo> getJtaConnectionBaseInfo() {
return jtaConnectionBaseInfo;
}
public static Map getJtaDataSources() {
return jtaDataSources;
}
public static void addJtaPersistenceUnits(String jndi, String persistenceUnitName) {
if (!jtaPersistenceUnits.containsKey(jndi)) {
jtaPersistenceUnits.put(jndi, new LinkedHashSet<>());
}
jtaPersistenceUnits.get(jndi)
.add(persistenceUnitName);
}
public static Map> getJtaPersistenceUnits() {
return jtaPersistenceUnits;
}
@Override
public Integer sortOrder() {
return Integer.MAX_VALUE - 500;
}
}