1 23 24 package org.apache.webdav.connector; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 29 import javax.transaction.xa.XAException ; 30 import javax.transaction.xa.XAResource ; 31 import javax.transaction.xa.Xid ; 32 33 import org.apache.commons.transaction.util.LoggerFacade; 34 import org.apache.commons.transaction.util.PrintWriterLogger; 35 import org.apache.commons.transaction.util.xa.AbstractTransactionalResource; 36 import org.apache.commons.transaction.util.xa.AbstractXAResource; 37 import org.apache.commons.transaction.util.xa.TransactionalResource; 38 import org.apache.webdav.lib.WebdavResource; 39 40 45 public class WebDAVXAResource extends AbstractXAResource { 46 47 protected WebdavResource webdavResource; 48 49 protected String owner; 50 51 protected int timeout = 10; 52 53 protected LoggerFacade loggerFacade; 54 55 public WebDAVXAResource(WebdavResource webdavResource, String owner) { 56 this.webdavResource = webdavResource; 57 this.owner = owner; 58 this.loggerFacade = new PrintWriterLogger(new PrintWriter (System.out), "WebDAVXAResource", false); 60 } 61 62 protected LoggerFacade getLoggerFacade() { 63 return loggerFacade; 64 } 65 66 protected void setLoggerFacade(PrintWriter out) { 67 loggerFacade = new PrintWriterLogger(out, "WebDAVXAResource", true); 68 } 69 70 public int getTransactionTimeout() throws XAException { 71 return timeout; 72 } 73 74 public boolean setTransactionTimeout(int seconds) throws XAException { 75 timeout = seconds; 76 return true; 77 } 78 79 public boolean isSameRM(XAResource xares) throws XAException { 80 return (xares != null && xares instanceof WebDAVXAResource && webdavResource 81 .equals(((WebDAVXAResource) xares).webdavResource)); 82 } 83 84 public Xid [] recover(int flag) throws XAException { 85 return null; 87 } 88 89 protected TransactionalResource createTransactionResource(Xid xid) throws Exception { 90 return new TransactionalWebDAVResource(xid, webdavResource, owner, timeout, getLoggerFacade()); 91 } 92 93 protected boolean includeBranchInXid() { 94 return true; 95 } 96 97 protected static class TransactionalWebDAVResource extends AbstractTransactionalResource { 98 99 WebdavResource webdavResource; 100 LoggerFacade loggerFacade; 101 102 public TransactionalWebDAVResource(Xid xid, WebdavResource webdavResource, String owner, int timeout, LoggerFacade loggerFacade) throws IOException { 103 super(xid); 104 this.webdavResource = webdavResource; 105 System.out.println("Statring "+webdavResource); 106 webdavResource.startTransaction(owner, timeout); 107 this.loggerFacade = loggerFacade; 108 } 109 110 public void commit() throws XAException { 111 try { 112 webdavResource.commitTransaction(); 113 } catch (IOException e) { 114 loggerFacade.logWarning("Could not commit transaction", e); 115 throw new XAException ("Could not commit transaction"); 116 } 117 } 118 119 public void rollback() throws XAException { 120 try { 121 webdavResource.abortTransaction(); 122 } catch (IOException e) { 123 loggerFacade.logWarning("Could not roll back transaction", e); 124 throw new XAException ("Could not roll back transaction"); 125 } 126 } 127 128 public int prepare() throws XAException { 129 return XA_OK; 130 } 131 132 public void begin() throws XAException { 133 } 134 135 public void resume() throws XAException { 136 } 137 138 public void suspend() throws XAException { 139 } 140 } 141 } | Popular Tags |