BaseKeyedPooledObjectFactory.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.pool2;

  18. import java.util.Objects;

  19. /**
  20.  * A base implementation of {@code KeyedPooledObjectFactory}.
  21.  * <p>
  22.  * All operations defined here are essentially no-op's.
  23.  * </p>
  24.  * <p>
  25.  * This class is immutable, and therefore thread-safe.
  26.  * </p>
  27.  *
  28.  * @see KeyedPooledObjectFactory
  29.  * @param <K> The type of keys managed by this factory.
  30.  * @param <V> Type of element managed by this factory.
  31.  * @since 2.0
  32.  */
  33. public abstract class BaseKeyedPooledObjectFactory<K, V> extends BaseObject implements KeyedPooledObjectFactory<K, V> {

  34.     /**
  35.      * Constructs a new instance.
  36.      */
  37.     public BaseKeyedPooledObjectFactory() {
  38.         // empty
  39.     }

  40.     /**
  41.      * Reinitializes an instance to be returned by the pool.
  42.      * <p>
  43.      * The default implementation is a no-op.
  44.      * </p>
  45.      *
  46.      * @param key the key used when selecting the object
  47.      * @param p a {@code PooledObject} wrapping the instance to be activated
  48.      */
  49.     @Override
  50.     public void activateObject(final K key, final PooledObject<V> p) throws Exception {
  51.         // The default implementation is a no-op.
  52.     }

  53.     /**
  54.      * Creates an instance that can be served by the pool.
  55.      *
  56.      * @param key the key used when constructing the object
  57.      * @return an instance that can be served by the pool
  58.      * @throws Exception if there is a problem creating a new instance,
  59.      *    this will be propagated to the code requesting an object.
  60.      */
  61.     public abstract V create(K key) throws Exception;

  62.     /**
  63.      * Destroys an instance no longer needed by the pool.
  64.      * <p>
  65.      * The default implementation is a no-op.
  66.      * </p>
  67.      *
  68.      * @param key the key used when selecting the instance
  69.      * @param p a {@code PooledObject} wrapping the instance to be destroyed
  70.      */
  71.     @Override
  72.     public void destroyObject(final K key, final PooledObject<V> p) throws Exception {
  73.         // The default implementation is a no-op.
  74.     }

  75.     @Override
  76.     public PooledObject<V> makeObject(final K key) throws Exception {
  77.         return wrap(
  78.                 Objects.requireNonNull(create(key), () -> String.format("BaseKeyedPooledObjectFactory(%s).create(key=%s) = null", getClass().getName(), key)));
  79.     }

  80.     /**
  81.      * Uninitializes an instance to be returned to the idle object pool.
  82.      * <p>
  83.      * The default implementation is a no-op.
  84.      * </p>
  85.      *
  86.      * @param key the key used when selecting the object
  87.      * @param p a {@code PooledObject} wrapping the instance to be passivated
  88.      */
  89.     @Override
  90.     public void passivateObject(final K key, final PooledObject<V> p) throws Exception {
  91.         // The default implementation is a no-op.
  92.     }

  93.     /**
  94.      * Ensures that the instance is safe to be returned by the pool.
  95.      * <p>
  96.      * The default implementation always returns {@code true}.
  97.      * </p>
  98.      *
  99.      * @param key the key used when selecting the object
  100.      * @param p a {@code PooledObject} wrapping the instance to be validated
  101.      * @return always {@code true} in this default implementation
  102.      */
  103.     @Override
  104.     public boolean validateObject(final K key, final PooledObject<V> p) {
  105.         return true;
  106.     }

  107.     /**
  108.      * Wraps the provided instance with an implementation of
  109.      * {@link PooledObject}.
  110.      *
  111.      * @param value the instance to wrap, should not be null.
  112.      * @return The provided instance, wrapped by a {@link PooledObject}
  113.      */
  114.     public abstract PooledObject<V> wrap(V value);
  115. }