1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xml.utils;
  58. import java.util.*;
  59. import org.apache.xalan.res.XSLTErrorResources;
  60. import org.apache.xalan.res.XSLMessages;
  61. /**
  62. * <meta name="usage" content="internal"/>
  63. * Pool of object of a given type to pick from to help memory usage
  64. */
  65. public class ObjectPool implements java.io.Serializable
  66. {
  67. /** Type of objects in this pool.
  68. * @serial */
  69. private final Class objectType;
  70. /** Vector of given objects this points to.
  71. * @serial */
  72. private final Vector freeStack;
  73. /**
  74. * Constructor ObjectPool
  75. *
  76. * @param type Type of objects for this pool
  77. */
  78. public ObjectPool(Class type)
  79. {
  80. objectType = type;
  81. freeStack = new Vector();
  82. }
  83. /**
  84. * Constructor ObjectPool
  85. *
  86. * @param className Fully qualified name of the type of objects for this pool.
  87. */
  88. public ObjectPool(String className)
  89. {
  90. try
  91. {
  92. objectType = Class.forName(className);
  93. }
  94. catch(ClassNotFoundException cnfe)
  95. {
  96. throw new WrappedRuntimeException(cnfe);
  97. }
  98. freeStack = new Vector();
  99. }
  100. /**
  101. * Constructor ObjectPool
  102. *
  103. *
  104. * @param type Type of objects for this pool
  105. * @param size Size of vector to allocate
  106. */
  107. public ObjectPool(Class type, int size)
  108. {
  109. objectType = type;
  110. freeStack = new Vector(size);
  111. }
  112. /**
  113. * Constructor ObjectPool
  114. *
  115. */
  116. public ObjectPool()
  117. {
  118. objectType = null;
  119. freeStack = new Vector();
  120. }
  121. /**
  122. * Get an instance of the given object in this pool if available
  123. *
  124. *
  125. * @return an instance of the given object if available or null
  126. */
  127. public synchronized Object getInstanceIfFree()
  128. {
  129. // Check if the pool is empty.
  130. if (!freeStack.isEmpty())
  131. {
  132. // Remove object from end of free pool.
  133. Object result = freeStack.lastElement();
  134. freeStack.setSize(freeStack.size() - 1);
  135. return result;
  136. }
  137. return null;
  138. }
  139. /**
  140. * Get an instance of the given object in this pool
  141. *
  142. *
  143. * @return An instance of the given object
  144. */
  145. public synchronized Object getInstance()
  146. {
  147. // Check if the pool is empty.
  148. if (freeStack.isEmpty())
  149. {
  150. // Create a new object if so.
  151. try
  152. {
  153. return objectType.newInstance();
  154. }
  155. catch (InstantiationException ex){}
  156. catch (IllegalAccessException ex){}
  157. // Throw unchecked exception for error in pool configuration.
  158. throw new RuntimeException(XSLMessages.createMessage(XSLTErrorResources.ER_EXCEPTION_CREATING_POOL, null)); //"exception creating new instance for pool");
  159. }
  160. else
  161. {
  162. // Remove object from end of free pool.
  163. Object result = freeStack.lastElement();
  164. freeStack.setSize(freeStack.size() - 1);
  165. return result;
  166. }
  167. }
  168. /**
  169. * Add an instance of the given object to the pool
  170. *
  171. *
  172. * @param obj Object to add.
  173. */
  174. public synchronized void freeInstance(Object obj)
  175. {
  176. // Make sure the object is of the correct type.
  177. // Remove safety. -sb
  178. // if (objectType.isInstance(obj))
  179. // {
  180. freeStack.addElement(obj);
  181. // }
  182. // else
  183. // {
  184. // throw new IllegalArgumentException("argument type invalid for pool");
  185. // }
  186. }
  187. }