You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2008/02/06 22:41:07 UTC

svn commit: r619163 - in /httpcomponents/httpclient/trunk: ./ module-client/src/main/java/org/apache/http/conn/ module-client/src/main/java/org/apache/http/conn/ssl/ module-client/src/main/java/org/apache/http/conn/util/

Author: olegk
Date: Wed Feb  6 13:41:06 2008
New Revision: 619163

URL: http://svn.apache.org/viewvc?rev=619163&view=rev
Log:
HTTPCLIENT-643: Automatic connect fail-over for multi-home remote servers

Added:
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/SocketUtils.java   (with props)
Modified:
    httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    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

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=619163&r1=619162&r2=619163&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Wed Feb  6 13:41:06 2008
@@ -1,6 +1,9 @@
 Changes since 4.0 Alpha 2
 -------------------
 
+* [HTTPCLIENT-643] Automatic connect fail-over for multi-home remote servers.
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
 * [HTTPCLIENT-735] unsetting of DEFAULT_PROXY and FORCED_ROUTE in hierarchies
   Contributed by Roland Weber <rolandw at apache.org>
 

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=619163&r1=619162&r2=619163&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  6 13:41:06 2008
@@ -36,6 +36,7 @@
 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;
 
@@ -89,9 +90,6 @@
             throw new IllegalArgumentException("Parameters may not be null.");
         }
 
-        // resolve the target hostname first
-        final InetSocketAddress target = new InetSocketAddress(host, port);
-
         if (sock == null)
             sock = createSocket();
 
@@ -107,7 +105,8 @@
         }
 
         int timeout = HttpConnectionParams.getConnectionTimeout(params);
-        sock.connect(target, timeout);
+
+        SocketUtils.connect(sock, 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=619163&r1=619162&r2=619163&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  6 13:41:06 2008
@@ -32,6 +32,7 @@
 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;
 
@@ -272,9 +273,6 @@
             throw new IllegalArgumentException("Parameters may not be null.");
         }
 
-        // resolve the target hostname first
-        final InetSocketAddress target = new InetSocketAddress(host, port);
-
         SSLSocket sslock = (SSLSocket)
             ((sock != null) ? sock : createSocket());
 
@@ -292,7 +290,8 @@
         int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
         int soTimeout = HttpConnectionParams.getSoTimeout(params);
 
-        sslock.connect(target, connTimeout);
+        SocketUtils.connect(sock, host, port, connTimeout);
+
         sslock.setSoTimeout(soTimeout);
         try {
             hostnameVerifier.verify(host, sslock);

Added: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/SocketUtils.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/SocketUtils.java?rev=619163&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/SocketUtils.java (added)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/SocketUtils.java Wed Feb  6 13:41:06 2008
@@ -0,0 +1,118 @@
+/*
+ * $HeadURL:$
+ * $Revision:$
+ * $Date:$
+ * 
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.conn.util;
+
+import java.io.IOException;
+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;
+
+public class SocketUtils {
+
+    /**
+     * 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 hostname Host name to connect to
+     * @param port the port to connect to
+     * @param timeout connection timeout
+     * 
+     * @throws  IOException if an error occurs during the connection
+     * @throws  SocketTimeoutException if timeout expires before connecting
+     */
+    public static void connect(
+            final Socket sock, 
+            final String hostname,
+            int port,
+            int timeout) throws IOException {
+        
+        InetAddress[] adrs = InetAddress.getAllByName(hostname);
+        List<InetAddress> list = new ArrayList<InetAddress>(adrs.length);
+        for (InetAddress adr: adrs) {
+            list.add(adr);
+        }
+        Collections.shuffle(list);
+        connect(sock, list, port, timeout);
+    }
+    
+    /**
+     * Attempts to connects the socket to any of the {@link InetAddress}es given as a 
+     * parameter, whichever succeeds first. 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 addresses array of addresses
+     * @param port the port to connect to
+     * @param timeout connection timeout
+     * 
+     * @throws  IOException if an error occurs during the connection
+     * @throws  SocketTimeoutException if timeout expires before connecting
+     */
+    public static void connect(
+            final Socket sock, 
+            final List<InetAddress> addresses,
+            int port,
+            int timeout) throws IOException {
+        if (sock == null) {
+            throw new IllegalArgumentException("Socket may not be null");
+        }
+        if (addresses == null) {
+            throw new IllegalArgumentException("List of addresses may not be null");
+        }
+        if (addresses.isEmpty()) {
+            throw new IllegalArgumentException("List of addresses may not be empty");
+        }
+        
+        IOException lastEx = null;
+        for (InetAddress address: addresses) {
+            try {
+                sock.connect(new InetSocketAddress(address, port), timeout);
+                return;
+            } catch (SocketTimeoutException ex) {
+                throw ex;
+            } catch (IOException ex) {
+                // keep the last exception and retry
+                lastEx = ex;
+            }
+        }
+        if (lastEx != null) {
+            throw lastEx;
+        }
+    }
+    
+}

Propchange: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/SocketUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/SocketUtils.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/conn/util/SocketUtils.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain