1 17 18 package org.apache.geronimo.connector.mock; 19 20 import java.util.HashSet ; 21 import java.util.Set ; 22 import javax.transaction.xa.XAException ; 23 import javax.transaction.xa.XAResource ; 24 import javax.transaction.xa.Xid ; 25 26 32 public class MockXAResource implements XAResource { 33 34 private final MockManagedConnection mockManagedConnection; 35 private int prepareResult = XAResource.XA_OK; 36 private Xid currentXid; 37 private int transactionTimeoutSeconds; 38 private final Set knownXids = new HashSet (); 39 private final Set successfulXids = new HashSet (); 40 private Xid prepared; 41 private Xid committed; 42 private Xid rolledback; 43 44 public MockXAResource(MockManagedConnection mockManagedConnection) { 45 this.mockManagedConnection = mockManagedConnection; 46 } 47 48 public void commit(Xid xid, boolean onePhase) throws XAException { 49 assert xid != null; 50 assert onePhase || prepared == xid; 51 committed = xid; 52 } 53 54 public void end(Xid xid, int flags) throws XAException { 56 assert xid != null; 57 assert knownXids.contains(xid); 58 assert flags == XAResource.TMSUSPEND || flags == XAResource.TMSUCCESS; 59 if (flags == XAResource.TMSUSPEND) { 60 assert currentXid == xid; 61 currentXid = null; 62 } 63 if (flags == XAResource.TMSUCCESS) { 64 successfulXids.add(xid); 65 if (xid.equals(currentXid)) { 66 currentXid = null; 67 } 68 } 69 } 70 71 public void forget(Xid xid) throws XAException { 72 } 74 75 public int getTransactionTimeout() throws XAException { 76 return transactionTimeoutSeconds; 77 } 78 79 public boolean isSameRM(XAResource xaResource) throws XAException { 80 if (!(xaResource instanceof MockXAResource)) { 81 return false; 82 } 83 MockXAResource other = (MockXAResource) xaResource; 84 return other.mockManagedConnection.getManagedConnectionFactory() == mockManagedConnection.getManagedConnectionFactory(); 85 } 86 87 public int prepare(Xid xid) throws XAException { 88 assert xid != null; 89 prepared = xid; 90 return prepareResult; 91 } 92 93 public Xid [] recover(int flag) throws XAException { 94 return new Xid [0]; 96 } 97 98 public void rollback(Xid xid) throws XAException { 99 assert xid != null; 100 rolledback = xid; 101 } 102 103 public boolean setTransactionTimeout(int seconds) throws XAException { 104 transactionTimeoutSeconds = seconds; 105 return true; 106 } 107 108 public void start(Xid xid, int flags) throws XAException { 110 assert currentXid == null :"Expected no xid when start called"; 111 assert xid != null: "Expected xid supplied to start"; 112 assert flags == XAResource.TMNOFLAGS || flags == XAResource.TMJOIN || flags == XAResource.TMRESUME; 113 if (flags == XAResource.TMNOFLAGS || flags == XAResource.TMJOIN) { 114 assert !knownXids.contains(xid); 115 knownXids.add(xid); 116 } 117 if (flags == XAResource.TMRESUME) { 118 assert knownXids.contains(xid); 119 } 120 currentXid = xid; 121 } 122 123 public void setPrepareResult(int prepareResult) { 124 this.prepareResult = prepareResult; 125 } 126 127 public Xid getCurrentXid() { 128 return currentXid; 129 } 130 131 public Set getKnownXids() { 132 return knownXids; 133 } 134 135 public Set getSuccessfulXids() { 136 return successfulXids; 137 } 138 139 public Xid getPrepared() { 140 return prepared; 141 } 142 143 public Xid getCommitted() { 144 return committed; 145 } 146 147 public Xid getRolledback() { 148 return rolledback; 149 } 150 151 public void clear() { 152 currentXid = null; 153 prepared = null; 154 rolledback = null; 155 committed = null; 156 knownXids.clear(); 157 successfulXids.clear(); 158 prepareResult = XAResource.XA_OK; 159 } 160 } 161 | Popular Tags |