Source code

    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 PoolableObjectFactory}.
    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 * @param <T> Type of element managed in this factory.
    027 *
    028 * @see PooledObjectFactory
    029 * @see BaseKeyedPooledObjectFactory
    030 *
    031 * @since 2.0
    032 */
    033public abstract class BasePooledObjectFactory<T> extends BaseObject implements PooledObjectFactory<T> {
    034
    035    /**
    036     *  No-op.
    037     *
    038     *  @param p ignored
    039     */
    040    @Override
    041    public void activateObject(final PooledObject<T> p) throws Exception {
    042        // The default implementation is a no-op.
    043    }
    044
    045    /**
    046     * Creates an object instance, to be wrapped in a {@link PooledObject}.
    047     * <p>This method <strong>must</strong> support concurrent, multi-threaded
    048     * activation.</p>
    049     *
    050     * @return an instance to be served by the pool
    051     *
    052     * @throws Exception if there is a problem creating a new instance,
    053     *    this will be propagated to the code requesting an object.
    054     */
    055    public abstract T create() throws Exception;
    056
    057    /**
    058     *  No-op.
    059     *
    060     *  @param p ignored
    061     */
    062    @Override
    063    public void destroyObject(final PooledObject<T> p)
    064        throws Exception  {
    065        // The default implementation is a no-op.
    066    }
    067
    068    @Override
    069    public PooledObject<T> makeObject() throws Exception {
    070        return wrap(create());
    071    }
    072
    073    /**
    074     *  No-op.
    075     *
    076     * @param p ignored
    077     */
    078    @Override
    079    public void passivateObject(final PooledObject<T> p)
    080        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
    101     *
    102     * @return The provided instance, wrapped by a {@link PooledObject}
    103     */
    104    public abstract PooledObject<T> wrap(T obj);
    105}