001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)FakeTransaction.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.messaging;
030:
031: import java.util.IdentityHashMap;
032: import java.util.Iterator;
033:
034: import javax.transaction.Status;
035: import javax.transaction.Transaction;
036: import javax.transaction.TransactionManager;
037: import javax.transaction.xa.XAResource;
038: import javax.transaction.xa.Xid;
039:
040: /** Simple utility class that implements a XAResource for testing.
041: * binding tests in independent threads.
042: * @author Sun Microsystems, Inc.
043: */
044: public class FakeTransaction implements javax.transaction.Transaction,
045: javax.transaction.xa.Xid {
046: private byte[] mXid;
047: private int mStatus;
048: private TransactionManager mTM;
049: private IdentityHashMap mXARs;
050:
051: FakeTransaction(TransactionManager tm, byte[] id) {
052: mTM = tm;
053: mXid = id;
054: mStatus = Status.STATUS_ACTIVE;
055: mXARs = new IdentityHashMap();
056: }
057:
058: public void commit() throws javax.transaction.RollbackException,
059: javax.transaction.HeuristicMixedException,
060: javax.transaction.HeuristicRollbackException,
061: java.lang.SecurityException,
062: javax.transaction.SystemException {
063: boolean mustAbort = false;
064:
065: if (mStatus == Status.STATUS_NO_TRANSACTION) {
066: throw new java.lang.IllegalStateException();
067: }
068: if (mStatus == Status.STATUS_ROLLEDBACK) {
069: throw new javax.transaction.RollbackException();
070: }
071: mStatus = Status.STATUS_PREPARING;
072: Iterator i = mXARs.keySet().iterator();
073: while (i.hasNext()) {
074: XAResource xar;
075:
076: xar = (XAResource) i.next();
077: try {
078: xar.prepare(this );
079: } catch (javax.transaction.xa.XAException xae) {
080: mustAbort = true;
081: }
082: }
083: mStatus = Status.STATUS_PREPARED;
084: if (mustAbort) {
085: mStatus = Status.STATUS_ROLLING_BACK;
086: } else {
087: mStatus = Status.STATUS_COMMITTING;
088: }
089: i = mXARs.keySet().iterator();
090: while (i.hasNext()) {
091: XAResource xar;
092:
093: xar = (XAResource) i.next();
094: try {
095: if (mustAbort) {
096: xar.rollback(this );
097: } else {
098: xar.commit(this , false);
099: }
100: } catch (javax.transaction.xa.XAException xae) {
101: }
102: }
103: if (mustAbort) {
104: mStatus = Status.STATUS_ROLLEDBACK;
105: } else {
106: mStatus = Status.STATUS_COMMITTED;
107: }
108: mStatus = Status.STATUS_NO_TRANSACTION;
109: }
110:
111: public boolean delistResource(
112: javax.transaction.xa.XAResource xAResource, int param)
113: throws java.lang.IllegalStateException,
114: javax.transaction.SystemException {
115: if (mXARs.get(xAResource) != this
116: || mStatus == Status.STATUS_PREPARED
117: || mStatus == Status.STATUS_NO_TRANSACTION) {
118: throw new java.lang.IllegalStateException();
119: }
120: return (true);
121: }
122:
123: public boolean enlistResource(
124: javax.transaction.xa.XAResource xAResource)
125: throws javax.transaction.RollbackException,
126: java.lang.IllegalStateException,
127: javax.transaction.SystemException {
128: if (mStatus == Status.STATUS_PREPARED
129: || mStatus == Status.STATUS_NO_TRANSACTION) {
130: throw new java.lang.IllegalStateException();
131: }
132: if (mStatus == Status.STATUS_MARKED_ROLLBACK) {
133: throw new javax.transaction.RollbackException();
134: }
135: try {
136: if (mXARs.get(xAResource) == null) {
137: mXARs.put(xAResource, this );
138: xAResource.start(this , XAResource.TMJOIN);
139: } else {
140: xAResource.start(this , XAResource.TMRESUME);
141: }
142: } catch (javax.transaction.xa.XAException xae) {
143: throw new javax.transaction.SystemException();
144: }
145: return (true);
146: }
147:
148: public int getStatus() throws javax.transaction.SystemException {
149: return (mStatus);
150: }
151:
152: public void registerSynchronization(
153: javax.transaction.Synchronization synchronization)
154: throws javax.transaction.RollbackException,
155: java.lang.IllegalStateException,
156: javax.transaction.SystemException {
157: }
158:
159: public void rollback() throws java.lang.IllegalStateException,
160: javax.transaction.SystemException {
161: if (mStatus == Status.STATUS_NO_TRANSACTION) {
162: throw new java.lang.IllegalStateException();
163: }
164: if (mStatus == Status.STATUS_ROLLEDBACK) {
165: throw new java.lang.IllegalStateException();
166: }
167: mStatus = Status.STATUS_ROLLING_BACK;
168: Iterator i = mXARs.keySet().iterator();
169: while (i.hasNext()) {
170: XAResource xar;
171:
172: xar = (XAResource) i.next();
173: try {
174: xar.rollback(this );
175: } catch (javax.transaction.xa.XAException xae) {
176: }
177: }
178: mStatus = Status.STATUS_ROLLEDBACK;
179: mStatus = Status.STATUS_NO_TRANSACTION;
180: }
181:
182: public void setRollbackOnly()
183: throws java.lang.IllegalStateException,
184: javax.transaction.SystemException {
185: if (mStatus == Status.STATUS_NO_TRANSACTION) {
186: throw new java.lang.IllegalStateException();
187: }
188: mStatus = Status.STATUS_MARKED_ROLLBACK;
189: }
190:
191: public byte[] getBranchQualifier() {
192: return (null);
193: }
194:
195: public int getFormatId() {
196: return (0);
197: }
198:
199: public byte[] getGlobalTransactionId() {
200: return (mXid);
201: }
202: }
|