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