KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > util > ObjectPool


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Yuval Lubowich and Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.core.util;
47
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50
51
52 /**
53  * The ObjectPool implements the Object Pool desgin pattern.
54  * The pool serves object to the asking object until the maximum number
55  * of objects is reached.
56  *
57  * The maximum number of objects is a parameter given upon instantiation.
58  * The object must have a default constructor (a constructor that does not receive
59  * parameters)
60  *
61  * @see java.lang.Class
62  * @version 1.0
63  * @author Yuval Lubowich and Amir Shevat
64  */

65 public abstract class ObjectPool {
66
67     private Object JavaDoc[] acquiredObjects = null;
68     private Object JavaDoc acquiredObjectsLock = new Object JavaDoc();
69     
70     private Object JavaDoc[] releasedObjects = null;
71     private Object JavaDoc releasedObjectsLock = new Object JavaDoc();
72     
73     private int maxNumOfObjects = 0;
74     private int objectsInAcquiredPool = 0;
75     private int objectsInReleasedPool = 0;
76     private Log log;
77     
78
79     /* The logical name give to the pool (used for monitoring) */
80     private String JavaDoc poolName = null;
81
82     /** This member holds the largest number of items that were simultaneously acquired from this pool. */
83     private int highWaterMark = 0;
84
85
86     /** Static Vector with all the ObjectPool object created in the system */
87     //private static Vector instances = new Vector(20);
88

89     
90     /**
91      * @param aNumOfObjects number of maximum objects in the pool
92      * @param anObjectClass the type of the objects
93      */

94     public ObjectPool(int numOfObjects, String JavaDoc poolName) {
95         this.poolName = poolName;
96         this.maxNumOfObjects = numOfObjects;
97         log=LogFactory.getLog("ObjectPool");
98
99         acquiredObjects = new Object JavaDoc[maxNumOfObjects];
100         releasedObjects = new Object JavaDoc[maxNumOfObjects];
101         
102         for(int i=0; i < maxNumOfObjects; i++)
103             acquiredObjects[i] = newInstance();
104
105         objectsInAcquiredPool = acquiredObjects.length;
106     }//ObjectPool
107

108     
109
110     /**
111      * get the maximum number of object in the pool
112      */

113     public final int getMaxNumOfObjects() {
114         return maxNumOfObjects;
115     }//getMaxNumOfObjects
116

117     /**
118      * get the current number of object in the pool
119      */

120     public final int getRemainingObjectsInPool() {
121         return objectsInAcquiredPool;
122     }//getRemainingObjectsInPool
123

124
125     /**
126      * Returns the logical pool name given to the pool on creation by it's creator.<br>
127      * Used for monitoring.<br>
128      */

129     public final String JavaDoc getPoolName(){
130         return poolName;
131     }//getPoolName
132

133
134     /**
135      * Returns the largest number of objects acquired from the pool
136      */

137     public final int getHighWaterMark() {
138         return highWaterMark;
139     }//getHighWaterMark
140

141
142     
143
144     /**
145      * retrieve an object from the pool.
146      *
147      * @return an object from the pool
148      */

149     public final Object JavaDoc acquireObject() {
150         synchronized (acquiredObjectsLock){
151             Object JavaDoc obj = null;
152             if(objectsInAcquiredPool == 0){
153                 flip();
154             }//if
155
if(objectsInAcquiredPool == 0){
156                 try{
157                     obj = newInstance();
158                 }//try
159
catch(Exception JavaDoc ex){
160                     if(log.isFatalEnabled()){
161                         log.fatal("Exception in init method of Object Pool - 'newInstance()'. ",ex);
162                     }//if
163
} //catch
164
}//if
165
else{
166                 objectsInAcquiredPool--;
167                 obj = acquiredObjects[objectsInAcquiredPool];
168                 acquiredObjects[objectsInAcquiredPool] = null;
169                 
170                 // Keep track of the largest amount of objects that were acquired from the pool
171
int nAcquiredObjects = acquiredObjects.length - objectsInAcquiredPool;
172                 if (nAcquiredObjects > highWaterMark)
173                     highWaterMark = nAcquiredObjects;
174             }//else
175
return obj;
176         }//synchronized
177
}//acquireObject
178

179
180     /**
181      * return an object to the pool.
182      *
183      * @param anObj the object
184      */

185     public final void releaseObject(Object JavaDoc theObj) {
186         synchronized(releasedObjectsLock){
187             
188             if (objectsInReleasedPool>= maxNumOfObjects)
189                 return;
190             
191             releasedObjects[objectsInReleasedPool++] = theObj;
192         }//synchronized
193
}//releaseObject
194

195     private final void flip(){
196         synchronized(releasedObjectsLock){
197             int tmp = objectsInAcquiredPool;
198             Object JavaDoc[] tmpArray = acquiredObjects;
199             
200             acquiredObjects = releasedObjects;
201             releasedObjects = tmpArray;
202             
203             objectsInAcquiredPool = objectsInReleasedPool;
204             objectsInReleasedPool = tmp;
205         }//synchronized
206
}///flip
207

208
209     /** IResetable interface implementation */
210     public final void reset() {
211         if(log.isDebugEnabled()){
212             log.debug("Resetting pool stats of " + poolName);
213         }//if
214

215         highWaterMark = 0;
216     } //reset
217

218
219     /**
220      * Classes iheriting from the ObjectPool Should implement this method.
221      * and return a new instance of the Object.
222      *
223      * @return
224      */

225     public abstract Object JavaDoc newInstance();
226
227     /**
228      * Returns the name of the class held by this pool.<br>
229      */

230     public abstract String JavaDoc getObjectInPoolClassName();
231
232 }
233
Popular Tags