KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fictional > resourceadapter > XAResourceImpl


1 /*
2  * Created on Jul 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 fictional.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 import org.objectweb.jonas.common.Log;
16 import org.objectweb.util.monolog.api.Logger;
17 import org.objectweb.util.monolog.api.BasicLevel;
18
19 /**
20  * @author Bob Kruse
21  *
22  * Jtest Resource Adapter
23  *
24  * used to test the J2EE Connector as implemented by JOnAS.
25  *
26  */

27 public class XAResourceImpl
28         implements XAResource,
29         java.io.Serializable
30 {
31     private ManagedConnection mc;
32     private Logger logger = null;
33     String cName = "XAResourceImpl";
34     int timeout = 0;
35     Xid currentXid;
36
37     public XAResourceImpl(ManagedConnection MC) { // constructor for XAResource
38
if (logger == null) {
39             logger = Log.getLogger("fictional.resourceadapter");
40         }
41         logger.log(BasicLevel.DEBUG, impl(this)+".constructor");
42         this.mc = MC;
43         xidState = RESET;
44     }
45     private String impl(Object obj) {
46         if (obj instanceof XAResource) {
47             return "XAResource";
48         } else if (obj instanceof XAResourceImpl) {
49             return "XAResourceImpl";
50         } else
51             return "XAResourceImpl. Is this an error";
52     }
53
54     //
55
// *****************
56
// XAResource methods
57
// *****************
58
//
59
protected Xid xidArray[];
60     // This XAResourceImpl instance STATE
61
final private int RESET = 0;
62     final private int STARTED = 1;
63     final private int ENDED = 2;
64     final private int PREPARED = 3;
65     final private int FORGOT = 4;
66     final private int COMMITTED = 5;
67     final private int ROLLEDBACK = 6;
68     
69     int xFlags;
70     int recoverFlag;
71     int xidState;
72     public int getXidState() {
73         return xidState;
74     }
75     public Xid getCurrentXid() {
76         return currentXid;
77     }
78     public ManagedConnection getCurrentMc() {
79         return mc;
80     }
81     public void commit(Xid xid, boolean onePhase) throws XAException
82     {
83         int curState=xidState;
84         if (xidState>RESET) {
85             xidState=COMMITTED;
86             logger.log(BasicLevel.DEBUG, impl(this)+".commit From State="+curState+
87                    " to State="+xidState+" xid="+xid+" onePhase="+onePhase);
88         }
89         else {
90             logger.log(BasicLevel.DEBUG, impl(this)+".commit error: State="+xidState
91                     +" should be="+STARTED+"or more. xid="+xid+" onePhase="+onePhase);
92         }
93     }
94     public void end(Xid xid, int flags) throws XAException
95     {
96         int curState=xidState;
97         xidState=ENDED;
98         JtestResourceAdapter jmc = (JtestResourceAdapter) mc;
99         jmc.resetXar();
100         logger.log(BasicLevel.DEBUG, impl(this)+".end From State="+curState+
101                " to State="+xidState+" for xid="+xid+" flags="+flags);
102     }
103     public void forget(Xid xid) throws XAException
104     {
105         logger.log(BasicLevel.DEBUG, impl(this)+".forget State="+xidState);
106         xidState=FORGOT;
107     }
108     public int prepare(Xid xid) throws XAException
109     {
110         logger.log(BasicLevel.DEBUG, impl(this)+".prepare State="+xidState);
111         xidState=PREPARED;
112         //return 1;
113
return XA_OK;
114     }
115     /**
116      * Obtain a list of prepared transaction branches from a resource manager.
117      */

118     public Xid[] recover(int flag) throws XAException
119     {
120         recoverFlag=flag;
121         logger.log(BasicLevel.DEBUG, impl(this)+".recover State="+xidState);
122         return xidArray;
123     }
124     public void rollback(Xid xid) throws XAException
125     {
126         int curState=xidState;
127         if (xidState >= STARTED) {
128             xidState=ROLLEDBACK;
129             logger.log(BasicLevel.DEBUG, impl(this)+".rollback From State="+curState+
130                    " to State="+xidState+" xid="+xid);
131         }
132         else {
133             logger.log(BasicLevel.DEBUG, impl(this)+".rollback error: State="+xidState
134                     +" should be="+STARTED+"or more. xid="+xid);
135         }
136     }
137     public void start(Xid xid, int flags) throws XAException
138     {
139         currentXid=xid;
140         xFlags=flags;
141         int curState=xidState;
142         if (xidState==RESET) {
143             xidState=STARTED;
144             logger.log(BasicLevel.DEBUG, impl(this)+".start From State="+curState+
145                        " to State="+xidState+" xid="+xid+" flags="+flags);
146         }
147         else {
148             logger.log(BasicLevel.DEBUG, impl(this)+".start error: State="+xidState
149                                  +" should be="+RESET+" xid="+xid+" flags="+flags);
150         }
151     }
152     public int getTransactionTimeout() throws XAException
153     {
154         logger.log(BasicLevel.DEBUG, impl(this)+".getTransactionTimeout timeout="+timeout);
155         return timeout;
156     }
157     public boolean setTransactionTimeout(int seconds) throws XAException
158     {
159         timeout=seconds;
160         logger.log(BasicLevel.DEBUG, impl(this)+".setTransactionTimeout seconds="+seconds);
161         return true;
162     }
163     /**
164      * Determine if the resource manager instance represented by the target object is the
165      * same as the resource manager instance represented by the parameter xares
166      */

167     public boolean isSameRM(XAResource xares) throws XAException
168     {
169         boolean a = true;
170         logger.log(BasicLevel.DEBUG, impl(this)+".isSameRM xares="+xares+" return="+a);
171         return a;
172     }
173 }
174
Popular Tags