View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.pool;
18  
19  /***
20   * A simple base impementation of {@link ObjectPool}.
21   * All optional operations are implemented as throwing
22   * {@link UnsupportedOperationException}.
23   *
24   * @author Rodney Waldhoff
25   * @version $Revision: 383290 $ $Date: 2006-03-05 02:00:15 -0500 (Sun, 05 Mar 2006) $
26   */
27  public abstract class BaseObjectPool implements ObjectPool {
28      public abstract Object borrowObject() throws Exception;
29      public abstract void returnObject(Object obj) throws Exception;
30      public abstract void invalidateObject(Object obj) throws Exception;
31  
32      /***
33       * Not supported in this base implementation.
34       */
35      public int getNumIdle() throws UnsupportedOperationException {
36          throw new UnsupportedOperationException();
37      }
38  
39      /***
40       * Not supported in this base implementation.
41       */
42      public int getNumActive() throws UnsupportedOperationException {
43          throw new UnsupportedOperationException();
44      }
45  
46      /***
47       * Not supported in this base implementation.
48       */
49      public void clear() throws Exception, UnsupportedOperationException {
50          throw new UnsupportedOperationException();
51      }
52  
53      /***
54       * Not supported in this base implementation.
55       */
56      public void addObject() throws Exception, UnsupportedOperationException {
57          throw new UnsupportedOperationException();
58      }
59  
60      public void close() throws Exception {
61          assertOpen();
62          closed = true;
63      }
64  
65      /***
66       * Not supported in this base implementation.
67       */
68      public void setFactory(PoolableObjectFactory factory) throws IllegalStateException, UnsupportedOperationException {
69          throw new UnsupportedOperationException();
70      }
71      
72      protected final boolean isClosed() {
73          return closed;
74      }
75      
76      protected final void assertOpen() throws IllegalStateException {
77          if(isClosed()) {
78              throw new IllegalStateException("Pool not open");
79          }
80      }
81      
82      private volatile boolean closed = false;
83  }