KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty > interceptor > TransactionContextBeforeAfter


1 /**
2  *
3  * Copyright 2003-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.jetty.interceptor;
18
19 import javax.transaction.HeuristicMixedException JavaDoc;
20 import javax.transaction.HeuristicRollbackException JavaDoc;
21 import javax.transaction.RollbackException JavaDoc;
22 import javax.transaction.SystemException JavaDoc;
23
24 import org.apache.geronimo.transaction.context.TransactionContext;
25 import org.apache.geronimo.transaction.context.TransactionContextManager;
26 import org.mortbay.http.HttpRequest;
27 import org.mortbay.http.HttpResponse;
28
29 /**
30  * @version $Rev: $ $Date: $
31  */

32 public class TransactionContextBeforeAfter implements BeforeAfter {
33
34     private final BeforeAfter next;
35     private final int oldTxIndex;
36     private final int newTxIndex;
37     private final TransactionContextManager transactionContextManager;
38
39     public TransactionContextBeforeAfter(BeforeAfter next, int oldTxIndex, int newTxIndex, TransactionContextManager transactionContextManager) {
40         this.next = next;
41         this.oldTxIndex = oldTxIndex;
42         this.newTxIndex = newTxIndex;
43         this.transactionContextManager = transactionContextManager;
44     }
45
46     public void before(Object JavaDoc[] context, HttpRequest httpRequest, HttpResponse httpResponse) {
47         TransactionContext oldTransactionContext = transactionContextManager.getContext();
48         TransactionContext newTransactionContext = null;
49         if (oldTransactionContext == null || !oldTransactionContext.isInheritable()) {
50             newTransactionContext = transactionContextManager.newUnspecifiedTransactionContext();
51         }
52         context[oldTxIndex] = oldTransactionContext;
53         context[newTxIndex] = newTransactionContext;
54
55         if (next != null) {
56             next.before(context, httpRequest, httpResponse);
57         }
58     }
59
60     public void after(Object JavaDoc[] context, HttpRequest httpRequest, HttpResponse httpResponse) {
61         try {
62             if (next != null) {
63                 next.after(context, httpRequest, httpResponse);
64             }
65         } finally {
66             TransactionContext oldTransactionContext = (TransactionContext) context[oldTxIndex];
67             TransactionContext newTransactionContext = (TransactionContext) context[newTxIndex];
68             try {
69                 if (newTransactionContext != null) {
70                     if (newTransactionContext != transactionContextManager.getContext()) {
71                         transactionContextManager.getContext().rollback();
72                         newTransactionContext.rollback();
73                         throw new RuntimeException JavaDoc("WRONG EXCEPTION! returned from servlet call with wrong tx context");
74                     }
75                     newTransactionContext.commit();
76
77                 } else {
78                     if (oldTransactionContext != transactionContextManager.getContext()) {
79                         if (transactionContextManager.getContext() != null) {
80                             transactionContextManager.getContext().rollback();
81                         }
82                         throw new RuntimeException JavaDoc("WRONG EXCEPTION! returned from servlet call with wrong tx context");
83                     }
84                 }
85             } catch (SystemException JavaDoc e) {
86                 throw new RuntimeException JavaDoc("WRONG EXCEPTION!", e);
87             } catch (HeuristicMixedException JavaDoc e) {
88                 throw new RuntimeException JavaDoc("WRONG EXCEPTION!", e);
89             } catch (HeuristicRollbackException JavaDoc e) {
90                 throw new RuntimeException JavaDoc("WRONG EXCEPTION!", e);
91             } catch (RollbackException JavaDoc e) {
92                 throw new RuntimeException JavaDoc("WRONG EXCEPTION!", e);
93             } finally {
94                 //this is redundant when we enter with an inheritable context and nothing goes wrong.
95
transactionContextManager.setContext(oldTransactionContext);
96             }
97         }
98     }
99
100 }
101
Popular Tags