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 */ 017package org.apache.commons.pool2; 018 019/** 020 * A simple base implementation of {@link ObjectPool}. 021 * Optional operations are implemented to either do nothing, return a value 022 * indicating it is unsupported or throw {@link UnsupportedOperationException}. 023 * <p> 024 * This class is intended to be thread-safe. 025 * 026 * @param <T> Type of element pooled in this pool. 027 * 028 * @since 2.0 029 */ 030public abstract class BaseObjectPool<T> extends BaseObject implements ObjectPool<T> { 031 032 @Override 033 public abstract T borrowObject() throws Exception; 034 035 @Override 036 public abstract void returnObject(T obj) throws Exception; 037 038 @Override 039 public abstract void invalidateObject(T obj) throws Exception; 040 041 /** 042 * Not supported in this base implementation. 043 * 044 * @return a negative value. 045 */ 046 @Override 047 public int getNumIdle() { 048 return -1; 049 } 050 051 /** 052 * Not supported in this base implementation. 053 * 054 * @return a negative value. 055 */ 056 @Override 057 public int getNumActive() { 058 return -1; 059 } 060 061 /** 062 * Not supported in this base implementation. 063 * 064 * @throws UnsupportedOperationException if the pool does not implement this 065 * method 066 */ 067 @Override 068 public void clear() throws Exception, UnsupportedOperationException { 069 throw new UnsupportedOperationException(); 070 } 071 072 /** 073 * Not supported in this base implementation. Subclasses should override 074 * this behavior. 075 * 076 * @throws UnsupportedOperationException if the pool does not implement this 077 * method 078 */ 079 @Override 080 public void addObject() throws Exception, UnsupportedOperationException { 081 throw new UnsupportedOperationException(); 082 } 083 084 /** 085 * {@inheritDoc} 086 * <p> 087 * This affects the behavior of <code>isClosed</code> and 088 * <code>assertOpen</code>. 089 * </p> 090 */ 091 @Override 092 public void close() { 093 closed = true; 094 } 095 096 /** 097 * Has this pool instance been closed. 098 * 099 * @return <code>true</code> when this pool has been closed. 100 */ 101 public final boolean isClosed() { 102 return closed; 103 } 104 105 /** 106 * Throws an <code>IllegalStateException</code> when this pool has been 107 * closed. 108 * 109 * @throws IllegalStateException when this pool has been closed. 110 * 111 * @see #isClosed() 112 */ 113 protected final void assertOpen() throws IllegalStateException { 114 if (isClosed()) { 115 throw new IllegalStateException("Pool not open"); 116 } 117 } 118 119 private volatile boolean closed = false; 120 121 @Override 122 protected void toStringAppendFields(final StringBuilder builder) { 123 builder.append("closed="); 124 builder.append(closed); 125 } 126}