001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027package org.apache.http.impl.conn;
028
029import java.io.IOException;
030import java.util.Date;
031import java.util.concurrent.TimeUnit;
032
033import org.apache.commons.logging.Log;
034import org.apache.http.HttpClientConnection;
035import org.apache.http.annotation.Contract;
036import org.apache.http.annotation.ThreadingBehavior;
037import org.apache.http.conn.ManagedHttpClientConnection;
038import org.apache.http.conn.routing.HttpRoute;
039import org.apache.http.pool.PoolEntry;
040
041/**
042 * @since 4.3
043 */
044@Contract(threading = ThreadingBehavior.SAFE)
045class CPoolEntry extends PoolEntry<HttpRoute, ManagedHttpClientConnection> {
046
047    private final Log log;
048    private volatile boolean routeComplete;
049
050    public CPoolEntry(
051            final Log log,
052            final String id,
053            final HttpRoute route,
054            final ManagedHttpClientConnection conn,
055            final long timeToLive, final TimeUnit tunit) {
056        super(id, route, conn, timeToLive, tunit);
057        this.log = log;
058    }
059
060    public void markRouteComplete() {
061        this.routeComplete = true;
062    }
063
064    public boolean isRouteComplete() {
065        return this.routeComplete;
066    }
067
068    public void closeConnection() throws IOException {
069        final HttpClientConnection conn = getConnection();
070        conn.close();
071    }
072
073    public void shutdownConnection() throws IOException {
074        final HttpClientConnection conn = getConnection();
075        conn.shutdown();
076    }
077
078    @Override
079    public boolean isExpired(final long now) {
080        final boolean expired = super.isExpired(now);
081        if (expired && this.log.isDebugEnabled()) {
082            this.log.debug("Connection " + this + " expired @ " + new Date(getExpiry()));
083        }
084        return expired;
085    }
086
087    @Override
088    public boolean isClosed() {
089        final HttpClientConnection conn = getConnection();
090        return !conn.isOpen();
091    }
092
093    @Override
094    public void close() {
095        try {
096            closeConnection();
097        } catch (final IOException ex) {
098            this.log.debug("I/O error closing connection", ex);
099        }
100    }
101
102}