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