/** * Perform a JNDI lookup for the given resource by name. * <p>Called for EntityManagerFactory and EntityManager lookup * when JNDI names are mapped for specific persistence units. * @param jndiName the JNDI name to look up * @param requiredType the required type of the object * @return the obtained object * @throws Exception if the JNDI lookup failed */ protected <T> T lookup(String jndiName, Class<T> requiredType) throws Exception { return new LocatorDelegate().lookup(jndiName, requiredType); }
@Override public <T> void afterCompletion(NativeWebRequest request, DeferredResult<T> deferredResult) { closeEntityManager(); }
@Override public <T> void preProcess(NativeWebRequest request, Callable<T> task) { bindEntityManager(); }
/** * Resolve the object against the application context. */ @Override protected Object getResourceToInject(Object target, @Nullable String requestingBeanName) { // Resolves to EntityManagerFactory or EntityManager. if (this.type != null) { return (this.type == PersistenceContextType.EXTENDED ? resolveExtendedEntityManager(target, requestingBeanName) : resolveEntityManager(requestingBeanName)); } else { // OK, so we need an EntityManagerFactory... return resolveEntityManagerFactory(requestingBeanName); } }
@Test public void testSetterOfWrongTypeAnnotatedWithPersistenceUnit() { PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor(); try { pabpp.postProcessProperties(null, new SetterOfWrongTypeAnnotatedWithPersistenceUnit(), "bean"); fail("Can't inject this setter"); } catch (IllegalStateException ex) { // Ok } }
@Test public void testSetterWithNoArgs() { PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor(); try { pabpp.postProcessProperties(null, new SetterWithNoArgs(), "bean"); fail("Can't inject this setter"); } catch (IllegalStateException ex) { // Ok } }
@Test public void testFieldOfWrongTypeAnnotatedWithPersistenceUnit() { PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor(); try { pabpp.postProcessProperties(null, new FieldOfWrongTypeAnnotatedWithPersistenceUnit(), "bean"); fail("Can't inject this field"); } catch (IllegalStateException ex) { // Ok } }
private EntityManagerFactory resolveEntityManagerFactory(@Nullable String requestingBeanName) { // Obtain EntityManagerFactory from JNDI? EntityManagerFactory emf = getPersistenceUnit(this.unitName); if (emf == null) { // Need to search for EntityManagerFactory beans. emf = findEntityManagerFactory(this.unitName, requestingBeanName); } return emf; }
@Override public void afterConcurrentHandlingStarted(WebRequest request) { if (!decrementParticipateCount(request)) { TransactionSynchronizationManager.unbindResource(obtainEntityManagerFactory()); } }
@Test public void testNoPropertiesPassedIn() { EntityManager mockEm = mock(EntityManager.class); given(mockEmf.createEntityManager()).willReturn(mockEm); PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor(); DefaultPrivatePersistenceContextFieldExtended dppcf = new DefaultPrivatePersistenceContextFieldExtended(); pabpp.postProcessProperties(null, dppcf, "bean"); assertNotNull(dppcf.em); }
@Test public void testPropertiesPassedIn() { Properties props = new Properties(); props.put("foo", "bar"); EntityManager mockEm = mock(EntityManager.class); given(mockEmf.createEntityManager(props)).willReturn(mockEm); PersistenceAnnotationBeanPostProcessor pabpp = new MockPersistenceAnnotationBeanPostProcessor(); DefaultPrivatePersistenceContextFieldExtendedWithProps dppcf = new DefaultPrivatePersistenceContextFieldExtendedWithProps(); pabpp.postProcessProperties(null, dppcf, "bean"); assertNotNull(dppcf.em); }
/** * Look up the EntityManagerFactory that this filter should use, * taking the current HTTP request as argument. * <p>The default implementation delegates to the {@code lookupEntityManagerFactory} * without arguments, caching the EntityManagerFactory reference once obtained. * @return the EntityManagerFactory to use * @see #lookupEntityManagerFactory() */ protected EntityManagerFactory lookupEntityManagerFactory(HttpServletRequest request) { EntityManagerFactory emf = this.entityManagerFactory; if (emf == null) { emf = lookupEntityManagerFactory(); this.entityManagerFactory = emf; } return emf; }
@Deprecated @Override public PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) { return postProcessProperties(pvs, bean, beanName); }
@Override public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { InjectionMetadata metadata = findPersistenceMetadata(beanName, beanType, null); metadata.checkConfigMembers(beanDefinition); }
private boolean decrementParticipateCount(WebRequest request) { String participateAttributeName = getParticipateAttributeName(); Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); if (count == null) { return false; } // Do not modify the Session: just clear the marker. if (count > 1) { request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST); } else { request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); } return true; }
/** * Return the name of the request attribute that identifies that a request is * already filtered. Default implementation takes the toString representation * of the EntityManagerFactory instance and appends ".FILTERED". * @see #PARTICIPATE_SUFFIX */ protected String getParticipateAttributeName() { return obtainEntityManagerFactory().toString() + PARTICIPATE_SUFFIX; }
@Override @PersistenceContext(unitName="unit2", type = PersistenceContextType.EXTENDED) public void setEntityManager(EntityManager em) { super.setEntityManager(em); } }
private boolean applyEntityManagerBindingInterceptor(WebAsyncManager asyncManager, String key) { CallableProcessingInterceptor cpi = asyncManager.getCallableInterceptor(key); if (cpi == null) { return false; } ((AsyncRequestInterceptor) cpi).bindEntityManager(); return true; }
@Override public <T> void afterCompletion(NativeWebRequest request, Callable<T> task) throws Exception { closeEntityManager(); }
private boolean applyEntityManagerBindingInterceptor(WebAsyncManager asyncManager, String key) { CallableProcessingInterceptor cpi = asyncManager.getCallableInterceptor(key); if (cpi == null) { return false; } ((AsyncRequestInterceptor) cpi).bindEntityManager(); return true; }