001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.orm.jpa.support;
018:
019: import java.io.IOException;
020:
021: import javax.persistence.EntityManager;
022: import javax.persistence.EntityManagerFactory;
023: import javax.persistence.PersistenceException;
024: import javax.servlet.FilterChain;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.springframework.dao.DataAccessResourceFailureException;
030: import org.springframework.orm.jpa.EntityManagerHolder;
031: import org.springframework.transaction.support.TransactionSynchronizationManager;
032: import org.springframework.web.context.WebApplicationContext;
033: import org.springframework.web.context.support.WebApplicationContextUtils;
034: import org.springframework.web.filter.OncePerRequestFilter;
035:
036: /**
037: * Servlet 2.3 Filter that binds a JPA EntityManager to the thread for the
038: * entire processing of the request. Intended for the "Open EntityManager in
039: * View" pattern, i.e. to allow for lazy loading in web views despite the
040: * original transactions already being completed.
041: *
042: * <p>This filter makes JPA EntityManagers available via the current thread,
043: * which will be autodetected by transaction managers. It is suitable for service
044: * layer transactions via {@link org.springframework.orm.jpa.JpaTransactionManager}
045: * or {@link org.springframework.transaction.jta.JtaTransactionManager} as well
046: * as for non-transactional read-only execution.
047: *
048: * <p>Looks up the EntityManagerFactory in Spring's root web application context.
049: * Supports a "entityManagerFactoryBeanName" filter init-param in <code>web.xml</code>;
050: * the default bean name is "entityManagerFactory". Looks up the EntityManagerFactory
051: * on each request, to avoid initialization order issues (when using ContextLoaderServlet,
052: * the root application context will get initialized <i>after</i> this filter).
053: *
054: * @author Juergen Hoeller
055: * @since 2.0
056: * @see OpenEntityManagerInViewInterceptor
057: * @see org.springframework.orm.jpa.JpaInterceptor
058: * @see org.springframework.orm.jpa.JpaTransactionManager
059: * @see org.springframework.orm.jpa.JpaTemplate#execute
060: * @see org.springframework.orm.jpa.SharedEntityManagerCreator
061: * @see org.springframework.transaction.support.TransactionSynchronizationManager
062: */
063: public class OpenEntityManagerInViewFilter extends OncePerRequestFilter {
064:
065: public static final String DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME = "entityManagerFactory";
066:
067: private String entityManagerFactoryBeanName = DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME;
068:
069: /**
070: * Set the bean name of the EntityManagerFactory to fetch from Spring's
071: * root application context. Default is "entityManagerFactory".
072: * @see #DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME
073: */
074: public void setEntityManagerFactoryBeanName(
075: String entityManagerFactoryBeanName) {
076: this .entityManagerFactoryBeanName = entityManagerFactoryBeanName;
077: }
078:
079: /**
080: * Return the bean name of the EntityManagerFactory to fetch from Spring's
081: * root application context.
082: */
083: protected String getEntityManagerFactoryBeanName() {
084: return this .entityManagerFactoryBeanName;
085: }
086:
087: protected void doFilterInternal(HttpServletRequest request,
088: HttpServletResponse response, FilterChain filterChain)
089: throws ServletException, IOException {
090:
091: EntityManagerFactory emf = lookupEntityManagerFactory(request);
092: boolean participate = false;
093:
094: if (TransactionSynchronizationManager.hasResource(emf)) {
095: // Do not modify the EntityManager: just set the participate flag.
096: participate = true;
097: } else {
098: logger
099: .debug("Opening JPA EntityManager in OpenEntityManagerInViewFilter");
100: try {
101: EntityManager em = createEntityManager(emf);
102: TransactionSynchronizationManager.bindResource(emf,
103: new EntityManagerHolder(em));
104: } catch (PersistenceException ex) {
105: throw new DataAccessResourceFailureException(
106: "Could not create JPA EntityManager", ex);
107: }
108: }
109:
110: try {
111: filterChain.doFilter(request, response);
112: }
113:
114: finally {
115: if (!participate) {
116: EntityManagerHolder emHolder = (EntityManagerHolder) TransactionSynchronizationManager
117: .unbindResource(emf);
118: logger
119: .debug("Closing JPA EntityManager in OpenEntityManagerInViewFilter");
120: emHolder.getEntityManager().close();
121: }
122: }
123: }
124:
125: /**
126: * Look up the EntityManagerFactory that this filter should use,
127: * taking the current HTTP request as argument.
128: * <p>Default implementation delegates to the <code>lookupEntityManagerFactory</code>
129: * without arguments.
130: * @return the EntityManagerFactory to use
131: * @see #lookupEntityManagerFactory()
132: */
133: protected EntityManagerFactory lookupEntityManagerFactory(
134: HttpServletRequest request) {
135: return lookupEntityManagerFactory();
136: }
137:
138: /**
139: * Look up the EntityManagerFactory that this filter should use.
140: * The default implementation looks for a bean with the specified name
141: * in Spring's root application context.
142: * @return the EntityManagerFactory to use
143: * @see #getEntityManagerFactoryBeanName
144: */
145: protected EntityManagerFactory lookupEntityManagerFactory() {
146: if (logger.isDebugEnabled()) {
147: logger.debug("Using EntityManagerFactory '"
148: + getEntityManagerFactoryBeanName()
149: + "' for OpenEntityManagerInViewFilter");
150: }
151: WebApplicationContext wac = WebApplicationContextUtils
152: .getRequiredWebApplicationContext(getServletContext());
153: return (EntityManagerFactory) wac.getBean(
154: getEntityManagerFactoryBeanName(),
155: EntityManagerFactory.class);
156: }
157:
158: /**
159: * Create a JPA EntityManager to be bound to a request.
160: * <p>Can be overridden in subclasses.
161: * @param emf the EntityManagerFactory to use
162: * @see javax.persistence.EntityManagerFactory#createEntityManager()
163: */
164: protected EntityManager createEntityManager(EntityManagerFactory emf) {
165: return emf.createEntityManager();
166: }
167:
168: }
|