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 PoolableObjectFactory}. 023 * <p> 024 * All operations defined here are essentially no-op's. 025 * <p> 026 * This class is immutable, and therefore thread-safe 027 * 028 * @param <T> Type of element managed in this factory. 029 * 030 * @see PooledObjectFactory 031 * @see BaseKeyedPooledObjectFactory 032 * 033 * @since 2.0 034 */ 035public abstract class BasePooledObjectFactory<T> extends BaseObject implements PooledObjectFactory<T> { 036 037 /** 038 * No-op. 039 * 040 * @param p ignored 041 */ 042 @Override 043 public void activateObject(final PooledObject<T> p) throws Exception { 044 // The default implementation is a no-op. 045 } 046 047 /** 048 * Creates an object instance, to be wrapped in a {@link PooledObject}. 049 * <p>This method <strong>must</strong> support concurrent, multi-threaded 050 * invocation.</p> 051 * 052 * @return an instance to be served by the pool, not null. 053 * 054 * @throws Exception if there is a problem creating a new instance, 055 * this will be propagated to the code requesting an object. 056 */ 057 public abstract T create() throws Exception; 058 059 /** 060 * No-op. 061 * 062 * @param p ignored 063 */ 064 @Override 065 public void destroyObject(final PooledObject<T> p) throws Exception { 066 // The default implementation is a no-op. 067 } 068 069 @Override 070 public PooledObject<T> makeObject() throws Exception { 071 return wrap(Objects.requireNonNull(create(), () -> String.format("BasePooledObjectFactory(%s).create() = null", getClass().getName()))); 072 } 073 074 /** 075 * No-op. 076 * 077 * @param p ignored 078 */ 079 @Override 080 public void passivateObject(final PooledObject<T> p) throws Exception { 081 // The default implementation is a no-op. 082 } 083 084 /** 085 * Always returns {@code true}. 086 * 087 * @param p ignored 088 * 089 * @return {@code true} 090 */ 091 @Override 092 public boolean validateObject(final PooledObject<T> p) { 093 return true; 094 } 095 096 /** 097 * Wraps the provided instance with an implementation of 098 * {@link PooledObject}. 099 * 100 * @param obj the instance to wrap, should not be null. 101 * 102 * @return The provided instance, wrapped by a {@link PooledObject} 103 */ 104 public abstract PooledObject<T> wrap(T obj); 105}