com.guicedee.guicedpersistence.db.DbStartup Maven / Gradle / Ivy
Show all versions of guiced-persistence Show documentation
package com.guicedee.guicedpersistence.db;
import com.google.inject.Provider;
import com.google.inject.persist.PersistService;
import com.google.inject.persist.UnitOfWork;
import com.guicedee.guicedinjection.GuiceContext;
import com.guicedee.guicedinjection.interfaces.IGuicePostStartup;
import com.guicedee.logger.LogFactory;
import javax.sql.DataSource;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DbStartup
implements IGuicePostStartup, Callable, Runnable, Provider
{
private static final Logger log = LogFactory.getLog("DbStartup");
/**
* A list of already loaded data sources identified by JNDI Name
*/
private static final Map loadedConnectionBaseInfos = new ConcurrentHashMap<>();
private static final Map, DataSource> loadedDataSources = new ConcurrentHashMap<>();
private static final Map jndiDataSources = new ConcurrentHashMap<>();
private static final List> availableDataSources = new CopyOnWriteArrayList<>();
private final Class extends Annotation> annotation;
private final String jndiName;
public DbStartup(Class extends Annotation> annotation,String jndiName)
{
this.annotation = annotation;
this.jndiName = jndiName;
}
/**
* .
* A list of already loaded data sources identified by JNDI Name
*
*
* @return Map String DataSource
*/
public static Map getLoadedConnectionBaseInfos()
{
return loadedConnectionBaseInfos;
}
/**
* Returns the data sources associated with an annotation
*
* @return
*/
public static Map, DataSource> getLoadedDataSources()
{
return loadedDataSources;
}
public String name()
{
return "DB Startup - @" + annotation.getSimpleName();
}
@Override
public void postLoad()
{
log.log(Level.CONFIG, "Entity Manager/Persist Service Starting - " + annotation.getSimpleName());
try
{
PersistService ps = GuiceContext.get(PersistService.class, annotation);
ps.start();
UnitOfWork ow = GuiceContext.get(UnitOfWork.class, annotation);
ow.end();
}
catch (Throwable T)
{
log.log(Level.SEVERE, "Persist Service Unable to start/end", T);
}
}
@Override
public Integer sortOrder()
{
return 55;
}
public static List> getAvailableDataSources()
{
return availableDataSources;
}
@Override
public void run()
{
postLoad();
}
@Override
public DbStartup call() throws Exception
{
postLoad();
return this;
}
public static Map getJndiDataSources() {
return jndiDataSources;
}
@Override
public DataSource get()
{
return getLoadedDataSources().get(annotation);
}
}