File: KeyedObjectPool.java

package info (click to toggle)
tomcat9 9.0.111-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,852 kB
  • sloc: java: 381,732; xml: 70,399; jsp: 4,682; sh: 1,336; perl: 324; makefile: 18; ansic: 14
file content (326 lines) | stat: -rw-r--r-- 12,843 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tomcat.dbcp.pool2;

import java.io.Closeable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * A "keyed" pooling interface.
 * <p>
 * A keyed pool maintains a pool of instances for each key value.
 * </p>
 * <p>
 * Example of use:
 * </p>
 * <pre style="border:solid thin; padding: 1ex;"
 * > Object obj = <code style="color:#00C">null</code>;
 * Object key = <code style="color:#C00">"Key"</code>;
 *
 * <code style="color:#00C">try</code> {
 *     obj = pool.borrowObject(key);
 *     <code style="color:#0C0">//...use the object...</code>
 * } <code style="color:#00C">catch</code> (Exception e) {
 *     <code style="color:#0C0">// invalidate the object</code>
 *     pool.invalidateObject(key, obj);
 *     <code style="color:#0C0">// do not return the object to the pool twice</code>
 *     obj = <code style="color:#00C">null</code>;
 * } <code style="color:#00C">finally</code> {
 *     <code style="color:#0C0">// make sure the object is returned to the pool</code>
 *     <code style="color:#00C">if</code> (<code style="color:#00C">null</code> != obj) {
 *         pool.returnObject(key, obj);
 *     }
 * }</pre>
 * <p>
 * {@link KeyedObjectPool} implementations <em>may</em> choose to store at most
 * one instance per key value, or may choose to maintain a pool of instances
 * for each key (essentially creating a {@link java.util.Map Map} of
 * {@link ObjectPool pools}).
 * </p>
 * <p>
 * See {@link org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool
 * GenericKeyedObjectPool} for an implementation.
 * </p>
 *
 * @param <K> The type of keys maintained by this pool.
 * @param <V> Type of element pooled in this pool.
 *
 *
 * @see KeyedPooledObjectFactory
 * @see ObjectPool
 * @see org.apache.tomcat.dbcp.pool2.impl.GenericKeyedObjectPool GenericKeyedObjectPool
 * @since 2.0
 */
public interface KeyedObjectPool<K, V> extends Closeable {

    /**
     * Creates an object using the {@link KeyedPooledObjectFactory factory} or
     * other implementation dependent mechanism, passivate it, and then place it
     * in the idle object pool. {@code addObject} is useful for
     * "pre-loading" a pool with idle objects (Optional operation).
     *
     * @param key the key a new instance should be added to
     * @throws Exception
     *              when {@link KeyedPooledObjectFactory#makeObject} fails.
     * @throws IllegalStateException
     *              after {@link #close} has been called on this pool.
     * @throws UnsupportedOperationException
     *              when this pool cannot add new idle objects.
     */
    void addObject(K key) throws Exception;

    /**
     * Calls {@link KeyedObjectPool#addObject(Object)} with each
     * key in {@code keys} for {@code count} number of times. This has
     * the same effect as calling {@link #addObjects(Object, int)}
     * for each key in the {@code keys} collection.
     *
     * @param keys
     *            {@link Collection} of keys to add objects for.
     * @param count
     *            the number of idle objects to add for each {@code key}.
     * @throws Exception
     *             when {@link KeyedObjectPool#addObject(Object)} fails.
     * @throws IllegalArgumentException
     *             when {@code keyedPool}, {@code keys}, or any value
     *             in {@code keys} is {@code null}.
     * @see #addObjects(Object, int)
     */
    default void addObjects(final Collection<K> keys, final int count) throws Exception {
        if (keys == null) {
            throw new IllegalArgumentException(PoolUtils.MSG_NULL_KEYS);
        }
        for (final K key : keys) {
            addObjects(key, count);
        }
    }

    /**
     * Calls {@link KeyedObjectPool#addObject(Object)}
     * {@code key} {@code count} number of times.
     *
     * @param key
     *            the key to add objects for.
     * @param count
     *            the number of idle objects to add for {@code key}.
     * @throws Exception
     *             when {@link KeyedObjectPool#addObject(Object)} fails.
     * @throws IllegalArgumentException
     *             when {@code key} is {@code null}.
     * @since 2.8.0
     */
    default void addObjects(final K key, final int count) throws Exception {
        if (key == null) {
            throw new IllegalArgumentException(PoolUtils.MSG_NULL_KEY);
        }
        for (int i = 0; i < count; i++) {
            addObject(key);
        }
    }

    /**
     * Borrows an instance from this pool for the specified {@code key}.
     * <p>
     * Instances returned from this method will have been either newly created
     * with {@link KeyedPooledObjectFactory#makeObject makeObject} or will be
     * a previously idle object and have been activated with
     * {@link KeyedPooledObjectFactory#activateObject activateObject} and then
     * (optionally) validated with
     * {@link KeyedPooledObjectFactory#validateObject validateObject}.
     * </p>
     * <p>
     * By contract, clients <strong>must</strong> return the borrowed object
     * using {@link #returnObject returnObject},
     * {@link #invalidateObject invalidateObject}, or a related method as
     * defined in an implementation or sub-interface, using a {@code key}
     * that is {@link Object#equals equivalent} to the one used to borrow the
     * instance in the first place.
     * </p>
     * <p>
     * The behavior of this method when the pool has been exhausted is not
     * strictly specified (although it may be specified by implementations).
     * </p>
     *
     * @param key the key used to obtain the object
     * @return an instance from this pool.
     * @throws IllegalStateException
     *              after {@link #close close} has been called on this pool
     * @throws Exception
     *              when {@link KeyedPooledObjectFactory#makeObject
     *              makeObject} throws an exception
     * @throws NoSuchElementException
     *              when the pool is exhausted and cannot or will not return
     *              another instance
     */
    V borrowObject(K key) throws Exception;

    /**
     * Clears the pool, removing all pooled instances (optional operation).
     *
     * @throws UnsupportedOperationException when this implementation doesn't
     *                                       support the operation
     *
     * @throws Exception if the pool cannot be cleared
     */
    void clear() throws Exception;

    /**
     * Clears the specified pool, removing all pooled instances corresponding to
     * the given {@code key} (optional operation).
     *
     * @param key the key to clear
     * @throws UnsupportedOperationException when this implementation doesn't
     *                                       support the operation
     *
     * @throws Exception if the key cannot be cleared
     */
    void clear(K key) throws Exception;

    /**
     * Closes this pool, and free any resources associated with it.
     * <p>
     * Calling {@link #addObject addObject} or
     * {@link #borrowObject borrowObject} after invoking this method on a pool
     * will cause them to throw an {@link IllegalStateException}.
     * </p>
     * <p>
     * Implementations should silently fail if not all resources can be freed.
     * </p>
     */
    @Override
    void close();

    /**
     * Gets a copy of the pool key list.
     * <p>
     * Note: The default implementation returns an empty list.
     * Implementations should override this method.
     * </p>
     *
     * @return a copy of the pool key list.
     * @since 2.12.0
     */
    default List<K> getKeys() {
        return Collections.emptyList();
    }

    /**
     * Gets the total number of instances currently borrowed from this pool but
     * not yet returned. Returns a negative value if this information is not
     * available.
     * @return the total number of instances currently borrowed from this pool but
     * not yet returned.
     */
    int getNumActive();

    /**
     * Gets the number of instances currently borrowed from but not yet
     * returned to the pool corresponding to the given {@code key}.
     * Returns a negative value if this information is not available.
     *
     * @param key the key to query
     * @return the number of instances currently borrowed from but not yet
     * returned to the pool corresponding to the given {@code key}.
     */
    int getNumActive(K key);

    /**
     * Gets the total number of instances currently idle in this pool.
     * Returns a negative value if this information is not available.
     * @return the total number of instances currently idle in this pool.
     */
    int getNumIdle();

    /**
     * Gets the number of instances corresponding to the given
     * {@code key} currently idle in this pool. Returns a negative value if
     * this information is not available.
     *
     * @param key the key to query
     * @return the number of instances corresponding to the given
     * {@code key} currently idle in this pool.
     */
    int getNumIdle(K key);

    /**
     * Invalidates an object from the pool.
     * <p>
     * By contract, {@code obj} <strong>must</strong> have been obtained
     * using {@link #borrowObject borrowObject} or a related method as defined
     * in an implementation or sub-interface using a {@code key} that is
     * equivalent to the one used to borrow the {@code Object} in the first
     * place.
     * </p>
     * <p>
     * This method should be used when an object that has been borrowed is
     * determined (due to an exception or other problem) to be invalid.
     * </p>
     *
     * @param key the key used to obtain the object
     * @param obj a {@link #borrowObject borrowed} instance to be returned.
     * @throws Exception if the instance cannot be invalidated
     */
    void invalidateObject(K key, V obj) throws Exception;

    /**
     * Invalidates an object from the pool, using the provided
     * {@link DestroyMode}.
     * <p>
     * By contract, {@code obj} <strong>must</strong> have been obtained
     * using {@link #borrowObject borrowObject} or a related method as defined
     * in an implementation or sub-interface using a {@code key} that is
     * equivalent to the one used to borrow the {@code Object} in the first
     * place.
     * </p>
     * <p>
     * This method should be used when an object that has been borrowed is
     * determined (due to an exception or other problem) to be invalid.
     * </p>
     *
     * @param key the key used to obtain the object
     * @param obj a {@link #borrowObject borrowed} instance to be returned.
     * @param destroyMode destroy activation context provided to the factory
     * @throws Exception if the instance cannot be invalidated
     * @since 2.9.0
     */
    default void invalidateObject(final K key, final V obj, final DestroyMode destroyMode) throws Exception {
        invalidateObject(key, obj);
    }

    /**
     * Return an instance to the pool. By contract, {@code obj}
     * <strong>must</strong> have been obtained using
     * {@link #borrowObject borrowObject} or a related method as defined in an
     * implementation or sub-interface using a {@code key} that is
     * equivalent to the one used to borrow the instance in the first place.
     *
     * @param key the key used to obtain the object
     * @param obj a {@link #borrowObject borrowed} instance to be returned.
     * @throws IllegalStateException
     *              if an attempt is made to return an object to the pool that
     *              is in any state other than allocated (i.e. borrowed).
     *              Attempting to return an object more than once or attempting
     *              to return an object that was never borrowed from the pool
     *              will trigger this exception.
     *
     * @throws Exception if an instance cannot be returned to the pool
     */
    void returnObject(K key, V obj) throws Exception;
}