1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Copyright 1999-2004 The Apache Software Foundation.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 /*
21  * $Id: ObjectPool.java,v 1.2.4.1 2005/09/15 08:15:50 suresh_emailid Exp $
22  */
23 package com.sun.org.apache.xml.internal.utils;
24 
25 import java.util.ArrayList;
26 
27 import com.sun.org.apache.xml.internal.res.XMLErrorResources;
28 import com.sun.org.apache.xml.internal.res.XMLMessages;
29 import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
30 
31 
32 /**
33  * Pool of object of a given type to pick from to help memory usage
34  * @xsl.usage internal
35  */
36 public class ObjectPool implements java.io.Serializable
37 {
38     static final long serialVersionUID = -8519013691660936643L;
39 
40   /** Type of objects in this pool.
41    *  @serial          */
42   private final Class objectType;
43 
44   /** Stack of given objects this points to.
45    *  @serial          */
46   private final ArrayList freeStack;
47 
48   /**
49    * Constructor ObjectPool
50    *
51    * @param type Type of objects for this pool
52    */
ObjectPool(Class type)53   public ObjectPool(Class type)
54   {
55     objectType = type;
56     freeStack = new ArrayList();
57   }
58 
59   /**
60    * Constructor ObjectPool
61    *
62    * @param className Fully qualified name of the type of objects for this pool.
63    */
ObjectPool(String className)64   public ObjectPool(String className)
65   {
66     try
67     {
68       objectType = ObjectFactory.findProviderClass(className, true);
69     }
70     catch(ClassNotFoundException cnfe)
71     {
72       throw new WrappedRuntimeException(cnfe);
73     }
74     freeStack = new ArrayList();
75   }
76 
77 
78   /**
79    * Constructor ObjectPool
80    *
81    *
82    * @param type Type of objects for this pool
83    * @param size Size of vector to allocate
84    */
ObjectPool(Class type, int size)85   public ObjectPool(Class type, int size)
86   {
87     objectType = type;
88     freeStack = new ArrayList(size);
89   }
90 
91   /**
92    * Constructor ObjectPool
93    *
94    */
ObjectPool()95   public ObjectPool()
96   {
97     objectType = null;
98     freeStack = new ArrayList();
99   }
100 
101   /**
102    * Get an instance of the given object in this pool if available
103    *
104    *
105    * @return an instance of the given object if available or null
106    */
getInstanceIfFree()107   public synchronized Object getInstanceIfFree()
108   {
109 
110     // Check if the pool is empty.
111     if (!freeStack.isEmpty())
112     {
113 
114       // Remove object from end of free pool.
115       Object result = freeStack.remove(freeStack.size() - 1);
116       return result;
117     }
118 
119     return null;
120   }
121 
122   /**
123    * Get an instance of the given object in this pool
124    *
125    *
126    * @return An instance of the given object
127    */
getInstance()128   public synchronized Object getInstance()
129   {
130 
131     // Check if the pool is empty.
132     if (freeStack.isEmpty())
133     {
134 
135       // Create a new object if so.
136       try
137       {
138         return objectType.newInstance();
139       }
140       catch (InstantiationException ex){}
141       catch (IllegalAccessException ex){}
142 
143       // Throw unchecked exception for error in pool configuration.
144       throw new RuntimeException(XMLMessages.createXMLMessage(XMLErrorResources.ER_EXCEPTION_CREATING_POOL, null)); //"exception creating new instance for pool");
145     }
146     else
147     {
148 
149       // Remove object from end of free pool.
150       Object result = freeStack.remove(freeStack.size() - 1);
151       return result;
152     }
153   }
154 
155   /**
156    * Add an instance of the given object to the pool
157    *
158    *
159    * @param obj Object to add.
160    */
freeInstance(Object obj)161   public synchronized void freeInstance(Object obj)
162   {
163 
164     // Make sure the object is of the correct type.
165     // Remove safety.  -sb
166     // if (objectType.isInstance(obj))
167     // {
168     freeStack.add(obj);
169     // }
170     // else
171     // {
172     //  throw new IllegalArgumentException("argument type invalid for pool");
173     // }
174   }
175 }
176