com.crashlytics.tools.android_2.1.0

ute(id, obj);
    }
  }
  
  public HttpRoute getRoute()
  {
    HttpPoolEntry local = ensurePoolEntry();
    return local.getEffectiveRoute();
  }
  
  public void open(HttpRoute route, HttpContext context, HttpParams params)
    throws IOException
  {
    Args.notNull(route, "Route");
    Args.notNull(params, "HTTP parameters");
    OperatedClientConnection conn;
    synchronized (this)
    {
      if (poolEntry == null) {
        throw new ConnectionShutdownException();
      }
      RouteTracker tracker = poolEntry.getTracker();
      Asserts.notNull(tracker, "Route tracker");
      Asserts.check(!tracker.isConnected(), "Connection already open");
      conn = (OperatedClientConnection)poolEntry.getConnection();
    }
    HttpHost proxy = route.getProxyHost();
    operator.openConnection(conn, proxy != null ? proxy : route.getTargetHost(), route.getLocalAddress(), context, params);
    synchronized (this)
    {
      if (poolEntry == null) {
        throw new InterruptedIOException();
      }
      RouteTracker tracker = poolEntry.getTracker();
      if (proxy == null) {
        tracker.connectTarget(conn.isSecure());
      } else {
        tracker.connectProxy(proxy, conn.isSecure());
      }
    }
  }
  
  public void tunnelTarget(boolean secure, HttpParams params)
    throws IOException
  {
    Args.notNull(params, "HTTP parameters");
    HttpHost target;
    OperatedClientConnection conn;
    synchronized (this)
    {
      if (poolEntry == null) {
        throw new ConnectionShutdownException();
      }
      RouteTracker tracker = poolEntry.getTracker();
      Asserts.notNull(tracker, "Route tracker");
      Asserts.check(tracker.isConnected(), "Connection not open");
      Asserts.check(!tracker.isTunnelled(), "Connection is already tunnelled");
      target = tracker.getTargetHost();
      conn = (OperatedClientConnection)poolEntry.getConnection();
    }
    conn.update(null, target, secure, params);
    synchronized (this)
    {
      if (poolEntry == null) {
        throw new InterruptedIOException();
      }
      RouteTracker tracker = poolEntry.getTracker();
      tracker.tunnelTarget(secure);
    }
  }
  
  public void tunnelProxy(HttpHost next, boolean secure, HttpParams params)
    throws IOException
  {
    Args.notNull(next, "Next proxy");
    Args.notNull(params, "HTTP parameters");
    OperatedClientConnection conn;
    synchronized (this)
    {
      if (poolEntry == null) {
        throw new ConnectionShutdownException();
      }
      RouteTracker tracker = poolEntry.getTracker();
      Asserts.notNull(tracker, "Route tracker");
      Asserts.check(tracker.isConnected(), "Connection not open");
      conn = (OperatedClientConnection)poolEntry.getConnection();
    }
    conn.update(null, next, secure, params);
    synchronized (this)
    {
      if (poolEntry == null) {
        throw new InterruptedIOException();
      }
      RouteTracker tracker = poolEntry.getTracker();
      tracker.tunnelProxy(next, secure);
    }
  }
  
  public void layerProtocol(HttpContext context, HttpParams params)
    throws IOException
  {
    Args.notNull(params, "HTTP parameters");
    HttpHost target;
    OperatedClientConnection conn;
    synchronized (this)
    {
      if (poolEntry == null) {
        throw new ConnectionShutdownException();
      }
      RouteTracker tracker = poolEntry.getTracker();
      Asserts.notNull(tracker, "Route tracker");
      Asserts.check(tracker.isConnected(), "Connection not open");
      Asserts.check(tracker.isTunnelled(), "Protocol layering without a tunnel not supported");
      Asserts.check(!tracker.isLayered(), "Multiple protocol layering not supported");
      target = tracker.getTargetHost();
      conn = (OperatedClientConnection)poolEntry.getConnection();
    }
    operator.updateSecureConnection(conn, target, context, params);
    synchronized (this)
    {
      if (poolEntry == null) {
        throw new InterruptedIOException();
      }
      RouteTracker tracker = poolEntry.getTracker();
      tracker.layerProtocol(conn.isSecure());
    }
  }
  
  public Object getState()
  {
    HttpPoolEntry local = ensurePoolEntry();
    return local.getState();
  }
  
  public void setState(Object state)
  {
    HttpPoolEntry local = ensurePoolEntry();
    local.setState(state);
  }
  
  public void markReusable()
  {
    reusable = true;
  }
  
  public void unmarkReusable()
  {
    reusable = false;
  }
  
  public boolean isMarkedReusable()
  {
    return reusable;
  }
  
  public void setIdleDuration(long duration, TimeUnit unit)
  {
    if (duration > 0L) {
      this.duration = unit.toMillis(duration);
    } else {
      this.duration = -1L;
    }
  }
  
  public void releaseConnection()
  {
    synchronized (this)
    {
      if (poolEntry == null) {
        return;
      }
      manager.releaseConnection(this, duration, TimeUnit.MILLISECONDS);
      poolEntry = null;
    }
  }
  
  public void abortConnection()
  {
    synchronized (this)
    {
      if (poolEntry == null) {
        return;
      }
      reusable = false;
      OperatedClientConnection conn = (OperatedClientConnection)poolEntry.getConnection();
      try
      {
        conn.shutdown();
      }
      catch (IOException ignore) {}
      manager.releaseConnection(this, duration, TimeUnit.MILLISECONDS);
      poolEntry = null;
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.conn.ManagedClientConnectionImpl
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.conn;

import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.annotation.Immutable;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.conn.HttpConnectionFactory;
import org.apache.http.conn.ManagedHttpClientConnection;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.io.DefaultHttpRequestWriterFactory;
import org.apache.http.io.HttpMessageParserFactory;
import org.apache.http.io.HttpMessageWriterFactory;

@Immutable
public class ManagedHttpClientConnectionFactory
  implements HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection>
{
  private static final AtomicLong COUNTER = new AtomicLong();
  public static final ManagedHttpClientConnectionFactory INSTANCE = new ManagedHttpClientConnectionFactory();
  private final Log log = LogFactory.getLog(DefaultManagedHttpClientConnection.class);
  private final Log headerlog = LogFactory.getLog("org.apache.http.headers");
  private final Log wirelog = LogFactory.getLog("org.apache.http.wire");
  private final HttpMessageWriterFactory<HttpRequest> requestWriterFactory;
  private final HttpMessageParserFactory<HttpResponse> responseParserFactory;
  
  public ManagedHttpClientConnectionFactory(HttpMessageWriterFactory<HttpRequest> requestWriterFactory, HttpMessageParserFactory<HttpResponse> responseParserFactory)
  {
    this.requestWriterFactory = (requestWriterFactory != null ? requestWriterFactory : DefaultHttpRequestWriterFactory.INSTANCE);
    
    this.responseParserFactory = (responseParserFactory != null ? responseParserFactory : DefaultHttpResponseParserFactory.INSTANCE);
  }
  
  public ManagedHttpClientConnectionFactory(HttpMessageParserFactory<HttpResponse> responseParserFactory)
  {
    this(null, responseParserFactory);
  }
  
  public ManagedHttpClientConnectionFactory()
  {
    this(null, null);
  }
  
  public ManagedHttpClientConnection create(HttpRoute route, ConnectionConfig config)
  {
    ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
    CharsetDecoder chardecoder = null;
    CharsetEncoder charencoder = null;
    Charset charset = cconfig.getCharset();
    CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null ? cconfig.getMalformedInputAction() : CodingErrorAction.REPORT;
    
    CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null ? cconfig.getUnmappableInputAction() : CodingErrorAction.REPORT;
    if (charset != null)
    {
      chardecoder = charset.newDecoder();
      chardecoder.onMalformedInput(malformedInputAction);
      chardecoder.onUnmappableCharacter(unmappableInputAction);
      charencoder = charset.newEncoder();
      charencoder.onMalformedInput(malformedInputAction);
      charencoder.onUnmappableCharacter(unmappableInputAction);
    }
    String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
    return new LoggingManagedHttpClientConnection(id, log, headerlog, wirelog, cconfig.getBufferSize(), cconfig.getFragmentSizeHint(), chardecoder, charencoder, cconfig.getMessageConstraints(), null, null, requestWriterFactory, responseParserFactory);
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.conn.ManagedHttpClientConnectionFactory
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.conn;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.apache.http.conn.ClientConnectionRequest;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.ManagedClientConnection;

class PoolingClientConnectionManager$1
  implements ClientConnectionRequest
{
  PoolingClientConnectionManager$1(PoolingClientConnectionManager paramPoolingClientConnectionManager, Future paramFuture) {}
  
  public void abortRequest()
  {
    val$future.cancel(true);
  }
  
  public ManagedClientConnection getConnection(long timeout, TimeUnit tunit)
    throws InterruptedException, ConnectionPoolTimeoutException
  {
    return this$0.leaseConnection(val$future, timeout, tunit);
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.conn.PoolingClientConnectionManager.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.conn;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.conn.ClientConnectionRequest;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.ManagedClientConnection;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.pool.ConnPoolControl;
import org.apache.http.pool.PoolStats;
import org.apache.http.util.Args;
import org.apache.http.util.Asserts;

@Deprecated
@ThreadSafe
public class PoolingClientConnectionManager
  implements ClientConnectionManager, ConnPoolControl<HttpRoute>
{
  private final Log log = LogFactory.getLog(getClass());
  private final SchemeRegistry schemeRegistry;
  private final HttpConnPool pool;
  private final ClientConnectionOperator operator;
  private final DnsResolver dnsResolver;
  
  public PoolingClientConnectionManager(SchemeRegistry schreg)
  {
    this(schreg, -1L, TimeUnit.MILLISECONDS);
  }
  
  public PoolingClientConnectionManager(SchemeRegistry schreg, DnsResolver dnsResolver)
  {
    this(schreg, -1L, TimeUnit.MILLISECONDS, dnsResolver);
  }
  
  public PoolingClientConnectionManager()
  {
    this(SchemeRegistryFactory.createDefault());
  }
  
  public PoolingClientConnectionManager(SchemeRegistry schemeRegistry, long timeToLive, TimeUnit tunit)
  {
    this(schemeRegistry, timeToLive, tunit, new SystemDefaultDnsResolver());
  }
  
  public PoolingClientConnectionManager(SchemeRegistry schemeRegistry, long timeToLive, TimeUnit tunit, DnsResolver dnsResolver)
  {
    Args.notNull(schemeRegistry, "Scheme registry");
    Args.notNull(dnsResolver, "DNS resolver");
    this.schemeRegistry = schemeRegistry;
    this.dnsResolver = dnsResolver;
    operator = createConnectionOperator(schemeRegistry);
    pool = new HttpConnPool(log, operator, 2, 20, timeToLive, tunit);
  }
  
  protected void finalize()
    throws Throwable
  {
    try
    {
      shutdown();
    }
    finally
    {
      super.finalize();
    }
  }
  
  protected ClientConnectionOperator createConnectionOperator(SchemeRegistry schreg)
  {
    return new DefaultClientConnectionOperator(schreg, dnsResolver);
  }
  
  public SchemeRegistry getSchemeRegistry()
  {
    return schemeRegistry;
  }
  
  private String format(HttpRoute route, Object state)
  {
    StringBuilder buf = new StringBuilder();
    buf.append("[route: ").append(route).append("]");
    if (state != null) {
      buf.append("[state: ").append(state).append("]");
    }
    return buf.toString();
  }
  
  private String formatStats(HttpRoute route)
  {
    StringBuilder buf = new StringBuilder();
    PoolStats totals = pool.getTotalStats();
    PoolStats stats = pool.getStats(route);
    buf.append("[total kept alive: ").append(totals.getAvailable()).append("; ");
    buf.append("route allocated: ").append(stats.getLeased() + stats.getAvailable());
    buf.append(" of ").append(stats.getMax()).append("; ");
    buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable());
    buf.append(" of ").append(totals.getMax()).append("]");
    return buf.toString();
  }
  
  private String format(HttpPoolEntry entry)
  {
    StringBuilder buf = new StringBuilder();
    buf.append("[id: ").append(entry.getId()).append("]");
    buf.append("[route: ").append(entry.getRoute()).append("]");
    Object state = entry.getState();
    if (state != null) {
      buf.append("[state: ").append(state).append("]");
    }
    return buf.toString();
  }
  
  public ClientConnectionRequest requestConnection(HttpRoute route, Object state)
  {
    Args.notNull(route, "HTTP route");
    if (log.isDebugEnabled()) {
      log.debug("Connection request: " + format(route, state) + formatStats(route));
    }
    final Future<HttpPoolEntry> future = pool.lease(route, state);
    
    new ClientConnectionRequest()
    {
      public void abortRequest()
      {
        future.cancel(true);
      }
      
      public ManagedClientConnection getConnection(long timeout, TimeUnit tunit)
        throws InterruptedException, ConnectionPoolTimeoutException
      {
        return leaseConnection(future, timeout, tunit);
      }
    };
  }
  
  ManagedClientConnection leaseConnection(Future<HttpPoolEntry> future, long timeout, TimeUnit tunit)
    throws InterruptedException, ConnectionPoolTimeoutException
  {
    try
    {
      HttpPoolEntry entry = (HttpPoolEntry)future.get(timeout, tunit);
      if ((entry == null) || (future.isCancelled())) {
        throw new InterruptedException();
      }
      Asserts.check(entry.getConnection() != null, "Pool entry with no connection");
      if (log.isDebugEnabled()) {
        log.debug("Connection leased: " + format(entry) + formatStats((HttpRoute)entry.getRoute()));
      }
      return new ManagedClientConnectionImpl(this, operator, entry);
    }
    catch (ExecutionException ex)
    {
      Throwable cause = ex.getCause();
      if (cause == null) {
        cause = ex;
      }
      log.error("Unexpected exception leasing connection from pool", cause);
      
      throw new InterruptedException();
    }
    catch (TimeoutException ex)
    {
      throw new ConnectionPoolTimeoutException("Timeout waiting for connection from pool");
    }
  }
  
  public void releaseConnection(ManagedClientConnection conn, long keepalive, TimeUnit tunit)
  {
    Args.check(conn instanceof ManagedClientConnectionImpl, "Connection class mismatch, connection not obtained from this manager");
    
    ManagedClientConnectionImpl managedConn = (ManagedClientConnectionImpl)conn;
    Asserts.check(managedConn.getManager() == this, "Connection not obtained from this manager");
    synchronized (managedConn)
    {
      HttpPoolEntry entry = managedConn.detach();
      if (entry == null) {
        return;
      }
      try
      {
        if ((managedConn.isOpen()) && (!managedConn.isMarkedReusable())) {
          try
          {
            managedConn.shutdown();
          }
          catch (IOException iox)
          {
            if (log.isDebugEnabled()) {
              log.debug("I/O exception shutting down released connection", iox);
            }
          }
        }
        if (managedConn.isMarkedReusable())
        {
          entry.updateExpiry(keepalive, tunit != null ? tunit : TimeUnit.MILLISECONDS);
          if (log.isDebugEnabled())
          {
            String s;
            String s;
            if (keepalive > 0L) {
              s = "for " + keepalive + " " + tunit;
            } else {
              s = "indefinitely";
            }
            log.debug("Connection " + format(entry) + " can be kept alive " + s);
          }
        }
      }
      finally
      {
        pool.release(entry, managedConn.isMarkedReusable());
      }
      if (log.isDebugEnabled()) {
        log.debug("Connection released: " + format(entry) + formatStats((HttpRoute)entry.getRoute()));
      }
    }
  }
  
  public void shutdown()
  {
    log.debug("Connection manager is shutting down");
    try
    {
      pool.shutdown();
    }
    catch (IOException ex)
    {
      log.debug("I/O exception shutting down connection manager", ex);
    }
    log.debug("Connection manager shut down");
  }
  
  public void closeIdleConnections(long idleTimeout, TimeUnit tunit)
  {
    if (log.isDebugEnabled()) {
      log.debug("Closing connections idle longer than " + idleTimeout + " " + tunit);
    }
    pool.closeIdle(idleTimeout, tunit);
  }
  
  public void closeExpiredConnections()
  {
    log.debug("Closing expired connections");
    pool.closeExpired();
  }
  
  public int getMaxTotal()
  {
    return pool.getMaxTotal();
  }
  
  public void setMaxTotal(int max)
  {
    pool.setMaxTotal(max);
  }
  
  public int getDefaultMaxPerRoute()
  {
    return pool.getDefaultMaxPerRoute();
  }
  
  public void setDefaultMaxPerRoute(int max)
  {
    pool.setDefaultMaxPerRoute(max);
  }
  
  public int getMaxPerRoute(HttpRoute route)
  {
    return pool.getMaxPerRoute(route);
  }
  
  public void setMaxPerRoute(HttpRoute route, int max)
  {
    pool.setMaxPerRoute(route, max);
  }
  
  public PoolStats getTotalStats()
  {
    return pool.getTotalStats();
  }
  
  public PoolStats getStats(HttpRoute route)
  {
    return pool.getStats(route);
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.conn.PoolingClientConnectionManager
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.conn;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpClientConnection;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.ConnectionRequest;

class PoolingHttpClientConnectionManager$1
  implements ConnectionRequest
{
  PoolingHttpClientConnectionManager$1(PoolingHttpClientConnectionManager paramPoolingHttpClientConnectionManager, Future paramFuture) {}
  
  public boolean cancel()
  {
    return val$future.cancel(true);
  }
  
  public HttpClientConnection get(long timeout, TimeUnit tunit)
    throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException
  {
    return this$0.leaseConnection(val$future, timeout, tunit);
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.conn.PoolingHttpClientConnectionManager.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.conn;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.http.HttpHost;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.SocketConfig;

class PoolingHttpClientConnectionManager$ConfigData
{
  private final Map<HttpHost, SocketConfig> socketConfigMap;
  private final Map<HttpHost, ConnectionConfig> connectionConfigMap;
  private volatile SocketConfig defaultSocketConfig;
  private volatile ConnectionConfig defaultConnectionConfig;
  
  PoolingHttpClientConnectionManager$ConfigData()
  {
    socketConfigMap = new ConcurrentHashMap();
    connectionConfigMap = new ConcurrentHashMap();
  }
  
  public SocketConfig getDefaultSocketConfig()
  {
    return defaultSocketConfig;
  }
  
  public void setDefaultSocketConfig(SocketConfig defaultSocketConfig)
  {
    this.defaultSocketConfig = defaultSocketConfig;
  }
  
  public ConnectionConfig getDefaultConnectionConfig()
  {
    return defaultConnectionConfig;
  }
  
  public void setDefaultConnectionConfig(ConnectionConfig defaultConnectionConfig)
  {
    this.defaultConnectionConfig = defaultConnectionConfig;
  }
  
  public SocketConfig getSocketConfig(HttpHost host)
  {
    return (SocketConfig)socketConfigMap.get(host);
  }
  
  public void setSocketConfig(HttpHost host, SocketConfig socketConfig)
  {
    socketConfigMap.put(host, socketConfig);
  }
  
  public ConnectionConfig getConnectionConfig(HttpHost host)
  {
    return (ConnectionConfig)connectionConfigMap.get(host);
  }
  
  public void setConnectionConfig(HttpHost host, ConnectionConfig connectionConfig)
  {
    connectionConfigMap.put(host, connectionConfig);
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.conn.PoolingHttpClientConnectionManager.ConfigData
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.conn;

import java.io.IOException;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.conn.HttpConnectionFactory;
import org.apache.http.conn.ManagedHttpClientConnection;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.pool.ConnFactory;

class PoolingHttpClientConnectionManager$InternalConnectionFactory
  implements ConnFactory<HttpRoute, ManagedHttpClientConnection>
{
  private final PoolingHttpClientConnectionManager.ConfigData configData;
  private final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory;
  
  PoolingHttpClientConnectionManager$InternalConnectionFactory(PoolingHttpClientConnectionManager.ConfigData configData, HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory)
  {
    this.configData = (configData != null ? configData : new PoolingHttpClientConnectionManager.ConfigData());
    this.connFactory = (connFactory != null ? connFactory : ManagedHttpClientConnectionFactory.INSTANCE);
  }
  
  public ManagedHttpClientConnection create(HttpRoute route)
    throws IOException
  {
    ConnectionConfig config = null;
    if (route.getProxyHost() != null) {
      config = configData.getConnectionConfig(route.getProxyHost());
    }
    if (config == null) {
      config = configData.getConnectionConfig(route.getTargetHost());
    }
    if (config == null) {
      config = configData.getDefaultConnectionConfig();
    }
    if (config == null) {
      config = ConnectionConfig.DEFAULT;
    }
    return (ManagedHttpClientConnection)connFactory.create(route, config);
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.conn.PoolingHttpClientConnectionManager.InternalConnectionFactory
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.conn;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpClientConnection;
import org.apache.http.HttpHost;
import org.apache.http.annotation.ThreadSafe;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Lookup;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.ConnectionRequest;
import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.HttpConnectionFactory;
import org.apache.http.conn.ManagedHttpClientConnection;
import org.apache.http.conn.SchemePortResolver;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.pool.ConnFactory;
import org.apache.http.pool.ConnPoolControl;
import org.apache.http.pool.PoolStats;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.Args;
import org.apache.http.util.Asserts;

@ThreadSafe
public class PoolingHttpClientConnectionManager
  implements HttpClientConnectionManager, ConnPoolControl<HttpRoute>, Closeable
{
  private final Log log = LogFactory.getLog(getClass());
  private final ConfigData configData;
  private final CPool pool;
  private final HttpClientConnectionOperator connectionOperator;
  
  private static Registry<ConnectionSocketFactory> getDefaultRegistry()
  {
    return RegistryBuilder.create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
  }
  
  public PoolingHttpClientConnectionManager()
  {
    this(getDefaultRegistry());
  }
  
  public PoolingHttpClientConnectionManager(long timeToLive, TimeUnit tunit)
  {
    this(getDefaultRegistry(), null, null, null, timeToLive, tunit);
  }
  
  public PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry)
  {
    this(socketFactoryRegistry, null, null);
  }
  
  public PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry, DnsResolver dnsResolver)
  {
    this(socketFactoryRegistry, null, dnsResolver);
  }
  
  public PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry, HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory)
  {
    this(socketFactoryRegistry, connFactory, null);
  }
  
  public PoolingHttpClientConnectionManager(HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory)
  {
    this(getDefaultRegistry(), connFactory, null);
  }
  
  public PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry, HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory, DnsResolver dnsResolver)
  {
    this(socketFactoryRegistry, connFactory, null, dnsResolver, -1L, TimeUnit.MILLISECONDS);
  }
  
  public PoolingHttpClientConnectionManager(Registry<ConnectionSocketFactory> socketFactoryRegistry, HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory, SchemePortResolver schemePortResolver, DnsResolver dnsResolver, long timeToLive, TimeUnit tunit)
  {
    configData = new ConfigData();
    pool = new CPool(new InternalConnectionFactory(configData, connFactory), 2, 20, timeToLive, tunit);
    
    connectionOperator = new HttpClientConnectionOperator(socketFactoryRegistry, schemePortResolver, dnsResolver);
  }
  
  PoolingHttpClientConnectionManager(CPool pool, Lookup<ConnectionSocketFactory> socketFactoryRegistry, SchemePortResolver schemePortResolver, DnsResolver dnsResolver)
  {
    configData = new ConfigData();
    this.pool = pool;
    connectionOperator = new HttpClientConnectionOperator(socketFactoryRegistry, schemePortResolver, dnsResolver);
  }
  
  protected void finalize()
    throws Throwable
  {
    try
    {
      shutdown();
    }
    finally
    {
      super.finalize();
    }
  }
  
  public void close()
  {
    shutdown();
  }
  
  private String format(HttpRoute route, Object state)
  {
    StringBuilder buf = new StringBuilder();
    buf.append("[route: ").append(route).append("]");
    if (state != null) {
      buf.append("[state: ").append(state).append("]");
    }
    return buf.toString();
  }
  
  private String formatStats(HttpRoute route)
  {
    StringBuilder buf = new StringBuilder();
    PoolStats totals = pool.getTotalStats();
    PoolStats stats = pool.getStats(route);
    buf.append("[total kept alive: ").append(totals.getAvailable()).append("; ");
    buf.append("route allocated: ").append(stats.getLeased() + stats.getAvailable());
    buf.append(" of ").append(stats.getMax()).append("; ");
    buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable());
    buf.append(" of ").append(totals.getMax()).append("]");
    return buf.toString();
  }
  
  private String format(CPoolEntry entry)
  {
    StringBuilder buf = new StringBuilder();
    buf.append("[id: ").append(entry.getId()).append("]");
    buf.append("[route: ").append(entry.getRoute()).append("]");
    Object state = entry.getState();
    if (state != null) {
      buf.append("[state: ").append(state).append("]");
    }
    return buf.toString();
  }
  
  public ConnectionRequest requestConnection(HttpRoute route, Object state)
  {
    Args.notNull(route, "HTTP route");
    if (log.isDebugEnabled()) {
      log.debug("Connection request: " + format(route, state) + formatStats(route));
    }
    final Future<CPoolEntry> future = pool.lease(route, state, null);
    new ConnectionRequest()
    {
      public boolean cancel()
      {
        return future.cancel(true);
      }
      
      public HttpClientConnection get(long timeout, TimeUnit tunit)
        throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException
      {
        return leaseConnection(future, timeout, tunit);
      }
    };
  }
  
  protected HttpClientConnection leaseConnection(Future<CPoolEntry> future, long timeout, TimeUnit tunit)
    throws InterruptedException, ExecutionException, ConnectionPoolTimeoutException
  {
    try
    {
      CPoolEntry entry = (CPoolEntry)future.get(timeout, tunit);
      if ((entry == null) || (future.isCancelled())) {
        throw new InterruptedException();
      }
      Asserts.check(entry.getConnection() != null, "Pool entry with no connection");
      if (log.isDebugEnabled()) {
        log.debug("Connection leased: " + format(entry) + formatStats((HttpRoute)entry.getRoute()));
      }
      return CPoolProxy.newProxy(entry);
    }
    catch (TimeoutException ex)
    {
      throw new ConnectionPoolTimeoutException("Timeout waiting for connection from pool");
    }
  }
  
  public void releaseConnection(HttpClientConnection managedConn, Object state, long keepalive, TimeUnit tunit)
  {
    Args.notNull(managedConn, "Managed connection");
    synchronized (managedConn)
    {
      CPoolEntry entry = CPoolProxy.detach(managedConn);
      if (entry == null) {
        return;
      }
      ManagedHttpClientConnection conn = (ManagedHttpClientConnection)entry.getConnection();
      try
      {
        if (conn.isOpen())
        {
          entry.setState(state);
          entry.updateExpiry(keepalive, tunit != null ? tunit : TimeUnit.MILLISECONDS);
          if (log.isDebugEnabled())
          {
            String s;
            String s;
            if (keepalive > 0L) {
              s = "for " + keepalive / 1000.0D + " seconds";
            } else {
              s = "indefinitely";
            }
            log.debug("Connection " + format(entry) + " can be kept alive " + s);
          }
        }
      }
      finally
      {
        pool.release(entry, (conn.isOpen()) && (entry.isRouteComplete()));
        if (log.isDebugEnabled()) {
          log.debug("Connection released: " + format(entry) + formatStats((HttpRoute)entry.getRoute()));
        }
      }
    }
  }
  
  public void connect(HttpClientConnection managedConn, HttpRoute route, int connectTimeout, HttpContext context)
    throws IOException
  {
    Args.notNull(managedConn, "Managed Connection");
    Args.notNull(route, "HTTP route");
    ManagedHttpClientConnection conn;
    synchronized (managedConn)
    {
      CPoolEntry entry = CPoolProxy.getPoolEntry(managedConn);
      conn = (ManagedHttpClientConnection)entry.getConnection();
    }
    HttpHost host;
    HttpHost host;
    if (route.getProxyHost() != null) {
      host = route.getProxyHost();
    } else {
      host = route.getTargetHost();
    }
    InetSocketAddress localAddress = route.getLocalSocketAddress();
    SocketConfig socketConfig = configData.getSocketConfig(host);
    if (socketConfig == null) {
      socketConfig = configData.getDefaultSocketConfig();
    }
    if (socketConfig == null) {
      socketConfig = SocketConfig.DEFAULT;
    }
    connectionOperator.connect(conn, host, localAddress, connectTimeout, socketConfig, context);
  }
  
  public void upgrade(HttpClientConnection managedConn, HttpRoute route, HttpContext context)
    throws IOException
  {
    Args.notNull(managedConn, "Managed Connection");
    Args.notNull(route, "HTTP route");
    ManagedHttpClientConnection conn;
    synchronized (managedConn)
    {
      CPoolEntry entry = CPoolProxy.getPoolEntry(managedConn);
      conn = (ManagedHttpClientConnection)entry.getConnection();
    }
    connectionOperator.upgrade(conn, route.getTargetHost(), context);
  }
  
  public void routeComplete(HttpClientConnection managedConn, HttpRoute route, HttpContext context)
    throws IOException
  {
    Args.notNull(managedConn, "Managed Connection");
    Args.notNull(route, "HTTP route");
    synchronized (managedConn)
    {
      CPoolEntry entry = CPoolProxy.getPoolEntry(managedConn);
      entry.markRouteComplete();
    }
  }
  
  public void shutdown()
  {
    log.debug("Connection manager is shutting down");
    try
    {
      pool.shutdown();
    }
    catch (IOException ex)
    {
      log.debug("I/O exception shutting down connection manager", ex);
    }
    log.debug("Connection manager shut down");
  }
  
  public void closeIdleConnections(long idleTimeout, TimeUnit tunit)
  {
    if (log.isDebugEnabled()) {
      log.debug("Closing connections idle longer than " + idleTimeout + " " + tunit);
    }
    pool.closeIdle(idleTimeout, tunit);
  }
  
  public void closeExpiredConnections()
  {
    log.debug("Closing expired connections");
    pool.closeExpired();
  }
  
  public int getMaxTotal()
  {
    return pool.getMaxTotal();
  }
  
  public void setMaxTotal(int max)
  {
    pool.setMaxTotal(max);
  }
  
  public int getDefaultMaxPerRoute()
  {
    return pool.getDefaultMaxPerRoute();
  }
  
  public void setDefaultMaxPerRoute(int max)
  {
    pool.setDefaultMaxPerRoute(max);
  }
  
  public int getMaxPerRoute(HttpRoute route)
  {
    return pool.getMaxPerRoute(route);
  }
  
  public void setMaxPerRoute(HttpRoute route, int max)
  {
    pool.setMaxPerRoute(route, max);
  }
  
  public PoolStats getTotalStats()
  {
    return pool.getTotalStats();
  }
  
  public PoolStats getStats(HttpRoute route)
  {
    return pool.getStats(route);
  }
  
  public SocketConfig getDefaultSocketConfig()
  {
    return configData.getDefaultSocketConfig();
  }
  
  public void setDefaultSocketConfig(SocketConfig defaultSocketConfig)
  {
    configData.setDefaultSocketConfig(defaultSocketConfig);
  }
  
  public ConnectionConfig getDefaultConnectionConfig()
  {
    return configData.getDefaultConnectionConfig();
  }
  
  public void setDefaultConnectionConfig(ConnectionConfig defaultConnectionConfig)
  {
    configData.setDefaultConnectionConfig(defaultConnectionConfig);
  }
  
  public SocketConfig getSocketConfig(HttpHost host)
  {
    return configData.getSocketConfig(host);
  }
  
  public void setSocketConfig(HttpHost host, SocketConfig socketConfig)
  {
    configData.setSocketConfig(host, socketConfig);
  }
  
  public ConnectionConfig getConnectionConfig(HttpHost host)
  {
    return configData.getConnectionConfig(host);
  }
  
  public void setConnectionConfig(HttpHost host, ConnectionConfig connectionConfig)
  {
    configData.setConnectionConfig(host, connectionConfig);
  }
  
  static class ConfigData
  {
    private final Map<HttpHost, SocketConfig> socketConfigMap;
    private final Map<HttpHost, ConnectionConfig> connectionConfigMap;
    private volatile SocketConfig defaultSocketConfig;
    private volatile ConnectionConfig defaultConnectionConfig;
    
    ConfigData()
    {
      socketConfigMap = new ConcurrentHashMap();
      connectionConfigMap = new ConcurrentHashMap();
    }
    
    public SocketConfig getDefaultSocketConfig()
    {
      return defaultSocketConfig;
    }
    
    public void setDefaultSocketConfig(SocketConfig defaultSocketConfig)
    {
      this.defaultSocketConfig = defaultSocketConfig;
    }
    
    public ConnectionConfig getDefaultConnectionConfig()
    {
      return defaultConnectionConfig;
    }
    
    public void setDefaultConnectionConfig(ConnectionConfig defaultConnectionConfig)
    {
      this.defaultConnectionConfig = defaultConnectionConfig;
    }
    
    public SocketConfig getSocketConfig(HttpHost host)
    {
      return (SocketConfig)socketConfigMap.get(host);
    }
    
    public void setSocketConfig(HttpHost host, SocketConfig socketConfig)
    {
      socketConfigMap.put(host, socketConfig);
    }
    
    public ConnectionConfig getConnectionConfig(HttpHost host)
    {
      return (ConnectionConfig)connectionConfigMap.get(host);
    }
    
    public void setConnectionConfig(HttpHost host, ConnectionConfig connectionConfig)
    {
      connectionConfigMap.put(host, connectionConfig);
    }
  }
  
  static class InternalConnectionFactory
    implements ConnFactory<HttpRoute, ManagedHttpClientConnection>
  {
    private final PoolingHttpClientConnectionManager.ConfigData configData;
    private final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory;
    
    InternalConnectionFactory(PoolingHttpClientConnectionManager.ConfigData configData, HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory)
    {
      this.configData = (configData != null ? configData : new PoolingHttpClientConnectionManager.ConfigData());
      this.connFactory = (connFactory != null ? connFactory : ManagedHttpClientConnectionFactory.INSTANCE);
    }
    
    public ManagedHttpClientConnection create(HttpRoute route)
      throws IOException
    {
      ConnectionConfig config = null;
      if (route.getProxyHost() != null) {
        config = configData.getConnectionConfig(route.getProxyHost());
      }
      if (config == null) {
        config = configData.getConnectionConfig(route.getTargetHost());
      }
      if (config == null) {
        config = configData.getDefaultConnectionConfig();
      }
      if (config == null) {
        config = ConnectionConfig.DEFAULT;
      }
      return (ManagedHttpClientConnection)connFactory.create(route, config);
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.conn.PoolingHttpClientConnectionManager
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.conn;

class ProxySelectorRoutePlanner$1 {}

/* Location:
 * Qualified Name:     org.apache.http.impl.conn.ProxySelectorRoutePlanner.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.conn;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import org.apache.http
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2019. Infinite Loop Ltd