1 package fr.jayasoft.ivy.repository.vfs; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 6 import org.apache.commons.httpclient.HostConfiguration; 7 import org.apache.commons.httpclient.HttpConnection; 8 import org.apache.commons.httpclient.HttpConnectionManager; 9 import org.apache.commons.httpclient.params.HttpConnectionManagerParams; 10 11 12 30 class IvyWebdavConnectionManager implements HttpConnectionManager { 31 32 39 static void finishLastResponse(HttpConnection conn) 40 { 41 InputStream lastResponse = conn.getLastResponseInputStream(); 42 if (lastResponse != null) 43 { 44 conn.setLastResponseInputStream(null); 45 try 46 { 47 lastResponse.close(); 48 } 49 catch (IOException ioe) 50 { 51 conn.close(); 53 } 54 } 55 } 56 57 60 protected ThreadLocal localHttpConnection = new ThreadLocal () 61 { 62 protected Object initialValue() 63 { 64 return new Entry(); 65 } 66 }; 67 68 71 private HttpConnectionManagerParams params = new HttpConnectionManagerParams(); 72 73 76 public void releaseLocalConnection() 77 { 78 if (getLocalHttpConnection() != null) 79 { 80 releaseConnection(getLocalHttpConnection()); 81 } 82 } 83 84 private static class Entry 85 { 86 89 private HttpConnection conn = null; 90 91 94 private long idleStartTime = Long.MAX_VALUE; 95 } 96 97 public IvyWebdavConnectionManager() 98 { 99 } 100 101 protected HttpConnection getLocalHttpConnection() 102 { 103 return ((Entry) localHttpConnection.get()).conn; 104 } 105 106 protected void setLocalHttpConnection(HttpConnection conn) 107 { 108 ((Entry) localHttpConnection.get()).conn = conn; 109 } 110 111 protected long getIdleStartTime() 112 { 113 return ((Entry) localHttpConnection.get()).idleStartTime; 114 } 115 116 protected void setIdleStartTime(long idleStartTime) 117 { 118 ((Entry) localHttpConnection.get()).idleStartTime = idleStartTime; 119 } 120 121 124 public HttpConnection getConnection(HostConfiguration hostConfiguration) 125 { 126 return getConnection(hostConfiguration, 0); 127 } 128 129 135 public boolean isConnectionStaleCheckingEnabled() 136 { 137 return this.params.isStaleCheckingEnabled(); 138 } 139 140 147 public void setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled) 148 { 149 this.params.setStaleCheckingEnabled(connectionStaleCheckingEnabled); 150 } 151 152 156 public HttpConnection getConnectionWithTimeout( 157 HostConfiguration hostConfiguration, long timeout) 158 { 159 160 HttpConnection httpConnection = getLocalHttpConnection(); 161 if (httpConnection == null) 162 { 163 httpConnection = new HttpConnection(hostConfiguration); 164 setLocalHttpConnection(httpConnection); 165 httpConnection.setHttpConnectionManager(this); 166 httpConnection.getParams().setDefaults(this.params); 167 } 168 else 169 { 170 171 if (!hostConfiguration.hostEquals(httpConnection) 174 || !hostConfiguration.proxyEquals(httpConnection)) 175 { 176 177 if (httpConnection.isOpen()) 178 { 179 httpConnection.close(); 180 } 181 182 httpConnection.setHost(hostConfiguration.getHost()); 183 httpConnection.setPort(hostConfiguration.getPort()); 184 httpConnection.setProtocol(hostConfiguration.getProtocol()); 185 httpConnection.setLocalAddress(hostConfiguration.getLocalAddress()); 186 187 httpConnection.setProxyHost(hostConfiguration.getProxyHost()); 188 httpConnection.setProxyPort(hostConfiguration.getProxyPort()); 189 } 190 else 191 { 192 finishLastResponse(httpConnection); 193 } 194 } 195 196 setIdleStartTime(Long.MAX_VALUE); 198 199 return httpConnection; 200 } 201 202 206 public HttpConnection getConnection( 207 HostConfiguration hostConfiguration, long timeout) 208 { 209 return getConnectionWithTimeout(hostConfiguration, timeout); 210 } 211 212 215 public void releaseConnection(HttpConnection conn) 216 { 217 if (conn != getLocalHttpConnection()) 218 { 219 throw new IllegalStateException ("Unexpected release of an unknown connection."); 220 } 221 222 finishLastResponse(getLocalHttpConnection()); 223 224 setIdleStartTime(System.currentTimeMillis()); 226 } 227 228 231 public void closeIdleConnections(long idleTimeout) 232 { 233 long maxIdleTime = System.currentTimeMillis() - idleTimeout; 234 if (getIdleStartTime() <= maxIdleTime) 235 { 236 getLocalHttpConnection().close(); 237 } 238 } 239 240 243 public HttpConnectionManagerParams getParams() { 244 return params; 245 } 246 247 250 public void setParams(HttpConnectionManagerParams params) { 251 this.params = params; 252 } 253 254 } 255 | Popular Tags |