Created
March 26, 2012 20:03
-
-
Save drobiazko/2209266 to your computer and use it in GitHub Desktop.
EntityManagerObjectProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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