KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > transaction > TransactionalControllerHandlerAdapter


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.web.transaction;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.springframework.core.Ordered;
30 import org.springframework.transaction.PlatformTransactionManager;
31 import org.springframework.transaction.TransactionDefinition;
32 import org.springframework.transaction.TransactionStatus;
33 import org.springframework.transaction.support.DefaultTransactionDefinition;
34 import org.springframework.web.servlet.HandlerAdapter;
35 import org.springframework.web.servlet.ModelAndView;
36 import org.springframework.web.servlet.mvc.Controller;
37 import org.springframework.web.servlet.mvc.LastModified;
38
39 /**
40  * HandlerAdapter that handles the request within a transaction. The transaction
41  * is rolled back when an exception is thrown by the controller.
42  * <p>
43  * NOTE: HandlerAdapters can't be chained. If your use the
44  * {@link org.riotfamily.cachius.spring.CacheableControllerHandlerAdapter} and a
45  * controller implements both {@link org.riotfamily.cachius.spring.CacheableController}
46  * and {@link TransactionalController} the adapter with the highest precedence
47  * will be used to handle the request. So if you want to cache a
48  * {@link TransactionalController} use an AOP proxy instead of this class.
49  *
50  * @author Felix Gnass [fgnass at neteye dot de]
51  * @since 6.4
52  */

53 public class TransactionalControllerHandlerAdapter
54         implements HandlerAdapter, Ordered {
55
56     private PlatformTransactionManager transactionManager;
57     
58     private int order = Integer.MAX_VALUE - 1;
59     
60     public TransactionalControllerHandlerAdapter(
61             PlatformTransactionManager transactionManager) {
62         
63         this.transactionManager = transactionManager;
64     }
65     
66     public int getOrder() {
67         return this.order;
68     }
69
70     public void setOrder(int order) {
71         this.order = order;
72     }
73
74     public boolean supports(Object JavaDoc handler) {
75         return handler instanceof TransactionalController;
76     }
77     
78     public ModelAndView handle(HttpServletRequest JavaDoc request,
79             HttpServletResponse JavaDoc response, Object JavaDoc handler)
80             throws Exception JavaDoc {
81         
82         Controller controller = (Controller) handler;
83         
84         DefaultTransactionDefinition def = new DefaultTransactionDefinition();
85         def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
86
87         TransactionStatus status = transactionManager.getTransaction(def);
88         ModelAndView mv;
89         try {
90             mv = controller.handleRequest(request, response);
91         }
92         catch (Exception JavaDoc ex) {
93             transactionManager.rollback(status);
94             throw ex;
95         }
96         transactionManager.commit(status);
97         return mv;
98     }
99     
100     public long getLastModified(HttpServletRequest JavaDoc request, Object JavaDoc handler) {
101         if (handler instanceof LastModified) {
102             return ((LastModified) handler).getLastModified(request);
103         }
104         return -1L;
105     }
106     
107 }
108
Popular Tags