1. /*
  2. * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java,v 1.21 2004/05/13 04:03:25 mbecke Exp $
  3. * $Revision: 1.21 $
  4. * $Date: 2004/05/13 04:03:25 $
  5. *
  6. * ====================================================================
  7. *
  8. * Copyright 2002-2004 The Apache Software Foundation
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * ====================================================================
  22. *
  23. * This software consists of voluntary contributions made by many
  24. * individuals on behalf of the Apache Software Foundation. For more
  25. * information on the Apache Software Foundation, please see
  26. * <http://www.apache.org/>.
  27. *
  28. */
  29. package org.apache.commons.httpclient;
  30. import java.io.IOException;
  31. import java.io.InputStream;
  32. import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
  33. /**
  34. * A connection manager that provides access to a single HttpConnection. This
  35. * manager makes no attempt to provide exclusive access to the contained
  36. * HttpConnection.
  37. *
  38. * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
  39. * @author Eric Johnson
  40. * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
  41. * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  42. * @author Laura Werner
  43. *
  44. * @since 2.0
  45. */
  46. public class SimpleHttpConnectionManager implements HttpConnectionManager {
  47. /**
  48. * Since the same connection is about to be reused, make sure the
  49. * previous request was completely processed, and if not
  50. * consume it now.
  51. * @param conn The connection
  52. */
  53. static void finishLastResponse(HttpConnection conn) {
  54. InputStream lastResponse = conn.getLastResponseInputStream();
  55. if (lastResponse != null) {
  56. conn.setLastResponseInputStream(null);
  57. try {
  58. lastResponse.close();
  59. } catch (IOException ioe) {
  60. //FIXME: badness - close to force reconnect.
  61. conn.close();
  62. }
  63. }
  64. }
  65. /** The http connection */
  66. private HttpConnection httpConnection;
  67. /**
  68. * Collection of parameters associated with this connection manager.
  69. */
  70. private HttpConnectionManagerParams params = new HttpConnectionManagerParams();
  71. /**
  72. * The time the connection was made idle.
  73. */
  74. private long idleStartTime = Long.MAX_VALUE;
  75. public SimpleHttpConnectionManager() {
  76. }
  77. /**
  78. * @see HttpConnectionManager#getConnection(HostConfiguration)
  79. */
  80. public HttpConnection getConnection(HostConfiguration hostConfiguration) {
  81. return getConnection(hostConfiguration, 0);
  82. }
  83. /**
  84. * Gets the staleCheckingEnabled value to be set on HttpConnections that are created.
  85. *
  86. * @return <code>true</code> if stale checking will be enabled on HttpConections
  87. *
  88. * @see HttpConnection#isStaleCheckingEnabled()
  89. *
  90. * @deprecated Use {@link HttpConnectionManagerParams#isStaleCheckingEnabled()},
  91. * {@link HttpConnectionManager#getParams()}.
  92. */
  93. public boolean isConnectionStaleCheckingEnabled() {
  94. return this.params.isStaleCheckingEnabled();
  95. }
  96. /**
  97. * Sets the staleCheckingEnabled value to be set on HttpConnections that are created.
  98. *
  99. * @param connectionStaleCheckingEnabled <code>true</code> if stale checking will be enabled
  100. * on HttpConections
  101. *
  102. * @see HttpConnection#setStaleCheckingEnabled(boolean)
  103. *
  104. * @deprecated Use {@link HttpConnectionManagerParams#setStaleCheckingEnabled(boolean)},
  105. * {@link HttpConnectionManager#getParams()}.
  106. */
  107. public void setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled) {
  108. this.params.setStaleCheckingEnabled(connectionStaleCheckingEnabled);
  109. }
  110. /**
  111. * @see HttpConnectionManager#getConnectionWithTimeout(HostConfiguration, long)
  112. *
  113. * @since 3.0
  114. */
  115. public HttpConnection getConnectionWithTimeout(
  116. HostConfiguration hostConfiguration, long timeout) {
  117. if (httpConnection == null) {
  118. httpConnection = new HttpConnection(hostConfiguration);
  119. httpConnection.setHttpConnectionManager(this);
  120. httpConnection.getParams().setDefaults(this.params);
  121. } else {
  122. // make sure the host and proxy are correct for this connection
  123. // close it and set the values if they are not
  124. if (!hostConfiguration.hostEquals(httpConnection)
  125. || !hostConfiguration.proxyEquals(httpConnection)) {
  126. if (httpConnection.isOpen()) {
  127. httpConnection.close();
  128. }
  129. httpConnection.setHost(hostConfiguration.getHost());
  130. httpConnection.setVirtualHost(hostConfiguration.getVirtualHost());
  131. httpConnection.setPort(hostConfiguration.getPort());
  132. httpConnection.setProtocol(hostConfiguration.getProtocol());
  133. httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());
  134. httpConnection.setProxyHost(hostConfiguration.getProxyHost());
  135. httpConnection.setProxyPort(hostConfiguration.getProxyPort());
  136. } else {
  137. finishLastResponse(httpConnection);
  138. }
  139. }
  140. // remove the connection from the timeout handler
  141. idleStartTime = Long.MAX_VALUE;
  142. return httpConnection;
  143. }
  144. /**
  145. * @see HttpConnectionManager#getConnection(HostConfiguration, long)
  146. *
  147. * @deprecated Use #getConnectionWithTimeout(HostConfiguration, long)
  148. */
  149. public HttpConnection getConnection(
  150. HostConfiguration hostConfiguration, long timeout) {
  151. return getConnectionWithTimeout(hostConfiguration, timeout);
  152. }
  153. /**
  154. * @see HttpConnectionManager#releaseConnection(org.apache.commons.httpclient.HttpConnection)
  155. */
  156. public void releaseConnection(HttpConnection conn) {
  157. if (conn != httpConnection) {
  158. throw new IllegalStateException("Unexpected release of an unknown connection.");
  159. }
  160. finishLastResponse(httpConnection);
  161. // track the time the connection was made idle
  162. idleStartTime = System.currentTimeMillis();
  163. }
  164. /**
  165. * Returns {@link HttpConnectionManagerParams parameters} associated
  166. * with this connection manager.
  167. *
  168. * @since 2.1
  169. *
  170. * @see HttpConnectionManagerParams
  171. */
  172. public HttpConnectionManagerParams getParams() {
  173. return this.params;
  174. }
  175. /**
  176. * Assigns {@link HttpConnectionManagerParams parameters} for this
  177. * connection manager.
  178. *
  179. * @since 2.1
  180. *
  181. * @see HttpConnectionManagerParams
  182. */
  183. public void setParams(final HttpConnectionManagerParams params) {
  184. if (params == null) {
  185. throw new IllegalArgumentException("Parameters may not be null");
  186. }
  187. this.params = params;
  188. }
  189. /**
  190. * @since 3.0
  191. */
  192. public void closeIdleConnections(long idleTimeout) {
  193. long maxIdleTime = System.currentTimeMillis() - idleTimeout;
  194. if (idleStartTime <= maxIdleTime) {
  195. httpConnection.close();
  196. }
  197. }
  198. }