1 25 26 27 package org.objectweb.jonas.resource; 28 29 import javax.resource.ResourceException ; 30 import javax.resource.spi.LocalTransaction ; 31 32 import javax.transaction.xa.XAResource ; 33 import javax.transaction.xa.XAException ; 34 import javax.transaction.xa.Xid ; 35 36 import org.objectweb.util.monolog.api.Logger; 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 46 public final class LocalXAWrapper 47 implements XAResource { 48 49 52 private static Logger logger = null; 53 54 57 protected boolean isInTransaction; 58 59 63 protected LocalTransaction localTrans = null; 64 65 LocalXAWrapper(LocalTransaction _localTrans, Logger _logger) { 66 isInTransaction = false; 67 localTrans = _localTrans; 68 logger = _logger; 69 } 70 71 79 public void commit(Xid xid, boolean flag) 80 throws XAException { 81 if (logger.isLoggable(BasicLevel.DEBUG)) { 82 logger.log(BasicLevel.DEBUG, "Xid =" + xid + "flag=" + flag); 83 } 84 85 86 if (isInTransaction) { 87 if (logger.isLoggable(BasicLevel.DEBUG)) { 90 logger.log(BasicLevel.DEBUG, "XA START without XA END"); 91 } 92 isInTransaction = false; 93 } 94 95 try { 96 localTrans.commit(); 97 } catch (ResourceException res) { 98 XAException xaE = new XAException (res.getMessage()); 99 xaE.initCause(res); 100 throw xaE; 101 } 102 } 103 104 108 public void end(Xid xid, int i) 109 throws XAException { 110 if (logger.isLoggable(BasicLevel.DEBUG)) { 111 logger.log(BasicLevel.DEBUG, "Xid =" + xid + "i=" + i); 112 } 113 if (!isInTransaction) { 114 logger.log(BasicLevel.ERROR, "END without START"); 115 XAException ex = new XAException (XAException.XA_RBPROTO); 116 throw(ex); 117 } 118 isInTransaction = false; 119 120 } 121 122 126 public void forget(Xid xid) 127 throws XAException { 128 if (logger.isLoggable(BasicLevel.DEBUG)) { 129 logger.log(BasicLevel.DEBUG, ""); 130 } 131 } 132 133 137 public int getTransactionTimeout() 138 throws XAException { 139 if (logger.isLoggable(BasicLevel.DEBUG)) { 140 logger.log(BasicLevel.DEBUG, ""); 141 } 142 return -1; 143 } 144 145 152 153 public boolean isSameRM(XAResource xaresource) 154 throws XAException { 155 if (logger.isLoggable(BasicLevel.DEBUG)) { 156 logger.log(BasicLevel.DEBUG, ""); 157 } 158 159 if (xaresource.equals(this)) { 160 if (logger.isLoggable(BasicLevel.DEBUG)) { 161 logger.log(BasicLevel.DEBUG, "isSameRM = true " + this); 162 } 163 return true; 164 } 165 if (logger.isLoggable(BasicLevel.DEBUG)) { 166 logger.log(BasicLevel.DEBUG, "isSameRM = false " + this); 167 } 168 return false; 169 } 170 171 175 public int prepare(Xid xid) 176 throws XAException { 177 if (logger.isLoggable(BasicLevel.DEBUG)) { 178 logger.log(BasicLevel.DEBUG, ""); 179 } 180 return XAResource.XA_OK; 181 } 182 183 186 public Xid [] recover(int i) 187 throws XAException { 188 if (logger.isLoggable(BasicLevel.DEBUG)) { 189 logger.log(BasicLevel.DEBUG, ""); 190 } 191 return null; 192 } 193 194 201 public void rollback(Xid xid) 202 throws XAException { 203 if (logger.isLoggable(BasicLevel.DEBUG)) { 204 logger.log(BasicLevel.DEBUG, "Xid =" + xid); 205 } 206 207 if (isInTransaction) { 208 if (logger.isLoggable(BasicLevel.DEBUG)) { 212 logger.log(BasicLevel.DEBUG, "XA START without XA END"); 213 } 214 isInTransaction = false; 215 } 216 217 try { 218 localTrans.rollback(); 219 } catch (ResourceException res) { 220 XAException xaE = new XAException ("Rollback failed"); 221 xaE.initCause(res); 222 throw xaE; 223 } 224 } 225 226 230 public boolean setTransactionTimeout(int i) 231 throws XAException { 232 if (logger.isLoggable(BasicLevel.DEBUG)) { 233 logger.log(BasicLevel.DEBUG, ""); 234 } 235 return false; 236 } 237 238 246 public void start(Xid xid, int i) 247 throws XAException { 248 if (logger.isLoggable(BasicLevel.DEBUG)) { 249 logger.log(BasicLevel.DEBUG, "Xid =" + xid + "i=" + i); 250 } 251 try { 252 if (i != XAResource.TMJOIN && i != XAResource.TMRESUME) { 253 if (isInTransaction) { 254 throw new XAException ("LocalXAWrapper.start: Local transaction already started"); 255 } 256 localTrans.begin(); 257 isInTransaction = true; 258 } 259 } catch (ResourceException res) { 260 XAException xaE = new XAException ("LocalTransaction.begin failed"); 261 xaE.initCause(res); 262 throw xaE; 263 } 264 265 if (logger.isLoggable(BasicLevel.DEBUG)) { 266 logger.log(BasicLevel.DEBUG, "OK"); 267 } 268 } 269 } 270 | Popular Tags |