hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r631713 - in /httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn: MultihomePlainSocketFactory.java PlainSocketFactory.java ssl/SSLSocketFactory.java util/SocketUtils.java
Date Wed, 27 Feb 2008 20:40:39 GMT
Author: olegk
Date: Wed Feb 27 12:40:35 2008
New Revision: 631713

URL: http://svn.apache.org/viewvc?rev=631713&view=rev
Log:
This patch effectively reverts HTTPCLIENT-643 changes. Multihome fail-over mechanism simply
cannot be properly implemented at the socket factory level. A failure in the connect method
can leave the socket in a inconsistent state on some platforms (Mac OS), making it impossible
to connect the same socket to a different address

Added:
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/MultihomePlainSocketFactory.java
      - copied, changed from r630772, httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/PlainSocketFactory.java
Removed:
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/SocketUtils.java
Modified:
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/PlainSocketFactory.java
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java

Copied: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/MultihomePlainSocketFactory.java
(from r630772, httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/PlainSocketFactory.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/MultihomePlainSocketFactory.java?p2=httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/MultihomePlainSocketFactory.java&p1=httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/PlainSocketFactory.java&r1=630772&r2=631713&rev=631713&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/PlainSocketFactory.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/MultihomePlainSocketFactory.java
Wed Feb 27 12:40:35 2008
@@ -35,39 +35,34 @@
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.Socket;
+import java.net.SocketTimeoutException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 
-import org.apache.http.conn.util.SocketUtils;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
 
-/**
- * The default class for creating sockets.
- * This class just uses the {@link java.net.Socket socket} API
- * in Java 1.4 or greater.
- * 
- * @author <a href="mailto:rolandw at apache.org">Roland Weber</a>
- * @author Michael Becke
- */
-public final class PlainSocketFactory implements SocketFactory {
+public final class MultihomePlainSocketFactory implements SocketFactory {
 
     /**
      * The factory singleton.
      */
     private static final
-        PlainSocketFactory DEFAULT_FACTORY = new PlainSocketFactory();
+    MultihomePlainSocketFactory DEFAULT_FACTORY = new MultihomePlainSocketFactory();
 
     /**
      * Gets the singleton instance of this class.
      * @return the one and only plain socket factory
      */
-    public static final PlainSocketFactory getSocketFactory() {
+    public static final MultihomePlainSocketFactory getSocketFactory() {
         return DEFAULT_FACTORY;
     }
 
     /**
      * Restricted default constructor.
      */
-    private PlainSocketFactory() {
+    private MultihomePlainSocketFactory() {
         super();
     }
 
@@ -77,7 +72,21 @@
         return new Socket();
     }
 
-    // non-javadoc, see interface org.apache.http.conn.SocketFactory
+    /**
+     * Attempts to connects the socket to any of the {@link InetAddress}es the 
+     * given host name resolves to. If connection to all addresses fail, the  
+     * last I/O exception is propagated to the caller.
+     * 
+     * @param sock socket to connect to any of the given addresses
+     * @param host Host name to connect to
+     * @param port the port to connect to
+     * @param localAddress local address
+     * @param localPort local port
+     * @param params HTTP parameters 
+     * 
+     * @throws  IOException if an error occurs during the connection
+     * @throws  SocketTimeoutException if timeout expires before connecting
+     */
     public Socket connectSocket(Socket sock, String host, int port, 
                                 InetAddress localAddress, int localPort,
                                 HttpParams params)
@@ -106,10 +115,31 @@
 
         int timeout = HttpConnectionParams.getConnectionTimeout(params);
 
-        SocketUtils.connect(sock, host, port, timeout);
+        InetAddress[] inetadrs = InetAddress.getAllByName(host);
+        List<InetAddress> addresses = new ArrayList<InetAddress>(inetadrs.length);
+        for (InetAddress inetadr: inetadrs) {
+            addresses.add(inetadr);
+        }
+        Collections.shuffle(addresses);
 
+        IOException lastEx = null;
+        for (InetAddress address: addresses) {
+            try {
+                sock.connect(new InetSocketAddress(address, port), timeout);
+                break;
+            } catch (SocketTimeoutException ex) {
+                throw ex;
+            } catch (IOException ex) {
+                // create new socket
+                sock = new Socket();
+                // keep the last exception and retry
+                lastEx = ex;
+            }
+        }
+        if (lastEx != null) {
+            throw lastEx;
+        }
         return sock;
-
     } // connectSocket
 
 

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/PlainSocketFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/PlainSocketFactory.java?rev=631713&r1=631712&r2=631713&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/PlainSocketFactory.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/PlainSocketFactory.java
Wed Feb 27 12:40:35 2008
@@ -36,7 +36,6 @@
 import java.net.InetSocketAddress;
 import java.net.Socket;
 
-import org.apache.http.conn.util.SocketUtils;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
 
@@ -106,7 +105,7 @@
 
         int timeout = HttpConnectionParams.getConnectionTimeout(params);
 
-        SocketUtils.connect(sock, host, port, timeout);
+        sock.connect(new InetSocketAddress(host, port), timeout);
 
         return sock;
 

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java?rev=631713&r1=631712&r2=631713&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/ssl/SSLSocketFactory.java
Wed Feb 27 12:40:35 2008
@@ -32,7 +32,6 @@
 package org.apache.http.conn.ssl;
 
 import org.apache.http.conn.LayeredSocketFactory;
-import org.apache.http.conn.util.SocketUtils;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
 
@@ -290,7 +289,7 @@
         int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
         int soTimeout = HttpConnectionParams.getSoTimeout(params);
 
-        SocketUtils.connect(sock, host, port, connTimeout);
+        sock.connect(new InetSocketAddress(host, port), connTimeout);
 
         sslock.setSoTimeout(soTimeout);
         try {



Mime
View raw message