View Javadoc

1   /* SingleHttpConnectionManager
2   *
3   * $Id: SingleHttpConnectionManager.java 3882 2005-10-10 09:31:18Z gojomo $
4   *
5   * Created on Mar 8, 2004
6   *
7   * Copyright (C) 2004 Internet Archive.
8   *
9   * This file is part of the Heritrix web crawler (crawler.archive.org).
10  *
11  * Heritrix is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * any later version.
15  *
16  * Heritrix is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser Public License
22  * along with Heritrix; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25  package org.archive.httpclient;
26  
27  import java.io.IOException;
28  import java.io.InputStream;
29  
30  import org.apache.commons.httpclient.HostConfiguration;
31  import org.apache.commons.httpclient.HttpConnection;
32  import org.apache.commons.httpclient.SimpleHttpConnectionManager;
33  
34  /***
35   * An HttpClient-compatible HttpConnection "manager" that actually
36   * just gives out a new connection each time -- skipping the overhead
37   * of connection management, since we already throttle our crawler
38   * with external mechanisms.
39   *
40   * @author gojomo
41   */
42  public class SingleHttpConnectionManager extends SimpleHttpConnectionManager {
43  
44      public SingleHttpConnectionManager() {
45          super();
46      }
47  
48      public HttpConnection getConnectionWithTimeout(
49          HostConfiguration hostConfiguration, long timeout) {
50  
51          HttpConnection conn = new HttpConnection(hostConfiguration);
52          conn.setHttpConnectionManager(this);
53          conn.getParams().setDefaults(this.getParams());
54          return conn;
55      }
56  
57      public void releaseConnection(HttpConnection conn) {
58          // ensure connection is closed
59          conn.close();
60          finishLast(conn);
61      }
62  
63      static void finishLast(HttpConnection conn) {
64          // copied from superclass because it wasn't made available to subclasses
65          InputStream lastResponse = conn.getLastResponseInputStream();
66          if (lastResponse != null) {
67              conn.setLastResponseInputStream(null);
68              try {
69                  lastResponse.close();
70              } catch (IOException ioe) {
71                  //FIXME: badness - close to force reconnect.
72                  conn.close();
73              }
74          }
75      }
76  }