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

com.jwebmp.guicedpersistence.scanners.PersistenceServiceLoadersBinder Maven / Gradle / Ivy

package com.jwebmp.guicedpersistence.scanners;

import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.Matchers;
import com.jwebmp.guicedinjection.GuiceContext;
import com.jwebmp.guicedinjection.abstractions.GuiceInjectorModule;
import com.jwebmp.guicedinjection.interfaces.IGuiceDefaultBinder;
import com.jwebmp.guicedpersistence.db.annotations.Transactional;
import com.jwebmp.guicedpersistence.injectors.CustomJpaLocalTxnInterceptor;
import com.jwebmp.guicedpersistence.injectors.GuicedPersistenceTxnInterceptor;
import com.jwebmp.guicedpersistence.services.IPropertiesConnectionInfoReader;
import com.jwebmp.guicedpersistence.services.IPropertiesEntityManagerReader;
import com.jwebmp.guicedpersistence.services.ITransactionHandler;

import java.util.ServiceLoader;
import java.util.Set;


public class PersistenceServiceLoadersBinder
		implements IGuiceDefaultBinder
{
	@SuppressWarnings("Convert2Diamond")
	public static final Key> PropertiesEntityManagerReader = Key.get(new TypeLiteral>() {});
	@SuppressWarnings("Convert2Diamond")
	public static final Key> PropertiesConnectionInfoReader = Key.get(new TypeLiteral>() {});
	@SuppressWarnings("Convert2Diamond")
	public static final Key> ITransactionHandlerReader = Key.get(new TypeLiteral>() {});

	@Override
	public void onBind(GuiceInjectorModule module)
	{
		Set propertiesEntityManager = GuiceContext.instance()
		                                                                          .getLoader(IPropertiesEntityManagerReader.class, true, ServiceLoader.load(
				                                                                         IPropertiesEntityManagerReader.class));
		Set IPropertiesConnectionInfoReader = GuiceContext.instance()
		                                                                                   .getLoader(IPropertiesConnectionInfoReader.class, true, ServiceLoader.load(
				                                                                                 IPropertiesConnectionInfoReader.class));
		Set transactionHandlerReader = GuiceContext.instance()
		                                                                .getLoader(ITransactionHandler.class, true, ServiceLoader.load(
				                                                                ITransactionHandler.class));

		module.bind(PersistenceServiceLoadersBinder.PropertiesEntityManagerReader)
		      .toInstance(propertiesEntityManager);

		module.bind(PersistenceServiceLoadersBinder.PropertiesConnectionInfoReader)
		      .toInstance(IPropertiesConnectionInfoReader);

		module.bind(PersistenceServiceLoadersBinder.ITransactionHandlerReader)
		      .toInstance(transactionHandlerReader);

		module.bindInterceptor(Matchers.any(), Matchers.annotatedWith(Transactional.class), new GuicedPersistenceTxnInterceptor());
		module.bindInterceptor(Matchers.any(), Matchers.annotatedWith(com.google.inject.persist.Transactional.class), new CustomJpaLocalTxnInterceptor());
		module.bindInterceptor(Matchers.any(), Matchers.annotatedWith(javax.transaction.Transactional.class), new CustomJpaLocalTxnInterceptor());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy