001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.connector.mock;
017:
018: import java.util.HashSet;
019: import java.util.Set;
020: import javax.transaction.xa.XAException;
021: import javax.transaction.xa.XAResource;
022: import javax.transaction.xa.Xid;
023:
024: /**
025: *
026: *
027: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
028: *
029: * */
030: public class MockXAResource implements XAResource {
031:
032: private final MockManagedConnection mockManagedConnection;
033: private int prepareResult = XAResource.XA_OK;
034: private Xid currentXid;
035: private int transactionTimeoutSeconds;
036: private final Set knownXids = new HashSet();
037: private final Set successfulXids = new HashSet();
038: private Xid prepared;
039: private Xid committed;
040: private Xid rolledback;
041:
042: public MockXAResource(MockManagedConnection mockManagedConnection) {
043: this .mockManagedConnection = mockManagedConnection;
044: }
045:
046: public void commit(Xid xid, boolean onePhase) throws XAException {
047: assert xid != null;
048: assert onePhase || prepared == xid;
049: committed = xid;
050: }
051:
052: //TODO TMFAIL? TMENDRSCAN?
053: public void end(Xid xid, int flags) throws XAException {
054: assert xid != null;
055: assert knownXids.contains(xid);
056: assert flags == XAResource.TMSUSPEND
057: || flags == XAResource.TMSUCCESS;
058: if (flags == XAResource.TMSUSPEND) {
059: assert currentXid == xid;
060: currentXid = null;
061: }
062: if (flags == XAResource.TMSUCCESS) {
063: successfulXids.add(xid);
064: if (xid.equals(currentXid)) {
065: currentXid = null;
066: }
067: }
068: }
069:
070: public void forget(Xid xid) throws XAException {
071: //todo
072: }
073:
074: public int getTransactionTimeout() throws XAException {
075: return transactionTimeoutSeconds;
076: }
077:
078: public boolean isSameRM(XAResource xaResource) throws XAException {
079: if (!(xaResource instanceof MockXAResource)) {
080: return false;
081: }
082: MockXAResource other = (MockXAResource) xaResource;
083: return other.mockManagedConnection
084: .getManagedConnectionFactory() == mockManagedConnection
085: .getManagedConnectionFactory();
086: }
087:
088: public int prepare(Xid xid) throws XAException {
089: assert xid != null;
090: prepared = xid;
091: return prepareResult;
092: }
093:
094: public Xid[] recover(int flag) throws XAException {
095: //todo
096: return new Xid[0];
097: }
098:
099: public void rollback(Xid xid) throws XAException {
100: assert xid != null;
101: rolledback = xid;
102: }
103:
104: public boolean setTransactionTimeout(int seconds)
105: throws XAException {
106: transactionTimeoutSeconds = seconds;
107: return true;
108: }
109:
110: //TODO TMSTARTRSCAN?
111: public void start(Xid xid, int flags) throws XAException {
112: assert currentXid == null : "Expected no xid when start called";
113: assert xid != null : "Expected xid supplied to start";
114: assert flags == XAResource.TMNOFLAGS
115: || flags == XAResource.TMJOIN
116: || flags == XAResource.TMRESUME;
117: if (flags == XAResource.TMNOFLAGS || flags == XAResource.TMJOIN) {
118: assert !knownXids.contains(xid);
119: knownXids.add(xid);
120: }
121: if (flags == XAResource.TMRESUME) {
122: assert knownXids.contains(xid);
123: }
124: currentXid = xid;
125: }
126:
127: public void setPrepareResult(int prepareResult) {
128: this .prepareResult = prepareResult;
129: }
130:
131: public Xid getCurrentXid() {
132: return currentXid;
133: }
134:
135: public Set getKnownXids() {
136: return knownXids;
137: }
138:
139: public Set getSuccessfulXids() {
140: return successfulXids;
141: }
142:
143: public Xid getPrepared() {
144: return prepared;
145: }
146:
147: public Xid getCommitted() {
148: return committed;
149: }
150:
151: public Xid getRolledback() {
152: return rolledback;
153: }
154:
155: public void clear() {
156: currentXid = null;
157: prepared = null;
158: rolledback = null;
159: committed = null;
160: knownXids.clear();
161: successfulXids.clear();
162: prepareResult = XAResource.XA_OK;
163: }
164: }
|