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