KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > jdo > support > OpenPersistenceManagerInViewFilter


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

62 public class OpenPersistenceManagerInViewFilter extends OncePerRequestFilter {
63
64     public static final String JavaDoc DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME = "persistenceManagerFactory";
65
66     private String JavaDoc persistenceManagerFactoryBeanName = DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME;
67
68
69     /**
70      * Set the bean name of the PersistenceManagerFactory to fetch from Spring's
71      * root application context. Default is "persistenceManagerFactory".
72      * @see #DEFAULT_PERSISTENCE_MANAGER_FACTORY_BEAN_NAME
73      */

74     public void setPersistenceManagerFactoryBeanName(String JavaDoc persistenceManagerFactoryBeanName) {
75         this.persistenceManagerFactoryBeanName = persistenceManagerFactoryBeanName;
76     }
77
78     /**
79      * Return the bean name of the PersistenceManagerFactory to fetch from Spring's
80      * root application context.
81      */

82     protected String JavaDoc getPersistenceManagerFactoryBeanName() {
83         return this.persistenceManagerFactoryBeanName;
84     }
85
86
87     protected void doFilterInternal(
88             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, FilterChain JavaDoc filterChain)
89             throws ServletException JavaDoc, IOException JavaDoc {
90
91         PersistenceManagerFactory pmf = lookupPersistenceManagerFactory(request);
92         boolean participate = false;
93
94         if (TransactionSynchronizationManager.hasResource(pmf)) {
95             // Do not modify the PersistenceManager: just set the participate flag.
96
participate = true;
97         }
98         else {
99             logger.debug("Opening JDO PersistenceManager in OpenPersistenceManagerInViewFilter");
100             PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
101             TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
102         }
103
104         try {
105             filterChain.doFilter(request, response);
106         }
107
108         finally {
109             if (!participate) {
110                 PersistenceManagerHolder pmHolder = (PersistenceManagerHolder)
111                         TransactionSynchronizationManager.unbindResource(pmf);
112                 logger.debug("Closing JDO PersistenceManager in OpenPersistenceManagerInViewFilter");
113                 PersistenceManagerFactoryUtils.releasePersistenceManager(pmHolder.getPersistenceManager(), pmf);
114             }
115         }
116     }
117
118     /**
119      * Look up the PersistenceManagerFactory that this filter should use,
120      * taking the current HTTP request as argument.
121      * <p>Default implementation delegates to the <code>lookupPersistenceManagerFactory</code>
122      * without arguments.
123      * @return the PersistenceManagerFactory to use
124      * @see #lookupPersistenceManagerFactory()
125      */

126     protected PersistenceManagerFactory lookupPersistenceManagerFactory(HttpServletRequest JavaDoc request) {
127         return lookupPersistenceManagerFactory();
128     }
129
130     /**
131      * Look up the PersistenceManagerFactory that this filter should use.
132      * The default implementation looks for a bean with the specified name
133      * in Spring's root application context.
134      * @return the PersistenceManagerFactory to use
135      * @see #getPersistenceManagerFactoryBeanName
136      */

137     protected PersistenceManagerFactory lookupPersistenceManagerFactory() {
138         if (logger.isDebugEnabled()) {
139             logger.debug("Using PersistenceManagerFactory '" + getPersistenceManagerFactoryBeanName() +
140                     "' for OpenPersistenceManagerInViewFilter");
141         }
142         WebApplicationContext wac =
143                 WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
144         return (PersistenceManagerFactory)
145                 wac.getBean(getPersistenceManagerFactoryBeanName(), PersistenceManagerFactory.class);
146     }
147
148 }
149
Popular Tags