1 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 59 public class OpenPersistenceManagerInViewInterceptor implements WebRequestInterceptor { 60 61 67 public static final String PARTICIPATE_SUFFIX = ".PARTICIPATE"; 68 69 70 protected final Log logger = LogFactory.getLog(getClass()); 71 72 private PersistenceManagerFactory persistenceManagerFactory; 73 74 75 79 public void setPersistenceManagerFactory(PersistenceManagerFactory pmf) { 80 this.persistenceManagerFactory = pmf; 81 } 82 83 87 public PersistenceManagerFactory getPersistenceManagerFactory() { 88 return persistenceManagerFactory; 89 } 90 91 92 public void preHandle(WebRequest request) throws DataAccessException { 93 if (TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory())) { 94 String participateAttributeName = getParticipateAttributeName(); 96 Integer count = (Integer ) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); 97 int newCount = (count != null) ? count.intValue() + 1 : 1; 98 request.setAttribute(getParticipateAttributeName(), new Integer (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 ex) throws DataAccessException { 113 String participateAttributeName = getParticipateAttributeName(); 114 Integer count = (Integer ) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST); 115 if (count != null) { 116 if (count.intValue() > 1) { 118 request.setAttribute(participateAttributeName, new Integer (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 139 protected String getParticipateAttributeName() { 140 return getPersistenceManagerFactory().toString() + PARTICIPATE_SUFFIX; 141 } 142 143 } 144 | Popular Tags |