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: 791364 $ $Date: 2009-07-05 21:52:27 -0400 (Sun, 05 Jul 2009) $
28 * @since Pool 1.0
29 */
30 public abstract class BaseObjectPool implements ObjectPool {
31 /**
32 * Obtains an instance from the pool.
33 *
34 * @return an instance from the pool
35 * @throws Exception if an instance cannot be obtained from the pool
36 */
37 public abstract Object borrowObject() throws Exception;
38
39 /**
40 * Returns an instance to the pool.
41 *
42 * @param obj instance to return to the pool
43 */
44 public abstract void returnObject(Object obj) throws Exception;
45
46 /**
47 * Invalidates an object from the pool.
48 * By contract, <code>obj</code> <strong>must</strong> have been obtained
49 * using {@link #borrowObject borrowObject}
50 * or a related method as defined in an implementation
51 * or sub-interface.
52 * <p>
53 * This method should be used when an object that has been borrowed
54 * is determined (due to an exception or other problem) to be invalid.
55 * </p>
56 *
57 * @param obj a {@link #borrowObject borrowed} instance to be disposed.
58 * @throws Exception
59 */
60 public abstract void invalidateObject(Object obj) throws Exception;
61
62 /**
63 * Not supported in this base implementation.
64 * @return a negative value.
65 *
66 * @throws UnsupportedOperationException
67 */
68 public int getNumIdle() throws UnsupportedOperationException {
69 return -1;
70 }
71
72 /**
73 * Not supported in this base implementation.
74 * @return a negative value.
75 *
76 * @throws UnsupportedOperationException
77 */
78 public int getNumActive() throws UnsupportedOperationException {
79 return -1;
80 }
81
82 /**
83 * Not supported in this base implementation.
84 *
85 * @throws UnsupportedOperationException
86 */
87 public void clear() throws Exception, UnsupportedOperationException {
88 throw new UnsupportedOperationException();
89 }
90
91 /**
92 * Not supported in this base implementation.
93 * Always throws an {@link UnsupportedOperationException},
94 * subclasses should override this behavior.
95 *
96 * @throws UnsupportedOperationException
97 */
98 public void addObject() throws Exception, UnsupportedOperationException {
99 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 }