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 base implementation of <code>KeyedPooledObjectFactory</code>. 021 * <p> 022 * All operations defined here are essentially no-op's. 023 * </p> 024 * This class is immutable, and therefore thread-safe. 025 * 026 * @see KeyedPooledObjectFactory 027 * 028 * @param <K> The type of keys managed by this factory. 029 * @param <V> Type of element managed by this factory. 030 * 031 * @version $Revision: 1333925 $ 032 * 033 * @since 2.0 034 */ 035public abstract class BaseKeyedPooledObjectFactory<K,V> 036 implements KeyedPooledObjectFactory<K,V> { 037 038 /** 039 * Create an instance that can be served by the pool. 040 * 041 * @param key the key used when constructing the object 042 * @return an instance that can be served by the pool 043 */ 044 public abstract V create(K key) 045 throws Exception; 046 047 /** 048 * Wrap the provided instance with an implementation of 049 * {@link PooledObject}. 050 * 051 * @param value the instance to wrap 052 * 053 * @return The provided instance, wrapped by a {@link PooledObject} 054 */ 055 public abstract PooledObject<V> wrap(V value); 056 057 @Override 058 public PooledObject<V> makeObject(K key) throws Exception { 059 return wrap(create(key)); 060 } 061 062 /** 063 * Destroy an instance no longer needed by the pool. 064 * <p> 065 * The default implementation is a no-op. 066 * 067 * @param key the key used when selecting the instance 068 * @param p a {@code PooledObject} wrapping the the instance to be destroyed 069 */ 070 @Override 071 public void destroyObject(K key, PooledObject<V> p) 072 throws Exception { 073 } 074 075 /** 076 * Ensures that the instance is safe to be returned by the pool. 077 * <p> 078 * The default implementation always returns <tt>true</tt>. 079 * 080 * @param key the key used when selecting the object 081 * @param p a {@code PooledObject} wrapping the the instance to be validated 082 * @return always <code>true</code> in the default implementation 083 */ 084 @Override 085 public boolean validateObject(K key, PooledObject<V> p) { 086 return true; 087 } 088 089 /** 090 * Reinitialize an instance to be returned by the pool. 091 * <p> 092 * The default implementation is a no-op. 093 * 094 * @param key the key used when selecting the object 095 * @param p a {@code PooledObject} wrapping the the instance to be activated 096 */ 097 @Override 098 public void activateObject(K key, PooledObject<V> p) 099 throws Exception { 100 } 101 102 /** 103 * Uninitialize an instance to be returned to the idle object pool. 104 * <p> 105 * The default implementation is a no-op. 106 * 107 * @param key the key used when selecting the object 108 * @param p a {@code PooledObject} wrapping the the instance to be passivated 109 */ 110 @Override 111 public void passivateObject(K key, PooledObject<V> p) 112 throws Exception { 113 } 114}