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 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   *
30   * @see PooledObjectFactory
31   * @see BaseKeyedPooledObjectFactory
32   *
33   * @since 2.0
34   */
35  public abstract class BasePooledObjectFactory<T> extends BaseObject implements PooledObjectFactory<T> {
36  
37      /**
38       *  No-op.
39       *
40       *  @param p ignored
41       */
42      @Override
43      public void activateObject(final PooledObject<T> p) throws Exception {
44          // The default implementation is a no-op.
45      }
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       *
54       * @throws Exception if there is a problem creating a new instance,
55       *    this will be propagated to the code requesting an object.
56       */
57      public abstract T create() throws Exception;
58  
59      /**
60       *  No-op.
61       *
62       *  @param p ignored
63       */
64      @Override
65      public void destroyObject(final PooledObject<T> p) throws Exception  {
66          // The default implementation is a no-op.
67      }
68  
69      @Override
70      public PooledObject<T> makeObject() throws Exception {
71          return wrap(Objects.requireNonNull(create(), () -> String.format("BasePooledObjectFactory(%s).create() = null", getClass().getName())));
72      }
73  
74      /**
75       *  No-op.
76       *
77       * @param p ignored
78       */
79      @Override
80      public void passivateObject(final PooledObject<T> p) throws Exception {
81          // The default implementation is a no-op.
82      }
83  
84      /**
85       * Always returns {@code true}.
86       *
87       * @param p ignored
88       *
89       * @return {@code true}
90       */
91      @Override
92      public boolean validateObject(final PooledObject<T> p) {
93          return true;
94      }
95  
96      /**
97       * Wraps the provided instance with an implementation of
98       * {@link PooledObject}.
99       *
100      * @param obj the instance to wrap, should not be null.
101      *
102      * @return The provided instance, wrapped by a {@link PooledObject}
103      */
104     public abstract PooledObject<T> wrap(T obj);
105 }