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          @Override
69          public SocketAddress resolveLocalAddress(final HttpHost host) {
70              return null;
71          }
72  
73          @Override
74          public SocketAddress resolveRemoteAddress(final HttpHost host) {
75              final String hostname = host.getHostName();
76              int port = host.getPort();
77              if (port == -1) {
78                  if (host.getSchemeName().equalsIgnoreCase("http")) {
79                      port = 80;
80                  } else if (host.getSchemeName().equalsIgnoreCase("https")) {
81                      port = 443;
82                  }
83              }
84              return new InetSocketAddress(hostname, port);
85          }
86  
87      }
88  
89      /**
90       * @deprecated (4.3) use {@link BasicNIOConnPool#BasicNIOConnPool(ConnectingIOReactor, NIOConnFactory, int)}
91       */
92      @Deprecated
93      public BasicNIOConnPool(
94              final ConnectingIOReactor ioreactor,
95              final NIOConnFactory<HttpHost, NHttpClientConnection> connFactory,
96              final HttpParams params) {
97          super(ioreactor, connFactory, 2, 20);
98          Args.notNull(params, "HTTP parameters");
99          this.connectTimeout = params.getIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 0);
100     }
101 
102     /**
103      * @deprecated (4.3) use {@link BasicNIOConnPool#BasicNIOConnPool(ConnectingIOReactor,
104      *   ConnectionConfig)}
105      */
106     @Deprecated
107     public BasicNIOConnPool(
108             final ConnectingIOReactor ioreactor, final HttpParams params) {
109         this(ioreactor, new BasicNIOConnFactory(params), params);
110     }
111 
112     /**
113      * @since 4.3
114      */
115     public BasicNIOConnPool(
116             final ConnectingIOReactor ioreactor,
117             final NIOConnFactory<HttpHost, NHttpClientConnection> connFactory,
118             final int connectTimeout) {
119         super(ioreactor, connFactory, new BasicAddressResolver(), 2, 20);
120         this.connectTimeout = connectTimeout;
121     }
122 
123     /**
124      * @since 4.3
125      */
126     public BasicNIOConnPool(
127             final ConnectingIOReactor ioreactor,
128             final int connectTimeout,
129             final ConnectionConfig config) {
130         this(ioreactor, new BasicNIOConnFactory(config), connectTimeout);
131     }
132 
133     /**
134      * @since 4.3
135      */
136     public BasicNIOConnPool(
137             final ConnectingIOReactor ioreactor,
138             final ConnectionConfig config) {
139         this(ioreactor, new BasicNIOConnFactory(config), 0);
140     }
141 
142     /**
143      * @since 4.3
144      */
145     public BasicNIOConnPool(final ConnectingIOReactor ioreactor) {
146         this(ioreactor, new BasicNIOConnFactory(ConnectionConfig.DEFAULT), 0);
147     }
148 
149     /**
150      * @deprecated (4.3) use {@link SocketAddressResolver}
151      */
152     @Deprecated
153     @Override
154     protected SocketAddress resolveRemoteAddress(final HttpHost host) {
155         return new InetSocketAddress(host.getHostName(), host.getPort());
156     }
157 
158     /**
159      * @deprecated (4.3) use {@link SocketAddressResolver}
160      */
161     @Deprecated
162     @Override
163     protected SocketAddress resolveLocalAddress(final HttpHost host) {
164         return null;
165     }
166 
167     @Override
168     protected BasicNIOPoolEntry createEntry(final HttpHost host, final NHttpClientConnection conn) {
169         final BasicNIOPoolEntry entry = new BasicNIOPoolEntry(
170                 Long.toString(COUNTER.getAndIncrement()), host, conn);
171         entry.setSocketTimeout(conn.getSocketTimeout());
172         return entry;
173     }
174 
175     @Override
176     public Future<BasicNIOPoolEntry> lease(
177             final HttpHost route,
178             final Object state,
179             final FutureCallback<BasicNIOPoolEntry> callback) {
180         return super.lease(route, state,
181                 this.connectTimeout, TimeUnit.MILLISECONDS, callback);
182     }
183 
184     @Override
185     public Future<BasicNIOPoolEntry> lease(
186             final HttpHost route,
187             final Object state) {
188         return super.lease(route, state,
189                 this.connectTimeout, TimeUnit.MILLISECONDS, null);
190     }
191 
192     @Override
193     protected void onLease(final BasicNIOPoolEntry entry) {
194         final NHttpClientConnection conn = entry.getConnection();
195         conn.setSocketTimeout(entry.getSocketTimeout());
196     }
197 
198     @Override
199     protected void onRelease(final BasicNIOPoolEntry entry) {
200         final NHttpClientConnection conn = entry.getConnection();
201         entry.setSocketTimeout(conn.getSocketTimeout());
202         conn.setSocketTimeout(0);
203     }
204 
205 }