KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > recover > interfaces > DummyXAResourceImpl


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.recover.interfaces;
23
24 import java.util.HashSet JavaDoc;
25
26 import javax.transaction.xa.XAException JavaDoc;
27 import javax.transaction.xa.XAResource JavaDoc;
28 import javax.transaction.xa.Xid JavaDoc;
29
30 import org.jboss.logging.Logger;
31 import org.jboss.tm.XidImpl;
32 import org.jboss.tm.recovery.RecoveryTestingException;
33
34 /**
35  * Dummy <code>XAResource</code> implementation used for transaction
36  * recovery testing.
37  *
38  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
39  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
40  * @version $Revision: 37406 $
41  */

42 public class DummyXAResourceImpl
43       implements XAResource JavaDoc, DummyXAResource
44 {
45    private Logger log = Logger.getLogger(this.getClass());
46    private HashSet JavaDoc prepared = new HashSet JavaDoc();
47
48    private int committedCount;
49    private int preparedCount;
50    private int forgetCount;
51    private int rollbackCount;
52    private int prepareErrorCode;
53    private int commitErrorCode;
54    private int rollbackErrorCode;
55    private RecoveryTestingException prepareException;
56    private RecoveryTestingException afterPrepareException;
57    private RecoveryTestingException commitException;
58    private RecoveryTestingException afterCommitException;
59    private RecoveryTestingException rollbackException;
60    private RecoveryTestingException afterRollbackException;
61    
62
63    public DummyXAResourceImpl()
64    {
65    }
66
67    public synchronized void clear()
68    {
69       log.info("clear");
70       committedCount = 0;
71       preparedCount = 0;
72       forgetCount = 0;
73       rollbackCount = 0;
74       prepared.clear();
75    }
76
77    public synchronized int getCommittedCount()
78    {
79       log.info("getCommittedCount");
80       return committedCount;
81    }
82
83    public synchronized void setCommittedCount(int committedCount)
84    {
85       log.info("setCommittedCount");
86       this.committedCount = committedCount;
87    }
88
89    public synchronized int getPreparedCount()
90    {
91       log.info("getPreparedCount");
92       return preparedCount;
93    }
94
95    public synchronized void setPreparedCount(int preparedCount)
96    {
97       log.info("setPreparedCount");
98       this.preparedCount = preparedCount;
99    }
100
101    public synchronized int getForgetCount()
102    {
103       log.info("getForgetCount");
104       return forgetCount;
105    }
106
107    public synchronized void setForgetCount(int forgetCount)
108    {
109       log.info("setForgetCount");
110       this.forgetCount = forgetCount;
111    }
112
113    public synchronized int getRollbackCount()
114    {
115       log.info("getRollbackCount");
116       return rollbackCount;
117    }
118
119    public synchronized void setRollbackCount(int rollbackCount)
120    {
121       log.info("setRollbackCount");
122       this.rollbackCount = rollbackCount;
123    }
124    
125    public synchronized void setPrepareErrorCode(int prepareErrorCode)
126    {
127       log.info("setPrepareErrorCode");
128       this.prepareErrorCode = prepareErrorCode;
129    }
130    
131    public synchronized void clearPrepareErrorCode()
132    {
133       log.info("clearPrepareErrorCode");
134       prepareErrorCode = 0;
135    }
136    
137    public synchronized void setCommitErrorCode(int commitErrorCode)
138    {
139       log.info("setCommitErrorCode");
140       this.commitErrorCode = commitErrorCode;
141    }
142    
143    public synchronized void clearCommitErrorCode()
144    {
145       log.info("clearCommitErrorCode");
146       commitErrorCode = 0;
147    }
148    
149    public synchronized void setRollbackErrorCode(int rollbackErrorCode)
150    {
151       log.info("setRollbackErrorCode");
152       this.rollbackErrorCode = rollbackErrorCode;
153    }
154    
155    public synchronized void clearRollbackErrorCode()
156    {
157       log.info("clearRollbackErrorCode");
158       rollbackErrorCode = 0;
159    }
160    
161    public synchronized void setPrepareException(RecoveryTestingException prepareException)
162    {
163       log.info("setPrepareException");
164       this.prepareException = prepareException;
165    }
166    
167    public synchronized void clearPrepareException()
168    {
169       log.info("clearPrepareException");
170       prepareException = null;
171    }
172    
173    public synchronized void setAfterPrepareException(
174                                  RecoveryTestingException afterPrepareException)
175    {
176       log.info("setAfterPrepareException");
177       this.afterPrepareException = afterPrepareException;
178    }
179    
180    public synchronized void clearAfterPrepareException()
181    {
182       log.info("clearAfterPrepareException");
183       afterPrepareException = null;
184    }
185    
186    public synchronized void setCommitException(RecoveryTestingException commitException)
187    {
188       log.info("setCommitException");
189       this.commitException = commitException;
190    }
191    
192    public synchronized void clearCommitException()
193    {
194       log.info("clearCommitException");
195       commitException = null;
196    }
197    
198    public synchronized void setAfterCommitException(
199                                  RecoveryTestingException afterCommitException)
200    {
201       log.info("setAfterCommitException");
202       this.afterCommitException = afterCommitException;
203    }
204
205    public synchronized void clearAfterCommitException()
206    {
207       log.info("clearAfterCommitException");
208       afterCommitException = null;
209    }
210
211    public synchronized void setRollbackException(RecoveryTestingException rollbackException)
212    {
213       log.info("setRollbackException");
214       this.rollbackException = rollbackException;
215    }
216    
217    public synchronized void clearRollbackException()
218    {
219       log.info("clearRollbackException");
220       rollbackException = null;
221    }
222    
223    public synchronized void setAfterRollbackException(
224                               RecoveryTestingException afterRollbackException)
225    {
226       log.info("setAfterRollbackException");
227       this.afterRollbackException = afterRollbackException;
228    }
229
230    public synchronized void clearAfterRollbackException()
231    {
232       log.info("clearAfterRollbackException");
233       afterRollbackException = null;
234    }
235
236    public synchronized void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc
237    {
238       log.info("commit " + XidImpl.toString(xid));
239
240       if (commitException != null)
241          throw commitException;
242
243       if (commitErrorCode != 0)
244          throw new XAException JavaDoc(commitErrorCode);
245       
246       if (!prepared.remove(xid) && !onePhase)
247          throw new RuntimeException JavaDoc("xid " + xid +
248                                     " is not in prepared map of size " +
249                                     prepared.size());
250       committedCount++;
251
252       if (afterCommitException != null)
253          throw afterCommitException;
254    }
255
256    public synchronized void end(Xid JavaDoc xid, int i) throws XAException JavaDoc
257    {
258       log.info("end " + XidImpl.toString(xid));
259    }
260
261    public synchronized void forget(Xid JavaDoc xid) throws XAException JavaDoc
262    {
263       log.info("forget " + XidImpl.toString(xid));
264
265       if (!prepared.remove(xid))
266          throw new RuntimeException JavaDoc("xid " + xid +
267                                     " is not in prepared map of size " +
268                                     prepared.size());
269       forgetCount++;
270    }
271
272    public synchronized int getTransactionTimeout() throws XAException JavaDoc
273    {
274       log.info("getTransactionTimeout");
275       return 30000;
276    }
277
278    public synchronized boolean isSameRM(XAResource JavaDoc xaResource) throws XAException JavaDoc
279    {
280       log.info("isSameRM");
281       return false;
282    }
283
284    public synchronized int prepare(Xid JavaDoc xid) throws XAException JavaDoc
285    {
286       log.info("prepare " + XidImpl.toString(xid));
287
288       if (prepareException != null)
289          throw prepareException;
290       
291       if (prepareErrorCode != 0)
292          throw new XAException JavaDoc(prepareErrorCode);
293
294       prepared.add(xid);
295       preparedCount++;
296       
297       if (afterPrepareException != null)
298          throw afterPrepareException;
299       
300       return XA_OK;
301    }
302
303    public synchronized Xid JavaDoc[] recover(int i) throws XAException JavaDoc
304    {
305       log.info("recover");
306       return (Xid JavaDoc[]) prepared.toArray(new Xid JavaDoc[prepared.size()]);
307    }
308
309    public synchronized void rollback(Xid JavaDoc xid) throws XAException JavaDoc
310    {
311       log.info("rollback " + XidImpl.toString(xid));
312
313       if (rollbackException != null)
314          throw rollbackException;
315       
316       if (rollbackErrorCode != 0)
317          throw new XAException JavaDoc(rollbackErrorCode);
318
319       prepared.remove(xid);
320
321       rollbackCount++;
322
323       if (afterRollbackException != null)
324          throw afterRollbackException;
325    }
326
327    public synchronized boolean setTransactionTimeout(int i) throws XAException JavaDoc
328    {
329       log.info("setTransactionTimeout");
330       return false;
331    }
332
333    public synchronized void start(Xid JavaDoc xid, int i) throws XAException JavaDoc
334    {
335       log.info("start " + XidImpl.toString(xid));
336    }
337    
338 }
339
Popular Tags