1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }