001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.pool; 019 020 /** 021 * A simple base implementation of <code>KeyedObjectPool</code>. 022 * Optional operations are implemented to either do nothing, return a value 023 * indicating it is unsupported or throw {@link UnsupportedOperationException}. 024 * 025 * @author Rodney Waldhoff 026 * @author Sandy McArthur 027 * @version $Revision: 777748 $ $Date: 2009-05-22 20:00:44 -0400 (Fri, 22 May 2009) $ 028 * @since Pool 1.0 029 */ 030 public abstract class BaseKeyedObjectPool implements KeyedObjectPool { 031 public abstract Object borrowObject(Object key) throws Exception; 032 public abstract void returnObject(Object key, Object obj) throws Exception; 033 public abstract void invalidateObject(Object key, Object obj) throws Exception; 034 035 /** 036 * Not supported in this base implementation. 037 * Always throws an {@link UnsupportedOperationException}, 038 * subclasses should override this behavior. 039 */ 040 public void addObject(Object key) throws Exception, UnsupportedOperationException { 041 throw new UnsupportedOperationException(); 042 } 043 044 /** 045 * Not supported in this base implementation. 046 * @return a negative value. 047 */ 048 public int getNumIdle(Object key) throws UnsupportedOperationException { 049 return -1; 050 } 051 052 /** 053 * Not supported in this base implementation. 054 * @return a negative value. 055 */ 056 public int getNumActive(Object key) throws UnsupportedOperationException { 057 return -1; 058 } 059 060 /** 061 * Not supported in this base implementation. 062 * @return a negative value. 063 */ 064 public int getNumIdle() throws UnsupportedOperationException { 065 return -1; 066 } 067 068 /** 069 * Not supported in this base implementation. 070 * @return a negative value. 071 */ 072 public int getNumActive() throws UnsupportedOperationException { 073 return -1; 074 } 075 076 /** 077 * Not supported in this base implementation. 078 */ 079 public void clear() throws Exception, UnsupportedOperationException { 080 throw new UnsupportedOperationException(); 081 } 082 083 /** 084 * Not supported in this base implementation. 085 */ 086 public void clear(Object key) throws Exception, UnsupportedOperationException { 087 throw new UnsupportedOperationException(); 088 } 089 090 /** 091 * Close this pool. 092 * This affects the behavior of <code>isClosed</code> and <code>assertOpen</code>. 093 */ 094 public void close() throws Exception { 095 closed = true; 096 } 097 098 /** 099 * 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 }