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 PoolableObjectFactory}.
23 * <p>
24 * All operations defined here are essentially no-op's.
25 * <p>
26 * This class is immutable, and therefore thread-safe
27 *
28 * @param <T> Type of element managed in this factory.
29 * @see PooledObjectFactory
30 * @see BaseKeyedPooledObjectFactory
31 * @since 2.0
32 */
33 public abstract class BasePooledObjectFactory<T> extends BaseObject implements PooledObjectFactory<T> {
34
35 /**
36 * Constructs a new instance.
37 */
38 public BasePooledObjectFactory() {
39 // empty
40 }
41
42 /**
43 * No-op.
44 *
45 * @param p ignored
46 */
47 @Override
48 public void activateObject(final PooledObject<T> p) throws Exception {
49 // The default implementation is a no-op.
50 }
51
52 /**
53 * Creates an object instance, to be wrapped in a {@link PooledObject}.
54 * <p>This method <strong>must</strong> support concurrent, multi-threaded
55 * invocation.</p>
56 *
57 * @return an instance to be served by the pool, not null.
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 T create() throws Exception;
62
63 /**
64 * No-op.
65 *
66 * @param p ignored
67 */
68 @Override
69 public void destroyObject(final PooledObject<T> p) throws Exception {
70 // The default implementation is a no-op.
71 }
72
73 @Override
74 public PooledObject<T> makeObject() throws Exception {
75 return wrap(Objects.requireNonNull(create(), () -> String.format("BasePooledObjectFactory(%s).create() = null", getClass().getName())));
76 }
77
78 /**
79 * No-op.
80 *
81 * @param p ignored
82 */
83 @Override
84 public void passivateObject(final PooledObject<T> p) throws Exception {
85 // The default implementation is a no-op.
86 }
87
88 /**
89 * Always returns {@code true}.
90 *
91 * @param p ignored
92 * @return {@code true}
93 */
94 @Override
95 public boolean validateObject(final PooledObject<T> p) {
96 return true;
97 }
98
99 /**
100 * Wraps the provided instance with an implementation of
101 * {@link PooledObject}.
102 *
103 * @param obj the instance to wrap, should not be null.
104 * @return The provided instance, wrapped by a {@link PooledObject}
105 */
106 public abstract PooledObject<T> wrap(T obj);
107 }