KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > j2ee > session > TransactionInterceptor


1 // ========================================================================
2
// $Id: TransactionInterceptor.java,v 1.4 2004/05/09 20:30:48 gregwilkins Exp $
3
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
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
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.j2ee.session;
17
18 //----------------------------------------
19

20 import javax.naming.Context JavaDoc;
21 import javax.naming.InitialContext JavaDoc;
22 import javax.naming.NamingException JavaDoc;
23 import javax.transaction.SystemException JavaDoc;
24 import javax.transaction.Transaction JavaDoc;
25 import javax.transaction.TransactionManager JavaDoc;
26
27 import org.jfox.ioc.logger.Logger;
28
29 //----------------------------------------
30

31 // We need to ensure that calls to the HttpSession implementation are
32
// made in Jetty's and not the User's Transaction Context. Otherwise
33
// if their transaction is rolledback, our state is lost and
34
// vice-versa...
35

36 public class TransactionInterceptor
37   extends AroundInterceptor
38 {
39   protected static final Logger _log=Logger.getLogger(TransactionInterceptor.class);
40   protected final ThreadLocal JavaDoc _theirTransaction =new ThreadLocal JavaDoc();
41   protected Context JavaDoc _ctx;
42
43   public
44     TransactionInterceptor()
45   {
46     super();
47
48     try
49     {
50       _ctx=new InitialContext JavaDoc();
51     }
52     catch (Exception JavaDoc e)
53     {
54       _log.error("could not create InitialContext", e);
55     }
56   }
57
58   protected TransactionManager JavaDoc
59     getTransactionManager()
60   {
61     try
62     {
63       return (TransactionManager JavaDoc)_ctx.lookup("java:/TransactionManager");
64     }
65     catch (NamingException JavaDoc e)
66     {
67       _log.error("could not find TransactionManager", e);
68     }
69
70     return null;
71   }
72
73   // despite the names (push/pop) these are not expected to be reentrant....
74
protected void
75     before()
76   {
77     try
78     {
79       Transaction JavaDoc tx=getTransactionManager().suspend();
80       _theirTransaction.set(tx);
81     }
82     catch (SystemException JavaDoc e)
83     {
84       _log.error("could not disassociate UserTransaction from current thread", e);
85     }
86   }
87
88   protected void
89     after()
90   {
91     try
92     {
93       Transaction JavaDoc tx=(Transaction JavaDoc)_theirTransaction.get();
94       getTransactionManager().resume(tx);
95     }
96     catch (Exception JavaDoc e)
97     {
98       _log.error("could not re-associate UserTransaction with current thread", e);
99     }
100     finally
101     {
102       _theirTransaction.set(null);
103     }
104   }
105
106   // public Object clone() { return this; } // Stateless - Context should be valid for whole webapp
107
}
108
Popular Tags