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 <code>KeyedObjectPool</code>.
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: 777748 $ $Date: 2009-05-22 20:00:44 -0400 (Fri, 22 May 2009) $
28 * @since Pool 1.0
29 */
30 public abstract class BaseKeyedObjectPool implements KeyedObjectPool {
31 public abstract Object borrowObject(Object key) throws Exception;
32 public abstract void returnObject(Object key, Object obj) throws Exception;
33 public abstract void invalidateObject(Object key, Object obj) throws Exception;
34
35 /**
36 * Not supported in this base implementation.
37 * Always throws an {@link UnsupportedOperationException},
38 * subclasses should override this behavior.
39 */
40 public void addObject(Object key) throws Exception, UnsupportedOperationException {
41 throw new UnsupportedOperationException();
42 }
43
44 /**
45 * Not supported in this base implementation.
46 * @return a negative value.
47 */
48 public int getNumIdle(Object key) throws UnsupportedOperationException {
49 return -1;
50 }
51
52 /**
53 * Not supported in this base implementation.
54 * @return a negative value.
55 */
56 public int getNumActive(Object key) throws UnsupportedOperationException {
57 return -1;
58 }
59
60 /**
61 * Not supported in this base implementation.
62 * @return a negative value.
63 */
64 public int getNumIdle() throws UnsupportedOperationException {
65 return -1;
66 }
67
68 /**
69 * Not supported in this base implementation.
70 * @return a negative value.
71 */
72 public int getNumActive() throws UnsupportedOperationException {
73 return -1;
74 }
75
76 /**
77 * Not supported in this base implementation.
78 */
79 public void clear() throws Exception, UnsupportedOperationException {
80 throw new UnsupportedOperationException();
81 }
82
83 /**
84 * Not supported in this base implementation.
85 */
86 public void clear(Object key) throws Exception, UnsupportedOperationException {
87 throw new UnsupportedOperationException();
88 }
89
90 /**
91 * Close this pool.
92 * This affects the behavior of <code>isClosed</code> and <code>assertOpen</code>.
93 */
94 public void close() throws Exception {
95 closed = true;
96 }
97
98 /**
99 * Not supported in this base implementation.
100 * Always throws an {@link UnsupportedOperationException},
101 * subclasses should override this behavior.
102 */
103 public void setFactory(KeyedPoolableObjectFactory factory) throws IllegalStateException, UnsupportedOperationException {
104 throw new UnsupportedOperationException();
105 }
106
107 /**
108 * Has this pool instance been closed.
109 * @return <code>true</code> when this pool has been closed.
110 * @since Pool 1.4
111 */
112 protected final boolean isClosed() {
113 return closed;
114 }
115
116 /**
117 * Throws an <code>IllegalStateException</code> when this pool has been closed.
118 * @throws IllegalStateException when this pool has been closed.
119 * @see #isClosed()
120 * @since Pool 1.4
121 */
122 protected final void assertOpen() throws IllegalStateException {
123 if(isClosed()) {
124 throw new IllegalStateException("Pool not open");
125 }
126 }
127
128 private volatile boolean closed = false;
129 }