BasePooledObjectFactory.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 PoolableObjectFactory}.
  21.  * <p>
  22.  * All operations defined here are essentially no-op's.
  23.  * <p>
  24.  * This class is immutable, and therefore thread-safe
  25.  *
  26.  * @param <T> Type of element managed in this factory.
  27.  * @see PooledObjectFactory
  28.  * @see BaseKeyedPooledObjectFactory
  29.  * @since 2.0
  30.  */
  31. public abstract class BasePooledObjectFactory<T> extends BaseObject implements PooledObjectFactory<T> {

  32.     /**
  33.      * Constructs a new instance.
  34.      */
  35.     public BasePooledObjectFactory() {
  36.         // empty
  37.     }

  38.     /**
  39.      *  No-op.
  40.      *
  41.      *  @param p ignored
  42.      */
  43.     @Override
  44.     public void activateObject(final PooledObject<T> p) throws Exception {
  45.         // The default implementation is a no-op.
  46.     }

  47.     /**
  48.      * Creates an object instance, to be wrapped in a {@link PooledObject}.
  49.      * <p>This method <strong>must</strong> support concurrent, multi-threaded
  50.      * invocation.</p>
  51.      *
  52.      * @return an instance to be served by the pool, not null.
  53.      * @throws Exception if there is a problem creating a new instance,
  54.      *    this will be propagated to the code requesting an object.
  55.      */
  56.     public abstract T create() throws Exception;

  57.     /**
  58.      *  No-op.
  59.      *
  60.      *  @param p ignored
  61.      */
  62.     @Override
  63.     public void destroyObject(final PooledObject<T> p) throws Exception  {
  64.         // The default implementation is a no-op.
  65.     }

  66.     @Override
  67.     public PooledObject<T> makeObject() throws Exception {
  68.         return wrap(Objects.requireNonNull(create(), () -> String.format("BasePooledObjectFactory(%s).create() = null", getClass().getName())));
  69.     }

  70.     /**
  71.      *  No-op.
  72.      *
  73.      * @param p ignored
  74.      */
  75.     @Override
  76.     public void passivateObject(final PooledObject<T> p) throws Exception {
  77.         // The default implementation is a no-op.
  78.     }

  79.     /**
  80.      * Always returns {@code true}.
  81.      *
  82.      * @param p ignored
  83.      * @return {@code true}
  84.      */
  85.     @Override
  86.     public boolean validateObject(final PooledObject<T> p) {
  87.         return true;
  88.     }

  89.     /**
  90.      * Wraps the provided instance with an implementation of
  91.      * {@link PooledObject}.
  92.      *
  93.      * @param obj the instance to wrap, should not be null.
  94.      * @return The provided instance, wrapped by a {@link PooledObject}
  95.      */
  96.     public abstract PooledObject<T> wrap(T obj);
  97. }