1 23 24 package org.objectweb.perseus.connector.ra.fos; 25 26 import org.objectweb.perseus.fos.api.FosException; 27 import org.objectweb.perseus.fos.api.FosLoggerFactory; 28 import org.objectweb.perseus.fos.api.FosTransaction; 29 import org.objectweb.perseus.fos.lib.FosTxContext; 30 import org.objectweb.util.monolog.api.BasicLevel; 31 import org.objectweb.util.monolog.api.Logger; 32 33 import java.io.IOException ; 34 import javax.transaction.xa.XAException ; 35 import javax.transaction.xa.XAResource ; 36 import javax.transaction.xa.Xid ; 37 38 41 public class FosXAResource implements XAResource { 42 45 private Logger logger; 46 51 Xid xid = null; 52 57 FosTxContext txContext = null; 58 63 boolean endWithNoSameRM = true; 64 67 boolean prepared = false; 68 71 FosXAResourceFactory xaResourceFactory; 72 73 79 FosXAResource(Logger el, FosXAResourceFactory fxarf) throws FosException { 80 logger = el; 81 if (FosLoggerFactory.DEBUG) 82 logger.log(BasicLevel.DEBUG, "Constructs a new FosXAResource."); 83 xaResourceFactory = fxarf; 84 } 85 86 89 FosTxContext getTxContext() throws FosException { 90 return txContext; 91 } 92 93 96 private void getTxContext(Xid xid, boolean create) 97 throws XAException { 98 if (this.xid != null) { 99 if (this.xid != xid) { 100 throw new XAException (XAException.XAER_PROTO); 102 } 103 logger.log(BasicLevel.WARN, "XAResource already inside DTP context: " 104 + xid 105 + " - ignored."); 106 return; 107 } 108 if (txContext != null) 109 throw new XAException (XAException.XAER_PROTO); 110 txContext = xaResourceFactory.getTxContext(xid); 112 if (txContext == null) { 113 if (!create) { 114 throw new XAException (XAException.XAER_PROTO); 116 } 117 try { 118 txContext = xaResourceFactory.createTxContext(xid); 120 } catch (FosException fe) { 121 throw new XAException (XAException.XAER_RMERR); 122 } 123 } 124 this.xid = xid; 125 } 126 127 129 131 135 public void start(Xid xid, int i) throws XAException { 136 getTxContext(xid, true); 137 } 138 139 142 public void end(Xid xid, int i) throws XAException { 143 if (this.xid != xid) 144 throw new XAException (XAException.XAER_PROTO); 145 if (endWithNoSameRM) { 146 } else { 149 this.xid = null; 150 txContext = null; 151 endWithNoSameRM = true; 152 xaResourceFactory.releaseXAResource(this); 153 } 154 } 155 156 163 public boolean isSameRM(XAResource resource) throws XAException { 164 if (resource instanceof FosXAResource) 165 return false; 166 if (((FosXAResource) resource).xaResourceFactory != xaResourceFactory) 167 return false; 168 ((FosXAResource) resource).endWithNoSameRM = false; 169 endWithNoSameRM = false; 170 return true; 171 } 172 173 175 178 public int prepare(Xid xid) throws XAException { 179 getTxContext(xid, false); 180 try { 181 if (txContext.prepare()) { 182 prepared = true; 183 return XAResource.XA_RDONLY; 184 } 185 prepared = true; 186 return XAResource.XA_OK; 187 } catch (FosException e) { 188 throw new XAException (XAException.XAER_RMERR); 189 } 190 } 191 192 195 public void commit(Xid xid, boolean b) throws XAException { 196 getTxContext(xid, false); 197 try { 198 if (!prepared) 199 throw new XAException (XAException.XAER_PROTO); 200 txContext.commit(); 201 xaResourceFactory.releaseTxContext(xid); 202 } catch (FosException e) { 203 throw new XAException (XAException.XAER_RMERR); 204 } finally { 205 prepared = false; 206 this.xid = null; 207 txContext = null; 208 } 209 } 210 211 214 public void rollback(Xid xid) throws XAException { 215 getTxContext(xid, false); 216 try { 217 txContext.rollback(); 218 xaResourceFactory.releaseTxContext(xid); 219 } catch (FosException e) { 220 throw new XAException (XAException.XAER_RMERR); 221 } finally { 222 prepared = false; 223 this.xid = null; 224 txContext = null; 225 } 226 } 227 228 230 233 public void forget(Xid xid) throws XAException { 234 } 235 236 240 public Xid [] recover(int i) throws XAException { 241 return xaResourceFactory.getXidForRecovery(); 242 } 243 244 247 250 public int getTransactionTimeout() throws XAException { 251 return 0; 252 } 253 254 257 public boolean setTransactionTimeout(int i) throws XAException { 258 return false; 259 } 260 } 261 | Popular Tags |