View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.pool;
19  
20  /***
21   * A simple base implementation of {@link ObjectPool}.
22   * Optional operations are implemented to either do nothing, return a value
23   * indicating it is unsupported or throw {@link UnsupportedOperationException}.
24   *
25   * @author Rodney Waldhoff
26   * @author Sandy McArthur
27   * @version $Revision: 480413 $ $Date: 2006-11-28 22:16:05 -0700 (Tue, 28 Nov 2006) $
28   * @since Pool 1.0
29   */
30  public abstract class BaseObjectPool implements ObjectPool {
31      public abstract Object borrowObject() throws Exception;
32      public abstract void returnObject(Object obj) throws Exception;
33      public abstract void invalidateObject(Object obj) throws Exception;
34  
35      /***
36       * Not supported in this base implementation.
37       * @return a negative value.
38       */
39      public int getNumIdle() throws UnsupportedOperationException {
40          return -1;
41      }
42  
43      /***
44       * Not supported in this base implementation.
45       * @return a negative value.
46       */
47      public int getNumActive() throws UnsupportedOperationException {
48          return -1;
49      }
50  
51      /***
52       * Not supported in this base implementation.
53       */
54      public void clear() throws Exception, UnsupportedOperationException {
55          throw new UnsupportedOperationException();
56      }
57  
58      /***
59       * Not supported in this base implementation.
60       * Always throws an {@link UnsupportedOperationException},
61       * subclasses should override this behavior.
62       */
63      public void addObject() throws Exception, UnsupportedOperationException {
64          throw new UnsupportedOperationException();
65      }
66  
67      /***
68       * Close this pool.
69       * This affects the behavior of <code>isClosed</code> and <code>assertOpen</code>.
70       */
71      public void close() throws Exception {
72          closed = true;
73      }
74  
75      /***
76       * Not supported in this base implementation.
77       * Always throws an {@link UnsupportedOperationException},
78       * subclasses should override this behavior.
79       */
80      public void setFactory(PoolableObjectFactory factory) throws IllegalStateException, UnsupportedOperationException {
81          throw new UnsupportedOperationException();
82      }
83  
84      /***
85       * Has this pool instance been closed.
86       * @return <code>true</code> when this pool has been closed.
87       */
88      protected final boolean isClosed() {
89          return closed;
90      }
91  
92      /***
93       * Throws an <code>IllegalStateException</code> when this pool has been closed.
94       * @throws IllegalStateException when this pool has been closed.
95       * @see #isClosed()
96       */
97      protected final void assertOpen() throws IllegalStateException {
98          if(isClosed()) {
99              throw new IllegalStateException("Pool not open");
100         }
101     }
102     
103     private volatile boolean closed = false;
104 }