1 31 32 package org.apache.commons.httpclient; 33 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 37 50 public class SimpleHttpConnectionManager implements HttpConnectionManager { 51 52 53 private HttpConnection httpConnection; 54 55 56 private boolean connectionStaleCheckingEnabled = true; 57 58 61 public SimpleHttpConnectionManager() { 62 super(); 63 } 64 65 68 public HttpConnection getConnection(HostConfiguration hostConfiguration) { 69 return getConnection(hostConfiguration, 0); 70 } 71 72 79 public boolean isConnectionStaleCheckingEnabled() { 80 return connectionStaleCheckingEnabled; 81 } 82 83 91 public void setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled) { 92 this.connectionStaleCheckingEnabled = connectionStaleCheckingEnabled; 93 } 94 95 98 public HttpConnection getConnection( 99 HostConfiguration hostConfiguration, long timeout) { 100 101 if (httpConnection == null) { 102 httpConnection = new HttpConnection(hostConfiguration); 103 httpConnection.setStaleCheckingEnabled(connectionStaleCheckingEnabled); 104 } else { 105 106 if (!hostConfiguration.hostEquals(httpConnection) 109 || !hostConfiguration.proxyEquals(httpConnection)) { 110 111 if (httpConnection.isOpen()) { 112 httpConnection.close(); 113 } 114 115 httpConnection.setStaleCheckingEnabled(connectionStaleCheckingEnabled); 116 117 httpConnection.setHost(hostConfiguration.getHost()); 118 httpConnection.setVirtualHost(hostConfiguration.getVirtualHost()); 119 httpConnection.setPort(hostConfiguration.getPort()); 120 httpConnection.setProtocol(hostConfiguration.getProtocol()); 121 httpConnection.setLocalAddress(hostConfiguration.getLocalAddress()); 122 123 httpConnection.setProxyHost(hostConfiguration.getProxyHost()); 124 httpConnection.setProxyPort(hostConfiguration.getProxyPort()); 125 } else { 126 finishLastResponse(httpConnection); 127 } 128 } 129 130 return httpConnection; 131 } 132 133 136 public void releaseConnection(HttpConnection conn) { 137 if (conn != httpConnection) { 138 throw new IllegalStateException ("Unexpected close on a different connection."); 139 } 140 141 finishLastResponse(httpConnection); 142 } 143 144 150 static void finishLastResponse(HttpConnection conn) { 151 InputStream lastResponse = conn.getLastResponseInputStream(); 152 if (lastResponse != null) { 153 conn.setLastResponseInputStream(null); 154 try { 155 lastResponse.close(); 156 } catch (IOException ioe) { 157 conn.close(); 159 } 160 } 161 } 162 } 163 | Popular Tags |