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 * @since 2.0 032 */ 033public abstract class BaseKeyedPooledObjectFactory<K, V> extends BaseObject 034 implements KeyedPooledObjectFactory<K, V> { 035 036 /** 037 * Create an instance that can be served by the pool. 038 * 039 * @param key the key used when constructing the object 040 * @return an instance that can be served by the pool 041 * 042 * @throws Exception if there is a problem creating a new instance, 043 * this will be propagated to the code requesting an object. 044 */ 045 public abstract V create(K key) 046 throws Exception; 047 048 /** 049 * Wrap the provided instance with an implementation of 050 * {@link PooledObject}. 051 * 052 * @param value the instance to wrap 053 * 054 * @return The provided instance, wrapped by a {@link PooledObject} 055 */ 056 public abstract PooledObject<V> wrap(V value); 057 058 @Override 059 public PooledObject<V> makeObject(final K key) throws Exception { 060 return wrap(create(key)); 061 } 062 063 /** 064 * Destroy an instance no longer needed by the pool. 065 * <p> 066 * The default implementation is a no-op. 067 * </p> 068 * 069 * @param key the key used when selecting the instance 070 * @param p a {@code PooledObject} wrapping the instance to be destroyed 071 */ 072 @Override 073 public void destroyObject(final K key, final PooledObject<V> p) 074 throws Exception { 075 // The default implementation is a no-op. 076 } 077 078 /** 079 * Ensures that the instance is safe to be returned by the pool. 080 * <p> 081 * The default implementation always returns {@code true}. 082 * </p> 083 * 084 * @param key the key used when selecting the object 085 * @param p a {@code PooledObject} wrapping the instance to be validated 086 * @return always <code>true</code> in the default implementation 087 */ 088 @Override 089 public boolean validateObject(final K key, final PooledObject<V> p) { 090 return true; 091 } 092 093 /** 094 * Reinitialize an instance to be returned by the pool. 095 * <p> 096 * The default implementation is a no-op. 097 * </p> 098 * 099 * @param key the key used when selecting the object 100 * @param p a {@code PooledObject} wrapping the instance to be activated 101 */ 102 @Override 103 public void activateObject(final K key, final PooledObject<V> p) 104 throws Exception { 105 // The default implementation is a no-op. 106 } 107 108 /** 109 * Uninitialize an instance to be returned to the idle object pool. 110 * <p> 111 * The default implementation is a no-op. 112 * </p> 113 * 114 * @param key the key used when selecting the object 115 * @param p a {@code PooledObject} wrapping the instance to be passivated 116 */ 117 @Override 118 public void passivateObject(final K key, final PooledObject<V> p) 119 throws Exception { 120 // The default implementation is a no-op. 121 } 122}