    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 * An interface defining life-cycle methods for instances to be served by an
    021 * {@link ObjectPool}.
    022 * <p>
    023 * By contract, when an {@link ObjectPool} delegates to a
    024 * {@link PooledObjectFactory},
    025 * </p>
    026 * <ol>
    027 *  <li>
    028 *   {@link #makeObject} is called whenever a new instance is needed.
    029 *  </li>
    030 *  <li>
    031 *   {@link #activateObject} is invoked on every instance that has been
    032 *   {@link #passivateObject passivated} before it is
    033 *   {@link ObjectPool#borrowObject borrowed} from the pool.
    034 *  </li>
    035 *  <li>
    036 *   {@link #validateObject} may be invoked on {@link #activateObject activated}
    037 *   instances to make sure they can be {@link ObjectPool#borrowObject borrowed}
    038 *   from the pool. {@link #validateObject} may also be used to
    039 *   test an instance being {@link ObjectPool#returnObject returned} to the pool
    040 *   before it is {@link #passivateObject passivated}. It will only be invoked
    041 *   on an activated instance.
    042 *  </li>
    043 *  <li>
    044 *   {@link #passivateObject} is invoked on every instance when it is returned
    045 *   to the pool.
    046 *  </li>
    047 *  <li>
    048 *   {@link #destroyObject} is invoked on every instance when it is being
    049 *   "dropped" from the pool (whether due to the response from
    050 *   {@link #validateObject}, or for reasons specific to the pool
    051 *   implementation.) There is no guarantee that the instance being destroyed
    052 *   will be considered active, passive or in a generally consistent state.
    053 *  </li>
    054 * </ol>
    055 * {@link PooledObjectFactory} must be thread-safe. The only promise
    056 * an {@link ObjectPool} makes is that the same instance of an object will not
    057 * be passed to more than one method of a {@code PoolableObjectFactory}
    058 * at a time.
    059 * <p>
    060 * While clients of a {@link KeyedObjectPool} borrow and return instances of
    061 * the underlying value type {@code V}, the factory methods act on instances of
    062 * {@link PooledObject PooledObject&lt;V&gt;}.  These are the object wrappers that
    063 * pools use to track and maintain state information about the objects that
    064 * they manage.
    065 * </p>
    066 *
    067 * @param <T> Type of element managed in this factory.
    068 *
    069 * @see ObjectPool
    070 *
    071 * @since 2.0
    072 */
    073public interface PooledObjectFactory<T> {
    074
    075  /**
    076   * Creates an instance that can be served by the pool and wrap it in a
    077   * {@link PooledObject} to be managed by the pool.
    078   *
    079   * @return a {@code PooledObject} wrapping an instance that can be served by the pool
    080   *
    081   * @throws Exception if there is a problem creating a new instance,
    082   *    this will be propagated to the code requesting an object.
    083   */
    084  PooledObject<T> makeObject() throws Exception;
    085
    086  /**
    087   * Destroys an instance no longer needed by the pool, using the default (NORMAL)
    088   * DestroyMode.
    089   * <p>
    090   * It is important for implementations of this method to be aware that there
    091   * is no guarantee about what state {@code obj} will be in and the
    092   * implementation should be prepared to handle unexpected errors.
    093   * </p>
    094   * <p>
    095   * Also, an implementation must take in to consideration that instances lost
    096   * to the garbage collector may never be destroyed.
    097   * </p>
    098   *
    099   * @param p a {@code PooledObject} wrapping the instance to be destroyed
    100   *
    101   * @throws Exception should be avoided as it may be swallowed by
    102   *    the pool implementation.
    103   *
    104   * @see #validateObject
    105   * @see ObjectPool#invalidateObject
    106   */
    107  void destroyObject(PooledObject<T> p) throws Exception;
    108
    109  /**
    110   * Destroys an instance no longer needed by the pool, using the provided
    111   * DestroyMode.
    112   *
    113   * @param p a {@code PooledObject} wrapping the instance to be destroyed
    114   * @param mode DestroyMode providing context to the factory
    115   *
    116   * @throws Exception should be avoided as it may be swallowed by
    117   *    the pool implementation.
    118   *
    119   * @see #validateObject
    120   * @see ObjectPool#invalidateObject
    121   * @see #destroyObject(PooledObject)
    122   * @see DestroyMode
    123   * @since 2.9.0
    124   */
    125  default void destroyObject(final PooledObject<T> p, final DestroyMode mode) throws Exception {
    126      destroyObject(p);
    127  }
    128
    129  /**
    130   * Ensures that the instance is safe to be returned by the pool.
    131   *
    132   * @param p a {@code PooledObject} wrapping the instance to be validated
    133   *
    134   * @return {@code false} if {@code obj} is not valid and should
    135   *         be dropped from the pool, {@code true} otherwise.
    136   */
    137  boolean validateObject(PooledObject<T> p);
    138
    139  /**
    140   * Reinitializes an instance to be returned by the pool.
    141   *
    142   * @param p a {@code PooledObject} wrapping the instance to be activated
    143   *
    144   * @throws Exception if there is a problem activating {@code obj},
    145   *    this exception may be swallowed by the pool.
    146   *
    147   * @see #destroyObject
    148   */
    149  void activateObject(PooledObject<T> p) throws Exception;
    150
    151  /**
    152   * Uninitializes an instance to be returned to the idle object pool.
    153   *
    154   * @param p a {@code PooledObject} wrapping the instance to be passivated
    155   *
    156   * @throws Exception if there is a problem passivating {@code obj},
    157   *    this exception may be swallowed by the pool.
    158   *
    159   * @see #destroyObject
    160   */
    161  void passivateObject(PooledObject<T> p) throws Exception;
    162}