FileDocCategorySizeDatePackage
RouteSpecificPool.javaAPI DocAndroid 1.5 API9207Wed May 06 22:41:10 BST 2009org.apache.http.impl.conn.tsccm

RouteSpecificPool

public class RouteSpecificPool extends Object
A connection sub-pool for a specific route, used by {@link ConnPoolByRoute}. The methods in this class are unsynchronized. It is expected that the containing pool takes care of synchronization.

Fields Summary
private final Log
log
protected final HttpRoute
route
The route this pool is for.
protected final int
maxEntries
the maximum number of entries allowed for this pool
protected final LinkedList
freeEntries
The list of free entries. This list is managed LIFO, to increase idle times and allow for closing connections that are not really needed.
protected final Queue
waitingThreads
The list of threads waiting for this pool.
protected int
numEntries
The number of created entries.
Constructors Summary
public RouteSpecificPool(HttpRoute route, int maxEntries)
Creates a new route-specific pool.

param
route the route for which to pool
param
maxEntries the maximum number of entries allowed for this pool



                                 
         
        this.route = route;
        this.maxEntries = maxEntries;
        this.freeEntries = new LinkedList<BasicPoolEntry>();
        this.waitingThreads = new LinkedList<WaitingThread>();
        this.numEntries = 0;
    
Methods Summary
public org.apache.http.impl.conn.tsccm.BasicPoolEntryallocEntry(java.lang.Object state)
Obtains a free entry from this pool, if one is available.

return
an available pool entry, or null if there is none

        if (!freeEntries.isEmpty()) {
            ListIterator<BasicPoolEntry> it = freeEntries.listIterator(freeEntries.size());
            while (it.hasPrevious()) {
                BasicPoolEntry entry = it.previous();
                if (LangUtils.equals(state, entry.getState())) {
                    it.remove();
                    return entry;
                }
            }
        }
        if (!freeEntries.isEmpty()) {
            BasicPoolEntry entry = freeEntries.remove();   
            entry.setState(null);
            OperatedClientConnection conn = entry.getConnection();
            try {
                conn.close();
            } catch (IOException ex) {
                log.debug("I/O error closing connection", ex);
            }
            return entry;
        }
        return null;
    
public voidcreatedEntry(org.apache.http.impl.conn.tsccm.BasicPoolEntry entry)
Indicates creation of an entry for this pool. The entry will not 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


        if (!route.equals(entry.getPlannedRoute())) {
            throw new IllegalArgumentException
                ("Entry not planned for this pool." +
                 "\npool: " + route +
                 "\nplan: " + entry.getPlannedRoute());
        }

        numEntries++;
    
public booleandeleteEntry(org.apache.http.impl.conn.tsccm.BasicPoolEntry entry)
Deletes an entry from this pool. Only entries that are currently free in this pool can be deleted. Allocated entries can not be deleted.

param
entry the entry to delete from this pool
return
true if the entry was found and deleted, or false if the entry was not found


        final boolean found = freeEntries.remove(entry);
        if (found)
            numEntries--;
        return found;
    
public voiddropEntry()
Forgets about an entry from this pool. This method is used to indicate that an entry {@link #allocEntry allocated} from this pool has been lost and will not be returned.

        if (numEntries < 1) {
            throw new IllegalStateException
                ("There is no entry that could be dropped.");
        }
        numEntries--;
    
public voidfreeEntry(org.apache.http.impl.conn.tsccm.BasicPoolEntry entry)
Returns an allocated entry to this pool.

param
entry the entry obtained from {@link #allocEntry allocEntry} or presented to {@link #createdEntry createdEntry}


        if (numEntries < 1) {
            throw new IllegalStateException
                ("No entry created for this pool. " + route);
        }
        if (numEntries <= freeEntries.size()) {
            throw new IllegalStateException
                ("No entry allocated from this pool. " + route);
        }
        freeEntries.add(entry);
    
public intgetCapacity()
Return remaining capacity of this pool

return
capacity

        return maxEntries - numEntries;
    
public final intgetEntryCount()
Obtains the number of entries. This includes not only the free entries, but also those that have been created and are currently issued to an application.

return
the number of entries for the route of this pool

        return numEntries;
    
public final intgetMaxEntries()
Obtains the maximum number of entries allowed for this pool.

return
the max entry number

        return maxEntries;
    
public final org.apache.http.conn.routing.HttpRoutegetRoute()
Obtains the route for which this pool is specific.

return
the route

        return route;
    
public booleanhasThread()
Checks whether there is a waiting thread in this pool.

return
true if there is a waiting thread, false otherwise

        return !this.waitingThreads.isEmpty();
    
public booleanisUnused()
Indicates whether this pool is unused. A pool is unused if there is neither an entry nor a waiting thread. All entries count, not only the free but also the allocated ones.

return
true if this pool is unused, false otherwise

        return (numEntries < 1) && waitingThreads.isEmpty();
    
public org.apache.http.impl.conn.tsccm.WaitingThreadnextThread()
Returns the next thread in the queue.

return
a waiting thread, or null if there is none

        return this.waitingThreads.peek();
    
public voidqueueThread(org.apache.http.impl.conn.tsccm.WaitingThread wt)
Adds a waiting thread. This pool makes no attempt to match waiting threads with pool entries. It is the caller's responsibility to check that there is no entry before adding a waiting thread.

param
wt the waiting thread

        if (wt == null) {
            throw new IllegalArgumentException
                ("Waiting thread must not be null.");
        }
        this.waitingThreads.add(wt);
    
public voidremoveThread(org.apache.http.impl.conn.tsccm.WaitingThread wt)
Removes a waiting thread, if it is queued.

param
wt the waiting thread

        if (wt == null)
            return;

        this.waitingThreads.remove(wt);