1 package org.apache.commons.vfs.provider.webdav; 2 3 import org.apache.commons.httpclient.HostConfiguration; 4 import org.apache.commons.httpclient.HttpConnection; 5 import org.apache.commons.httpclient.HttpConnectionManager; 6 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 10 25 public class WebdavConnectionManager implements HttpConnectionManager 26 { 27 private static class ConnectionParameters 28 { 29 private boolean staleCheck; 30 31 public boolean isStaleCheckingEnabled() 32 { 33 return staleCheck; 34 } 35 36 public void setStaleCheckingEnabled(boolean b) 37 { 38 staleCheck = b; 39 } 40 41 public void populateParameters(HttpConnection connection) 42 { 43 connection.setStaleCheckingEnabled(staleCheck); 44 } 45 } 46 47 54 static void finishLastResponse(HttpConnection conn) 55 { 56 InputStream lastResponse = conn.getLastResponseInputStream(); 57 if (lastResponse != null) 58 { 59 conn.setLastResponseInputStream(null); 60 try 61 { 62 lastResponse.close(); 63 } 64 catch (IOException ioe) 65 { 66 conn.close(); 68 } 69 } 70 } 71 72 75 protected ThreadLocal localHttpConnection = new ThreadLocal () 76 { 77 protected Object initialValue() 78 { 79 return new Entry(); 80 } 81 }; 82 83 86 private ConnectionParameters params = new ConnectionParameters(); 87 88 91 public void releaseLocalConnection() 92 { 93 if (getLocalHttpConnection() != null) 94 { 95 releaseConnection(getLocalHttpConnection()); 96 } 97 } 98 99 private static class Entry 100 { 101 104 private HttpConnection conn = null; 105 106 109 private long idleStartTime = Long.MAX_VALUE; 110 } 111 112 public WebdavConnectionManager() 113 { 114 } 115 116 protected HttpConnection getLocalHttpConnection() 117 { 118 return ((Entry) localHttpConnection.get()).conn; 119 } 120 121 protected void setLocalHttpConnection(HttpConnection conn) 122 { 123 ((Entry) localHttpConnection.get()).conn = conn; 124 } 125 126 protected long getIdleStartTime() 127 { 128 return ((Entry) localHttpConnection.get()).idleStartTime; 129 } 130 131 protected void setIdleStartTime(long idleStartTime) 132 { 133 ((Entry) localHttpConnection.get()).idleStartTime = idleStartTime; 134 } 135 136 139 public HttpConnection getConnection(HostConfiguration hostConfiguration) 140 { 141 return getConnection(hostConfiguration, 0); 142 } 143 144 152 public boolean isConnectionStaleCheckingEnabled() 153 { 154 return this.params.isStaleCheckingEnabled(); 155 } 156 157 166 public void setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled) 167 { 168 this.params.setStaleCheckingEnabled(connectionStaleCheckingEnabled); 169 } 170 171 175 public HttpConnection getConnectionWithTimeout( 176 HostConfiguration hostConfiguration, long timeout) 177 { 178 179 HttpConnection httpConnection = getLocalHttpConnection(); 180 if (httpConnection == null) 181 { 182 httpConnection = new HttpConnection(hostConfiguration); 183 setLocalHttpConnection(httpConnection); 184 httpConnection.setHttpConnectionManager(this); 185 this.params.populateParameters(httpConnection); 186 } 187 else 188 { 189 190 if (!hostConfiguration.hostEquals(httpConnection) 193 || !hostConfiguration.proxyEquals(httpConnection)) 194 { 195 196 if (httpConnection.isOpen()) 197 { 198 httpConnection.close(); 199 } 200 201 httpConnection.setHost(hostConfiguration.getHost()); 202 httpConnection.setPort(hostConfiguration.getPort()); 203 httpConnection.setProtocol(hostConfiguration.getProtocol()); 204 httpConnection.setLocalAddress(hostConfiguration.getLocalAddress()); 205 206 httpConnection.setProxyHost(hostConfiguration.getProxyHost()); 207 httpConnection.setProxyPort(hostConfiguration.getProxyPort()); 208 } 209 else 210 { 211 finishLastResponse(httpConnection); 212 } 213 } 214 215 setIdleStartTime(Long.MAX_VALUE); 217 218 return httpConnection; 219 } 220 221 225 public HttpConnection getConnection( 226 HostConfiguration hostConfiguration, long timeout) 227 { 228 return getConnectionWithTimeout(hostConfiguration, timeout); 229 } 230 231 234 public void releaseConnection(HttpConnection conn) 235 { 236 if (conn != getLocalHttpConnection()) 237 { 238 throw new IllegalStateException ("Unexpected release of an unknown connection."); 239 } 240 241 finishLastResponse(getLocalHttpConnection()); 242 243 setIdleStartTime(System.currentTimeMillis()); 245 } 246 247 250 public void closeIdleConnections(long idleTimeout) 251 { 252 long maxIdleTime = System.currentTimeMillis() - idleTimeout; 253 if (getIdleStartTime() <= maxIdleTime) 254 { 255 getLocalHttpConnection().close(); 256 } 257 } 258 } 259 | Popular Tags |