1 28 29 package org.apache.commons.transaction.memory.jca; 30 31 import java.io.PrintWriter ; 32 33 import javax.transaction.xa.XAException ; 34 import javax.transaction.xa.XAResource ; 35 import javax.transaction.xa.Xid ; 36 37 import org.apache.commons.transaction.memory.TransactionalMapWrapper; 38 import org.apache.commons.transaction.util.LoggerFacade; 39 import org.apache.commons.transaction.util.PrintWriterLogger; 40 import org.apache.commons.transaction.util.xa.AbstractTransactionalResource; 41 import org.apache.commons.transaction.util.xa.AbstractXAResource; 42 import org.apache.commons.transaction.util.xa.TransactionalResource; 43 44 49 public class MapXAResource extends AbstractXAResource { 50 51 TransactionalMapWrapper map; 52 LoggerFacade loggerFacade; 53 54 public MapXAResource(TransactionalMapWrapper map) { 55 this.map = map; 56 this.loggerFacade = new PrintWriterLogger(new PrintWriter (System.out), "WebDAVXAResource", false); 58 } 59 60 public MapXAResource(TransactionalMapWrapper map, LoggerFacade loggerFacade) { 61 this.map = map; 62 this.loggerFacade = loggerFacade; 63 } 64 65 public int getTransactionTimeout() throws XAException { 66 return 0; 67 } 68 69 public boolean setTransactionTimeout(int seconds) throws XAException { 70 return false; 71 } 72 73 public boolean isSameRM(XAResource xares) throws XAException { 74 return (xares != null && xares instanceof MapXAResource && map.equals(((MapXAResource) xares).map)); 75 } 76 77 public Xid [] recover(int flag) throws XAException { 78 return null; 79 } 80 81 public LoggerFacade getLoggerFacade() { 82 return loggerFacade; 83 } 84 85 public void setLoggerFacade(LoggerFacade loggerFacade) { 86 this.loggerFacade = loggerFacade; 87 } 88 89 protected void setLoggerFacade(PrintWriter out) { 90 loggerFacade = new PrintWriterLogger(out, "WebDAVXAResource", true); 91 } 92 93 protected TransactionalResource createTransactionResource(Xid xid) throws Exception { 94 return new MapTransactionalResource(xid, map, getLoggerFacade()); 95 } 96 97 protected boolean includeBranchInXid() { 98 return true; 99 } 100 101 protected static class MapTransactionalResource extends AbstractTransactionalResource { 102 103 TransactionalMapWrapper map; 104 private TransactionalMapWrapper.TxContext txContext = null; 105 106 LoggerFacade loggerFacade; 107 108 public MapTransactionalResource(Xid xid, TransactionalMapWrapper map, LoggerFacade loggerFacade) { 109 super(xid); 110 this.map = map; 111 this.loggerFacade = loggerFacade; 112 } 113 114 public void commit() throws XAException { 115 try { 116 map.commitTransaction(); 117 } catch (IllegalStateException e) { 118 throw new XAException (e.toString()); 119 } 120 } 121 122 public void rollback() throws XAException { 123 if (isSuspended()) 126 resume(); 127 128 try { 129 map.rollbackTransaction(); 130 } catch (IllegalStateException e) { 131 throw new XAException (e.toString()); 132 } 133 } 134 135 public int prepare() throws XAException { 136 if (isSuspended()) 139 resume(); 140 141 if (map.isTransactionMarkedForRollback()) { 142 throw new XAException (XAException.XA_RBROLLBACK); 143 } 144 145 return (map.isReadOnly() ? XA_RDONLY : XA_OK); 146 } 147 148 public void suspend() throws XAException { 149 if (isSuspended()) { 150 throw new XAException (XAException.XAER_PROTO); 151 } 152 this.txContext = map.suspendTransaction(); 153 } 154 155 public void resume() throws XAException { 156 if (!isSuspended()) { 157 throw new XAException (XAException.XAER_PROTO); 158 } 159 map.resumeTransaction(this.txContext); 160 this.txContext = null; 161 } 162 163 public void begin() throws XAException { 164 if (isSuspended()) { 165 throw new XAException (XAException.XAER_PROTO); 166 } 167 this.map.startTransaction(); 168 } 169 170 private boolean isSuspended() { 171 return this.txContext != null; 172 } 173 } 174 } | Popular Tags |