KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > utils > ObjectPool


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 org.apache.xml.utils;
20
21 import java.util.Vector JavaDoc;
22
23 import org.apache.xml.res.XMLErrorResources;
24 import org.apache.xml.res.XMLMessages;
25
26
27 /**
28  * Pool of object of a given type to pick from to help memory usage
29  * @xsl.usage internal
30  */

31 public class ObjectPool implements java.io.Serializable JavaDoc
32 {
33
34   /** Type of objects in this pool.
35    * @serial */

36   private final Class JavaDoc objectType;
37
38   /** Vector of given objects this points to.
39    * @serial */

40   private final Vector JavaDoc freeStack;
41
42   /**
43    * Constructor ObjectPool
44    *
45    * @param type Type of objects for this pool
46    */

47   public ObjectPool(Class JavaDoc type)
48   {
49     objectType = type;
50     freeStack = new Vector JavaDoc();
51   }
52   
53   /**
54    * Constructor ObjectPool
55    *
56    * @param className Fully qualified name of the type of objects for this pool.
57    */

58   public ObjectPool(String JavaDoc className)
59   {
60     try
61     {
62       objectType = ObjectFactory.findProviderClass(
63         className, ObjectFactory.findClassLoader(), true);
64     }
65     catch(ClassNotFoundException JavaDoc cnfe)
66     {
67       throw new WrappedRuntimeException(cnfe);
68     }
69     freeStack = new Vector JavaDoc();
70   }
71
72
73   /**
74    * Constructor ObjectPool
75    *
76    *
77    * @param type Type of objects for this pool
78    * @param size Size of vector to allocate
79    */

80   public ObjectPool(Class JavaDoc type, int size)
81   {
82     objectType = type;
83     freeStack = new Vector JavaDoc(size);
84   }
85
86   /**
87    * Constructor ObjectPool
88    *
89    */

90   public ObjectPool()
91   {
92     objectType = null;
93     freeStack = new Vector JavaDoc();
94   }
95
96   /**
97    * Get an instance of the given object in this pool if available
98    *
99    *
100    * @return an instance of the given object if available or null
101    */

102   public synchronized Object JavaDoc getInstanceIfFree()
103   {
104
105     // Check if the pool is empty.
106
if (!freeStack.isEmpty())
107     {
108
109       // Remove object from end of free pool.
110
Object JavaDoc result = freeStack.lastElement();
111
112       freeStack.setSize(freeStack.size() - 1);
113
114       return result;
115     }
116
117     return null;
118   }
119
120   /**
121    * Get an instance of the given object in this pool
122    *
123    *
124    * @return An instance of the given object
125    */

126   public synchronized Object JavaDoc getInstance()
127   {
128
129     // Check if the pool is empty.
130
if (freeStack.isEmpty())
131     {
132
133       // Create a new object if so.
134
try
135       {
136         return objectType.newInstance();
137       }
138       catch (InstantiationException JavaDoc ex){}
139       catch (IllegalAccessException JavaDoc ex){}
140
141       // Throw unchecked exception for error in pool configuration.
142
throw new RuntimeException JavaDoc(XMLMessages.createXMLMessage(XMLErrorResources.ER_EXCEPTION_CREATING_POOL, null)); //"exception creating new instance for pool");
143
}
144     else
145     {
146
147       // Remove object from end of free pool.
148
Object JavaDoc result = freeStack.lastElement();
149
150       freeStack.setSize(freeStack.size() - 1);
151
152       return result;
153     }
154   }
155
156   /**
157    * Add an instance of the given object to the pool
158    *
159    *
160    * @param obj Object to add.
161    */

162   public synchronized void freeInstance(Object JavaDoc obj)
163   {
164
165     // Make sure the object is of the correct type.
166
// Remove safety. -sb
167
// if (objectType.isInstance(obj))
168
// {
169
freeStack.addElement(obj);
170     // }
171
// else
172
// {
173
// throw new IllegalArgumentException("argument type invalid for pool");
174
// }
175
}
176 }
177
Popular Tags