| 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.conn; |
| 28 |
|
| 29 |
import java.net.InetSocketAddress; |
| 30 |
import java.net.SocketAddress; |
| 31 |
import java.util.concurrent.TimeUnit; |
| 32 |
import java.util.concurrent.atomic.AtomicLong; |
| 33 |
|
| 34 |
import org.apache.commons.logging.Log; |
| 35 |
import org.apache.http.HttpHost; |
| 36 |
import org.apache.http.conn.routing.HttpRoute; |
| 37 |
import org.apache.http.nio.conn.scheme.AsyncScheme; |
| 38 |
import org.apache.http.nio.conn.scheme.AsyncSchemeRegistry; |
| 39 |
import org.apache.http.nio.pool.AbstractNIOConnPool; |
| 40 |
import org.apache.http.nio.reactor.ConnectingIOReactor; |
| 41 |
import org.apache.http.nio.reactor.IOSession; |
| 42 |
|
| 43 |
@Deprecated |
| 44 |
class HttpNIOConnPool extends AbstractNIOConnPool<HttpRoute, IOSession, HttpPoolEntry> { |
| 45 |
|
| 46 |
private static final AtomicLong COUNTER = new AtomicLong(1); |
| 47 |
|
| 48 |
private final Log log; |
| 49 |
private final AsyncSchemeRegistry schemeRegistry; |
| 50 |
private final long connTimeToLive; |
| 51 |
private final TimeUnit timeUnit; |
| 52 |
|
| 53 |
HttpNIOConnPool( |
| 54 |
final Log log, |
| 55 |
final ConnectingIOReactor ioReactor, |
| 56 |
final AsyncSchemeRegistry schemeRegistry, |
| 57 |
final long connTimeToLive, final TimeUnit timeUnit) { |
| 58 |
super(ioReactor, new HttpNIOConnPoolFactory(), 2, 20); |
| 59 |
this.log = log; |
| 60 |
this.schemeRegistry = schemeRegistry; |
| 61 |
this.connTimeToLive = connTimeToLive; |
| 62 |
this.timeUnit = timeUnit; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
protected SocketAddress resolveLocalAddress(final HttpRoute route) { |
| 67 |
return new InetSocketAddress(route.getLocalAddress(), 0); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
protected SocketAddress resolveRemoteAddress(final HttpRoute route) { |
| 72 |
HttpHost firsthop = route.getProxyHost(); |
| 73 |
if (firsthop == null) { |
| 74 |
firsthop = route.getTargetHost(); |
| 75 |
} |
| 76 |
final String hostname = firsthop.getHostName(); |
| 77 |
int port = firsthop.getPort(); |
| 78 |
if (port < 0) { |
| 79 |
final AsyncScheme scheme = this.schemeRegistry.getScheme(firsthop); |
| 80 |
port = scheme.resolvePort(port); |
| 81 |
} |
| 82 |
return new InetSocketAddress(hostname, port); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
protected HttpPoolEntry createEntry(final HttpRoute route, final IOSession session) { |
| 87 |
final String id = Long.toString(COUNTER.getAndIncrement()); |
| 88 |
return new HttpPoolEntry(this.log, id, route, session, this.connTimeToLive, this.timeUnit); |
| 89 |
} |
| 90 |
|
| 91 |
} |