View Javadoc

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.pool;
28  
29  import java.util.HashSet;
30  import java.util.Iterator;
31  import java.util.LinkedList;
32  import java.util.Set;
33  
34  import org.apache.http.util.Args;
35  import org.apache.http.util.Asserts;
36  
37  abstract class RouteSpecificPool<T, C, E extends PoolEntry<T, C>> {
38  
39      private final T route;
40      private final Set<E> leased;
41      private final LinkedList<E> available;
42      private final LinkedList<PoolEntryFuture<E>> pending;
43  
44      RouteSpecificPool(final T route) {
45          super();
46          this.route = route;
47          this.leased = new HashSet<E>();
48          this.available = new LinkedList<E>();
49          this.pending = new LinkedList<PoolEntryFuture<E>>();
50      }
51  
52      protected abstract E createEntry(C conn);
53  
54      public final T getRoute() {
55          return route;
56      }
57  
58      public int getLeasedCount() {
59          return this.leased.size();
60      }
61  
62      public int getPendingCount() {
63          return this.pending.size();
64      }
65  
66      public int getAvailableCount() {
67          return this.available.size();
68      }
69  
70      public int getAllocatedCount() {
71          return this.available.size() + this.leased.size();
72      }
73  
74      public E getFree(final Object state) {
75          if (!this.available.isEmpty()) {
76              if (state != null) {
77                  final Iterator<E> it = this.available.iterator();
78                  while (it.hasNext()) {
79                      final E entry = it.next();
80                      if (state.equals(entry.getState())) {
81                          it.remove();
82                          this.leased.add(entry);
83                          return entry;
84                      }
85                  }
86              }
87              final Iterator<E> it = this.available.iterator();
88              while (it.hasNext()) {
89                  final E entry = it.next();
90                  if (entry.getState() == null) {
91                      it.remove();
92                      this.leased.add(entry);
93                      return entry;
94                  }
95              }
96          }
97          return null;
98      }
99  
100     public E getLastUsed() {
101         if (!this.available.isEmpty()) {
102             return this.available.getLast();
103         } else {
104             return null;
105         }
106     }
107 
108     public boolean remove(final E entry) {
109         Args.notNull(entry, "Pool entry");
110         if (!this.available.remove(entry)) {
111             if (!this.leased.remove(entry)) {
112                 return false;
113             }
114         }
115         return true;
116     }
117 
118     public void free(final E entry, final boolean reusable) {
119         Args.notNull(entry, "Pool entry");
120         final boolean found = this.leased.remove(entry);
121         Asserts.check(found, "Entry %s has not been leased from this pool", entry);
122         if (reusable) {
123             this.available.addFirst(entry);
124         }
125     }
126 
127     public E add(final C conn) {
128         final E entry = createEntry(conn);
129         this.leased.add(entry);
130         return entry;
131     }
132 
133     public void queue(final PoolEntryFuture<E> future) {
134         if (future == null) {
135             return;
136         }
137         this.pending.add(future);
138     }
139 
140     public PoolEntryFuture<E> nextPending() {
141         return this.pending.poll();
142     }
143 
144     public void unqueue(final PoolEntryFuture<E> future) {
145         if (future == null) {
146             return;
147         }
148 
149         this.pending.remove(future);
150     }
151 
152     public void shutdown() {
153         for (final PoolEntryFuture<E> future: this.pending) {
154             future.cancel(true);
155         }
156         this.pending.clear();
157         for (final E entry: this.available) {
158             entry.close();
159         }
160         this.available.clear();
161         for (final E entry: this.leased) {
162             entry.close();
163         }
164         this.leased.clear();
165     }
166 
167     @Override
168     public String toString() {
169         final StringBuilder buffer = new StringBuilder();
170         buffer.append("[route: ");
171         buffer.append(this.route);
172         buffer.append("][leased: ");
173         buffer.append(this.leased.size());
174         buffer.append("][available: ");
175         buffer.append(this.available.size());
176         buffer.append("][pending: ");
177         buffer.append(this.pending.size());
178         buffer.append("]");
179         return buffer.toString();
180     }
181 
182 }