KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > ra > LocalAndXATransaction


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Coridan.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46
47 package org.mr.ra;
48
49 import javax.jms.JMSException JavaDoc;
50 import javax.resource.ResourceException JavaDoc;
51 import javax.resource.spi.LocalTransaction JavaDoc;
52 import javax.transaction.xa.XAException JavaDoc;
53 import javax.transaction.xa.XAResource JavaDoc;
54 import javax.transaction.xa.Xid JavaDoc;
55
56 import org.apache.commons.logging.Log;
57 import org.apache.commons.logging.LogFactory;
58 import org.mr.api.jms.TransactionContext;
59
60
61 /**
62  * Used to provide a LocalTransaction and XAResource to a JMS session.
63  */

64 public class LocalAndXATransaction implements XAResource JavaDoc, LocalTransaction JavaDoc {
65
66     static final private Log log = LogFactory.getLog(LocalAndXATransaction.class);
67     final private TransactionContext transactionContext;
68     private boolean inManagedTx = false;
69     
70     public LocalAndXATransaction(TransactionContext transactionContext) {
71         this.transactionContext = transactionContext;
72     }
73
74     public void setInManagedTx(boolean inManagedTx) throws JMSException JavaDoc {
75         this.inManagedTx = inManagedTx;
76     }
77
78     public void begin() throws ResourceException JavaDoc {
79         try {
80             transactionContext.begin();
81             setInManagedTx(true);
82         } catch (JMSException JavaDoc e) {
83             throw new ResourceException JavaDoc("begin failed.", e);
84         }
85     }
86
87     public void commit() throws ResourceException JavaDoc {
88         try {
89             transactionContext.commit();
90         } catch (JMSException JavaDoc e) {
91             throw new ResourceException JavaDoc("commit failed.", e);
92         } finally {
93             try {
94                 setInManagedTx(false);
95             } catch (JMSException JavaDoc e) {
96                 throw new ResourceException JavaDoc("commit failed.",e);
97             }
98         }
99     }
100
101     public void rollback() throws ResourceException JavaDoc {
102         try {
103             transactionContext.rollback();
104         } catch (JMSException JavaDoc e) {
105             throw new ResourceException JavaDoc("rollback failed.", e);
106         } finally {
107             try {
108                 setInManagedTx(false);
109             } catch (JMSException JavaDoc e) {
110                 throw new ResourceException JavaDoc("rollback failed.",e);
111             }
112         }
113     }
114
115     public void commit(Xid JavaDoc arg0, boolean arg1) throws XAException JavaDoc {
116         transactionContext.commit(arg0, arg1);
117     }
118
119     public void end(Xid JavaDoc arg0, int arg1) throws XAException JavaDoc {
120         try {
121             transactionContext.end(arg0, arg1);
122         } finally {
123             try {
124                 setInManagedTx(false);
125             } catch (JMSException JavaDoc e) {
126                 throw (XAException JavaDoc)new XAException JavaDoc(XAException.XAER_PROTO).initCause(e);
127             }
128         }
129     }
130
131     public void forget(Xid JavaDoc arg0) throws XAException JavaDoc {
132         transactionContext.forget(arg0);
133     }
134
135     public int getTransactionTimeout() throws XAException JavaDoc {
136         return transactionContext.getTransactionTimeout();
137     }
138
139     public boolean isSameRM(XAResource JavaDoc xaresource) throws XAException JavaDoc {
140         if (xaresource == null) {
141             return false;
142         }
143         // Do we have to unwrap?
144
if (xaresource instanceof LocalAndXATransaction) {
145             xaresource = ((LocalAndXATransaction)xaresource).transactionContext;
146         }
147         boolean b = transactionContext.isSameRM(xaresource);
148         return b;
149         //return transactionContext.isSameRM(xaresource);
150
}
151
152     public int prepare(Xid JavaDoc arg0) throws XAException JavaDoc {
153         return transactionContext.prepare(arg0);
154     }
155
156     public Xid JavaDoc[] recover(int arg0) throws XAException JavaDoc {
157         return transactionContext.recover(arg0);
158     }
159
160     public void rollback(Xid JavaDoc arg0) throws XAException JavaDoc {
161         transactionContext.rollback(arg0);
162     }
163
164     public boolean setTransactionTimeout(int arg0) throws XAException JavaDoc {
165         return transactionContext.setTransactionTimeout(arg0);
166     }
167
168     public void start(Xid JavaDoc arg0, int arg1) throws XAException JavaDoc {
169         transactionContext.start(arg0, arg1);
170         try {
171             setInManagedTx(true);
172         } catch (JMSException JavaDoc e) {
173             throw (XAException JavaDoc)new XAException JavaDoc(XAException.XAER_PROTO).initCause(e);
174         }
175     }
176
177     public boolean isInManagedTx() {
178         return inManagedTx;
179     }
180 }
Popular Tags