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  /**
20   * An interface defining life-cycle methods for instances to be served by an
21   * {@link ObjectPool}.
22   * <p>
23   * By contract, when an {@link ObjectPool} delegates to a
24   * {@link PooledObjectFactory},
25   * </p>
26   * <ol>
27   *  <li>
28   *   {@link #makeObject} is called whenever a new instance is needed.
29   *  </li>
30   *  <li>
31   *   {@link #activateObject} is invoked on every instance that has been
32   *   {@link #passivateObject passivated} before it is
33   *   {@link ObjectPool#borrowObject borrowed} from the pool.
34   *  </li>
35   *  <li>
36   *   {@link #validateObject} may be invoked on {@link #activateObject activated}
37   *   instances to make sure they can be {@link ObjectPool#borrowObject borrowed}
38   *   from the pool. {@link #validateObject} may also be used to
39   *   test an instance being {@link ObjectPool#returnObject returned} to the pool
40   *   before it is {@link #passivateObject passivated}. It will only be invoked
41   *   on an activated instance.
42   *  </li>
43   *  <li>
44   *   {@link #passivateObject} is invoked on every instance when it is returned
45   *   to the pool.
46   *  </li>
47   *  <li>
48   *   {@link #destroyObject} is invoked on every instance when it is being
49   *   "dropped" from the pool (whether due to the response from
50   *   {@link #validateObject}, or for reasons specific to the pool
51   *   implementation.) There is no guarantee that the instance being destroyed
52   *   will be considered active, passive or in a generally consistent state.
53   *  </li>
54   * </ol>
55   * {@link PooledObjectFactory} must be thread-safe. The only promise
56   * an {@link ObjectPool} makes is that the same instance of an object will not
57   * be passed to more than one method of a {@code PoolableObjectFactory}
58   * at a time.
59   * <p>
60   * While clients of a {@link KeyedObjectPool} borrow and return instances of
61   * the underlying value type {@code V}, the factory methods act on instances of
62   * {@link PooledObject PooledObject&lt;V&gt;}.  These are the object wrappers that
63   * pools use to track and maintain state information about the objects that
64   * they manage.
65   * </p>
66   *
67   * @param <T> Type of element managed in this factory.
68   *
69   * @see ObjectPool
70   *
71   * @since 2.0
72   */
73  public interface PooledObjectFactory<T> {
74  
75    /**
76     * Reinitializes an instance to be returned by the pool.
77     *
78     * @param p a {@code PooledObject} wrapping the instance to be activated
79     *
80     * @throws Exception if there is a problem activating {@code obj},
81     *    this exception may be swallowed by the pool.
82     *
83     * @see #destroyObject
84     */
85    void activateObject(PooledObject<T> p) throws Exception;
86  
87    /**
88     * Destroys an instance no longer needed by the pool, using the default (NORMAL)
89     * DestroyMode.
90     * <p>
91     * It is important for implementations of this method to be aware that there
92     * is no guarantee about what state {@code obj} will be in and the
93     * implementation should be prepared to handle unexpected errors.
94     * </p>
95     * <p>
96     * Also, an implementation must take in to consideration that instances lost
97     * to the garbage collector may never be destroyed.
98     * </p>
99     *
100    * @param p a {@code PooledObject} wrapping the instance to be destroyed
101    *
102    * @throws Exception should be avoided as it may be swallowed by
103    *    the pool implementation.
104    *
105    * @see #validateObject
106    * @see ObjectPool#invalidateObject
107    */
108   void destroyObject(PooledObject<T> p) throws Exception;
109 
110   /**
111    * Destroys an instance no longer needed by the pool, using the provided
112    * DestroyMode.
113    *
114    * @param p a {@code PooledObject} wrapping the instance to be destroyed
115    * @param destroyMode DestroyMode providing context to the factory
116    *
117    * @throws Exception should be avoided as it may be swallowed by
118    *    the pool implementation.
119    *
120    * @see #validateObject
121    * @see ObjectPool#invalidateObject
122    * @see #destroyObject(PooledObject)
123    * @see DestroyMode
124    * @since 2.9.0
125    */
126   default void destroyObject(final PooledObject<T> p, final DestroyMode destroyMode) throws Exception {
127       destroyObject(p);
128   }
129 
130   /**
131    * Creates an instance that can be served by the pool and wrap it in a
132    * {@link PooledObject} to be managed by the pool.
133    *
134    * @return a {@code PooledObject} wrapping an instance that can be served by the pool, not null.
135    *
136    * @throws Exception if there is a problem creating a new instance,
137    *    this will be propagated to the code requesting an object.
138    */
139   PooledObject<T> makeObject() throws Exception;
140 
141   /**
142    * Uninitializes an instance to be returned to the idle object pool.
143    *
144    * @param p a {@code PooledObject} wrapping the instance to be passivated
145    *
146    * @throws Exception if there is a problem passivating {@code obj},
147    *    this exception may be swallowed by the pool.
148    *
149    * @see #destroyObject
150    */
151   void passivateObject(PooledObject<T> p) throws Exception;
152 
153   /**
154    * Ensures that the instance is safe to be returned by the pool.
155    *
156    * @param p a {@code PooledObject} wrapping the instance to be validated
157    *
158    * @return {@code false} if {@code obj} is not valid and should
159    *         be dropped from the pool, {@code true} otherwise.
160    */
161   boolean validateObject(PooledObject<T> p);
162 }