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