View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/SimpleHttpConnectionManager.java,v 1.23 2004/10/16 22:40:08 mbecke Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
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  
30  package org.apache.commons.httpclient;
31  
32  import java.io.IOException;
33  import java.io.InputStream;
34  
35  import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
36  
37  /***
38   * A connection manager that provides access to a single HttpConnection.  This
39   * manager makes no attempt to provide exclusive access to the contained
40   * HttpConnection.
41   *
42   * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
43   * @author Eric Johnson
44   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
45   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
46   * @author Laura Werner
47   * 
48   * @since 2.0
49   */
50  public class SimpleHttpConnectionManager implements HttpConnectionManager {
51  
52      /***
53       * Since the same connection is about to be reused, make sure the
54       * previous request was completely processed, and if not
55       * consume it now.
56       * @param conn The connection
57       */
58      static void finishLastResponse(HttpConnection conn) {
59          InputStream lastResponse = conn.getLastResponseInputStream();
60          if (lastResponse != null) {
61              conn.setLastResponseInputStream(null);
62              try {
63                  lastResponse.close();
64              } catch (IOException ioe) {
65                  //FIXME: badness - close to force reconnect.
66                  conn.close();
67              }
68          }
69      }
70      
71      /*** The http connection */
72      protected HttpConnection httpConnection;
73  
74      /***
75       * Collection of parameters associated with this connection manager.
76       */
77      private HttpConnectionManagerParams params = new HttpConnectionManagerParams(); 
78  
79      /***
80       * The time the connection was made idle.
81       */
82      private long idleStartTime = Long.MAX_VALUE;
83      
84      public SimpleHttpConnectionManager() {
85      }
86      
87      /***
88       * @see HttpConnectionManager#getConnection(HostConfiguration)
89       */
90      public HttpConnection getConnection(HostConfiguration hostConfiguration) {
91          return getConnection(hostConfiguration, 0);
92      }
93  
94      /***
95       * Gets the staleCheckingEnabled value to be set on HttpConnections that are created.
96       * 
97       * @return <code>true</code> if stale checking will be enabled on HttpConections
98       * 
99       * @see HttpConnection#isStaleCheckingEnabled()
100      * 
101      * @deprecated Use {@link HttpConnectionManagerParams#isStaleCheckingEnabled()},
102      * {@link HttpConnectionManager#getParams()}.
103      */
104     public boolean isConnectionStaleCheckingEnabled() {
105         return this.params.isStaleCheckingEnabled();
106     }
107 
108     /***
109      * Sets the staleCheckingEnabled value to be set on HttpConnections that are created.
110      * 
111      * @param connectionStaleCheckingEnabled <code>true</code> if stale checking will be enabled 
112      * on HttpConections
113      * 
114      * @see HttpConnection#setStaleCheckingEnabled(boolean)
115      * 
116      * @deprecated Use {@link HttpConnectionManagerParams#setStaleCheckingEnabled(boolean)},
117      * {@link HttpConnectionManager#getParams()}.
118      */
119     public void setConnectionStaleCheckingEnabled(boolean connectionStaleCheckingEnabled) {
120         this.params.setStaleCheckingEnabled(connectionStaleCheckingEnabled);
121     }
122     
123     /***
124      * @see HttpConnectionManager#getConnectionWithTimeout(HostConfiguration, long)
125      * 
126      * @since 3.0
127      */
128     public HttpConnection getConnectionWithTimeout(
129         HostConfiguration hostConfiguration, long timeout) {
130 
131         if (httpConnection == null) {
132             httpConnection = new HttpConnection(hostConfiguration);
133             httpConnection.setHttpConnectionManager(this);
134             httpConnection.getParams().setDefaults(this.params);
135         } else {
136 
137             // make sure the host and proxy are correct for this connection
138             // close it and set the values if they are not
139             if (!hostConfiguration.hostEquals(httpConnection)
140                 || !hostConfiguration.proxyEquals(httpConnection)) {
141                     
142                 if (httpConnection.isOpen()) {
143                     httpConnection.close();
144                 }
145 
146                 httpConnection.setHost(hostConfiguration.getHost());
147                 httpConnection.setPort(hostConfiguration.getPort());
148                 httpConnection.setProtocol(hostConfiguration.getProtocol());
149                 httpConnection.setLocalAddress(hostConfiguration.getLocalAddress());
150 
151                 httpConnection.setProxyHost(hostConfiguration.getProxyHost());
152                 httpConnection.setProxyPort(hostConfiguration.getProxyPort());
153             } else {
154                 finishLastResponse(httpConnection);
155             }
156         }
157 
158         // remove the connection from the timeout handler
159         idleStartTime = Long.MAX_VALUE;
160         
161         return httpConnection;
162     }
163 
164 	/***
165 	 * @see HttpConnectionManager#getConnection(HostConfiguration, long)
166 	 * 
167 	 * @deprecated Use #getConnectionWithTimeout(HostConfiguration, long)
168 	 */
169 	public HttpConnection getConnection(
170 		HostConfiguration hostConfiguration, long timeout) {
171         return getConnectionWithTimeout(hostConfiguration, timeout);
172 	}
173 
174     /***
175      * @see HttpConnectionManager#releaseConnection(org.apache.commons.httpclient.HttpConnection)
176      */
177     public void releaseConnection(HttpConnection conn) {
178         if (conn != httpConnection) {
179             throw new IllegalStateException("Unexpected release of an unknown connection.");
180         }
181 
182         finishLastResponse(httpConnection);
183         
184         // track the time the connection was made idle
185         idleStartTime = System.currentTimeMillis();
186     }
187 
188     /***
189      * Returns {@link HttpConnectionManagerParams parameters} associated 
190      * with this connection manager.
191      * 
192      * @since 2.1
193      * 
194      * @see HttpConnectionManagerParams
195      */
196     public HttpConnectionManagerParams getParams() {
197         return this.params;
198     }
199 
200     /***
201      * Assigns {@link HttpConnectionManagerParams parameters} for this 
202      * connection manager.
203      * 
204      * @since 2.1
205      * 
206      * @see HttpConnectionManagerParams
207      */
208     public void setParams(final HttpConnectionManagerParams params) {
209         if (params == null) {
210             throw new IllegalArgumentException("Parameters may not be null");
211         }
212         this.params = params;
213     }
214     
215     /***
216      * @since 3.0
217      */
218     public void closeIdleConnections(long idleTimeout) {
219         long maxIdleTime = System.currentTimeMillis() - idleTimeout;
220         if (idleStartTime <= maxIdleTime) {
221             httpConnection.close();
222         }
223     }
224 }