Source code

    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}.
    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     * Reinitialize an instance to be returned by the pool.
    040     * <p>
    041     * The default implementation is a no-op.
    042     * </p>
    043     *
    044     * @param key the key used when selecting the object
    045     * @param p a {@code PooledObject} wrapping the instance to be activated
    046     */
    047    @Override
    048    public void activateObject(final K key, final PooledObject<V> p)
    049        throws Exception {
    050        // The default implementation is a no-op.
    051    }
    052
    053    /**
    054     * Create an instance that can be served by the pool.
    055     *
    056     * @param key the key used when constructing the object
    057     * @return an instance that can be served by the pool
    058     *
    059     * @throws Exception if there is a problem creating a new instance,
    060     *    this will be propagated to the code requesting an object.
    061     */
    062    public abstract V create(K key)
    063        throws Exception;
    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    @Override
    081    public PooledObject<V> makeObject(final K key) throws Exception {
    082        return wrap(create(key));
    083    }
    084
    085    /**
    086     * Uninitialize an instance to be returned to the idle object pool.
    087     * <p>
    088     * The default implementation is a no-op.
    089     * </p>
    090     *
    091     * @param key the key used when selecting the object
    092     * @param p a {@code PooledObject} wrapping the instance to be passivated
    093     */
    094    @Override
    095    public void passivateObject(final K key, final PooledObject<V> p)
    096        throws Exception {
    097        // The default implementation is a no-op.
    098    }
    099
    100    /**
    101     * Ensures that the instance is safe to be returned by the pool.
    102     * <p>
    103     * The default implementation always returns {@code true}.
    104     * </p>
    105     *
    106     * @param key the key used when selecting the object
    107     * @param p a {@code PooledObject} wrapping the instance to be validated
    108     * @return always {@code true} in the default implementation
    109     */
    110    @Override
    111    public boolean validateObject(final K key, final PooledObject<V> p) {
    112        return true;
    113    }
    114
    115    /**
    116     * Wrap the provided instance with an implementation of
    117     * {@link PooledObject}.
    118     *
    119     * @param value the instance to wrap
    120     *
    121     * @return The provided instance, wrapped by a {@link PooledObject}
    122     */
    123    public abstract PooledObject<V> wrap(V value);
    124}