    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 * <p>
    025 * This class is immutable, and therefore thread-safe.
    026 * </p>
    027 *
    028 * @see KeyedPooledObjectFactory
    029 *
    030 * @param <K> The type of keys managed by this factory.
    031 * @param <V> Type of element managed by this factory.
    032 *
    033 * @since 2.0
    034 */
    035public abstract class BaseKeyedPooledObjectFactory<K, V> extends BaseObject
    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(final 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     * </p>
    070     *
    071     * @param key the key used when selecting the instance
    072     * @param p a {@code PooledObject} wrapping the instance to be destroyed
    073     */
    074    @Override
    075    public void destroyObject(final K key, final PooledObject<V> p)
    076        throws Exception {
    077        // The default implementation is a no-op.
    078    }
    079
    080    /**
    081     * Ensures that the instance is safe to be returned by the pool.
    082     * <p>
    083     * The default implementation always returns {@code true}.
    084     * </p>
    085     *
    086     * @param key the key used when selecting the object
    087     * @param p a {@code PooledObject} wrapping the instance to be validated
    088     * @return always <code>true</code> in the default implementation
    089     */
    090    @Override
    091    public boolean validateObject(final K key, final PooledObject<V> p) {
    092        return true;
    093    }
    094
    095    /**
    096     * Reinitialize an instance to be returned by the pool.
    097     * <p>
    098     * The default implementation is a no-op.
    099     * </p>
    100     *
    101     * @param key the key used when selecting the object
    102     * @param p a {@code PooledObject} wrapping the instance to be activated
    103     */
    104    @Override
    105    public void activateObject(final K key, final PooledObject<V> p)
    106        throws Exception {
    107        // The default implementation is a no-op.
    108    }
    109
    110    /**
    111     * Uninitialize an instance to be returned to the idle object pool.
    112     * <p>
    113     * The default implementation is a no-op.
    114     * </p>
    115     *
    116     * @param key the key used when selecting the object
    117     * @param p a {@code PooledObject} wrapping the instance to be passivated
    118     */
    119    @Override
    120    public void passivateObject(final K key, final PooledObject<V> p)
    121        throws Exception {
    122        // The default implementation is a no-op.
    123    }
    124}