1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.pool;
  17. /**
  18. * A "keyed" pooling interface.
  19. * <p>
  20. * A keyed pool pools instances of multiple types. Each
  21. * type may be accessed using an arbitrary key.
  22. * <p>
  23. * Example of use:
  24. * <table border="1" cellspacing="0" cellpadding="3" align="center" bgcolor="#FFFFFF"><tr><td><pre>
  25. * Object obj = <font color="#0000CC">null</font>
  26. * Object key = <font color="#CC0000">"Key"</font>
  27. *
  28. * <font color="#0000CC">try</font> {
  29. * obj = pool.borrowObject(key);
  30. * <font color="#00CC00">//...use the object...</font>
  31. * } <font color="#0000CC">catch</font>(Exception e) {
  32. * <font color="#00CC00">//...handle any exceptions...</font>
  33. * } <font color="#0000CC">finally</font> {
  34. * <font color="#00CC00">// make sure the object is returned to the pool</font>
  35. * <font color="#0000CC">if</font>(<font color="#0000CC">null</font> != obj) {
  36. * pool.returnObject(key,obj);
  37. * }
  38. * }</pre></td></tr></table>
  39. *
  40. * <p>
  41. * {@link KeyedObjectPool} implementations <i>may</i> choose to store at most
  42. * one instance per key value, or may choose to maintain a pool of instances
  43. * for each key (essentially creating a {@link java.util.Map Map} of
  44. * {@link ObjectPool pools}).
  45. * </p>
  46. *
  47. * @see KeyedPoolableObjectFactory
  48. * @see KeyedObjectPoolFactory
  49. * @see ObjectPool
  50. *
  51. * @author Rodney Waldhoff
  52. * @version $Revision: 1.14 $ $Date: 2004/02/28 12:16:21 $
  53. */
  54. public interface KeyedObjectPool {
  55. /**
  56. * Obtain an instance from my pool
  57. * for the specified <i>key</i>.
  58. * By contract, clients MUST return
  59. * the borrowed object using
  60. * {@link #returnObject(java.lang.Object,java.lang.Object) <tt>returnObject</tt>},
  61. * or a related method as defined in an implementation
  62. * or sub-interface,
  63. * using a <i>key</i> that is equivalent to the one used to
  64. * borrow the instance in the first place.
  65. *
  66. * @param key the key used to obtain the object
  67. * @return an instance from my pool.
  68. */
  69. Object borrowObject(Object key) throws Exception;
  70. /**
  71. * Return an instance to my pool.
  72. * By contract, <i>obj</i> MUST have been obtained
  73. * using {@link #borrowObject(java.lang.Object) <tt>borrowObject</tt>}
  74. * or a related method as defined in an implementation
  75. * or sub-interface
  76. * using a <i>key</i> that is equivalent to the one used to
  77. * borrow the <tt>Object</tt> in the first place.
  78. *
  79. * @param key the key used to obtain the object
  80. * @param obj a {@link #borrowObject(java.lang.Object) borrowed} instance to be returned.
  81. */
  82. void returnObject(Object key, Object obj) throws Exception;
  83. /**
  84. * Invalidates an object from the pool
  85. * By contract, <i>obj</i> MUST have been obtained
  86. * using {@link #borrowObject borrowObject}
  87. * or a related method as defined in an implementation
  88. * or sub-interface
  89. * using a <i>key</i> that is equivalent to the one used to
  90. * borrow the <tt>Object</tt> in the first place.
  91. * <p>
  92. * This method should be used when an object that has been borrowed
  93. * is determined (due to an exception or other problem) to be invalid.
  94. * If the connection should be validated before or after borrowing,
  95. * then the {@link PoolableObjectFactory#validateObject} method should be
  96. * used instead.
  97. *
  98. * @param obj a {@link #borrowObject borrowed} instance to be returned.
  99. */
  100. void invalidateObject(Object key, Object obj) throws Exception;
  101. /**
  102. * Create an object using my {@link #setFactory factory} or other
  103. * implementation dependent mechanism, and place it into the pool.
  104. * addObject() is useful for "pre-loading" a pool with idle objects.
  105. * (Optional operation).
  106. */
  107. void addObject(Object key) throws Exception;
  108. /**
  109. * Returns the number of instances
  110. * corresponding to the given <i>key</i>
  111. * currently idle in my pool (optional operation).
  112. * Throws {@link UnsupportedOperationException}
  113. * if this information is not available.
  114. *
  115. * @param key the key
  116. * @return the number of instances corresponding to the given <i>key</i> currently idle in my pool
  117. * @throws UnsupportedOperationException when this implementation doesn't support the operation
  118. */
  119. int getNumIdle(Object key) throws UnsupportedOperationException;
  120. /**
  121. * Returns the number of instances
  122. * currently borrowed from but not yet returned
  123. * to my pool corresponding to the
  124. * given <i>key</i> (optional operation).
  125. * Throws {@link UnsupportedOperationException}
  126. * if this information is not available.
  127. *
  128. * @param key the key
  129. * @return the number of instances corresponding to the given <i>key</i> currently borrowed in my pool
  130. * @throws UnsupportedOperationException when this implementation doesn't support the operation
  131. */
  132. int getNumActive(Object key) throws UnsupportedOperationException;
  133. /**
  134. * Returns the total number of instances
  135. * currently idle in my pool (optional operation).
  136. * Throws {@link UnsupportedOperationException}
  137. * if this information is not available.
  138. *
  139. * @return the total number of instances currently idle in my pool
  140. * @throws UnsupportedOperationException when this implementation doesn't support the operation
  141. */
  142. int getNumIdle() throws UnsupportedOperationException;
  143. /**
  144. * Returns the total number of instances
  145. * current borrowed from my pool but not
  146. * yet returned (optional operation).
  147. * Throws {@link UnsupportedOperationException}
  148. * if this information is not available.
  149. *
  150. * @return the total number of instances currently borrowed from my pool
  151. * @throws UnsupportedOperationException when this implementation doesn't support the operation
  152. */
  153. int getNumActive() throws UnsupportedOperationException;
  154. /**
  155. * Clears my pool, removing all pooled instances
  156. * (optional operation).
  157. * Throws {@link UnsupportedOperationException}
  158. * if the pool cannot be cleared.
  159. * @throws UnsupportedOperationException when this implementation doesn't support the operation
  160. */
  161. void clear() throws Exception, UnsupportedOperationException;
  162. /**
  163. * Clears the specified pool, removing all
  164. * pooled instances corresponding to
  165. * the given <i>key</i> (optional operation).
  166. * Throws {@link UnsupportedOperationException}
  167. * if the pool cannot be cleared.
  168. * @param key the key to clear
  169. * @throws UnsupportedOperationException when this implementation doesn't support the operation
  170. */
  171. void clear(Object key) throws Exception, UnsupportedOperationException;
  172. /**
  173. * Close this pool, and free any resources associated with it.
  174. */
  175. void close() throws Exception;
  176. /**
  177. * Sets the {@link KeyedPoolableObjectFactory factory} I use
  178. * to create new instances (optional operation).
  179. * @param factory the {@link KeyedPoolableObjectFactory} I use to create new instances.
  180. * @throws IllegalStateException when the factory cannot be set at this time
  181. * @throws UnsupportedOperationException when this implementation doesn't support the operation
  182. */
  183. void setFactory(KeyedPoolableObjectFactory factory) throws IllegalStateException, UnsupportedOperationException;
  184. }