KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > tm > resource > LocalResource


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.tm.resource;
8
9 import javax.transaction.xa.XAException JavaDoc;
10 import javax.transaction.xa.XAResource JavaDoc;
11 import javax.transaction.xa.Xid JavaDoc;
12
13 import org.jboss.tm.LastResource;
14
15 /**
16  * A LocalResource.
17  *
18  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
19  * @version $Revision: 41193 $
20  */

21 public class LocalResource extends Resource implements LastResource
22 {
23    /** Whether to fail the commit */
24    private boolean failLocal = false;
25    
26    /**
27     * Create a new LocalResource.
28     *
29     * @param id the id
30     */

31    public LocalResource(Integer JavaDoc id)
32    {
33       super(id);
34    }
35
36    public void failLocal()
37    {
38       failLocal = true;
39    }
40    
41    public int prepare(Xid JavaDoc xid) throws XAException JavaDoc
42    {
43       XAException JavaDoc e = new XAException JavaDoc("Prepare called on local resource");
44       e.errorCode = XAException.XAER_PROTO;
45       throw e;
46    }
47
48    public void commit(Xid JavaDoc xid, boolean onePhase) throws XAException JavaDoc
49    {
50       State state = getState(xid);
51       if (state.resState != ACTIVE && state.resState != ENDED)
52       {
53          state.resState = ERROR;
54          throw new XAException JavaDoc(XAException.XAER_PROTO);
55       }
56       if (failLocal)
57       {
58          state.resState = ROLLEDBACK;
59          state.removed = true;
60          throw new XAException JavaDoc(XAException.XA_RBROLLBACK);
61       }
62       else
63       {
64          state.resState = COMMITTED;
65          state.removed = true;
66       }
67    }
68
69    public boolean isSameRM(XAResource JavaDoc res)
70    {
71       return res == this;
72    }
73 }
74
Popular Tags