1 22 package org.jboss.mq; 23 24 import javax.transaction.xa.XAException ; 25 import javax.transaction.xa.XAResource ; 26 import javax.transaction.xa.Xid ; 27 28 import org.jboss.logging.Logger; 29 30 37 public class SpyXAResource implements XAResource 38 { 39 40 private static final Logger log = Logger.getLogger(SpyXAResource.class); 41 42 43 private static boolean trace = log.isTraceEnabled(); 44 45 46 SpySession session; 47 48 53 SpyXAResource(SpySession session) 54 { 55 trace = log.isTraceEnabled(); 56 57 this.session = session; 58 59 if (trace) 60 log.trace("Created " + this); 61 } 62 63 public boolean setTransactionTimeout(int arg1) throws XAException 64 { 65 return false; 66 } 67 68 public int getTransactionTimeout() throws XAException 69 { 70 return 0; 71 } 72 73 public boolean isSameRM(XAResource arg1) throws XAException 74 { 75 if (!(arg1 instanceof SpyXAResource)) 76 return false; 77 return ((SpyXAResource) arg1).session.connection.spyXAResourceManager == session.connection.spyXAResourceManager; 78 } 79 80 public void commit(Xid xid, boolean onePhase) throws XAException 81 { 82 if (trace) 83 log.trace("Commit xid=" + xid + ", onePhase=" + onePhase + " " + this); 84 85 xid = convertXid(xid); 86 try 87 { 88 session.connection.spyXAResourceManager.commit(xid, onePhase); 89 } 90 catch (Throwable t) 91 { 92 throw SpyXAException.getAsXAException("Resource manager error during commit", t); 93 } 94 } 95 96 public void end(Xid xid, int flags) throws XAException 97 { 98 if (trace) 99 log.trace("End xid=" + xid + ", flags=" + flags + " " +this); 100 101 xid = convertXid(xid); 102 synchronized (session.runLock) 103 { 104 105 switch (flags) 106 { 107 case TMSUSPEND : 108 session.unsetCurrentTransactionId(xid); 109 session.connection.spyXAResourceManager.suspendTx(xid); 110 break; 111 case TMFAIL : 112 session.unsetCurrentTransactionId(xid); 113 session.connection.spyXAResourceManager.endTx(xid, false); 114 break; 115 case TMSUCCESS : 116 session.unsetCurrentTransactionId(xid); 117 session.connection.spyXAResourceManager.endTx(xid, true); 118 break; 119 } 120 } 121 } 122 123 public void forget(Xid xid) throws XAException 124 { 125 if (trace) 126 log.trace("Forget xid=" + xid + " " + this); 127 128 xid = convertXid(xid); 129 try 130 { 131 session.connection.spyXAResourceManager.forget(xid); 132 } 133 catch (Throwable t) 134 { 135 throw SpyXAException.getAsXAException("Resource manager error during forget", t); 136 } 137 } 138 139 public int prepare(Xid xid) throws XAException 140 { 141 if (trace) 142 log.trace("Prepare xid=" + xid + " " + this); 143 144 xid = convertXid(xid); 145 try 146 { 147 return session.connection.spyXAResourceManager.prepare(xid); 148 } 149 catch (Throwable t) 150 { 151 throw SpyXAException.getAsXAException("Resource manager error during prepare", t); 152 } 153 } 154 155 public Xid [] recover(int arg1) throws XAException 156 { 157 if (trace) 158 log.trace("Recover arg1=" + arg1 + " " + this); 159 160 try 161 { 162 return session.connection.spyXAResourceManager.recover(arg1); 163 } 164 catch (Throwable t) 165 { 166 throw SpyXAException.getAsXAException("Resource manager error during recover", t); 167 } 168 } 169 170 public void rollback(Xid xid) throws XAException 171 { 172 if (trace) 173 log.trace("Rollback xid=" + xid + " " + this); 174 175 xid = convertXid(xid); 176 try 177 { 178 session.connection.spyXAResourceManager.rollback(xid); 179 } 180 catch (Throwable t) 181 { 182 throw SpyXAException.getAsXAException("Resource manager error during rollback", t); 183 } 184 } 185 186 public void start(Xid xid, int flags) throws XAException 187 { 188 if (trace) 189 log.trace("Start xid=" + xid + ", flags=" + flags + " " + this); 190 191 xid = convertXid(xid); 192 193 boolean convertTx = false; 194 if (session.getCurrentTransactionId() != null) 195 { 196 if (flags == TMNOFLAGS && session.getCurrentTransactionId() instanceof Long ) 197 { 198 convertTx = true; 199 } 200 else 201 { 202 XAException e = new XAException ("Attempt to enlist in " + xid + " with TMNOFLAGS when already enlisted " + session.getCurrentTransactionId()); 203 e.errorCode = XAException.XAER_OUTSIDE; throw e; 205 } 206 } 207 208 synchronized (session.runLock) 209 { 210 211 switch (flags) 212 { 213 case TMNOFLAGS : 214 if (convertTx) 215 { 216 session.setCurrentTransactionId(session.connection.spyXAResourceManager 219 .convertTx((Long ) session.getCurrentTransactionId(), xid)); 220 } 221 else 222 { 223 session.setCurrentTransactionId(session.connection.spyXAResourceManager.startTx(xid)); 224 } 225 break; 226 case TMJOIN : 227 session.setCurrentTransactionId(session.connection.spyXAResourceManager.joinTx(xid)); 228 break; 229 case TMRESUME : 230 session.setCurrentTransactionId(session.connection.spyXAResourceManager.resumeTx(xid)); 231 break; 232 } 233 session.runLock.notify(); 234 } 235 } 236 237 public String toString() 238 { 239 return "SpyXAResource[session=" + session + ']'; 240 } 241 242 249 protected Xid convertXid(Xid xid) throws XAException 250 { 251 if (xid == null) 252 { 253 XAException e = new XAException ("Null xid"); 254 e.errorCode = XAException.XAER_NOTA; 255 throw e; 256 } 257 258 if (xid instanceof JBossMQXid) 259 return xid; 260 261 return new JBossMQXid(xid); 262 } 263 } | Popular Tags |