KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > resource > LocalXAWrapper


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: LocalXAWrapper.java,v 1.8 2005/04/28 08:43:25 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas.resource;
28
29 import javax.resource.ResourceException JavaDoc;
30 import javax.resource.spi.LocalTransaction JavaDoc;
31
32 import javax.transaction.xa.XAResource JavaDoc;
33 import javax.transaction.xa.XAException JavaDoc;
34 import javax.transaction.xa.Xid JavaDoc;
35
36 import org.objectweb.util.monolog.api.Logger;
37 import org.objectweb.util.monolog.api.BasicLevel;
38
39 /**
40  * A LocalXAWrapper that intercepts the XA calls for an RAR that only
41  * supports LocalTransactions and translates them to the appropriate
42  * Local Transaction methods.
43  *
44  * @author Eric.Hardesty@bull.com
45  */

46 public final class LocalXAWrapper
47     implements XAResource JavaDoc {
48
49     /**
50      * The Logger instance where messages are written.
51      */

52     private static Logger logger = null;
53
54     /**
55      * The boolean to determine if in a current transaction
56      */

57     protected boolean isInTransaction;
58
59     /**
60      * The LocalTransaction object to make the begin(), commit(),
61      * and rollback() calls.
62      */

63     protected LocalTransaction JavaDoc localTrans = null;
64
65     LocalXAWrapper(LocalTransaction JavaDoc _localTrans, Logger _logger) {
66         isInTransaction = false;
67         localTrans = _localTrans;
68         logger = _logger;
69     }
70
71     /**
72      * Commit the localTransaction, the params aren't used for a
73      * local transaction.
74      *
75      *@param xid transaction xid
76      *@param flag for interface compliance
77      *@exception XAException Exception trying to commit local transaction
78      */

79     public void commit(Xid JavaDoc xid, boolean flag)
80         throws XAException JavaDoc {
81         if (logger.isLoggable(BasicLevel.DEBUG)) {
82             logger.log(BasicLevel.DEBUG, "Xid =" + xid + "flag=" + flag);
83         }
84
85
86         if (isInTransaction) {
87             // Unbalance start/end. Probably a close is missing.
88
// We decide to unset isInTransaction here to avoid a further error
89
if (logger.isLoggable(BasicLevel.DEBUG)) {
90                 logger.log(BasicLevel.DEBUG, "XA START without XA END");
91             }
92             isInTransaction = false;
93         }
94
95         try {
96             localTrans.commit();
97         } catch (ResourceException JavaDoc res) {
98             XAException JavaDoc xaE = new XAException JavaDoc(res.getMessage());
99             xaE.initCause(res);
100             throw xaE;
101         }
102     }
103
104     /**
105      * No method to map for a local transaction.
106      *@param xid transaction xid
107      */

108     public void end(Xid JavaDoc xid, int i)
109         throws XAException JavaDoc {
110         if (logger.isLoggable(BasicLevel.DEBUG)) {
111             logger.log(BasicLevel.DEBUG, "Xid =" + xid + "i=" + i);
112         }
113         if (!isInTransaction) {
114             logger.log(BasicLevel.ERROR, "END without START");
115             XAException JavaDoc ex = new XAException JavaDoc(XAException.XA_RBPROTO);
116             throw(ex);
117         }
118         isInTransaction = false;
119
120     }
121
122     /**
123      * No method to map for a local transaction.
124      *@param xid transaction xid
125      */

126     public void forget(Xid JavaDoc xid)
127         throws XAException JavaDoc {
128         if (logger.isLoggable(BasicLevel.DEBUG)) {
129             logger.log(BasicLevel.DEBUG, "");
130         }
131     }
132
133     /**
134      * No method to map for a local transaction, just
135      * return no timeout.
136      */

137     public int getTransactionTimeout()
138         throws XAException JavaDoc {
139         if (logger.isLoggable(BasicLevel.DEBUG)) {
140             logger.log(BasicLevel.DEBUG, "");
141         }
142         return -1;
143     }
144
145     /**
146      * Determine if the wrapper instance is the same as the
147      * wrapper instance being passed in
148      * @param xaresource An XAResource object
149      * @return True if same RM instance, otherwise false.
150      * @throws XAException no throw in this implementation
151      */

152
153     public boolean isSameRM(XAResource JavaDoc xaresource)
154         throws XAException JavaDoc {
155         if (logger.isLoggable(BasicLevel.DEBUG)) {
156             logger.log(BasicLevel.DEBUG, "");
157         }
158
159         if (xaresource.equals(this)) {
160             if (logger.isLoggable(BasicLevel.DEBUG)) {
161                 logger.log(BasicLevel.DEBUG, "isSameRM = true " + this);
162             }
163             return true;
164         }
165         if (logger.isLoggable(BasicLevel.DEBUG)) {
166             logger.log(BasicLevel.DEBUG, "isSameRM = false " + this);
167         }
168         return false;
169     }
170
171     /**
172      * No method to map for a local transaction, just return XA_OK.
173      *@param xid transaction xid
174      */

175     public int prepare(Xid JavaDoc xid)
176         throws XAException JavaDoc {
177         if (logger.isLoggable(BasicLevel.DEBUG)) {
178             logger.log(BasicLevel.DEBUG, "");
179         }
180         return XAResource.XA_OK;
181     }
182
183     /**
184      * No method to map for a local transaction.
185      */

186     public Xid JavaDoc[] recover(int i)
187         throws XAException JavaDoc {
188         if (logger.isLoggable(BasicLevel.DEBUG)) {
189             logger.log(BasicLevel.DEBUG, "");
190         }
191         return null;
192     }
193
194     /**
195      * Rollback the localTransaction, the param isn't used for a
196      * local transaction.
197      *@param xid transaction xid
198      *
199      *@exception XAException Exception trying to rollback local transaction
200      */

201     public void rollback(Xid JavaDoc xid)
202         throws XAException JavaDoc {
203         if (logger.isLoggable(BasicLevel.DEBUG)) {
204             logger.log(BasicLevel.DEBUG, "Xid =" + xid);
205         }
206
207         if (isInTransaction) {
208             // Unbalance start/end. Probably a close is missing.
209
// We decide to unset isInTransaction here to avoid a further error, but
210
// I'm not sure it's a good idea.
211
if (logger.isLoggable(BasicLevel.DEBUG)) {
212                 logger.log(BasicLevel.DEBUG, "XA START without XA END");
213             }
214             isInTransaction = false;
215         }
216
217         try {
218             localTrans.rollback();
219         } catch (ResourceException JavaDoc res) {
220             XAException JavaDoc xaE = new XAException JavaDoc("Rollback failed");
221             xaE.initCause(res);
222             throw xaE;
223         }
224     }
225
226     /**
227      * No method to map for a local transaction, just
228      * return no timeout.
229      */

230     public boolean setTransactionTimeout(int i)
231         throws XAException JavaDoc {
232         if (logger.isLoggable(BasicLevel.DEBUG)) {
233             logger.log(BasicLevel.DEBUG, "");
234         }
235         return false;
236     }
237
238     /**
239      * Only start a local transaction if a new transaction is being
240      * attempted, just return if joining or resuming.
241      *
242      *@param xid transaction xid
243      *@exception XAException Transaction already started or error
244      * starting a new local transaction
245      */

246     public void start(Xid JavaDoc xid, int i)
247         throws XAException JavaDoc {
248         if (logger.isLoggable(BasicLevel.DEBUG)) {
249             logger.log(BasicLevel.DEBUG, "Xid =" + xid + "i=" + i);
250         }
251         try {
252             if (i != XAResource.TMJOIN && i != XAResource.TMRESUME) {
253                 if (isInTransaction) {
254                     throw new XAException JavaDoc("LocalXAWrapper.start: Local transaction already started");
255                 }
256                 localTrans.begin();
257                 isInTransaction = true;
258             }
259         } catch (ResourceException JavaDoc res) {
260             XAException JavaDoc xaE = new XAException JavaDoc("LocalTransaction.begin failed");
261             xaE.initCause(res);
262             throw xaE;
263         }
264
265         if (logger.isLoggable(BasicLevel.DEBUG)) {
266             logger.log(BasicLevel.DEBUG, "OK");
267         }
268     }
269 }
270
Popular Tags