KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jdo.PersistenceManager;
20 import javax.jdo.PersistenceManagerFactory;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.springframework.core.AttributeAccessor;
26 import org.springframework.dao.DataAccessException;
27 import org.springframework.orm.jdo.PersistenceManagerFactoryUtils;
28 import org.springframework.orm.jdo.PersistenceManagerHolder;
29 import org.springframework.transaction.support.TransactionSynchronizationManager;
30 import org.springframework.ui.ModelMap;
31 import org.springframework.web.context.request.WebRequestInterceptor;
32 import org.springframework.web.context.request.WebRequest;
33
34 /**
35  * Spring web HandlerInterceptor that binds a JDO PersistenceManager to the
36  * thread for the entire processing of the request. Intended for the "Open
37  * PersistenceManager in View" pattern, i.e. to allow for lazy loading in
38  * web views despite the original transactions already being completed.
39  *
40  * <p>This filter works similar to the AOP JdoInterceptor: It just makes JDO
41  * PersistenceManagers available via the thread. It is suitable for
42  * non-transactional execution but also for middle tier transactions via
43  * JdoTransactionManager or JtaTransactionManager. In the latter case,
44  * PersistenceManagers pre-bound by this filter will automatically be used
45  * for the transactions.
46  *
47  * <p>In contrast to OpenPersistenceManagerInViewFilter, this interceptor is set
48  * up in a Spring application context and can thus take advantage of bean wiring.
49  * It derives from JdoAccessor to inherit common JDO configuration properties.
50  *
51  * @author Juergen Hoeller
52  * @since 1.1
53  * @see OpenPersistenceManagerInViewFilter
54  * @see org.springframework.orm.jdo.JdoInterceptor
55  * @see org.springframework.orm.jdo.JdoTransactionManager
56  * @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager
57  * @see org.springframework.transaction.support.TransactionSynchronizationManager
58  */

59 public class OpenPersistenceManagerInViewInterceptor implements WebRequestInterceptor {
60
61     /**
62      * Suffix that gets appended to the PersistenceManagerFactory toString
63      * representation for the "participate in existing persistence manager
64      * handling" request attribute.
65      * @see #getParticipateAttributeName
66      */

67     public static final String JavaDoc PARTICIPATE_SUFFIX = ".PARTICIPATE";
68
69
70     protected final Log logger = LogFactory.getLog(getClass());
71
72     private PersistenceManagerFactory persistenceManagerFactory;
73
74
75     /**
76      * Set the JDO PersistenceManagerFactory that should be used to create
77      * PersistenceManagers.
78      */

79     public void setPersistenceManagerFactory(PersistenceManagerFactory pmf) {
80         this.persistenceManagerFactory = pmf;
81     }
82
83     /**
84      * Return the JDO PersistenceManagerFactory that should be used to create
85      * PersistenceManagers.
86      */

87     public PersistenceManagerFactory getPersistenceManagerFactory() {
88         return persistenceManagerFactory;
89     }
90
91
92     public void preHandle(WebRequest request) throws DataAccessException {
93         if (TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory())) {
94             // Do not modify the PersistenceManager: just mark the request accordingly.
95
String JavaDoc participateAttributeName = getParticipateAttributeName();
96             Integer JavaDoc count = (Integer JavaDoc) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
97             int newCount = (count != null) ? count.intValue() + 1 : 1;
98             request.setAttribute(getParticipateAttributeName(), new Integer JavaDoc(newCount), WebRequest.SCOPE_REQUEST);
99         }
100         else {
101             logger.debug("Opening JDO PersistenceManager in OpenPersistenceManagerInViewInterceptor");
102             PersistenceManager pm =
103                     PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), true);
104             TransactionSynchronizationManager.bindResource(
105                     getPersistenceManagerFactory(), new PersistenceManagerHolder(pm));
106         }
107     }
108
109     public void postHandle(WebRequest request, ModelMap model) {
110     }
111
112     public void afterCompletion(WebRequest request, Exception JavaDoc ex) throws DataAccessException {
113         String JavaDoc participateAttributeName = getParticipateAttributeName();
114         Integer JavaDoc count = (Integer JavaDoc) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
115         if (count != null) {
116             // Do not modify the PersistenceManager: just clear the marker.
117
if (count.intValue() > 1) {
118                 request.setAttribute(participateAttributeName, new Integer JavaDoc(count.intValue() - 1), WebRequest.SCOPE_REQUEST);
119             }
120             else {
121                 request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
122             }
123         }
124         else {
125             PersistenceManagerHolder pmHolder = (PersistenceManagerHolder)
126                     TransactionSynchronizationManager.unbindResource(getPersistenceManagerFactory());
127             logger.debug("Closing JDO PersistenceManager in OpenPersistenceManagerInViewInterceptor");
128             PersistenceManagerFactoryUtils.releasePersistenceManager(
129                     pmHolder.getPersistenceManager(), getPersistenceManagerFactory());
130         }
131     }
132
133     /**
134      * Return the name of the request attribute that identifies that a request is
135      * already filtered. Default implementation takes the toString representation
136      * of the PersistenceManagerFactory instance and appends ".FILTERED".
137      * @see #PARTICIPATE_SUFFIX
138      */

139     protected String JavaDoc getParticipateAttributeName() {
140         return getPersistenceManagerFactory().toString() + PARTICIPATE_SUFFIX;
141     }
142
143 }
144
Popular Tags