All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.guicedee.guicedpersistence.db.DbStartup Maven / Gradle / Ivy

Go to download

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

There is a newer version: 62
Show newest version
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 annotation;
	private final String jndiName;

	public DbStartup(Class 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); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy