001: /*
002: * Created on December 10, 2003
003: *
004: * LocalTransactionImpl.java is used to test the J2EE Connector
005: * as implemented by JOnAS. This class implements
006: * javax.resource.spi.LocalTransaction and
007: * javax.resource.cci.LocalTransaction
008: */
009: package ersatz.resourceadapter;
010:
011: import javax.resource.ResourceException;
012: import javax.resource.spi.ManagedConnection;
013: import javax.resource.spi.ConnectionEvent;
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 LocalTransactionImpl implements
024: javax.resource.spi.LocalTransaction,
025: javax.resource.cci.LocalTransaction {
026: private ManagedConnection mc;
027: String cName = "LocalTransactionImpl";
028: private boolean sendEvent;
029: //
030: // This LocalTransactionImpl instance STATE
031: final private int RESET = 0;
032: final private int BEGUN = 1;
033: final private int ENDED = 2;
034: final private int PREPARED = 3;
035: final private int FORGOT = 4;
036: final private int COMMITTED = 5;
037: final private int ROLLEDBACK = 6;
038:
039: int txState;
040:
041: public LocalTransactionImpl(ManagedConnection MC, boolean se) {
042: Utility.log(cName + ".constructor");
043: this .mc = MC;
044: txState = RESET;
045: sendEvent = se;
046: }
047:
048: public void setSendEvent(boolean event) {
049: sendEvent = event;
050: }
051:
052: public int getTxState() {
053: return txState;
054: }
055:
056: public ManagedConnection getCurrentMc() {
057: return mc;
058: }
059:
060: public void begin() throws ResourceException {
061: Utility.log(cName + ".begin (enter) txState=" + txState);
062: int curState = txState;
063: ManagedConnectionImpl omc = (ManagedConnectionImpl) mc;
064: try {
065: if (txState == RESET) {
066: if (sendEvent) {
067: omc.sendEvent(
068: ConnectionEvent.LOCAL_TRANSACTION_STARTED,
069: null, omc.getCHandle());
070: txState = BEGUN;
071: Utility
072: .log(cName
073: + ".begin (exit) (sendEvent(LOCAL_TRANSACTION_STARTED="
074: + ConnectionEvent.LOCAL_TRANSACTION_STARTED
075: + "))" + " From State=" + curState
076: + " to State=" + txState);
077: } else {
078: txState = BEGUN;
079: }
080: omc.inLocalTrans = true;
081: } else {
082: IllegalStateException ex = new IllegalStateException(
083: "LocalTransaction Already active");
084: Utility.log(cName + ".begin (exit) error: State="
085: + txState + " should be=" + RESET + ". "
086: + ex.getMessage());
087: throw ex;
088: }
089:
090: } catch (Exception e) {
091: Utility.log(cName
092: + ".begin (exit) error: unable to sendEvent"
093: + " with 'LOCAL_TRANSACTION_STARTED' "
094: + e.toString());
095: }
096: }
097:
098: public void commit() throws ResourceException {
099: Utility.log(cName + ".commit (enter) txState=" + txState);
100: int curState = txState;
101: ManagedConnectionImpl omc = (ManagedConnectionImpl) mc;
102: try {
103: if (txState == BEGUN) {
104: if (sendEvent) {
105: omc
106: .sendEvent(
107: ConnectionEvent.LOCAL_TRANSACTION_COMMITTED,
108: null, omc.getCHandle());
109: txState = COMMITTED;
110: Utility
111: .log(cName
112: + ".commit (exit) (sendEvent(LOCAL_TRANSACTION_COMMITTED="
113: + ConnectionEvent.LOCAL_TRANSACTION_COMMITTED
114: + "))" + " From State=" + curState
115: + " to State=" + txState);
116: } else {
117: txState = COMMITTED;
118: }
119: } else {
120: IllegalStateException ex = new IllegalStateException(
121: "LocalTransaction Illegal State during commit");
122: Utility.log(cName + ".commit (exit) error: State="
123: + txState + " should be=" + BEGUN + ". "
124: + ex.getMessage());
125: omc.inLocalTrans = false;
126: throw ex;
127: }
128: } catch (Exception e) {
129: Utility.log(cName
130: + ".commit (exit) error: unable to sendEvent"
131: + " with 'LOCAL_TRANSACTION_COMMITTED' "
132: + e.toString());
133: }
134: omc.inLocalTrans = false;
135: }
136:
137: public void rollback() throws ResourceException {
138: int curState = txState;
139: Utility.log(cName + ".rollback (enter) txState=" + txState);
140: ManagedConnectionImpl omc = (ManagedConnectionImpl) mc;
141: try {
142: if (txState == BEGUN) {
143: if (sendEvent) {
144: omc
145: .sendEvent(
146: ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK,
147: null, omc.getCHandle());
148: txState = ROLLEDBACK;
149: Utility
150: .log(cName
151: + ".rollback (exit) (sendEvent(LOCAL_TRANSACTION_ROLLEDBACK="
152: + ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK
153: + "))" + " From State=" + curState
154: + " to State=" + txState);
155: } else {
156: txState = ROLLEDBACK;
157: }
158: } else {
159: IllegalStateException ex = new IllegalStateException(
160: "LocalTransaction Illegal State during rollback");
161: Utility.log(cName + ".rollback (exit) error: State="
162: + txState + " should be=" + BEGUN + ". "
163: + ex.getMessage());
164: omc.inLocalTrans = false;
165: throw ex;
166: }
167: } catch (Exception e) {
168: Utility.log(cName
169: + ".rollback (exit) error: unable to sendEvent"
170: + " with 'LOCAL_TRANSACTION_ROLLEDBACK' "
171: + e.toString());
172: }
173: omc.inLocalTrans = false;
174: }
175: }
|