001: /*
002: * Created on Jul 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 fictional.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: import org.objectweb.jonas.common.Log;
016: import org.objectweb.util.monolog.api.Logger;
017: import org.objectweb.util.monolog.api.BasicLevel;
018:
019: /**
020: * @author Bob Kruse
021: *
022: * Jtest Resource Adapter
023: *
024: * used to test the J2EE Connector as implemented by JOnAS.
025: *
026: */
027: public class XAResourceImpl implements XAResource, java.io.Serializable {
028: private ManagedConnection mc;
029: private Logger logger = null;
030: String cName = "XAResourceImpl";
031: int timeout = 0;
032: Xid currentXid;
033:
034: public XAResourceImpl(ManagedConnection MC) { // constructor for XAResource
035: if (logger == null) {
036: logger = Log.getLogger("fictional.resourceadapter");
037: }
038: logger.log(BasicLevel.DEBUG, impl(this ) + ".constructor");
039: this .mc = MC;
040: xidState = RESET;
041: }
042:
043: private String impl(Object obj) {
044: if (obj instanceof XAResource) {
045: return "XAResource";
046: } else if (obj instanceof XAResourceImpl) {
047: return "XAResourceImpl";
048: } else
049: return "XAResourceImpl. Is this an error";
050: }
051:
052: //
053: // *****************
054: // XAResource methods
055: // *****************
056: //
057: protected Xid xidArray[];
058: // This XAResourceImpl instance STATE
059: final private int RESET = 0;
060: final private int STARTED = 1;
061: final private int ENDED = 2;
062: final private int PREPARED = 3;
063: final private int FORGOT = 4;
064: final private int COMMITTED = 5;
065: final private int ROLLEDBACK = 6;
066:
067: int xFlags;
068: int recoverFlag;
069: int xidState;
070:
071: public int getXidState() {
072: return xidState;
073: }
074:
075: public Xid getCurrentXid() {
076: return currentXid;
077: }
078:
079: public ManagedConnection getCurrentMc() {
080: return mc;
081: }
082:
083: public void commit(Xid xid, boolean onePhase) throws XAException {
084: int curState = xidState;
085: if (xidState > RESET) {
086: xidState = COMMITTED;
087: logger.log(BasicLevel.DEBUG, impl(this )
088: + ".commit From State=" + curState + " to State="
089: + xidState + " xid=" + xid + " onePhase="
090: + onePhase);
091: } else {
092: logger.log(BasicLevel.DEBUG, impl(this )
093: + ".commit error: State=" + xidState
094: + " should be=" + STARTED + "or more. xid=" + xid
095: + " onePhase=" + onePhase);
096: }
097: }
098:
099: public void end(Xid xid, int flags) throws XAException {
100: int curState = xidState;
101: xidState = ENDED;
102: JtestResourceAdapter jmc = (JtestResourceAdapter) mc;
103: jmc.resetXar();
104: logger.log(BasicLevel.DEBUG, impl(this ) + ".end From State="
105: + curState + " to State=" + xidState + " for xid="
106: + xid + " flags=" + flags);
107: }
108:
109: public void forget(Xid xid) throws XAException {
110: logger.log(BasicLevel.DEBUG, impl(this ) + ".forget State="
111: + xidState);
112: xidState = FORGOT;
113: }
114:
115: public int prepare(Xid xid) throws XAException {
116: logger.log(BasicLevel.DEBUG, impl(this ) + ".prepare State="
117: + xidState);
118: xidState = PREPARED;
119: //return 1;
120: return XA_OK;
121: }
122:
123: /**
124: * Obtain a list of prepared transaction branches from a resource manager.
125: */
126: public Xid[] recover(int flag) throws XAException {
127: recoverFlag = flag;
128: logger.log(BasicLevel.DEBUG, impl(this ) + ".recover State="
129: + xidState);
130: return xidArray;
131: }
132:
133: public void rollback(Xid xid) throws XAException {
134: int curState = xidState;
135: if (xidState >= STARTED) {
136: xidState = ROLLEDBACK;
137: logger.log(BasicLevel.DEBUG, impl(this )
138: + ".rollback From State=" + curState + " to State="
139: + xidState + " xid=" + xid);
140: } else {
141: logger.log(BasicLevel.DEBUG, impl(this )
142: + ".rollback error: State=" + xidState
143: + " should be=" + STARTED + "or more. xid=" + xid);
144: }
145: }
146:
147: public void start(Xid xid, int flags) throws XAException {
148: currentXid = xid;
149: xFlags = flags;
150: int curState = xidState;
151: if (xidState == RESET) {
152: xidState = STARTED;
153: logger.log(BasicLevel.DEBUG, impl(this )
154: + ".start From State=" + curState + " to State="
155: + xidState + " xid=" + xid + " flags=" + flags);
156: } else {
157: logger.log(BasicLevel.DEBUG, impl(this )
158: + ".start error: State=" + xidState + " should be="
159: + RESET + " xid=" + xid + " flags=" + flags);
160: }
161: }
162:
163: public int getTransactionTimeout() throws XAException {
164: logger.log(BasicLevel.DEBUG, impl(this )
165: + ".getTransactionTimeout timeout=" + timeout);
166: return timeout;
167: }
168:
169: public boolean setTransactionTimeout(int seconds)
170: throws XAException {
171: timeout = seconds;
172: logger.log(BasicLevel.DEBUG, impl(this )
173: + ".setTransactionTimeout seconds=" + seconds);
174: return true;
175: }
176:
177: /**
178: * Determine if the resource manager instance represented by the target object is the
179: * same as the resource manager instance represented by the parameter xares
180: */
181: public boolean isSameRM(XAResource xares) throws XAException {
182: boolean a = true;
183: logger.log(BasicLevel.DEBUG, impl(this ) + ".isSameRM xares="
184: + xares + " return=" + a);
185: return a;
186: }
187: }
|