001: /*
002: * Created on December 10, 2003
003: *
004: * XAResourceImpl.java is used to test the J2EE Connector
005: * as implemented by JOnAS. This class implements the XAResource Interface
006: *
007: */
008: package ersatz.resourceadapter;
009:
010: import javax.resource.spi.ManagedConnection;
011: import javax.transaction.xa.XAResource;
012: import javax.transaction.xa.Xid;
013: import javax.transaction.xa.XAException;
014:
015: /**
016: * @author Bob Kruse
017: *
018: * Jtest Resource Adapter
019: *
020: * used to test the J2EE Connector as implemented by JOnAS.
021: *
022: */
023: public class XAResourceImpl implements XAResource, java.io.Serializable {
024: private ManagedConnection mc;
025: String cName = "XAResourceImpl";
026: int timeout = 0;
027: Xid currentXid;
028: protected Xid xidArray[];
029: // This XAResourceImpl instance STATE
030: final private int RESET = 0;
031: final private int STARTED = 1;
032: final private int ENDED = 2;
033: final private int PREPARED = 3;
034: final private int FORGOT = 4;
035: final private int COMMITTED = 5;
036: final private int ROLLEDBACK = 6;
037:
038: int xFlags;
039: int recoverFlag;
040: int xidState;
041:
042: public XAResourceImpl(ManagedConnection MC) { // constructor for XAResource
043: Utility.log(cName + ".constructor");
044: this .mc = MC;
045: xidState = RESET;
046: }
047:
048: public int getXidState() {
049: return xidState;
050: }
051:
052: public Xid getCurrentXid() {
053: return currentXid;
054: }
055:
056: public ManagedConnection getCurrentMc() {
057: return mc;
058: }
059:
060: public void commit(Xid xid, boolean onePhase) throws XAException {
061: int curState = xidState;
062: if (xidState > RESET) {
063: xidState = COMMITTED;
064: Utility.log(cName + ".commit From State=" + curState
065: + " to State=" + xidState + " xid=" + xid
066: + " onePhase=" + onePhase);
067: } else {
068: Utility.log(cName + ".commit error: State=" + xidState
069: + " should be=" + STARTED + "or more. xid=" + xid
070: + " onePhase=" + onePhase);
071: }
072: }
073:
074: public void end(Xid xid, int flags) throws XAException {
075: int curState = xidState;
076: xidState = ENDED;
077: ManagedConnectionImpl jmc = (ManagedConnectionImpl) mc;
078: jmc.resetXar();
079: Utility.log(cName + ".end From State=" + curState
080: + " to State=" + xidState + " for xid=" + xid
081: + " flags=" + flags);
082: }
083:
084: public void forget(Xid xid) throws XAException {
085: Utility.log(cName + ".forget State=" + xidState);
086: xidState = FORGOT;
087: }
088:
089: public int prepare(Xid xid) throws XAException {
090: Utility.log(cName + ".prepare State=" + xidState);
091: xidState = PREPARED;
092: //return 1;
093: return XA_OK;
094: }
095:
096: /**
097: * Obtain a list of prepared transaction branches from a resource manager.
098: */
099: public Xid[] recover(int flag) throws XAException {
100: recoverFlag = flag;
101: Utility.log(cName + ".recover State=" + xidState);
102: return xidArray;
103: }
104:
105: public void rollback(Xid xid) throws XAException {
106: int curState = xidState;
107: if (xidState >= STARTED) {
108: xidState = ROLLEDBACK;
109: Utility.log(cName + ".rollback From State=" + curState
110: + " to State=" + xidState + " xid=" + xid);
111: } else {
112: Utility.log(cName + ".rollback error: State=" + xidState
113: + " should be=" + STARTED + "or more. xid=" + xid);
114: }
115: }
116:
117: public void start(Xid xid, int flags) throws XAException {
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
126: + " flags=" + flags);
127: } else {
128: Utility.log(cName + ".start error: State=" + xidState
129: + " should be=" + RESET + " xid=" + xid + " flags="
130: + flags);
131: }
132: }
133:
134: public int getTransactionTimeout() throws XAException {
135: Utility
136: .log(cName + ".getTransactionTimeout timeout="
137: + timeout);
138: return timeout;
139: }
140:
141: public boolean setTransactionTimeout(int seconds)
142: throws XAException {
143: timeout = seconds;
144: Utility
145: .log(cName + ".setTransactionTimeout seconds="
146: + seconds);
147: return true;
148: }
149:
150: /**
151: * Determine if the resource manager instance represented by the target object is the
152: * same as the resource manager instance represented by the parameter xares
153: */
154: public boolean isSameRM(XAResource xares) throws XAException {
155: boolean a = true;
156: Utility
157: .log(cName + ".isSameRM xares=" + xares + " return="
158: + a);
159: return a;
160: }
161: }
|