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 */
017package org.apache.commons.pool2;
018
019import java.io.Closeable;
020import java.util.NoSuchElementException;
021
022/**
023 * A pooling simple interface.
024 * <p>
025 * Example of use:
026 * </p>
027 * <pre style="border:solid thin; padding: 1ex;"
028 * > Object obj = <code style="color:#00C">null</code>;
029 *
030 * <code style="color:#00C">try</code> {
031 *     obj = pool.borrowObject();
032 *     <code style="color:#00C">try</code> {
033 *         <code style="color:#0C0">//...use the object...</code>
034 *     } <code style="color:#00C">catch</code>(Exception e) {
035 *         <code style="color:#0C0">// invalidate the object</code>
036 *         pool.invalidateObject(obj);
037 *         <code style="color:#0C0">// do not return the object to the pool twice</code>
038 *         obj = <code style="color:#00C">null</code>;
039 *     } <code style="color:#00C">finally</code> {
040 *         <code style="color:#0C0">// make sure the object is returned to the pool</code>
041 *         <code style="color:#00C">if</code>(<code style="color:#00C">null</code> != obj) {
042 *             pool.returnObject(obj);
043 *        }
044 *     }
045 * } <code style="color:#00C">catch</code>(Exception e) {
046 *       <code style="color:#0C0">// failed to borrow an object</code>
047 * }</pre>
048 * <p>
049 * See {@link BaseObjectPool} for a simple base implementation.
050 * </p>
051 *
052 * @param <T> Type of element pooled in this pool.
053 *
054 * @see PooledObjectFactory
055 * @see KeyedObjectPool
056 * @see BaseObjectPool
057 *
058 * @since 2.0
059 */
060public interface ObjectPool<T> extends Closeable {
061
062    /**
063     * Obtains an instance from this pool.
064     * <p>
065     * Instances returned from this method will have been either newly created
066     * with {@link PooledObjectFactory#makeObject} or will be a previously
067     * idle object and have been activated with
068     * {@link PooledObjectFactory#activateObject} and then validated with
069     * {@link PooledObjectFactory#validateObject}.
070     * </p>
071     * <p>
072     * By contract, clients <strong>must</strong> return the borrowed instance
073     * using {@link #returnObject}, {@link #invalidateObject}, or a related
074     * method as defined in an implementation or sub-interface.
075     * </p>
076     * <p>
077     * The behaviour of this method when the pool has been exhausted
078     * is not strictly specified (although it may be specified by
079     * implementations).
080     * </p>
081     *
082     * @return an instance from this pool.
083     *
084     * @throws IllegalStateException
085     *              after {@link #close close} has been called on this pool.
086     * @throws Exception
087     *              when {@link PooledObjectFactory#makeObject} throws an
088     *              exception.
089     * @throws NoSuchElementException
090     *              when the pool is exhausted and cannot or will not return
091     *              another instance.
092     */
093    T borrowObject() throws Exception, NoSuchElementException,
094            IllegalStateException;
095
096    /**
097     * Returns an instance to the pool. By contract, <code>obj</code>
098     * <strong>must</strong> have been obtained using {@link #borrowObject()} or
099     * a related method as defined in an implementation or sub-interface.
100     *
101     * @param obj a {@link #borrowObject borrowed} instance to be returned.
102     *
103     * @throws IllegalStateException
104     *              if an attempt is made to return an object to the pool that
105     *              is in any state other than allocated (i.e. borrowed).
106     *              Attempting to return an object more than once or attempting
107     *              to return an object that was never borrowed from the pool
108     *              will trigger this exception.
109     *
110     * @throws Exception if an instance cannot be returned to the pool
111     */
112    void returnObject(T obj) throws Exception;
113
114    /**
115     * Invalidates an object from the pool.
116     * <p>
117     * By contract, <code>obj</code> <strong>must</strong> have been obtained
118     * using {@link #borrowObject} or a related method as defined in an
119     * implementation or sub-interface.
120     * </p>
121     * <p>
122     * This method should be used when an object that has been borrowed is
123     * determined (due to an exception or other problem) to be invalid.
124     * </p>
125     *
126     * @param obj a {@link #borrowObject borrowed} instance to be disposed.
127     *
128     * @throws Exception if the instance cannot be invalidated
129     */
130    void invalidateObject(T obj) throws Exception;
131
132    /**
133     * Creates an object using the {@link PooledObjectFactory factory} or other
134     * implementation dependent mechanism, passivate it, and then place it in
135     * the idle object pool. <code>addObject</code> is useful for "pre-loading"
136     * a pool with idle objects. (Optional operation).
137     *
138     * @throws Exception
139     *              when {@link PooledObjectFactory#makeObject} fails.
140     * @throws IllegalStateException
141     *              after {@link #close} has been called on this pool.
142     * @throws UnsupportedOperationException
143     *              when this pool cannot add new idle objects.
144     */
145    void addObject() throws Exception, IllegalStateException,
146            UnsupportedOperationException;
147
148    /**
149     * Returns the number of instances currently idle in this pool. This may be
150     * considered an approximation of the number of objects that can be
151     * {@link #borrowObject borrowed} without creating any new instances.
152     * Returns a negative value if this information is not available.
153     * @return the number of instances currently idle in this pool.
154     */
155    int getNumIdle();
156
157    /**
158     * Returns the number of instances currently borrowed from this pool. Returns
159     * a negative value if this information is not available.
160     * @return the number of instances currently borrowed from this pool.
161     */
162    int getNumActive();
163
164    /**
165     * Clears any objects sitting idle in the pool, releasing any associated
166     * resources (optional operation). Idle objects cleared must be
167     * {@link PooledObjectFactory#destroyObject(PooledObject)}.
168     *
169     * @throws UnsupportedOperationException
170     *              if this implementation does not support the operation
171     *
172     * @throws Exception if the pool cannot be cleared
173     */
174    void clear() throws Exception, UnsupportedOperationException;
175
176    /**
177     * Closes this pool, and free any resources associated with it.
178     * <p>
179     * Calling {@link #addObject} or {@link #borrowObject} after invoking this
180     * method on a pool will cause them to throw an {@link IllegalStateException}.
181     * </p>
182     * <p>
183     * Implementations should silently fail if not all resources can be freed.
184     * </p>
185     */
186    @Override
187    void close();
188}