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 * A base implementation of <code>KeyedPooledObjectFactory</code>.
021 * <p>
022 * All operations defined here are essentially no-op's.
023 * </p>
024 * This class is immutable, and therefore thread-safe.
025 *
026 * @see KeyedPooledObjectFactory
027 *
028 * @param <K> The type of keys managed by this factory.
029 * @param <V> Type of element managed by this factory.
030 *
031 * @version $Revision: 1333925 $
032 *
033 * @since 2.0
034 */
035public abstract class BaseKeyedPooledObjectFactory<K,V>
036        implements KeyedPooledObjectFactory<K,V> {
037
038    /**
039     * Create an instance that can be served by the pool.
040     *
041     * @param key the key used when constructing the object
042     * @return an instance that can be served by the pool
043     *
044     * @throws Exception if there is a problem creating a new instance,
045     *    this will be propagated to the code requesting an object.
046     */
047    public abstract V create(K key)
048        throws Exception;
049
050    /**
051     * Wrap the provided instance with an implementation of
052     * {@link PooledObject}.
053     *
054     * @param value the instance to wrap
055     *
056     * @return The provided instance, wrapped by a {@link PooledObject}
057     */
058    public abstract PooledObject<V> wrap(V value);
059
060    @Override
061    public PooledObject<V> makeObject(K key) throws Exception {
062        return wrap(create(key));
063    }
064
065    /**
066     * Destroy an instance no longer needed by the pool.
067     * <p>
068     * The default implementation is a no-op.
069     *
070     * @param key the key used when selecting the instance
071     * @param p a {@code PooledObject} wrapping the the instance to be destroyed
072     */
073    @Override
074    public void destroyObject(K key, PooledObject<V> p)
075        throws Exception {
076    }
077
078    /**
079     * Ensures that the instance is safe to be returned by the pool.
080     * <p>
081     * The default implementation always returns <tt>true</tt>.
082     *
083     * @param key the key used when selecting the object
084     * @param p a {@code PooledObject} wrapping the the instance to be validated
085     * @return always <code>true</code> in the default implementation
086     */
087    @Override
088    public boolean validateObject(K key, PooledObject<V> p) {
089        return true;
090    }
091
092    /**
093     * Reinitialize an instance to be returned by the pool.
094     * <p>
095     * The default implementation is a no-op.
096     *
097     * @param key the key used when selecting the object
098     * @param p a {@code PooledObject} wrapping the the instance to be activated
099     */
100    @Override
101    public void activateObject(K key, PooledObject<V> p)
102        throws Exception {
103    }
104
105    /**
106     * Uninitialize an instance to be returned to the idle object pool.
107     * <p>
108     * The default implementation is a no-op.
109     *
110     * @param key the key used when selecting the object
111     * @param p a {@code PooledObject} wrapping the the instance to be passivated
112     */
113    @Override
114    public void passivateObject(K key, PooledObject<V> p)
115        throws Exception {
116    }
117}