Skip to content

Instantly share code, notes, and snippets.

@drobiazko
Created March 26, 2012 20:03
Show Gist options
  • Save drobiazko/2209266 to your computer and use it in GitHub Desktop.
Save drobiazko/2209266 to your computer and use it in GitHub Desktop.
EntityManagerObjectProvider
package org.apache.tapestry5.internal.jpa;
import org.apache.tapestry5.ioc.AnnotationProvider;
import org.apache.tapestry5.ioc.ObjectCreator;
import org.apache.tapestry5.ioc.ObjectLocator;
import org.apache.tapestry5.ioc.ObjectProvider;
import org.apache.tapestry5.ioc.services.PlasticProxyFactory;
import org.apache.tapestry5.jpa.EntityManagerManager;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
public class EntityManagerObjectProvider implements ObjectProvider
{
private EntityManager proxy;
public <T> T provide(final Class<T> objectType, final AnnotationProvider annotationProvider,
final ObjectLocator locator)
{
if (objectType.equals(EntityManager.class))
return objectType.cast(getOrCreateProxy(annotationProvider, locator));
return null;
}
private synchronized EntityManager getOrCreateProxy(
final AnnotationProvider annotationProvider, final ObjectLocator objectLocator)
{
if (proxy == null)
{
final PlasticProxyFactory proxyFactory = objectLocator.getService("PlasticProxyFactory",
PlasticProxyFactory.class);
final PersistenceContext annotation = annotationProvider
.getAnnotation(PersistenceContext.class);
proxy = proxyFactory.createProxy(EntityManager.class, new ObjectCreator()
{
public Object createObject()
{
final EntityManagerManager entityManagerManager = objectLocator
.getService(EntityManagerManager.class);
return JpaInternalUtils.getEntityManager(entityManagerManager, annotation);
}
}, "<EntityManagerProxy>");
}
return proxy;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment