View Javadoc
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  
19  import java.util.Objects;
20  
21  /**
22   * A base implementation of {@code KeyedPooledObjectFactory}.
23   * <p>
24   * All operations defined here are essentially no-op's.
25   * </p>
26   * <p>
27   * This class is immutable, and therefore thread-safe.
28   * </p>
29   *
30   * @see KeyedPooledObjectFactory
31   *
32   * @param <K> The type of keys managed by this factory.
33   * @param <V> Type of element managed by this factory.
34   *
35   * @since 2.0
36   */
37  public abstract class BaseKeyedPooledObjectFactory<K, V> extends BaseObject implements KeyedPooledObjectFactory<K, V> {
38  
39      /**
40       * Reinitializes an instance to be returned by the pool.
41       * <p>
42       * The default implementation is a no-op.
43       * </p>
44       *
45       * @param key the key used when selecting the object
46       * @param p a {@code PooledObject} wrapping the instance to be activated
47       */
48      @Override
49      public void activateObject(final K key, final PooledObject<V> p) throws Exception {
50          // The default implementation is a no-op.
51      }
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       *
59       * @throws Exception if there is a problem creating a new instance,
60       *    this will be propagated to the code requesting an object.
61       */
62      public abstract V create(K key) throws Exception;
63  
64      /**
65       * Destroys an instance no longer needed by the pool.
66       * <p>
67       * The default implementation is a no-op.
68       * </p>
69       *
70       * @param key the key used when selecting the instance
71       * @param p a {@code PooledObject} wrapping the instance to be destroyed
72       */
73      @Override
74      public void destroyObject(final K key, final PooledObject<V> p) throws Exception {
75          // The default implementation is a no-op.
76      }
77  
78      @Override
79      public PooledObject<V> makeObject(final K key) throws Exception {
80          return wrap(
81                  Objects.requireNonNull(create(key), () -> String.format("BaseKeyedPooledObjectFactory(%s).create(key=%s) = null", getClass().getName(), key)));
82      }
83  
84      /**
85       * Uninitializes an instance to be returned to the idle object pool.
86       * <p>
87       * The default implementation is a no-op.
88       * </p>
89       *
90       * @param key the key used when selecting the object
91       * @param p a {@code PooledObject} wrapping the instance to be passivated
92       */
93      @Override
94      public void passivateObject(final K key, final PooledObject<V> p) throws Exception {
95          // The default implementation is a no-op.
96      }
97  
98      /**
99       * Ensures that the instance is safe to be returned by the pool.
100      * <p>
101      * The default implementation always returns {@code true}.
102      * </p>
103      *
104      * @param key the key used when selecting the object
105      * @param p a {@code PooledObject} wrapping the instance to be validated
106      * @return always {@code true} in this default implementation
107      */
108     @Override
109     public boolean validateObject(final K key, final PooledObject<V> p) {
110         return true;
111     }
112 
113     /**
114      * Wraps the provided instance with an implementation of
115      * {@link PooledObject}.
116      *
117      * @param value the instance to wrap, should not be null.
118      * @return The provided instance, wrapped by a {@link PooledObject}
119      */
120     public abstract PooledObject<V> wrap(V value);
121 }