    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 simple base implementation of {@link ObjectPool}.
    021 * Optional operations are implemented to either do nothing, return a value
    022 * indicating it is unsupported or throw {@link UnsupportedOperationException}.
    023 * <p>
    024 * This class is intended to be thread-safe.
    025 *
    026 * @param <T> Type of element pooled in this pool.
    027 *
    028 * @version $Revision: 1333925 $
    029 *
    030 * @since 2.0
    031 */
    032public abstract class BaseObjectPool<T> implements ObjectPool<T> {
    033    /**
    034     * Obtains an instance from the pool.
    035     *
    036     * @return an instance from the pool
    037     *
    038     * @throws Exception if an instance cannot be obtained from the pool
    039     */
    040    @Override
    041    public abstract T borrowObject() throws Exception;
    042
    043    /**
    044     * Returns an instance to the pool.
    045     *
    046     * @param obj instance to return to the pool
    047     */
    048    @Override
    049    public abstract void returnObject(T obj) throws Exception;
    050
    051    /**
    052     * Invalidates an object from the pool.
    053     * <p>
    054     * By contract, <code>obj</code> <strong>must</strong> have been obtained
    055     * using {@link #borrowObject borrowObject}.
    056     * <p>
    057     * This method should be used when an object that has been borrowed is
    058     * determined (due to an exception or other problem) to be invalid.
    059     *
    060     * @param obj a {@link #borrowObject borrowed} instance to be disposed.
    061     * @throws Exception
    062     */
    063    @Override
    064    public abstract void invalidateObject(T obj) throws Exception;
    065
    066    /**
    067     * Not supported in this base implementation.
    068     *
    069     * @return a negative value.
    070     */
    071    @Override
    072    public int getNumIdle() {
    073        return -1;
    074    }
    075
    076    /**
    077     * Not supported in this base implementation.
    078     *
    079     * @return a negative value.
    080     */
    081    @Override
    082    public int getNumActive() {
    083        return -1;
    084    }
    085
    086    /**
    087     * Not supported in this base implementation.
    088     *
    089     * @throws UnsupportedOperationException
    090     */
    091    @Override
    092    public void clear() throws Exception, UnsupportedOperationException {
    093        throw new UnsupportedOperationException();
    094    }
    095
    096    /**
    097     * Not supported in this base implementation.Always throws an
    098     * {@link UnsupportedOperationException}, subclasses should override this
    099     * behavior.
    100     *
    101     * @throws UnsupportedOperationException
    102     */
    103    @Override
    104    public void addObject() throws Exception, UnsupportedOperationException {
    105        throw new UnsupportedOperationException();
    106    }
    107
    108    /**
    109     * Close this pool. This affects the behavior of <code>isClosed</code> and
    110     * <code>assertOpen</code>.
    111     */
    112    @Override
    113    public void close() {
    114        closed = true;
    115    }
    116
    117    /**
    118     * Has this pool instance been closed.
    119     *
    120     * @return <code>true</code> when this pool has been closed.
    121     */
    122    public final boolean isClosed() {
    123        return closed;
    124    }
    125
    126    /**
    127     * Throws an <code>IllegalStateException</code> when this pool has been
    128     * closed.
    129     *
    130     * @throws IllegalStateException when this pool has been closed.
    131     *
    132     * @see #isClosed()
    133     */
    134    protected final void assertOpen() throws IllegalStateException {
    135        if (isClosed()) {
    136            throw new IllegalStateException("Pool not open");
    137        }
    138    }
    139
    140    private volatile boolean closed = false;
    141}