KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jpa > support > OpenEntityManagerInViewFilter


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.orm.jpa.support;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.persistence.EntityManager;
22 import javax.persistence.EntityManagerFactory;
23 import javax.persistence.PersistenceException;
24 import javax.servlet.FilterChain JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.springframework.dao.DataAccessResourceFailureException;
30 import org.springframework.orm.jpa.EntityManagerHolder;
31 import org.springframework.transaction.support.TransactionSynchronizationManager;
32 import org.springframework.web.context.WebApplicationContext;
33 import org.springframework.web.context.support.WebApplicationContextUtils;
34 import org.springframework.web.filter.OncePerRequestFilter;
35
36 /**
37  * Servlet 2.3 Filter that binds a JPA EntityManager to the thread for the
38  * entire processing of the request. Intended for the "Open EntityManager in
39  * View" pattern, i.e. to allow for lazy loading in web views despite the
40  * original transactions already being completed.
41  *
42  * <p>This filter works similar to the AOP JpaInterceptor: It just makes JPA
43  * EntityManagers available via the thread. It is suitable for
44  * non-transactional execution but also for business layer transactions via
45  * JpaTransactionManager or JtaTransactionManager. In the latter case,
46  * EntityManagers pre-bound by this filter will automatically be used
47  * for the transactions.
48  *
49  * <p>Looks up the EntityManagerFactory in Spring's root web application context.
50  * Supports a "entityManagerFactoryBeanName" filter init-param in <code>web.xml</code>;
51  * the default bean name is "entityManagerFactory". Looks up the EntityManagerFactory
52  * on each request, to avoid initialization order issues (when using ContextLoaderServlet,
53  * the root application context will get initialized <i>after</i> this filter).
54  *
55  * @author Juergen Hoeller
56  * @since 2.0
57  * @see OpenEntityManagerInViewInterceptor
58  * @see org.springframework.orm.jpa.JpaInterceptor
59  * @see org.springframework.orm.jpa.JpaTransactionManager
60  * @see org.springframework.orm.jpa.JpaTemplate#execute
61  * @see org.springframework.orm.jpa.SharedEntityManagerCreator
62  * @see org.springframework.transaction.support.TransactionSynchronizationManager
63  */

64 public class OpenEntityManagerInViewFilter extends OncePerRequestFilter {
65
66     public static final String JavaDoc DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME = "entityManagerFactory";
67
68     private String JavaDoc entityManagerFactoryBeanName = DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME;
69
70
71     /**
72      * Set the bean name of the EntityManagerFactory to fetch from Spring's
73      * root application context. Default is "entityManagerFactory".
74      * @see #DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME
75      */

76     public void setEntityManagerFactoryBeanName(String JavaDoc entityManagerFactoryBeanName) {
77         this.entityManagerFactoryBeanName = entityManagerFactoryBeanName;
78     }
79
80     /**
81      * Return the bean name of the EntityManagerFactory to fetch from Spring's
82      * root application context.
83      */

84     protected String JavaDoc getEntityManagerFactoryBeanName() {
85         return this.entityManagerFactoryBeanName;
86     }
87
88
89     protected void doFilterInternal(
90             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, FilterChain JavaDoc filterChain)
91             throws ServletException JavaDoc, IOException JavaDoc {
92
93         EntityManagerFactory emf = lookupEntityManagerFactory(request);
94         boolean participate = false;
95
96         if (TransactionSynchronizationManager.hasResource(emf)) {
97             // Do not modify the EntityManager: just set the participate flag.
98
participate = true;
99         }
100         else {
101             logger.debug("Opening JPA EntityManager in OpenEntityManagerInViewFilter");
102             try {
103                 EntityManager em = createEntityManager(emf);
104                 TransactionSynchronizationManager.bindResource(emf, new EntityManagerHolder(em));
105             }
106             catch (PersistenceException ex) {
107                 throw new DataAccessResourceFailureException("Could not create JPA EntityManager", ex);
108             }
109         }
110
111         try {
112             filterChain.doFilter(request, response);
113         }
114
115         finally {
116             if (!participate) {
117                 EntityManagerHolder emHolder = (EntityManagerHolder)
118                         TransactionSynchronizationManager.unbindResource(emf);
119                 logger.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(HttpServletRequest JavaDoc request) {
134         return lookupEntityManagerFactory();
135     }
136
137     /**
138      * Look up the EntityManagerFactory that this filter should use.
139      * The default implementation looks for a bean with the specified name
140      * in Spring's root application context.
141      * @return the EntityManagerFactory to use
142      * @see #getEntityManagerFactoryBeanName
143      */

144     protected EntityManagerFactory lookupEntityManagerFactory() {
145         if (logger.isDebugEnabled()) {
146             logger.debug("Using EntityManagerFactory '" + getEntityManagerFactoryBeanName() +
147                     "' for OpenEntityManagerInViewFilter");
148         }
149         WebApplicationContext wac =
150                 WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
151         return (EntityManagerFactory)
152                 wac.getBean(getEntityManagerFactoryBeanName(), EntityManagerFactory.class);
153     }
154
155     /**
156      * Create a JPA EntityManager to be bound to a request.
157      * <p>Can be overridden in subclasses.
158      * @param emf the EntityManagerFactory to use
159      * @see javax.persistence.EntityManagerFactory#createEntityManager()
160      */

161     protected EntityManager createEntityManager(EntityManagerFactory emf) {
162         return emf.createEntityManager();
163     }
164
165 }
166
Popular Tags