View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.nio.pool;
28  
29  import java.net.InetSocketAddress;
30  import java.net.SocketAddress;
31  import java.util.concurrent.Future;
32  import java.util.concurrent.TimeUnit;
33  import java.util.concurrent.atomic.AtomicLong;
34  
35  import org.apache.http.HttpHost;
36  import org.apache.http.annotation.ThreadSafe;
37  import org.apache.http.concurrent.FutureCallback;
38  import org.apache.http.config.ConnectionConfig;
39  import org.apache.http.nio.NHttpClientConnection;
40  import org.apache.http.nio.pool.AbstractNIOConnPool;
41  import org.apache.http.nio.pool.NIOConnFactory;
42  import org.apache.http.nio.pool.SocketAddressResolver;
43  import org.apache.http.nio.reactor.ConnectingIOReactor;
44  import org.apache.http.params.CoreConnectionPNames;
45  import org.apache.http.params.HttpParams;
46  import org.apache.http.util.Args;
47  
48  /**
49   * A very basic {@link org.apache.http.pool.ConnPool} implementation that
50   * represents a pool of non-blocking {@link NHttpClientConnection} connections
51   * identified by an {@link HttpHost} instance. Please note this pool
52   * implementation does not support complex routes via a proxy cannot
53   * differentiate between direct and proxied connections.
54   *
55   * @see HttpHost
56   * @since 4.2
57   */
58  @SuppressWarnings("deprecation")
59  @ThreadSafe
60  public class BasicNIOConnPool extends AbstractNIOConnPool<HttpHost, NHttpClientConnection, BasicNIOPoolEntry> {
61  
62      private static final AtomicLong COUNTER = new AtomicLong();
63  
64      private final int connectTimeout;
65  
66      static class BasicAddressResolver implements SocketAddressResolver<HttpHost> {
67  
68          public SocketAddress resolveLocalAddress(final HttpHost host) {
69              return null;
70          }
71  
72          public SocketAddress resolveRemoteAddress(final HttpHost host) {
73              final String hostname = host.getHostName();
74              int port = host.getPort();
75              if (port == -1) {
76                  if (host.getSchemeName().equalsIgnoreCase("http")) {
77                      port = 80;
78                  } else if (host.getSchemeName().equalsIgnoreCase("https")) {
79                      port = 443;
80                  }
81              }
82              return new InetSocketAddress(hostname, port);
83          }
84  
85      }
86  
87      /**
88       * @deprecated (4.3) use {@link BasicNIOConnPool#BasicNIOConnPool(ConnectingIOReactor, NIOConnFactory, int)}
89       */
90      @Deprecated
91      public BasicNIOConnPool(
92              final ConnectingIOReactor ioreactor,
93              final NIOConnFactory<HttpHost, NHttpClientConnection> connFactory,
94              final HttpParams params) {
95          super(ioreactor, connFactory, 2, 20);
96          Args.notNull(params, "HTTP parameters");
97          this.connectTimeout = params.getIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 0);
98      }
99  
100     /**
101      * @deprecated (4.3) use {@link BasicNIOConnPool#BasicNIOConnPool(ConnectingIOReactor,
102      *   ConnectionConfig)}
103      */
104     @Deprecated
105     public BasicNIOConnPool(
106             final ConnectingIOReactor ioreactor, final HttpParams params) {
107         this(ioreactor, new BasicNIOConnFactory(params), params);
108     }
109 
110     /**
111      * @since 4.3
112      */
113     public BasicNIOConnPool(
114             final ConnectingIOReactor ioreactor,
115             final NIOConnFactory<HttpHost, NHttpClientConnection> connFactory,
116             final int connectTimeout) {
117         super(ioreactor, connFactory, new BasicAddressResolver(), 2, 20);
118         this.connectTimeout = connectTimeout;
119     }
120 
121     /**
122      * @since 4.3
123      */
124     public BasicNIOConnPool(
125             final ConnectingIOReactor ioreactor,
126             final int connectTimeout,
127             final ConnectionConfig config) {
128         this(ioreactor, new BasicNIOConnFactory(config), connectTimeout);
129     }
130 
131     /**
132      * @since 4.3
133      */
134     public BasicNIOConnPool(
135             final ConnectingIOReactor ioreactor,
136             final ConnectionConfig config) {
137         this(ioreactor, new BasicNIOConnFactory(config), 0);
138     }
139 
140     /**
141      * @since 4.3
142      */
143     public BasicNIOConnPool(final ConnectingIOReactor ioreactor) {
144         this(ioreactor, new BasicNIOConnFactory(ConnectionConfig.DEFAULT), 0);
145     }
146 
147     /**
148      * @deprecated (4.3) use {@link SocketAddressResolver}
149      */
150     @Deprecated
151     @Override
152     protected SocketAddress resolveRemoteAddress(final HttpHost host) {
153         return new InetSocketAddress(host.getHostName(), host.getPort());
154     }
155 
156     /**
157      * @deprecated (4.3) use {@link SocketAddressResolver}
158      */
159     @Deprecated
160     @Override
161     protected SocketAddress resolveLocalAddress(final HttpHost host) {
162         return null;
163     }
164 
165     @Override
166     protected BasicNIOPoolEntry createEntry(final HttpHost host, final NHttpClientConnection conn) {
167         final BasicNIOPoolEntry entry = new BasicNIOPoolEntry(
168                 Long.toString(COUNTER.getAndIncrement()), host, conn);
169         entry.setSocketTimeout(conn.getSocketTimeout());
170         return entry;
171     }
172 
173     @Override
174     public Future<BasicNIOPoolEntry> lease(
175             final HttpHost route,
176             final Object state,
177             final FutureCallback<BasicNIOPoolEntry> callback) {
178         return super.lease(route, state,
179                 this.connectTimeout, TimeUnit.MILLISECONDS, callback);
180     }
181 
182     @Override
183     public Future<BasicNIOPoolEntry> lease(
184             final HttpHost route,
185             final Object state) {
186         return super.lease(route, state,
187                 this.connectTimeout, TimeUnit.MILLISECONDS, null);
188     }
189 
190     @Override
191     protected void onLease(final BasicNIOPoolEntry entry) {
192         final NHttpClientConnection conn = entry.getConnection();
193         conn.setSocketTimeout(entry.getSocketTimeout());
194     }
195 
196     @Override
197     protected void onRelease(final BasicNIOPoolEntry entry) {
198         final NHttpClientConnection conn = entry.getConnection();
199         entry.setSocketTimeout(conn.getSocketTimeout());
200         conn.setSocketTimeout(0);
201     }
202 
203 }