001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2007
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.common.web.transaction;
025:
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.springframework.transaction.PlatformTransactionManager;
032: import org.springframework.transaction.TransactionDefinition;
033: import org.springframework.transaction.TransactionStatus;
034: import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
035: import org.springframework.transaction.interceptor.TransactionAttribute;
036: import org.springframework.util.Assert;
037: import org.springframework.web.servlet.HandlerInterceptor;
038: import org.springframework.web.servlet.ModelAndView;
039:
040: /**
041: * HandlerInterceptor that executes the handler within a transactional context.
042: * @author Felix Gnass [fgnass at neteye dot de]
043: * @since 6.5
044: */
045: public class TransactionalHandlerInterceptor implements
046: HandlerInterceptor {
047:
048: private static final String TX_STATUS_ATTRIBUTE = TransactionalHandlerInterceptor.class
049: .getName()
050: + ".status";
051:
052: private static Log log = LogFactory
053: .getLog(TransactionalHandlerInterceptor.class);
054:
055: private TransactionAttribute transactionAttribute = new DefaultTransactionAttribute(
056: TransactionDefinition.PROPAGATION_REQUIRED);
057:
058: private PlatformTransactionManager transactionManager;
059:
060: public TransactionalHandlerInterceptor(PlatformTransactionManager tm) {
061: this .transactionManager = tm;
062: }
063:
064: /**
065: * Sets the {@link TransactionAttribute}. If not set, a
066: * {@link DefaultTransactionAttribute} with
067: * {@link TransactionDefinition#PROPAGATION_REQUIRED} is used.
068: */
069: public void setTransactionAttribute(
070: TransactionAttribute transactionAttribute) {
071: this .transactionAttribute = transactionAttribute;
072: }
073:
074: public boolean preHandle(HttpServletRequest request,
075: HttpServletResponse response, Object handler)
076: throws Exception {
077:
078: if (handler instanceof TransactionalHandler) {
079: Assert
080: .isNull(
081: request.getAttribute(TX_STATUS_ATTRIBUTE),
082: "A TransactionStatus was already bound to the request. "
083: + "Nested TransactionalHandlers are currently not supported.");
084:
085: log.debug("Beginning transaction");
086: TransactionStatus status = transactionManager
087: .getTransaction(transactionAttribute);
088:
089: request.setAttribute(TX_STATUS_ATTRIBUTE, status);
090: }
091: return true;
092: }
093:
094: public void postHandle(HttpServletRequest request,
095: HttpServletResponse response, Object handler,
096: ModelAndView modelAndView) throws Exception {
097:
098: if (handler instanceof TransactionalHandler) {
099: TransactionStatus status = (TransactionStatus) request
100: .getAttribute(TX_STATUS_ATTRIBUTE);
101:
102: request.removeAttribute(TX_STATUS_ATTRIBUTE);
103: log.debug("Committing transaction");
104: transactionManager.commit(status);
105: }
106: }
107:
108: public void afterCompletion(HttpServletRequest request,
109: HttpServletResponse response, Object handler, Exception ex)
110: throws Exception {
111:
112: if (handler instanceof TransactionalHandler) {
113: TransactionStatus status = (TransactionStatus) request
114: .getAttribute(TX_STATUS_ATTRIBUTE);
115:
116: if (status != null) {
117: request.removeAttribute(TX_STATUS_ATTRIBUTE);
118: log.debug("Rolling back transaction");
119: transactionManager.rollback(status);
120: }
121: }
122: }
123: }
|