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
019/**
020 * An interface defining life-cycle methods for
021 * instances to be served by a {@link KeyedObjectPool}.
022 * <p>
023 * By contract, when an {@link KeyedObjectPool}
024 * delegates to a {@link KeyedPooledObjectFactory},
025 * <ol>
026 *  <li>
027 *   {@link #makeObject} is called whenever a new instance is needed.
028 *  </li>
029 *  <li>
030 *   {@link #activateObject} is invoked on every instance that has been
031 *   {@link #passivateObject passivated} before it is
032 *   {@link KeyedObjectPool#borrowObject borrowed} from the pool.
033 *  </li>
034 *  <li>
035 *   {@link #validateObject} may be invoked on {@link #activateObject activated}
036 *   instances to make sure they can be
037 *   {@link KeyedObjectPool#borrowObject borrowed} from the pool.
038 *   <code>validateObject</code> may also be used to test an
039 *   instance being {@link KeyedObjectPool#returnObject returned} to the pool
040 *   before it is {@link #passivateObject passivated}. It will only be invoked
041 *   on an activated instance.
042 *  </li>
043 *  <li>
044 *   {@link #passivateObject passivateObject}
045 *   is invoked on every instance when it is returned to the pool.
046 *  </li>
047 *  <li>
048 *   {@link #destroyObject destroyObject}
049 *   is invoked on every instance when it is being "dropped" from the
050 *   pool (whether due to the response from <code>validateObject</code>,
051 *   or for reasons specific to the pool implementation.) There is no
052 *   guarantee that the instance being destroyed will
053 *   be considered active, passive or in a generally consistent state.
054 *  </li>
055 * </ol>
056 * {@link KeyedPooledObjectFactory} must be thread-safe. The only promise
057 * an {@link KeyedObjectPool} makes is that the same instance of an object will
058 * not be passed to more than one method of a
059 * <code>KeyedPoolableObjectFactory</code> at a time.
060 * <p>
061 * While clients of a {@link KeyedObjectPool} borrow and return instances of
062 * the underlying value type V, the factory methods act on instances of
063 * {@link PooledObject PooledObject&lt;V&gt;}.  These are the object wrappers that
064 * pools use to track and maintain state informations about the objects that
065 * they manage.
066 *
067 * @see KeyedObjectPool
068 * @see BaseKeyedPooledObjectFactory
069 *
070 * @param <K> The type of keys managed by this factory.
071 * @param <V> Type of element managed by this factory.
072 *
073 * @version $Revision: 1333925 $
074 *
075 * @since 2.0
076 */
077public interface KeyedPooledObjectFactory<K,V> {
078    /**
079     * Create an instance that can be served by the pool and
080     * wrap it in a {@link PooledObject} to be managed by the pool.
081     *
082     * @param key the key used when constructing the object
083     *
084     * @return a {@code PooledObject} wrapping an instance that can
085     * be served by the pool.
086     *
087     * @throws Exception if there is a problem creating a new instance,
088     *    this will be propagated to the code requesting an object.
089     */
090    PooledObject<V> makeObject(K key) throws Exception;
091
092    /**
093     * Destroy an instance no longer needed by the pool.
094     * <p>
095     * It is important for implementations of this method to be aware that there
096     * is no guarantee about what state <code>obj</code> will be in and the
097     * implementation should be prepared to handle unexpected errors.
098     * <p>
099     * Also, an implementation must take in to consideration that instances lost
100     * to the garbage collector may never be destroyed.
101     *
102     * @param key the key used when selecting the instance
103     * @param p a {@code PooledObject} wrapping the instance to be destroyed
104     *
105     * @throws Exception should be avoided as it may be swallowed by
106     *    the pool implementation.
107     *
108     * @see #validateObject
109     * @see KeyedObjectPool#invalidateObject
110     */
111    void destroyObject(K key, PooledObject<V> p) throws Exception;
112
113    /**
114     * Ensures that the instance is safe to be returned by the pool.
115     *
116     * @param key the key used when selecting the object
117     * @param p a {@code PooledObject} wrapping the instance to be validated
118     *
119     * @return <code>false</code> if <code>obj</code> is not valid and should
120     *         be dropped from the pool, <code>true</code> otherwise.
121     */
122    boolean validateObject(K key, PooledObject<V> p);
123
124    /**
125     * Reinitialize an instance to be returned by the pool.
126     *
127     * @param key the key used when selecting the object
128     * @param p a {@code PooledObject} wrapping the instance to be activated
129     *
130     * @throws Exception if there is a problem activating <code>obj</code>,
131     *    this exception may be swallowed by the pool.
132     *
133     * @see #destroyObject
134     */
135    void activateObject(K key, PooledObject<V> p) throws Exception;
136
137    /**
138     * Uninitialize an instance to be returned to the idle object pool.
139     *
140     * @param key the key used when selecting the object
141     * @param p a {@code PooledObject} wrapping the instance to be passivated
142     *
143     * @throws Exception if there is a problem passivating <code>obj</code>,
144     *    this exception may be swallowed by the pool.
145     *
146     * @see #destroyObject
147     */
148    void passivateObject(K key, PooledObject<V> p) throws Exception;
149}
150