KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > connector > ra > fos > FosXAResource


1 /**
2  * perseus/connector: this is an implementation of some JCA-related technologies
3  * (resource adapters and managers) for the ObjectWeb consortium.
4  * Copyright (C) 2001-2002 France Telecom R&D
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 of the License, or (at your option) 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 USA
19  *
20  * Contact: pascal.dechamboux@rd.francetelecom.com
21  *
22  */

23
24 package org.objectweb.perseus.connector.ra.fos;
25
26 import org.objectweb.perseus.fos.api.FosException;
27 import org.objectweb.perseus.fos.api.FosLoggerFactory;
28 import org.objectweb.perseus.fos.api.FosTransaction;
29 import org.objectweb.perseus.fos.lib.FosTxContext;
30 import org.objectweb.util.monolog.api.BasicLevel;
31 import org.objectweb.util.monolog.api.Logger;
32
33 import java.io.IOException JavaDoc;
34 import javax.transaction.xa.XAException JavaDoc;
35 import javax.transaction.xa.XAResource JavaDoc;
36 import javax.transaction.xa.Xid JavaDoc;
37
38 /**
39  * @author S. Chassande-Barrioz, P. Dechamboux
40  */

41 public class FosXAResource implements XAResource JavaDoc {
42     /**
43      * The logger into which traces about FosXAResource are produced.
44      */

45     private Logger logger;
46     /**
47      * The XID of the transaction associated with this XAResource.
48      * txContext and xid work together: they should be both null
49      * or not at the same time.
50      */

51     Xid JavaDoc xid = null;
52     /**
53      * The FosTransaction associated to the "xid" transaction.
54      * txContext and xid work together: they should be both null
55      * or not at the same time.
56      */

57     FosTxContext txContext = null;
58     /**
59      * A flag that specifies that "end" is called while "isSameRM" has never
60      * replied true. If true, it means that the XAResource has been kept by
61      * JTA in order to managed this transaction in a distributed scope.
62      */

63     boolean endWithNoSameRM = true;
64     /**
65      * Signals if prepared has been done.
66      */

67     boolean prepared = false;
68     /**
69      * The factory that has requtested the creation of this XAResource.
70      */

71     FosXAResourceFactory xaResourceFactory;
72
73     /**
74      * Constructs a FosTxContext.
75      * @param el The logger to be used to produced XAResource-related traces.
76      * @param ftcf The factory that has requested the creation of this
77      * FosTxContext.
78      */

79     FosXAResource(Logger el, FosXAResourceFactory fxarf) throws FosException {
80         logger = el;
81         if (FosLoggerFactory.DEBUG)
82             logger.log(BasicLevel.DEBUG, "Constructs a new FosXAResource.");
83         xaResourceFactory = fxarf;
84     }
85
86     /**
87      *
88      */

89     FosTxContext getTxContext() throws FosException {
90         return txContext;
91     }
92
93     /**
94      * Assigns the relevant TxContext to this XAResource.
95      */

96     private void getTxContext(Xid JavaDoc xid, boolean create)
97         throws XAException JavaDoc {
98         if (this.xid != null) {
99             if (this.xid != xid) {
100                 // We are inside a start/end section: should be the same xid !!!
101
throw new XAException JavaDoc(XAException.XAER_PROTO);
102             }
103             logger.log(BasicLevel.WARN, "XAResource already inside DTP context: "
104                 + xid
105                 + " - ignored.");
106             return;
107         }
108         if (txContext != null)
109             throw new XAException JavaDoc(XAException.XAER_PROTO);
110         // Try getting the FosTxContext associated to this xid
111
txContext = xaResourceFactory.getTxContext(xid);
112         if (txContext == null) {
113             if (!create) {
114                 // No transaction exist with this XID and must not create one!!
115
throw new XAException JavaDoc(XAException.XAER_PROTO);
116             }
117             try {
118                 // Creates a new XA transaction with this XID
119
txContext = xaResourceFactory.createTxContext(xid);
120             } catch (FosException fe) {
121                 throw new XAException JavaDoc(XAException.XAER_RMERR);
122             }
123         }
124         this.xid = xid;
125     }
126
127     // IMPLEMENTATION OF METHODS FROM THE (jta)XAResource INTERFACE
128

129     // Management of transaction enrolment: start, end, isSameRM
130

131     /**
132      * Assigns an actual FOS transaction context to the XAResource
133      * within the give DTP context.
134      */

135     public void start(Xid JavaDoc xid, int i) throws XAException JavaDoc {
136         getTxContext(xid, true);
137     }
138
139     /**
140      * Releases this XAResource if it is no more used.
141      */

142     public void end(Xid JavaDoc xid, int i) throws XAException JavaDoc {
143         if (this.xid != xid)
144             throw new XAException JavaDoc(XAException.XAER_PROTO);
145         if (endWithNoSameRM) {
146             // First XAResource to be used within this XID: assumes it has been
147
// registered by JTA !!
148
} else {
149             this.xid = null;
150             txContext = null;
151             endWithNoSameRM = true;
152             xaResourceFactory.releaseXAResource(this);
153         }
154     }
155
156     /**
157      * Used by JTA in order to verify that it has not already registered a
158      * XAResource to manage this transaction context from this RM. If it is the
159      * case, both XAResource are warned of this fact (endWithNoSameRM = false).
160      * This means that these XAResource will be released at "end" time.
161      * @resource The resource to be compared against this one wrt RM.
162      */

163     public boolean isSameRM(XAResource JavaDoc resource) throws XAException JavaDoc {
164         if (resource instanceof FosXAResource)
165             return false;
166         if (((FosXAResource) resource).xaResourceFactory != xaResourceFactory)
167             return false;
168         ((FosXAResource) resource).endWithNoSameRM = false;
169         endWithNoSameRM = false;
170         return true;
171     }
172
173     // Management of transaction termination: prepare, commit, rollback
174

175     /**
176      * Prepares the underlying FosTxContext (prepare phase of the 2PC).
177      */

178     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
179         getTxContext(xid, false);
180         try {
181             if (txContext.prepare()) {
182                 prepared = true;
183                 return XAResource.XA_RDONLY;
184             }
185             prepared = true;
186             return XAResource.XA_OK;
187         } catch (FosException e) {
188             throw new XAException JavaDoc(XAException.XAER_RMERR);
189         }
190     }
191
192     /**
193      * Commits the underlying FosTxContext (commit phase of the 2PC).
194      */

195     public void commit(Xid JavaDoc xid, boolean b) throws XAException JavaDoc {
196         getTxContext(xid, false);
197         try {
198             if (!prepared)
199                 throw new XAException JavaDoc(XAException.XAER_PROTO);
200             txContext.commit();
201             xaResourceFactory.releaseTxContext(xid);
202         } catch (FosException e) {
203             throw new XAException JavaDoc(XAException.XAER_RMERR);
204         } finally {
205             prepared = false;
206             this.xid = null;
207             txContext = null;
208         }
209     }
210
211     /**
212      * Roolbacks the underlying FosTxContext.
213      */

214     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
215         getTxContext(xid, false);
216         try {
217             txContext.rollback();
218             xaResourceFactory.releaseTxContext(xid);
219         } catch (FosException e) {
220             throw new XAException JavaDoc(XAException.XAER_RMERR);
221         } finally {
222             prepared = false;
223             this.xid = null;
224             txContext = null;
225         }
226     }
227
228     // Management of recovery: forget, recover
229

230     /**
231      *
232      */

233     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
234     }
235
236     /**
237      * Gets the Xid of distributed transactions to be recovered from the FOS
238      * manager.
239      */

240     public Xid JavaDoc[] recover(int i) throws XAException JavaDoc {
241         return xaResourceFactory.getXidForRecovery();
242     }
243
244     // Management of transaction timeout: getTransactionTimeout,
245
// setTransactionTimeout
246

247     /**
248      *
249      */

250     public int getTransactionTimeout() throws XAException JavaDoc {
251         return 0;
252     }
253
254     /**
255      *
256      */

257     public boolean setTransactionTimeout(int i) throws XAException JavaDoc {
258         return false;
259     }
260 }
261
Popular Tags