commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient HttpConnectionManager.java
Date Mon, 22 Jul 2002 15:38:36 GMT
dion        2002/07/22 08:38:36

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HttpConnectionManager.java
  Log:
  Refactoring so I can understand getConnection
  
  Revision  Changes    Path
  1.7       +89 -61    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnectionManager.java
  
  Index: HttpConnectionManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnectionManager.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- HttpConnectionManager.java	21 Jul 2002 04:12:45 -0000	1.6
  +++ HttpConnectionManager.java	22 Jul 2002 15:38:36 -0000	1.7
  @@ -79,7 +79,8 @@
   public class HttpConnectionManager
   {
       // -------------------------------------------------------- Class Variables
  -    static private final Log log = LogFactory.getLog(HttpConnectionManager.class.getName());
  +    private static final Log log = LogFactory.getLog(
  +        HttpConnectionManager.class);
   
       // ----------------------------------------------------- Instance Variables
       private HashMap mapHosts = new HashMap();
  @@ -167,94 +168,103 @@
        * @exception java.net.MalformedURLException
        * @exception org.apache.commons.HttpException - If no connection becomes availble
before the timeout expires
        */
  -    public HttpConnection getConnection(String sURL) throws HttpException, MalformedURLException
  +    public HttpConnection getConnection(String sURL) throws HttpException,
  +        MalformedURLException
       {
           return getConnection(sURL, 0);
       }
   
       /**
  +     * Return the port provided if not -1 (default), return 443 if the
  +     * protocol is HTTPS, otherwise 80
  +     *
  +     * @param protocol the protocol to use to get the port for, e.g. http or
  +     *      https
  +     * @param the port provided with the url (could be -1)
  +     * @return the port for the specified port and protocol.
  +     */
  +    private static int getPort(String protocol, int port) {
  +        // default to provided port
  +        int portForProtocol = port;
  +        if (portForProtocol == -1) {
  +            if (protocol.equalsIgnoreCase("HTTPS")) {
  +                portForProtocol = 443;
  +            } else {
  +                portForProtocol = 80;
  +            }
  +        }
  +        return portForProtocol;
  +    }
  +    
  +    /**
        * Get an HttpConnection for a given URL.  The URL must be fully
        * specified (i.e. contain a protocol and a host (and optional port number).
        * If the maximum number of connections for the host has been reached, this
  -     * method will block for <code>timeout</code> milliseconds or until a connection

  -     * becomes available.  If no connection becomes available before the timeout expires
  -     * an HttpException exception will be thrown.
  +     * method will block for <code>timeout</code> milliseconds or until a 
  +     * connection becomes available.  If no connection becomes available before
  +     * the timeout expires an HttpException exception will be thrown.
        *
        * @param sURL - a fully specified URL.
  -     * @param timeout - the time (in milliseconds) to wait for a connection to become available
  +     * @param timeout - the time (in milliseconds) to wait for a connection 
  +     *      to become available
        * @return an HttpConnection for the given host:port
        * @exception java.net.MalformedURLException
  -     * @exception org.apache.commons.HttpException - If no connection becomes availble
before the timeout expires
  +     * @exception org.apache.commons.HttpException - If no connection becomes 
  +     *      availble before the timeout expires
        */
  -    public HttpConnection getConnection(String sURL, long timeout) throws HttpException,
MalformedURLException
  +    public HttpConnection getConnection(String sURL, long timeout) 
  +    throws HttpException, MalformedURLException
       {
  -        HttpConnection conn = null;
  -
  -        if(sURL == null){
  +        // FIXME: This method is too big
  +        if (sURL == null) {
               throw new MalformedURLException("sURL = null");
           }
   
  -        URL url = null;
  -        try{
  -            url = new URL(sURL);
  -        }catch(MalformedURLException e){
  -            log.error("HttpConnectionManager.getConnection:  Invalid url '" + sURL + "'.
 Exception = " + e.toString());
  -            throw e;
  -        }
  -
  +        URL url = new URL(sURL);
           // Get the protocol and port (use default port if not specified)
           String protocol = url.getProtocol();
           String host = url.getHost();
  -        int port = url.getPort();
  -        boolean isSecure = protocol.equalsIgnoreCase("HTTPS");
  -        if(port == -1){
  -            if(isSecure){
  -                port = 443;
  -            }else{
  -                port = 80;
  -            }
  -        }
  -        String key = host + ":" + port;
  +        int port = HttpConnectionManager.getPort(protocol, url.getPort());
  +        String hostAndPort = host + ":" + port;
   
  -        if(log.isDebugEnabled()){
  -            log.debug("HttpConnectionManager.getConnection:  key = " + key);
  +        if (log.isDebugEnabled()) {
  +            log.debug("HttpConnectionManager.getConnection:  key = "
  +                + hostAndPort);
           }
   
           // Look for a list of connections for the given host:port
  -        LinkedList listConnections = null;
  -        synchronized(mapHosts){
  -            listConnections = (LinkedList)mapHosts.get(key);
  -            if(listConnections == null){
  -                // First time for this host:port
  -                listConnections = new LinkedList();
  -                mapHosts.put(key, listConnections);
  -                mapNumConnections.put(key, new Integer(0));
  -            }
  -        }
  -
  -        // 
  -        synchronized(listConnections){
  -            if(listConnections.size() > 0){
  +        LinkedList listConnections = getConnections(hostAndPort);
  +        
  +        HttpConnection conn = null;
  +        // get a connection from the 'pool', waiting for 'timeout' if none
  +        // are currently available
  +        synchronized(listConnections) {
  +            if (listConnections.size() > 0) {
                   conn = (HttpConnection)listConnections.removeFirst();
  -            }else{
  -                Integer numConnections = (Integer)mapNumConnections.get(key);
  -                if(numConnections == null){
  -                    log.error("HttpConnectionManager.getConnection:  No connection count
for " + key);
  +            } else {
  +                // get number of connections to host:port
  +                Integer numConnections = (Integer)mapNumConnections.get(hostAndPort);
  +                if (numConnections == null) {
  +                    log.error("HttpConnectionManager.getConnection:  "
  +                        + "No connection count for " + hostAndPort);
                       // This should never happen, but just in case we'll try to recover.
                       numConnections = new Integer(0);
  -                    mapNumConnections.put(key, numConnections);
  +                    mapNumConnections.put(hostAndPort, numConnections);
                   }
  -                if(numConnections.intValue() < maxConnections){
  +                
  +                if (numConnections.intValue() < maxConnections) {
                       // Create a new connection
                       if(log.isDebugEnabled()){
  -                        log.debug("HttpConnectionManager.getConnection:  creating connection
for " + host + ":" + port + " via " + proxyHost + ":" + proxyPort);
  +                        log.debug("HttpConnectionManager.getConnection:  "
  +                            + "creating connection for " + hostAndPort 
  +                            + " via " + proxyHost + ":" + proxyPort);
                       }
  +                    boolean isSecure = protocol.equalsIgnoreCase("HTTPS");
                       conn = new HttpConnection(proxyHost, proxyPort, host, port, isSecure);
                       numConnections = new Integer(numConnections.intValue()+1);
  -                    mapNumConnections.put(key, numConnections);
  -                }else{
  +                    mapNumConnections.put(hostAndPort, numConnections);
  +                } else {
                       // No connections available, so wait
  -
                       // Start the timeout thread
                       TimeoutThread threadTimeout = new TimeoutThread();
                       threadTimeout.setTimeout(timeout);
  @@ -263,13 +273,13 @@
   
                       // wait for the connection to be available
                       while(conn == null){    // spin lock
  -                        try{
  +                        try {
                               log.debug("HttpConnectionManager.getConnection:  waiting for
connection for " + host + ":" + port);
                               listConnections.wait();
  -                        }catch(InterruptedException e){
  +                        } catch (InterruptedException e) {
                               throw new HttpException("Timeout waiting for connection.");
                           }
  -                        if(listConnections.size() > 0){
  +                        if (listConnections.size() > 0) {
                               conn = (HttpConnection)listConnections.removeFirst();
                               threadTimeout.interrupt();
                           }
  @@ -281,6 +291,24 @@
           return conn;
       }
   
  +    /**
  +     * Get the list of connections available for the given host and port
  +     */
  +    private LinkedList getConnections(String hostAndPort) {
  +        // Look for a list of connections for the given host:port
  +        LinkedList listConnections = null;
  +        synchronized (mapHosts) {
  +            listConnections = (LinkedList) mapHosts.get(hostAndPort);
  +            if (listConnections == null) {
  +                // First time for this host:port
  +                listConnections = new LinkedList();
  +                mapHosts.put(hostAndPort, listConnections);
  +                mapNumConnections.put(hostAndPort, new Integer(0));
  +            }
  +        }
  +        return listConnections;
  +    }
  +    
       /**
        * Make the given HttpConnection available for use by other requests.
        * If another thread is blocked in getConnection() waiting for a connection
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message