001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.pool;
019    
020    import java.util.NoSuchElementException;
021    
022    /**
023     * A "keyed" pooling interface.
024     * <p>
025     * A keyed pool pools instances of multiple types. Each
026     * type may be accessed using an arbitrary key.
027     * </p>
028     * <p>
029     * Example of use:
030     * <pre style="border:solid thin; padding: 1ex;"
031     * > Object obj = <code style="color:#00C">null</code>;
032     * Object key = <code style="color:#C00">"Key"</code>;
033     *
034     * <code style="color:#00C">try</code> {
035     *     obj = pool.borrowObject(key);
036     *     <code style="color:#0C0">//...use the object...</code>
037     * } <code style="color:#00C">catch</code>(Exception e) {
038     *     <code style="color:#0C0">// invalidate the object</code>
039     *     pool.invalidateObject(key, obj);
040     *     <code style="color:#0C0">// do not return the object to the pool twice</code>
041     *     obj = <code style="color:#00C">null</code>;
042     * } <code style="color:#00C">finally</code> {
043     *     <code style="color:#0C0">// make sure the object is returned to the pool</code>
044     *     <code style="color:#00C">if</code>(<code style="color:#00C">null</code> != obj) {
045     *         pool.returnObject(key, obj);
046     *     }
047     * }</pre>
048     * </p>
049     * <p>
050     * {@link KeyedObjectPool} implementations <i>may</i> choose to store at most
051     * one instance per key value, or may choose to maintain a pool of instances
052     * for each key (essentially creating a {@link java.util.Map Map} of
053     * {@link ObjectPool pools}).
054     * </p>
055     *
056     * <p>See {@link BaseKeyedObjectPool} for a simple base implementation.</p>
057     *
058     * @author Rodney Waldhoff
059     * @author Sandy McArthur
060     * @version $Revision: 962893 $ $Date: 2010-07-10 10:37:27 -0700 (Sat, 10 Jul 2010) $
061     * @see KeyedPoolableObjectFactory
062     * @see KeyedObjectPoolFactory
063     * @see ObjectPool
064     * @see BaseKeyedObjectPool
065     * @since Pool 1.0
066     */
067    public interface KeyedObjectPool {
068        /**
069         * Obtains an instance from this pool for the specified <code>key</code>.
070         * <p>
071         * Instances returned from this method will have been either newly created with
072         * {@link KeyedPoolableObjectFactory#makeObject makeObject} or will be a previously idle object and
073         * have been activated with {@link KeyedPoolableObjectFactory#activateObject activateObject} and
074         * then validated with {@link KeyedPoolableObjectFactory#validateObject validateObject}.
075         * <p>
076         * By contract, clients <strong>must</strong> return the borrowed object using
077         * {@link #returnObject returnObject}, {@link #invalidateObject invalidateObject}, or a related method
078         * as defined in an implementation or sub-interface,
079         * using a <code>key</code> that is {@link Object#equals equivalent} to the one used to
080         * borrow the instance in the first place.
081         * <p>
082         * The behaviour of this method when the pool has been exhausted
083         * is not strictly specified (although it may be specified by implementations).
084         * Older versions of this method would return <code>null</code> to indicate exhaustion,
085         * newer versions are encouraged to throw a {@link NoSuchElementException}.
086         *
087         * @param key the key used to obtain the object
088         * @return an instance from this pool.
089         * @throws IllegalStateException after {@link #close close} has been called on this pool
090         * @throws Exception when {@link KeyedPoolableObjectFactory#makeObject makeObject} throws an exception
091         * @throws NoSuchElementException when the pool is exhausted and cannot or will not return another instance
092         */
093        Object borrowObject(Object key) throws Exception, NoSuchElementException, IllegalStateException;
094    
095        /**
096         * Return an instance to the pool.
097         * By contract, <code>obj</code> <strong>must</strong> have been obtained
098         * using {@link #borrowObject borrowObject}
099         * or a related method as defined in an implementation
100         * or sub-interface
101         * using a <code>key</code> that is equivalent to the one used to
102         * borrow the instance in the first place.
103         *
104         * @param key the key used to obtain the object
105         * @param obj a {@link #borrowObject borrowed} instance to be returned.
106         * @throws Exception 
107         */
108        void returnObject(Object key, Object obj) throws Exception;
109    
110        /**
111         * <p>Invalidates an object from the pool.</p>
112         * 
113         * <p>By contract, <code>obj</code> <strong>must</strong> have been obtained
114         * using {@link #borrowObject borrowObject} or a related method as defined
115         * in an implementation or sub-interface using a <code>key</code> that is
116         * equivalent to the one used to borrow the <code>Object</code> in the first place.</p>
117         *
118         * <p>This method should be used when an object that has been borrowed
119         * is determined (due to an exception or other problem) to be invalid.</p>
120         *
121         * @param key the key used to obtain the object
122         * @param obj a {@link #borrowObject borrowed} instance to be returned.
123         * @throws Exception 
124         */
125        void invalidateObject(Object key, Object obj) throws Exception;
126    
127        /**
128         * Create an object using the {@link KeyedPoolableObjectFactory factory} or other
129         * implementation dependent mechanism, passivate it, and then place it in the idle object pool.
130         * <code>addObject</code> is useful for "pre-loading" a pool with idle objects
131         * (Optional operation).
132         *
133         * @param key the key a new instance should be added to
134         * @throws Exception when {@link KeyedPoolableObjectFactory#makeObject} fails.
135         * @throws IllegalStateException after {@link #close} has been called on this pool.
136         * @throws UnsupportedOperationException when this pool cannot add new idle objects.
137         */
138        void addObject(Object key) throws Exception, IllegalStateException, UnsupportedOperationException;
139    
140        /**
141         * Returns the number of instances
142         * corresponding to the given <code>key</code>
143         * currently idle in this pool (optional operation).
144         * Returns a negative value if this information is not available.
145         *
146         * @param key the key to query
147         * @return the number of instances corresponding to the given <code>key</code> currently idle in this pool or a negative value if unsupported
148         * @throws UnsupportedOperationException <strong>deprecated</strong>: when this implementation doesn't support the operation
149         */
150        int getNumIdle(Object key) throws UnsupportedOperationException;
151    
152        /**
153         * Returns the number of instances
154         * currently borrowed from but not yet returned
155         * to the pool corresponding to the
156         * given <code>key</code> (optional operation).
157         * Returns a negative value if this information is not available.
158         *
159         * @param key the key to query
160         * @return the number of instances corresponding to the given <code>key</code> currently borrowed in this pool or a negative value if unsupported
161         * @throws UnsupportedOperationException <strong>deprecated</strong>: when this implementation doesn't support the operation
162         */
163        int getNumActive(Object key) throws UnsupportedOperationException;
164    
165        /**
166         * Returns the total number of instances
167         * currently idle in this pool (optional operation).
168         * Returns a negative value if this information is not available.
169         *
170         * @return the total number of instances currently idle in this pool or a negative value if unsupported
171         * @throws UnsupportedOperationException <strong>deprecated</strong>: when this implementation doesn't support the operation
172         */
173        int getNumIdle() throws UnsupportedOperationException;
174    
175        /**
176         * Returns the total number of instances
177         * current borrowed from this pool but not
178         * yet returned (optional operation).
179         * Returns a negative value if this information is not available.
180         *
181         * @return the total number of instances currently borrowed from this pool or a negative value if unsupported
182         * @throws UnsupportedOperationException <strong>deprecated</strong>: when this implementation doesn't support the operation
183         */
184        int getNumActive() throws UnsupportedOperationException;
185    
186        /**
187         * Clears the pool, removing all pooled instances (optional operation).
188         * Throws {@link UnsupportedOperationException} if the pool cannot be cleared.
189         *
190         * @throws UnsupportedOperationException when this implementation doesn't support the operation
191         */
192        void clear() throws Exception, UnsupportedOperationException;
193    
194        /**
195         * Clears the specified pool, removing all
196         * pooled instances corresponding to
197         * the given <code>key</code> (optional operation).
198         * Throws {@link UnsupportedOperationException} if the pool cannot be cleared.
199         *
200         * @param key the key to clear
201         * @throws UnsupportedOperationException when this implementation doesn't support the operation
202         */
203        void clear(Object key) throws Exception, UnsupportedOperationException;
204    
205        /**
206         * Close this pool, and free any resources associated with it.
207         * <p>
208         * Calling {@link #addObject addObject} or {@link #borrowObject borrowObject} after invoking
209         * this method on a pool will cause them to throw an {@link IllegalStateException}.
210         * </p>
211         *
212         * @throws Exception
213         */
214        void close() throws Exception;
215    
216        /**
217         * Sets the {@link KeyedPoolableObjectFactory factory} the pool uses
218         * to create new instances (optional operation).
219         * Trying to change the <code>factory</code> after a pool has been used will frequently
220         * throw an {@link UnsupportedOperationException}. It is up to the pool
221         * implementation to determine when it is acceptable to call this method.
222         *
223         * @param factory the {@link KeyedPoolableObjectFactory} used to create new instances.
224         * @throws IllegalStateException when the factory cannot be set at this time
225         * @throws UnsupportedOperationException when this implementation doesn't support the operation
226         * @deprecated to be removed in pool 2.0
227         */
228        void setFactory(KeyedPoolableObjectFactory factory) throws IllegalStateException, UnsupportedOperationException;
229    }