1 17 18 package org.apache.geronimo.transaction.manager; 19 20 import java.util.Set ; 21 import java.util.HashSet ; 22 23 import javax.transaction.xa.XAException ; 24 import javax.transaction.xa.XAResource ; 25 import javax.transaction.xa.Xid ; 26 27 30 public class MockResource implements NamedXAResource { 31 private String xaResourceName = "mockResource"; 32 private Xid currentXid; 33 private MockResourceManager manager; 34 private int timeout = 0; 35 private boolean prepared; 36 private boolean committed; 37 private boolean rolledback; 38 private Set preparedXids = new HashSet (); 39 private Set knownXids = new HashSet (); 40 private Set finishedXids = new HashSet (); 42 public MockResource(MockResourceManager manager, String xaResourceName) { 43 this.manager = manager; 44 this.xaResourceName = xaResourceName; 45 } 46 47 public int getTransactionTimeout() throws XAException { 48 return timeout; 49 } 50 51 public boolean setTransactionTimeout(int seconds) throws XAException { 52 return false; 53 } 54 55 public Xid getCurrentXid() { 56 return currentXid; 57 } 58 59 public void start(Xid xid, int flags) throws XAException { 60 if (this.currentXid != null) { 61 throw new XAException (XAException.XAER_PROTO); 62 } 63 if (flags == XAResource.TMRESUME && !knownXids.contains(xid)) { 64 throw new XAException (XAException.XAER_PROTO); 65 } 66 if (finishedXids.contains(xid)) { 67 throw new XAException (XAException.XAER_PROTO); 68 } 69 if ((flags & XAResource.TMJOIN) != 0) { 70 manager.join(xid, this); 71 } else { 72 manager.newTx(xid, this); 73 } 74 this.currentXid = xid; 75 if (!knownXids.contains(xid)) { 76 knownXids.add(xid); 77 } 78 } 79 80 public void end(Xid xid, int flags) throws XAException { 81 if (!knownXids.contains(xid)) { 82 throw new XAException (XAException.XAER_PROTO); 83 } 84 if (flags == XAResource.TMSUSPEND) { 85 if (currentXid == null) { 86 throw new XAException (XAException.XAER_PROTO); 87 } else if (this.currentXid != xid) { 88 throw new XAException (XAException.XAER_PROTO); 89 } 90 } else if (flags == XAResource.TMFAIL || flags == XAResource.TMSUCCESS) { 91 if (finishedXids.contains(xid)) { 92 throw new XAException (XAException.XAER_PROTO); 93 } 94 finishedXids.add(xid); 95 } 96 this.currentXid = null; 97 } 98 99 public int prepare(Xid xid) throws XAException { 100 if (!finishedXids.contains(xid)) { 101 throw new XAException (XAException.XAER_PROTO); 102 } 103 prepared = true; 104 preparedXids.add(xid); 105 return XAResource.XA_OK; 106 } 107 108 public void commit(Xid xid, boolean onePhase) throws XAException { 109 if (!finishedXids.contains(xid)) { 110 throw new XAException (XAException.XAER_PROTO); 111 } 112 preparedXids.remove(xid); 113 committed = true; 114 } 115 116 public void rollback(Xid xid) throws XAException { 117 if (!finishedXids.contains(xid)) { 118 throw new XAException (XAException.XAER_PROTO); 119 } 120 rolledback = true; 121 preparedXids.remove(xid); 122 manager.forget(xid, this); 123 } 124 125 public boolean isSameRM(XAResource xaResource) throws XAException { 126 if (xaResource instanceof MockResource) { 127 return manager == ((MockResource) xaResource).manager; 128 } 129 return false; 130 } 131 132 public void forget(Xid xid) throws XAException { 133 throw new UnsupportedOperationException (); 134 } 135 136 public Xid [] recover(int flag) throws XAException { 137 return (Xid []) preparedXids.toArray(new Xid [preparedXids.size()]); 138 } 139 140 public boolean isPrepared() { 141 return prepared; 142 } 143 144 public boolean isCommitted() { 145 return committed; 146 } 147 148 public boolean isRolledback() { 149 return rolledback; 150 } 151 152 public String getName() { 153 return xaResourceName; 154 } 155 156 } 157 | Popular Tags |