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. /*
  17. * $Id: ObjectPool.java,v 1.11 2004/02/17 04:21:14 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. import java.util.Vector;
  21. import com.sun.org.apache.xml.internal.res.XMLErrorResources;
  22. import com.sun.org.apache.xml.internal.res.XMLMessages;
  23. /**
  24. * Pool of object of a given type to pick from to help memory usage
  25. * @xsl.usage internal
  26. */
  27. public class ObjectPool implements java.io.Serializable
  28. {
  29. /** Type of objects in this pool.
  30. * @serial */
  31. private final Class objectType;
  32. /** Vector of given objects this points to.
  33. * @serial */
  34. private final Vector freeStack;
  35. /**
  36. * Constructor ObjectPool
  37. *
  38. * @param type Type of objects for this pool
  39. */
  40. public ObjectPool(Class type)
  41. {
  42. objectType = type;
  43. freeStack = new Vector();
  44. }
  45. /**
  46. * Constructor ObjectPool
  47. *
  48. * @param className Fully qualified name of the type of objects for this pool.
  49. */
  50. public ObjectPool(String className)
  51. {
  52. try
  53. {
  54. objectType = ObjectFactory.findProviderClass(
  55. className, ObjectFactory.findClassLoader(), true);
  56. }
  57. catch(ClassNotFoundException cnfe)
  58. {
  59. throw new WrappedRuntimeException(cnfe);
  60. }
  61. freeStack = new Vector();
  62. }
  63. /**
  64. * Constructor ObjectPool
  65. *
  66. *
  67. * @param type Type of objects for this pool
  68. * @param size Size of vector to allocate
  69. */
  70. public ObjectPool(Class type, int size)
  71. {
  72. objectType = type;
  73. freeStack = new Vector(size);
  74. }
  75. /**
  76. * Constructor ObjectPool
  77. *
  78. */
  79. public ObjectPool()
  80. {
  81. objectType = null;
  82. freeStack = new Vector();
  83. }
  84. /**
  85. * Get an instance of the given object in this pool if available
  86. *
  87. *
  88. * @return an instance of the given object if available or null
  89. */
  90. public synchronized Object getInstanceIfFree()
  91. {
  92. // Check if the pool is empty.
  93. if (!freeStack.isEmpty())
  94. {
  95. // Remove object from end of free pool.
  96. Object result = freeStack.lastElement();
  97. freeStack.setSize(freeStack.size() - 1);
  98. return result;
  99. }
  100. return null;
  101. }
  102. /**
  103. * Get an instance of the given object in this pool
  104. *
  105. *
  106. * @return An instance of the given object
  107. */
  108. public synchronized Object getInstance()
  109. {
  110. // Check if the pool is empty.
  111. if (freeStack.isEmpty())
  112. {
  113. // Create a new object if so.
  114. try
  115. {
  116. return objectType.newInstance();
  117. }
  118. catch (InstantiationException ex){}
  119. catch (IllegalAccessException ex){}
  120. // Throw unchecked exception for error in pool configuration.
  121. throw new RuntimeException(XMLMessages.createXMLMessage(XMLErrorResources.ER_EXCEPTION_CREATING_POOL, null)); //"exception creating new instance for pool");
  122. }
  123. else
  124. {
  125. // Remove object from end of free pool.
  126. Object result = freeStack.lastElement();
  127. freeStack.setSize(freeStack.size() - 1);
  128. return result;
  129. }
  130. }
  131. /**
  132. * Add an instance of the given object to the pool
  133. *
  134. *
  135. * @param obj Object to add.
  136. */
  137. public synchronized void freeInstance(Object obj)
  138. {
  139. // Make sure the object is of the correct type.
  140. // Remove safety. -sb
  141. // if (objectType.isInstance(obj))
  142. // {
  143. freeStack.addElement(obj);
  144. // }
  145. // else
  146. // {
  147. // throw new IllegalArgumentException("argument type invalid for pool");
  148. // }
  149. }
  150. }