KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ersatz > resourceadapter > XAResourceImpl


1 /*
2  * Created on December 10, 2003
3  *
4  * XAResourceImpl.java is used to test the J2EE Connector
5  * as implemented by JOnAS. This class implements the XAResource Interface
6  *
7  */

8 package ersatz.resourceadapter;
9
10 import javax.resource.spi.ManagedConnection;
11 import javax.transaction.xa.XAResource;
12 import javax.transaction.xa.Xid;
13 import javax.transaction.xa.XAException;
14
15 /**
16  * @author Bob Kruse
17  *
18  * Jtest Resource Adapter
19  *
20  * used to test the J2EE Connector as implemented by JOnAS.
21  *
22  */

23 public class XAResourceImpl
24         implements XAResource,
25         java.io.Serializable
26 {
27     private ManagedConnection mc;
28     String cName = "XAResourceImpl";
29     int timeout = 0;
30     Xid currentXid;
31     protected Xid xidArray[];
32     // This XAResourceImpl instance STATE
33
final private int RESET = 0;
34     final private int STARTED = 1;
35     final private int ENDED = 2;
36     final private int PREPARED = 3;
37     final private int FORGOT = 4;
38     final private int COMMITTED = 5;
39     final private int ROLLEDBACK = 6;
40     
41     int xFlags;
42     int recoverFlag;
43     int xidState;
44
45     public XAResourceImpl(ManagedConnection MC) { // constructor for XAResource
46
Utility.log(cName+".constructor");
47         this.mc = MC;
48         xidState = RESET;
49     }
50
51     public int getXidState() {
52         return xidState;
53     }
54     public Xid getCurrentXid() {
55         return currentXid;
56     }
57     public ManagedConnection getCurrentMc() {
58         return mc;
59     }
60     public void commit(Xid xid, boolean onePhase) throws XAException
61     {
62         int curState=xidState;
63         if (xidState>RESET) {
64             xidState=COMMITTED;
65             Utility.log(cName+".commit From State="+curState+
66                    " to State="+xidState+" xid="+xid+" onePhase="+onePhase);
67         }
68         else {
69             Utility.log(cName+".commit error: State="+xidState
70                     +" should be="+STARTED+"or more. xid="+xid+" onePhase="+onePhase);
71         }
72     }
73     public void end(Xid xid, int flags) throws XAException
74     {
75         int curState=xidState;
76         xidState=ENDED;
77         ManagedConnectionImpl jmc = (ManagedConnectionImpl) mc;
78         jmc.resetXar();
79         Utility.log(cName+".end From State="+curState+
80                " to State="+xidState+" for xid="+xid+" flags="+flags);
81     }
82     public void forget(Xid xid) throws XAException
83     {
84         Utility.log(cName+".forget State="+xidState);
85         xidState=FORGOT;
86     }
87     public int prepare(Xid xid) throws XAException
88     {
89         Utility.log(cName+".prepare State="+xidState);
90         xidState=PREPARED;
91         //return 1;
92
return XA_OK;
93     }
94     /**
95      * Obtain a list of prepared transaction branches from a resource manager.
96      */

97     public Xid[] recover(int flag) throws XAException
98     {
99         recoverFlag=flag;
100         Utility.log(cName+".recover State="+xidState);
101         return xidArray;
102     }
103     public void rollback(Xid xid) throws XAException
104     {
105         int curState=xidState;
106         if (xidState >= STARTED) {
107             xidState=ROLLEDBACK;
108             Utility.log(cName+".rollback From State="+curState+
109                    " to State="+xidState+" xid="+xid);
110         }
111         else {
112             Utility.log(cName+".rollback error: State="+xidState
113                     +" should be="+STARTED+"or more. xid="+xid);
114         }
115     }
116     public void start(Xid xid, int flags) throws XAException
117     {
118         Utility.log(cName+".start xid="+xid+" flags="+flags);
119         currentXid=xid;
120         xFlags=flags;
121         int curState=xidState;
122         if (xidState==RESET) {
123             xidState=STARTED;
124             Utility.log(cName+".start From State="+curState+
125                        " to State="+xidState+" xid="+xid+" flags="+flags);
126         }
127         else {
128             Utility.log(cName+".start error: State="+xidState
129                                  +" should be="+RESET+" xid="+xid+" flags="+flags);
130         }
131     }
132     public int getTransactionTimeout() throws XAException
133     {
134         Utility.log(cName+".getTransactionTimeout timeout="+timeout);
135         return timeout;
136     }
137     public boolean setTransactionTimeout(int seconds) throws XAException
138     {
139         timeout=seconds;
140         Utility.log(cName+".setTransactionTimeout seconds="+seconds);
141         return true;
142     }
143     /**
144      * Determine if the resource manager instance represented by the target object is the
145      * same as the resource manager instance represented by the parameter xares
146      */

147     public boolean isSameRM(XAResource xares) throws XAException
148     {
149         boolean a = true;
150         Utility.log(cName+".isSameRM xares="+xares+" return="+a);
151         return a;
152     }
153 }
154
Popular Tags