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