Example usage for org.apache.http.util Args check

List of usage examples for org.apache.http.util Args check

Introduction

In this page you can find the example usage for org.apache.http.util Args check.

Prototype

public static void check(boolean z, String str) 

Source Link

Usage

From source file:org.apache.http.impl.conn.BasicClientConnectionManager.java

public void releaseConnection(final ManagedClientConnection conn, final long keepalive, final TimeUnit tunit) {
    Args.check(conn instanceof ManagedClientConnectionImpl,
            "Connection class mismatch, " + "connection not obtained from this manager");
    final ManagedClientConnectionImpl managedConn = (ManagedClientConnectionImpl) conn;
    synchronized (managedConn) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("Releasing connection " + conn);
        }/* w  w w  .j av  a  2 s  .  c o m*/
        if (managedConn.getPoolEntry() == null) {
            return; // already released
        }
        final ClientConnectionManager manager = managedConn.getManager();
        Asserts.check(manager == this, "Connection not obtained from this manager");
        synchronized (this) {
            if (this.shutdown) {
                shutdownConnection(managedConn);
                return;
            }
            try {
                if (managedConn.isOpen() && !managedConn.isMarkedReusable()) {
                    shutdownConnection(managedConn);
                }
                if (managedConn.isMarkedReusable()) {
                    this.poolEntry.updateExpiry(keepalive, tunit != null ? tunit : TimeUnit.MILLISECONDS);
                    if (this.log.isDebugEnabled()) {
                        final String s;
                        if (keepalive > 0) {
                            s = "for " + keepalive + " " + tunit;
                        } else {
                            s = "indefinitely";
                        }
                        this.log.debug("Connection can be kept alive " + s);
                    }
                }
            } finally {
                managedConn.detach();
                this.conn = null;
                if (this.poolEntry.isClosed()) {
                    this.poolEntry = null;
                }
            }
        }
    }
}

From source file:org.apache.http.impl.conn.JMeterPoolingClientConnectionManager.java

@Override
public void releaseConnection(final ManagedClientConnection conn, final long keepalive, final TimeUnit tunit) {

    Args.check(conn instanceof ManagedClientConnectionImpl,
            "Connection class mismatch, " + "connection not obtained from this manager");
    final ManagedClientConnectionImpl managedConn = (ManagedClientConnectionImpl) conn;
    Asserts.check(managedConn.getManager() == this, "Connection not obtained from this manager");
    synchronized (managedConn) {
        final HttpPoolEntry entry = managedConn.detach();
        if (entry == null) {
            return;
        }//from ww w .jav  a 2 s  .  co m
        try {
            if (managedConn.isOpen() && !managedConn.isMarkedReusable()) {
                try {
                    managedConn.shutdown();
                } catch (final IOException iox) {
                    if (this.log.isDebugEnabled()) {
                        this.log.debug("I/O exception shutting down released connection", iox);
                    }
                }
            }
            // Only reusable connections can be kept alive
            if (managedConn.isMarkedReusable()) {
                entry.updateExpiry(keepalive, tunit != null ? tunit : TimeUnit.MILLISECONDS);
                if (this.log.isDebugEnabled()) {
                    final String s;
                    if (keepalive > 0) {
                        s = "for " + keepalive + " " + tunit;
                    } else {
                        s = "indefinitely";
                    }
                    this.log.debug("Connection " + format(entry) + " can be kept alive " + s);
                }
            }
        } finally {
            this.pool.release(entry, managedConn.isMarkedReusable());
        }
        if (this.log.isDebugEnabled()) {
            this.log.debug("Connection released: " + format(entry) + formatStats(entry.getRoute()));
        }
    }
}

From source file:org.apache.http.impl.conn.PoolingClientConnectionManager.java

public void releaseConnection(final ManagedClientConnection conn, final long keepalive, final TimeUnit tunit) {

    Args.check(conn instanceof ManagedClientConnectionImpl,
            "Connection class mismatch, " + "connection not obtained from this manager");
    final ManagedClientConnectionImpl managedConn = (ManagedClientConnectionImpl) conn;
    Asserts.check(managedConn.getManager() == this, "Connection not obtained from this manager");
    synchronized (managedConn) {
        final HttpPoolEntry entry = managedConn.detach();
        if (entry == null) {
            return;
        }// w ww  .ja  v a2  s.c  om
        try {
            if (managedConn.isOpen() && !managedConn.isMarkedReusable()) {
                try {
                    managedConn.shutdown();
                } catch (final IOException iox) {
                    if (this.log.isDebugEnabled()) {
                        this.log.debug("I/O exception shutting down released connection", iox);
                    }
                }
            }
            // Only reusable connections can be kept alive
            if (managedConn.isMarkedReusable()) {
                entry.updateExpiry(keepalive, tunit != null ? tunit : TimeUnit.MILLISECONDS);
                if (this.log.isDebugEnabled()) {
                    final String s;
                    if (keepalive > 0) {
                        s = "for " + keepalive + " " + tunit;
                    } else {
                        s = "indefinitely";
                    }
                    this.log.debug("Connection " + format(entry) + " can be kept alive " + s);
                }
            }
        } finally {
            this.pool.release(entry, managedConn.isMarkedReusable());
        }
        if (this.log.isDebugEnabled()) {
            this.log.debug("Connection released: " + format(entry) + formatStats(entry.getRoute()));
        }
    }
}

From source file:org.apache.http.impl.conn.SingleClientConnManager.java

public void releaseConnection(final ManagedClientConnection conn, final long validDuration,
        final TimeUnit timeUnit) {
    Args.check(conn instanceof ConnAdapter,
            "Connection class mismatch, " + "connection not obtained from this manager");
    assertStillUp();// w w  w .j ava 2s .  c om

    if (log.isDebugEnabled()) {
        log.debug("Releasing connection " + conn);
    }

    final ConnAdapter sca = (ConnAdapter) conn;
    synchronized (sca) {
        if (sca.poolEntry == null) {
            return; // already released
        }
        final ClientConnectionManager manager = sca.getManager();
        Asserts.check(manager == this, "Connection not obtained from this manager");
        try {
            // make sure that the response has been read completely
            if (sca.isOpen() && (this.alwaysShutDown || !sca.isMarkedReusable())) {
                if (log.isDebugEnabled()) {
                    log.debug("Released connection open but not reusable.");
                }

                // make sure this connection will not be re-used
                // we might have gotten here because of a shutdown trigger
                // shutdown of the adapter also clears the tracked route
                sca.shutdown();
            }
        } catch (final IOException iox) {
            if (log.isDebugEnabled()) {
                log.debug("Exception shutting down released connection.", iox);
            }
        } finally {
            sca.detach();
            synchronized (this) {
                managedConn = null;
                lastReleaseTime = System.currentTimeMillis();
                if (validDuration > 0) {
                    connectionExpiresTime = timeUnit.toMillis(validDuration) + lastReleaseTime;
                } else {
                    connectionExpiresTime = Long.MAX_VALUE;
                }
            }
        }
    }
}

From source file:org.apache.http.impl.conn.tsccm.RouteSpecificPool.java

/**
 * Indicates creation of an entry for this pool.
 * The entry will <i>not</i> be added to the list of free entries,
 * it is only recognized as belonging to this pool now. It can then
 * be passed to {@link #freeEntry freeEntry}.
 *
 * @param entry     the entry that was created for this pool
 *//* w ww.  j av  a 2  s.  com*/
public void createdEntry(final BasicPoolEntry entry) {
    Args.check(route.equals(entry.getPlannedRoute()), "Entry not planned for this pool");
    numEntries++;
}

From source file:org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.java

public void releaseConnection(final ManagedClientConnection conn, final long validDuration,
        final TimeUnit timeUnit) {
    Args.check(conn instanceof BasicPooledConnAdapter,
            "Connection class mismatch, " + "connection not obtained from this manager");
    final BasicPooledConnAdapter hca = (BasicPooledConnAdapter) conn;
    if (hca.getPoolEntry() != null) {
        Asserts.check(hca.getManager() == this, "Connection not obtained from this manager");
    }// ww  w  .j a v a2 s  .  c  om
    synchronized (hca) {
        final BasicPoolEntry entry = (BasicPoolEntry) hca.getPoolEntry();
        if (entry == null) {
            return;
        }
        try {
            // make sure that the response has been read completely
            if (hca.isOpen() && !hca.isMarkedReusable()) {
                // In MTHCM, there would be a call to
                // SimpleHttpConnectionManager.finishLastResponse(conn);
                // Consuming the response is handled outside in 4.0.

                // make sure this connection will not be re-used
                // Shut down rather than close, we might have gotten here
                // because of a shutdown trigger.
                // Shutdown of the adapter also clears the tracked route.
                hca.shutdown();
            }
        } catch (final IOException iox) {
            if (log.isDebugEnabled()) {
                log.debug("Exception shutting down released connection.", iox);
            }
        } finally {
            final boolean reusable = hca.isMarkedReusable();
            if (log.isDebugEnabled()) {
                if (reusable) {
                    log.debug("Released connection is reusable.");
                } else {
                    log.debug("Released connection is not reusable.");
                }
            }
            hca.detach();
            pool.freeEntry(entry, reusable, validDuration, timeUnit);
        }
    }
}

From source file:org.apache.http.impl.nio.client.PipeliningClientExchangeHandlerImpl.java

public PipeliningClientExchangeHandlerImpl(final Log log, final HttpHost target,
        final List<? extends HttpAsyncRequestProducer> requestProducers,
        final List<? extends HttpAsyncResponseConsumer<T>> responseConsumers,
        final HttpClientContext localContext, final BasicFuture<List<T>> resultFuture,
        final NHttpClientConnectionManager connmgr, final HttpProcessor httpProcessor,
        final ConnectionReuseStrategy connReuseStrategy, final ConnectionKeepAliveStrategy keepaliveStrategy) {
    super(log, localContext, resultFuture, connmgr, connReuseStrategy, keepaliveStrategy);
    Args.notNull(target, "HTTP target");
    Args.notEmpty(requestProducers, "Request producer list");
    Args.notEmpty(responseConsumers, "Response consumer list");
    Args.check(requestProducers.size() == responseConsumers.size(),
            "Number of request producers does not match that of response consumers");
    this.target = target;
    this.requestProducerQueue = new ConcurrentLinkedQueue<HttpAsyncRequestProducer>(requestProducers);
    this.responseConsumerQueue = new ConcurrentLinkedQueue<HttpAsyncResponseConsumer<T>>(responseConsumers);
    this.requestQueue = new ConcurrentLinkedQueue<HttpRequest>();
    this.resultQueue = new ConcurrentLinkedQueue<T>();
    this.localContext = localContext;
    this.resultFuture = resultFuture;
    this.httpProcessor = httpProcessor;
    this.requestProducerRef = new AtomicReference<HttpAsyncRequestProducer>(null);
    this.responseConsumerRef = new AtomicReference<HttpAsyncResponseConsumer<T>>(null);
}