KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ra > LocalAndXATransaction


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

18 package org.apache.activemq.ra;
19
20 import javax.jms.JMSException JavaDoc;
21 import javax.resource.ResourceException JavaDoc;
22 import javax.resource.spi.LocalTransaction JavaDoc;
23 import javax.transaction.xa.XAException JavaDoc;
24 import javax.transaction.xa.XAResource JavaDoc;
25 import javax.transaction.xa.Xid JavaDoc;
26
27 import org.apache.activemq.TransactionContext;
28
29 /**
30  * Used to provide a LocalTransaction and XAResource to a JMS session.
31  */

32 public class LocalAndXATransaction implements XAResource JavaDoc, LocalTransaction JavaDoc {
33
34     final private TransactionContext transactionContext;
35     private boolean inManagedTx;
36     
37     public LocalAndXATransaction(TransactionContext transactionContext) {
38         this.transactionContext=transactionContext;
39     }
40
41     public void setInManagedTx(boolean inManagedTx) throws JMSException JavaDoc {
42         this.inManagedTx=inManagedTx;
43         if( !inManagedTx )
44             transactionContext.cleanup();
45     }
46
47     public void begin() throws ResourceException JavaDoc {
48         try {
49             transactionContext.begin();
50             setInManagedTx(true);
51         } catch (JMSException JavaDoc e) {
52             throw new ResourceException JavaDoc("begin failed.", e);
53         }
54     }
55
56     public void commit() throws ResourceException JavaDoc {
57         try {
58             transactionContext.commit();
59         } catch (JMSException JavaDoc e) {
60             throw new ResourceException JavaDoc("commit failed.", e);
61         } finally {
62             try {
63                 setInManagedTx(false);
64             } catch (JMSException JavaDoc e) {
65                 throw new ResourceException JavaDoc("commit failed.",e);
66             }
67         }
68     }
69
70     public void rollback() throws ResourceException JavaDoc {
71         try {
72             transactionContext.rollback();
73         } catch (JMSException JavaDoc e) {
74             throw new ResourceException JavaDoc("rollback failed.", e);
75         } finally {
76             try {
77                 setInManagedTx(false);
78             } catch (JMSException JavaDoc e) {
79                 throw new ResourceException JavaDoc("rollback failed.",e);
80             }
81         }
82     }
83
84     public void commit(Xid JavaDoc arg0, boolean arg1) throws XAException JavaDoc {
85         transactionContext.commit(arg0, arg1);
86     }
87
88     public void end(Xid JavaDoc arg0, int arg1) throws XAException JavaDoc {
89         try {
90             transactionContext.end(arg0, arg1);
91         } finally {
92             try {
93                 setInManagedTx(false);
94             } catch (JMSException JavaDoc e) {
95                 throw (XAException JavaDoc)new XAException JavaDoc(XAException.XAER_PROTO).initCause(e);
96             }
97         }
98     }
99
100     public void forget(Xid JavaDoc arg0) throws XAException JavaDoc {
101         transactionContext.forget(arg0);
102     }
103
104     public int getTransactionTimeout() throws XAException JavaDoc {
105         return transactionContext.getTransactionTimeout();
106     }
107
108     public boolean isSameRM(XAResource JavaDoc xaresource) throws XAException JavaDoc {
109         if (xaresource == null)
110             return false;
111         // Do we have to unwrap?
112
if (xaresource instanceof LocalAndXATransaction) {
113             xaresource = ((LocalAndXATransaction)xaresource).transactionContext;
114         }
115         return transactionContext.isSameRM(xaresource);
116     }
117
118     public int prepare(Xid JavaDoc arg0) throws XAException JavaDoc {
119         return transactionContext.prepare(arg0);
120     }
121
122     public Xid JavaDoc[] recover(int arg0) throws XAException JavaDoc {
123         return transactionContext.recover(arg0);
124     }
125
126     public void rollback(Xid JavaDoc arg0) throws XAException JavaDoc {
127         transactionContext.rollback(arg0);
128     }
129
130     public boolean setTransactionTimeout(int arg0) throws XAException JavaDoc {
131         return transactionContext.setTransactionTimeout(arg0);
132     }
133
134     
135     public void start(Xid JavaDoc arg0, int arg1) throws XAException JavaDoc {
136         transactionContext.start(arg0, arg1);
137         try {
138             setInManagedTx(true);
139         } catch (JMSException JavaDoc e) {
140             throw (XAException JavaDoc)new XAException JavaDoc(XAException.XAER_PROTO).initCause(e);
141         }
142     }
143
144     public boolean isInManagedTx() {
145         return inManagedTx;
146     }
147
148     public void cleanup() {
149         transactionContext.cleanup();
150         inManagedTx=false;
151     }
152 }
153
Popular Tags