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