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.ThreadingBehavior;
37  import org.apache.http.annotation.Contract;
38  import org.apache.http.concurrent.FutureCallback;
39  import org.apache.http.config.ConnectionConfig;
40  import org.apache.http.nio.NHttpClientConnection;
41  import org.apache.http.nio.pool.AbstractNIOConnPool;
42  import org.apache.http.nio.pool.NIOConnFactory;
43  import org.apache.http.nio.pool.SocketAddressResolver;
44  import org.apache.http.nio.reactor.ConnectingIOReactor;
45  import org.apache.http.params.CoreConnectionPNames;
46  import org.apache.http.params.HttpParams;
47  import org.apache.http.util.Args;
48  
49  /**
50   * A very basic {@link org.apache.http.pool.ConnPool} implementation that
51   * represents a pool of non-blocking {@link NHttpClientConnection} connections
52   * identified by an {@link HttpHost} instance. Please note this pool
53   * implementation does not support complex routes via a proxy cannot
54   * differentiate between direct and proxied connections.
55   *
56   * @see HttpHost
57   * @since 4.2
58   */
59  @SuppressWarnings("deprecation")
60  @Contract(threading = ThreadingBehavior.SAFE)
61  public class BasicNIOConnPool extends AbstractNIOConnPool<HttpHost, NHttpClientConnection, BasicNIOPoolEntry> {
62  
63      private static final AtomicLong COUNTER = new AtomicLong();
64  
65      private final int connectTimeout;
66  
67      static class BasicAddressResolver implements SocketAddressResolver<HttpHost> {
68  
69          @Override
70          public SocketAddress resolveLocalAddress(final HttpHost host) {
71              return null;
72          }
73  
74          @Override
75          public SocketAddress resolveRemoteAddress(final HttpHost host) {
76              final String hostname = host.getHostName();
77              int port = host.getPort();
78              if (port == -1) {
79                  if (host.getSchemeName().equalsIgnoreCase("http")) {
80                      port = 80;
81                  } else if (host.getSchemeName().equalsIgnoreCase("https")) {
82                      port = 443;
83                  }
84              }
85              return new InetSocketAddress(hostname, port);
86          }
87  
88      }
89  
90      /**
91       * @deprecated (4.3) use {@link BasicNIOConnPool#BasicNIOConnPool(ConnectingIOReactor, NIOConnFactory, int)}
92       */
93      @Deprecated
94      public BasicNIOConnPool(
95              final ConnectingIOReactor ioreactor,
96              final NIOConnFactory<HttpHost, NHttpClientConnection> connFactory,
97              final HttpParams params) {
98          super(ioreactor, connFactory, 2, 20);
99          Args.notNull(params, "HTTP parameters");
100         this.connectTimeout = params.getIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 0);
101     }
102 
103     /**
104      * @deprecated (4.3) use {@link BasicNIOConnPool#BasicNIOConnPool(ConnectingIOReactor,
105      *   ConnectionConfig)}
106      */
107     @Deprecated
108     public BasicNIOConnPool(
109             final ConnectingIOReactor ioreactor, final HttpParams params) {
110         this(ioreactor, new BasicNIOConnFactory(params), params);
111     }
112 
113     /**
114      * @since 4.3
115      */
116     public BasicNIOConnPool(
117             final ConnectingIOReactor ioreactor,
118             final NIOConnFactory<HttpHost, NHttpClientConnection> connFactory,
119             final int connectTimeout) {
120         super(ioreactor, connFactory, new BasicAddressResolver(), 2, 20);
121         this.connectTimeout = connectTimeout;
122     }
123 
124     /**
125      * @since 4.3
126      */
127     public BasicNIOConnPool(
128             final ConnectingIOReactor ioreactor,
129             final int connectTimeout,
130             final ConnectionConfig config) {
131         this(ioreactor, new BasicNIOConnFactory(config), connectTimeout);
132     }
133 
134     /**
135      * @since 4.3
136      */
137     public BasicNIOConnPool(
138             final ConnectingIOReactor ioreactor,
139             final ConnectionConfig config) {
140         this(ioreactor, new BasicNIOConnFactory(config), 0);
141     }
142 
143     /**
144      * @since 4.3
145      */
146     public BasicNIOConnPool(final ConnectingIOReactor ioreactor) {
147         this(ioreactor, new BasicNIOConnFactory(ConnectionConfig.DEFAULT), 0);
148     }
149 
150     /**
151      * @deprecated (4.3) use {@link SocketAddressResolver}
152      */
153     @Deprecated
154     @Override
155     protected SocketAddress resolveRemoteAddress(final HttpHost host) {
156         return new InetSocketAddress(host.getHostName(), host.getPort());
157     }
158 
159     /**
160      * @deprecated (4.3) use {@link SocketAddressResolver}
161      */
162     @Deprecated
163     @Override
164     protected SocketAddress resolveLocalAddress(final HttpHost host) {
165         return null;
166     }
167 
168     @Override
169     protected BasicNIOPoolEntry createEntry(final HttpHost host, final NHttpClientConnection conn) {
170         final BasicNIOPoolEntry entry = new BasicNIOPoolEntry(
171                 Long.toString(COUNTER.getAndIncrement()), host, conn);
172         entry.setSocketTimeout(conn.getSocketTimeout());
173         return entry;
174     }
175 
176     @Override
177     public Future<BasicNIOPoolEntry> lease(
178             final HttpHost route,
179             final Object state,
180             final FutureCallback<BasicNIOPoolEntry> callback) {
181         return super.lease(route, state,
182                 this.connectTimeout, TimeUnit.MILLISECONDS, callback);
183     }
184 
185     @Override
186     public Future<BasicNIOPoolEntry> lease(
187             final HttpHost route,
188             final Object state) {
189         return super.lease(route, state,
190                 this.connectTimeout, TimeUnit.MILLISECONDS, null);
191     }
192 
193     @Override
194     protected void onLease(final BasicNIOPoolEntry entry) {
195         final NHttpClientConnection conn = entry.getConnection();
196         conn.setSocketTimeout(entry.getSocketTimeout());
197     }
198 
199     @Override
200     protected void onRelease(final BasicNIOPoolEntry entry) {
201         final NHttpClientConnection conn = entry.getConnection();
202         entry.setSocketTimeout(conn.getSocketTimeout());
203         conn.setSocketTimeout(0);
204     }
205 
206 }