hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1457537 - in /httpcomponents/httpcore/trunk/httpcore-nio/src: main/java/org/apache/http/impl/nio/pool/ main/java/org/apache/http/nio/pool/ test/java/org/apache/http/nio/pool/
Date Sun, 17 Mar 2013 19:38:04 GMT
Author: olegk
Date: Sun Mar 17 19:38:04 2013
New Revision: 1457537

URL: http://svn.apache.org/r1457537
Log:
Use external SocketAddressResolver strategy to resolve connection route to a local / remote
address

Added:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/SocketAddressResolver.java
  (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/pool/BasicNIOConnPool.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/AbstractNIOConnPool.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/pool/BasicNIOConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/pool/BasicNIOConnPool.java?rev=1457537&r1=1457536&r2=1457537&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/pool/BasicNIOConnPool.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/impl/nio/pool/BasicNIOConnPool.java
Sun Mar 17 19:38:04 2013
@@ -39,6 +39,7 @@ import org.apache.http.config.Connection
 import org.apache.http.nio.NHttpClientConnection;
 import org.apache.http.nio.pool.AbstractNIOConnPool;
 import org.apache.http.nio.pool.NIOConnFactory;
+import org.apache.http.nio.pool.SocketAddressResolver;
 import org.apache.http.nio.reactor.ConnectingIOReactor;
 import org.apache.http.params.HttpConnectionParams;
 import org.apache.http.params.HttpParams;
@@ -63,6 +64,18 @@ public class BasicNIOConnPool extends Ab
 
     private final int connectTimeout;
 
+    static class BasicAddressResolver implements SocketAddressResolver<HttpHost> {
+
+        public SocketAddress resolveLocalAddress(final HttpHost host) {
+            return null;
+        }
+
+        public SocketAddress resolveRemoteAddress(final HttpHost host) {
+            return new InetSocketAddress(host.getHostName(), host.getPort());
+        }
+
+    }
+
     /**
      * @deprecated (4.3) use {@link BasicNIOConnPool#BasicNIOConnPool(ConnectingIOReactor,
NIOConnFactory, int)}
      */
@@ -93,7 +106,7 @@ public class BasicNIOConnPool extends Ab
             final ConnectingIOReactor ioreactor,
             final NIOConnFactory<HttpHost, NHttpClientConnection> connFactory,
             final int connectTimeout) {
-        super(ioreactor, connFactory, 2, 20);
+        super(ioreactor, connFactory, new BasicAddressResolver(), 2, 20);
         this.connectTimeout = connectTimeout;
     }
 
@@ -116,11 +129,19 @@ public class BasicNIOConnPool extends Ab
         this(ioreactor, new BasicNIOConnFactory(config), 0);
     }
 
+    /**
+     * @deprecated (4.3) use {@link SocketAddressResolver}
+     */
+    @Deprecated
     @Override
     protected SocketAddress resolveRemoteAddress(final HttpHost host) {
         return new InetSocketAddress(host.getHostName(), host.getPort());
     }
 
+    /**
+     * @deprecated (4.3) use {@link SocketAddressResolver}
+     */
+    @Deprecated
     @Override
     protected SocketAddress resolveLocalAddress(final HttpHost host) {
         return null;

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/AbstractNIOConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/AbstractNIOConnPool.java?rev=1457537&r1=1457536&r2=1457537&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/AbstractNIOConnPool.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/AbstractNIOConnPool.java
Sun Mar 17 19:38:04 2013
@@ -70,6 +70,7 @@ public abstract class AbstractNIOConnPoo
 
     private final ConnectingIOReactor ioreactor;
     private final NIOConnFactory<T, C> connFactory;
+    private final SocketAddressResolver<T> addressResolver;
     private final SessionRequestCallback sessionRequestCallback;
     private final Map<T, RouteSpecificPool<T, C, E>> routeToPool;
     private final LinkedList<LeaseRequest<T, C, E>> leasingRequests;
@@ -83,6 +84,11 @@ public abstract class AbstractNIOConnPoo
     private volatile int defaultMaxPerRoute;
     private volatile int maxTotal;
 
+    /**
+     * @deprecated use {@link AbstractNIOConnPool#AbstractNIOConnPool(ConnectingIOReactor,
+     *   NIOConnFactory, SocketAddressResolver, int, int)}
+     */
+    @Deprecated
     public AbstractNIOConnPool(
             final ConnectingIOReactor ioreactor,
             final NIOConnFactory<T, C> connFactory,
@@ -95,6 +101,17 @@ public abstract class AbstractNIOConnPoo
         Args.positive(maxTotal, "Max total value");
         this.ioreactor = ioreactor;
         this.connFactory = connFactory;
+        this.addressResolver = new SocketAddressResolver<T>() {
+
+            public SocketAddress resolveLocalAddress(final T route) throws IOException {
+                return AbstractNIOConnPool.this.resolveLocalAddress(route);
+            }
+
+            public SocketAddress resolveRemoteAddress(final T route) throws IOException {
+                return AbstractNIOConnPool.this.resolveRemoteAddress(route);
+            }
+
+        };
         this.sessionRequestCallback = new InternalSessionRequestCallback();
         this.routeToPool = new HashMap<T, RouteSpecificPool<T, C, E>>();
         this.leasingRequests = new LinkedList<LeaseRequest<T, C, E>>();
@@ -107,9 +124,51 @@ public abstract class AbstractNIOConnPoo
         this.maxTotal = maxTotal;
     }
 
-    protected abstract SocketAddress resolveRemoteAddress(T route);
+    /**
+     * @since 4.3
+     */
+    public AbstractNIOConnPool(
+            final ConnectingIOReactor ioreactor,
+            final NIOConnFactory<T, C> connFactory,
+            final SocketAddressResolver<T> addressResolver,
+            final int defaultMaxPerRoute,
+            final int maxTotal) {
+        super();
+        Args.notNull(ioreactor, "I/O reactor");
+        Args.notNull(connFactory, "Connection factory");
+        Args.notNull(addressResolver, "Address resolver");
+        Args.positive(defaultMaxPerRoute, "Max per route value");
+        Args.positive(maxTotal, "Max total value");
+        this.ioreactor = ioreactor;
+        this.connFactory = connFactory;
+        this.addressResolver = addressResolver;
+        this.sessionRequestCallback = new InternalSessionRequestCallback();
+        this.routeToPool = new HashMap<T, RouteSpecificPool<T, C, E>>();
+        this.leasingRequests = new LinkedList<LeaseRequest<T, C, E>>();
+        this.pending = new HashSet<SessionRequest>();
+        this.leased = new HashSet<E>();
+        this.available = new LinkedList<E>();
+        this.maxPerRoute = new HashMap<T, Integer>();
+        this.lock = new ReentrantLock();
+        this.defaultMaxPerRoute = defaultMaxPerRoute;
+        this.maxTotal = maxTotal;
+    }
+
+    /**
+     * @deprecated (4.3) use {@link SocketAddressResolver}
+     */
+    @Deprecated
+    protected SocketAddress resolveRemoteAddress(final T route) {
+        return null;
+    }
 
-    protected abstract SocketAddress resolveLocalAddress(T route);
+    /**
+     * @deprecated (4.3) use {@link SocketAddressResolver}
+     */
+    @Deprecated
+    protected SocketAddress resolveLocalAddress(final T route) {
+        return null;
+    }
 
     protected abstract E createEntry(T route, C conn);
 
@@ -288,11 +347,19 @@ public abstract class AbstractNIOConnPoo
                     }
                 }
                 it.remove();
+
+                final SocketAddress localAddress;
+                final SocketAddress remoteAddress;
+                try {
+                    remoteAddress = this.addressResolver.resolveRemoteAddress(route);
+                    localAddress = this.addressResolver.resolveLocalAddress(route);
+                } catch (final IOException ex) {
+                    future.failed(ex);
+                    continue;
+                }
+
                 final SessionRequest sessionRequest = this.ioreactor.connect(
-                        resolveRemoteAddress(route),
-                        resolveLocalAddress(route),
-                        route,
-                        this.sessionRequestCallback);
+                        remoteAddress, localAddress, route, this.sessionRequestCallback);
                 final int timout = request.getConnectTimeout() < Integer.MAX_VALUE ?
                         (int) request.getConnectTimeout() : Integer.MAX_VALUE;
                 sessionRequest.setConnectTimeout(timout);

Added: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/SocketAddressResolver.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/SocketAddressResolver.java?rev=1457537&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/SocketAddressResolver.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/SocketAddressResolver.java
Sun Mar 17 19:38:04 2013
@@ -0,0 +1,44 @@
+/*
+ * ====================================================================
+ * 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.nio.pool;
+
+import java.io.IOException;
+import java.net.SocketAddress;
+
+/**
+ * Strategy that resolves an abstract connection route to a local or a remote {@link SocketAddress}.
+ * .
+ * @since 4.3
+ */
+public interface SocketAddressResolver<T> {
+
+    SocketAddress resolveLocalAddress(T route) throws IOException;
+
+    SocketAddress resolveRemoteAddress(T route) throws IOException;
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/SocketAddressResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/SocketAddressResolver.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/pool/SocketAddressResolver.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java?rev=1457537&r1=1457536&r2=1457537&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/pool/TestNIOConnPool.java
Sun Mar 17 19:38:04 2013
@@ -30,6 +30,7 @@ import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.SocketAddress;
 import java.net.SocketTimeoutException;
+import java.net.UnknownHostException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
@@ -73,21 +74,30 @@ public class TestNIOConnPool {
 
     }
 
-    static class LocalSessionPool extends AbstractNIOConnPool<String, IOSession, LocalPoolEntry>
{
+    static class LocalAddressResolver implements SocketAddressResolver<String> {
 
-        public LocalSessionPool(
-                final ConnectingIOReactor ioreactor, final int defaultMaxPerRoute, final
int maxTotal) {
-            super(ioreactor, new LocalConnFactory(), defaultMaxPerRoute, maxTotal);
+        public SocketAddress resolveLocalAddress(final String route) {
+            return null;
         }
 
-        @Override
-        protected SocketAddress resolveRemoteAddress(final String route) {
+        public SocketAddress resolveRemoteAddress(final String route) {
             return InetSocketAddress.createUnresolved(route, 80);
         }
 
-        @Override
-        protected SocketAddress resolveLocalAddress(final String route) {
-            return InetSocketAddress.createUnresolved(route, 80);
+    }
+
+    static class LocalSessionPool extends AbstractNIOConnPool<String, IOSession, LocalPoolEntry>
{
+
+        public LocalSessionPool(
+                final ConnectingIOReactor ioreactor, final int defaultMaxPerRoute, final
int maxTotal) {
+            super(ioreactor, new LocalConnFactory(), new LocalAddressResolver(), defaultMaxPerRoute,
maxTotal);
+        }
+
+        public LocalSessionPool(
+                final ConnectingIOReactor ioreactor,
+                final SocketAddressResolver<String> addressResolver,
+                final int defaultMaxPerRoute, final int maxTotal) {
+            super(ioreactor, new LocalConnFactory(), addressResolver, defaultMaxPerRoute,
maxTotal);
         }
 
         @Override
@@ -283,6 +293,28 @@ public class TestNIOConnPool {
     }
 
     @Test
+    public void testConnectUnknownHost() throws Exception {
+        final SessionRequest sessionRequest = Mockito.mock(SessionRequest.class);
+        Mockito.when(sessionRequest.getAttachment()).thenReturn("somehost");
+        Mockito.when(sessionRequest.getException()).thenReturn(new IOException());
+        final ConnectingIOReactor ioreactor = Mockito.mock(ConnectingIOReactor.class);
+        @SuppressWarnings("unchecked")
+        final SocketAddressResolver<String> addressResolver = Mockito.mock(SocketAddressResolver.class);
+        Mockito.when(addressResolver.resolveRemoteAddress("somehost")).thenThrow(new UnknownHostException());
+        final LocalSessionPool pool = new LocalSessionPool(ioreactor, addressResolver, 2,
10);
+        final Future<LocalPoolEntry> future = pool.lease("somehost", null);
+
+        Assert.assertTrue(future.isDone());
+        Assert.assertFalse(future.isCancelled());
+        try {
+            future.get();
+            Assert.fail("ExecutionException should have been thrown");
+        } catch (final ExecutionException ex) {
+            Assert.assertTrue(ex.getCause() instanceof UnknownHostException);
+        }
+    }
+
+    @Test
     public void testLeaseRelease() throws Exception {
         final IOSession iosession1 = Mockito.mock(IOSession.class);
         final SessionRequest sessionRequest1 = Mockito.mock(SessionRequest.class);



Mime
View raw message