KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > transaction > util > xa > AbstractXAResource


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/java/org/apache/commons/transaction/util/xa/AbstractXAResource.java,v 1.2 2004/11/29 18:28:17 luetzkendorf Exp $
3 <<<<<<< .mine
4  * $Revision: 1.2 $
5  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
6 =======
7  * $Revision$
8  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
9 >>>>>>> .r168169
10  *
11  * ====================================================================
12  *
13  * Copyright 2004 The Apache Software Foundation
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  *
27  */

28
29 package org.apache.commons.transaction.util.xa;
30
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.transaction.Status JavaDoc;
35 import javax.transaction.xa.XAException JavaDoc;
36 import javax.transaction.xa.XAResource JavaDoc;
37 import javax.transaction.xa.Xid JavaDoc;
38
39 import org.apache.commons.transaction.util.LoggerFacade;
40
41 /**
42  * Abstract XAResource doing all the tedious tasks shared by many XAResouce implementations.
43  */

44 public abstract class AbstractXAResource implements XAResource JavaDoc, Status JavaDoc {
45
46     // there might be at least one active transaction branch per thread
47
private ThreadLocal JavaDoc activeTransactionBranch = new ThreadLocal JavaDoc();
48
49     private Map JavaDoc suspendedContexts = new HashMap JavaDoc();
50     private Map JavaDoc activeContexts = new HashMap JavaDoc();
51
52     public abstract boolean isSameRM(XAResource JavaDoc xares) throws XAException JavaDoc;
53
54     public abstract Xid JavaDoc[] recover(int flag) throws XAException JavaDoc;
55
56     protected abstract LoggerFacade getLoggerFacade();
57
58     protected abstract boolean includeBranchInXid();
59     
60     public void forget(Xid JavaDoc xid) throws XAException JavaDoc {
61         if (getLoggerFacade().isFineEnabled()) {
62             getLoggerFacade().logFine("Forgetting transaction branch " + xid);
63         }
64         TransactionalResource ts = getTransactionalResource(xid);
65         if (ts == null) {
66             throw new XAException JavaDoc(XAException.XAER_NOTA);
67         }
68         setCurrentlyActiveTransactionalResource(null);
69         removeActiveTransactionalResource(xid);
70         removeSuspendedTransactionalResource(xid);
71     }
72
73     public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc {
74         TransactionalResource ts = getTransactionalResource(xid);
75         if (ts == null) {
76             throw new XAException JavaDoc(XAException.XAER_NOTA);
77         }
78
79         if (getLoggerFacade().isFineEnabled()) {
80             getLoggerFacade().logFine("Committing transaction branch " + ts);
81         }
82
83         if (ts.getStatus() == STATUS_MARKED_ROLLBACK) {
84             throw new XAException JavaDoc(XAException.XA_RBROLLBACK);
85         }
86
87         if (ts.getStatus() != STATUS_PREPARED) {
88             if (onePhase) {
89                 ts.prepare();
90             } else {
91                 throw new XAException JavaDoc(XAException.XAER_PROTO);
92             }
93         }
94         ts.commit();
95         setCurrentlyActiveTransactionalResource(null);
96         removeActiveTransactionalResource(xid);
97         removeSuspendedTransactionalResource(xid);
98     }
99
100     public void rollback(Xid JavaDoc xid) throws XAException JavaDoc {
101         TransactionalResource ts = getTransactionalResource(xid);
102         if (ts == null) {
103             throw new XAException JavaDoc(XAException.XAER_NOTA);
104         }
105
106         if (getLoggerFacade().isFineEnabled()) {
107             getLoggerFacade().logFine("Rolling back transaction branch " + ts);
108         }
109
110         ts.rollback();
111         setCurrentlyActiveTransactionalResource(null);
112         removeActiveTransactionalResource(xid);
113         removeSuspendedTransactionalResource(xid);
114     }
115
116     public int prepare(Xid JavaDoc xid) throws XAException JavaDoc {
117         TransactionalResource ts = getTransactionalResource(xid);
118         if (ts == null) {
119             throw new XAException JavaDoc(XAException.XAER_NOTA);
120         }
121
122         if (getLoggerFacade().isFineEnabled()) {
123             getLoggerFacade().logFine("Preparing transaction branch " + ts);
124         }
125
126         if (ts.getStatus() == STATUS_MARKED_ROLLBACK) {
127             throw new XAException JavaDoc(XAException.XA_RBROLLBACK);
128         }
129             
130         int result = ts.prepare();
131         ts.setStatus(STATUS_PREPARED);
132         return result;
133     }
134
135     public void end(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
136         TransactionalResource ts = getActiveTransactionalResource(xid);
137         if (ts == null) {
138             throw new XAException JavaDoc(XAException.XAER_NOTA);
139         }
140         if (getCurrentlyActiveTransactionalResource() == null) {
141             throw new XAException JavaDoc(XAException.XAER_INVAL);
142         }
143         if (getLoggerFacade().isFineEnabled()) {
144             getLoggerFacade().logFine(new StringBuffer JavaDoc(128)
145                 .append("Thread ").append(Thread.currentThread())
146                 .append(flags == TMSUSPEND ? " suspends" : flags == TMFAIL ? " fails" : " ends")
147                 .append(" work on behalf of transaction branch ")
148                 .append(ts).toString());
149         }
150
151         switch (flags) {
152             case TMSUSPEND :
153                 ts.suspend();
154                 addSuspendedTransactionalResource(xid, ts);
155                 removeActiveTransactionalResource(xid);
156                 break;
157             case TMFAIL :
158                 ts.setStatus(STATUS_MARKED_ROLLBACK);
159                 break;
160             case TMSUCCESS :
161                 break;
162         }
163         setCurrentlyActiveTransactionalResource(null);
164     }
165
166     public void start(Xid JavaDoc xid, int flags) throws XAException JavaDoc {
167         if (getCurrentlyActiveTransactionalResource() != null) {
168             throw new XAException JavaDoc(XAException.XAER_INVAL);
169         }
170         if (getLoggerFacade().isFineEnabled()) {
171             getLoggerFacade().logFine(new StringBuffer JavaDoc(128)
172                     .append("Thread ").append(Thread.currentThread())
173                     .append(flags == TMNOFLAGS ? " starts" : flags == TMJOIN ? " joins" : " resumes")
174                     .append(" work on behalf of transaction branch ")
175                     .append(xid).toString());
176         }
177         
178         TransactionalResource ts;
179         switch (flags) {
180             // a new transaction
181
case TMNOFLAGS :
182             case TMJOIN :
183             default :
184                 try {
185                     ts = createTransactionResource(xid);
186                     ts.begin();
187                 } catch (Exception JavaDoc e) {
188                     getLoggerFacade().logSevere("Could not create new transactional resource", e);
189                     throw new XAException JavaDoc(e.getMessage());
190                 }
191                 break;
192             case TMRESUME :
193                 ts = getSuspendedTransactionalResource(xid);
194                 if (ts == null) {
195                     throw new XAException JavaDoc(XAException.XAER_NOTA);
196                 }
197                 ts.resume();
198                 removeSuspendedTransactionalResource(xid);
199                 break;
200         }
201         setCurrentlyActiveTransactionalResource(ts);
202         addAcitveTransactionalResource(xid, ts);
203     }
204
205     abstract protected TransactionalResource createTransactionResource(Xid JavaDoc xid) throws Exception JavaDoc;
206
207     protected TransactionalResource getCurrentlyActiveTransactionalResource() {
208         TransactionalResource context = (TransactionalResource) activeTransactionBranch.get();
209         return context;
210     }
211
212     protected void setCurrentlyActiveTransactionalResource(TransactionalResource context) {
213         activeTransactionBranch.set(context);
214     }
215
216     protected TransactionalResource getTransactionalResource(Xid JavaDoc xid) {
217         TransactionalResource ts = getActiveTransactionalResource(xid);
218         if (ts != null) return ts;
219         else return getSuspendedTransactionalResource(xid);
220     }
221     protected TransactionalResource getActiveTransactionalResource(Xid JavaDoc xid) {
222         Xid JavaDoc wxid = XidWrapper.wrap(xid, includeBranchInXid());
223         return (TransactionalResource) activeContexts.get(wxid);
224     }
225
226     protected TransactionalResource getSuspendedTransactionalResource(Xid JavaDoc xid) {
227         Xid JavaDoc wxid = XidWrapper.wrap(xid, includeBranchInXid());
228         return (TransactionalResource) suspendedContexts.get(wxid);
229     }
230
231     protected void addAcitveTransactionalResource(Xid JavaDoc xid, TransactionalResource txContext) {
232         Xid JavaDoc wxid = XidWrapper.wrap(xid, includeBranchInXid());
233         activeContexts.put(wxid, txContext);
234     }
235
236     protected void addSuspendedTransactionalResource(Xid JavaDoc xid, TransactionalResource txContext) {
237         Xid JavaDoc wxid = XidWrapper.wrap(xid, includeBranchInXid());
238         suspendedContexts.put(wxid, txContext);
239     }
240
241     protected void removeActiveTransactionalResource(Xid JavaDoc xid) {
242         Xid JavaDoc wxid = XidWrapper.wrap(xid, includeBranchInXid());
243         activeContexts.remove(wxid);
244     }
245
246     protected void removeSuspendedTransactionalResource(Xid JavaDoc xid) {
247         Xid JavaDoc wxid = XidWrapper.wrap(xid, includeBranchInXid());
248         suspendedContexts.remove(wxid);
249     }
250
251 }
252
Popular Tags