1 23 24 package org.apache.webdav.connector; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.Iterator ; 31 32 import javax.resource.ResourceException ; 33 import javax.resource.spi.ConnectionEvent ; 34 import javax.resource.spi.ConnectionEventListener ; 35 import javax.resource.spi.ConnectionRequestInfo ; 36 import javax.resource.spi.LocalTransaction ; 37 import javax.resource.spi.ManagedConnection ; 38 import javax.resource.spi.ManagedConnectionMetaData ; 39 import javax.security.auth.Subject ; 40 import javax.transaction.xa.XAResource ; 41 42 import org.apache.commons.httpclient.HttpException; 43 import org.apache.commons.httpclient.URIException; 44 import org.apache.webdav.lib.WebdavResource; 45 46 51 public class WebDAVManagedConnection implements ManagedConnection { 52 53 protected WebDAVXAResource xares = null; 54 55 protected WebDAVLocalTransaction tx = null; 56 57 protected String name = null; 58 59 protected WebdavResource webdavResource; 60 61 protected WebDAVConnectionSpec webDAVConnectionSpec; 62 63 protected WebDAVConnection connection = null; 64 65 protected List listeners = new ArrayList (); 66 67 protected PrintWriter out; 68 69 public WebDAVManagedConnection(ConnectionRequestInfo cxRequestInfo) throws HttpException, IOException { 70 open((WebDAVConnectionSpec) cxRequestInfo); 71 } 72 73 public WebdavResource getWebdavResource() { 74 return webdavResource; 75 } 76 77 public void close() { 78 ConnectionEvent event = new ConnectionEvent (this, ConnectionEvent.CONNECTION_CLOSED); 79 event.setConnectionHandle(connection); 80 for (Iterator it = listeners.iterator(); it.hasNext();) { 81 ((ConnectionEventListener ) it.next()).connectionClosed(event); 82 } 83 } 84 85 88 public Object getConnection(Subject subject, ConnectionRequestInfo cxRequestInfo) throws ResourceException { 89 90 if (connection == null) { 91 connection = new WebDAVConnection(this); 92 } 93 return connection; 94 } 95 96 99 public void destroy() throws ResourceException { 100 101 if (connection != null) { 102 connection.invalidate(); 103 connection = null; 104 } 105 106 listeners = null; 107 name = null; 108 xares = null; 109 tx = null; 110 try { 111 webdavResource.close(); 112 } catch (IOException e) { 113 throw new ResourceException (e); 114 } 115 } 116 117 120 public void cleanup() throws ResourceException { 121 126 if (connection != null) { 127 connection.invalidate(); 128 connection = null; 129 } 130 131 name = null; 132 xares = null; 133 tx = null; 134 try { 135 webdavResource.close(); 136 } catch (IOException e) { 137 throw new ResourceException (e); 138 } 139 } 140 141 144 public void associateConnection(Object connection) throws ResourceException { 145 if (!(connection instanceof WebDAVConnection)) { 146 throw new ResourceException ("Connection is not of type WebDAVConnection"); 147 } 148 149 this.connection = (WebDAVConnection) connection; 150 try { 151 open(this.connection.mc.webDAVConnectionSpec); 152 } catch (URIException e) { 153 throw new ResourceException ("Could not associate connection", e); 154 } catch (IOException e) { 155 throw new ResourceException ("Could not associate connection", e); 156 } 157 this.connection.mc = this; 158 } 159 160 163 public void addConnectionEventListener(ConnectionEventListener listener) { 164 165 listeners.add(listener); 166 } 167 168 171 public void removeConnectionEventListener(ConnectionEventListener listener) { 172 173 listeners.remove(listener); 174 } 175 176 179 public XAResource getXAResource() throws ResourceException { 180 return xares; 181 } 182 183 186 public LocalTransaction getLocalTransaction() throws ResourceException { 187 return tx; 188 } 189 190 193 public ManagedConnectionMetaData getMetaData() throws ResourceException { 194 195 return null; 196 } 197 198 201 public void setLogWriter(PrintWriter out) throws ResourceException { 202 this.out = out; 203 xares.setLoggerFacade(out); 204 } 205 206 209 public PrintWriter getLogWriter() throws ResourceException { 210 211 return out; 212 } 213 214 protected void open(WebDAVConnectionSpec webDAVConnectionSpec) throws IOException , URIException { 215 this.webDAVConnectionSpec = webDAVConnectionSpec; 216 System.out.println("Opening: "+webDAVConnectionSpec.getHttpURL()); webdavResource = new WebdavResource(webDAVConnectionSpec.getHttpURL()); 218 System.out.println("Opened"); String owner = webDAVConnectionSpec.getHttpURL().getUser(); 220 if (owner == null) 221 owner = "WebDAV Connector"; 222 223 tx = new WebDAVLocalTransaction(webdavResource, owner, webDAVConnectionSpec.getTimeout()); 224 xares = new WebDAVXAResource(webdavResource, owner); 225 } 226 227 } | Popular Tags |