KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > pool > ObjectPool


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 //NOTE: Tabs are used instead of spaces for indentation.
25
// Make sure that your editor does not replace tabs with spaces.
26
// Set the tab length using your favourite editor to your
27
// visual preference.
28

29 /*
30  * Filename: $File$
31  *
32  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
33  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
34  * All rights reserved.
35  *
36  * This software is the confidential and proprietary information
37  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
38  * You shall not disclose such Confidential Information and shall
39  * use it only in accordance with the terms of the license
40  * agreement you entered into with iPlanet/Sun Microsystems.
41  */

42  
43 /**
44  * <BR> <I>$Source: /cvs/glassfish/appserv-commons/src/java/com/sun/enterprise/util/pool/ObjectPool.java,v $</I>
45  * @author $Author: tcfujii $
46  * @version $Revision: 1.3 $ $Date: 2005/12/25 04:12:27 $
47  */

48  
49 package com.sun.enterprise.util.pool;
50
51 import java.util.List JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.ArrayList JavaDoc;
54 import java.util.Collection JavaDoc;
55
56 import java.lang.ref.Reference JavaDoc;
57 import java.lang.ref.SoftReference JavaDoc;
58
59
60 import com.sun.enterprise.util.scheduler.PeriodicallyServicable;
61 import com.sun.enterprise.util.scheduler.PeriodicEventScheduler;
62
63 import com.sun.enterprise.util.ApproximateClock;
64
65 import com.sun.enterprise.util.collection.DList;
66 import com.sun.enterprise.util.collection.DListNode;
67 import com.sun.enterprise.util.collection.DListNodeFactory;
68
69 import com.sun.enterprise.util.collection.FastStack;
70 //Bug 4677074 begin
71
import java.util.logging.Logger JavaDoc;
72 import java.util.logging.Level JavaDoc;
73 import com.sun.logging.LogDomains;
74 //Bug 4677074 end
75

76
77 public class ObjectPool
78     extends com.sun.enterprise.util.pool.AbstractPool
79     implements PeriodicallyServicable
80 {
81 //Bug 4677074 begin
82
static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER);
83 //Bug 4677074 end
84
protected DList list;
85     protected int minSize;
86     protected int initialSize;
87     protected int maxLimit;
88     protected long maxIdleTime;
89     
90     protected Boolean JavaDoc isBounded;
91     
92     /**
93      * Create an Unbounded pool.
94      * @param The ObjectFactory to create objects
95      * @param The minimum number of objects to be held in the pool (initial size)
96      * @param The pool limit (maximum number of objects in the pool).
97      * @param The maximum idle time after which the object may be removed from the pool.
98      */

99     public ObjectPool(ObjectFactory factory, int minSize, int initialSize, long maxIdleTime) {
100         super();
101         super.factory = factory;
102         this.minSize = minSize;
103         this.initialSize = initialSize;
104         this.maxIdleTime = maxIdleTime;
105         
106         setMaxLimit(-1);
107         
108         initPool();
109     }
110     
111     /**
112      * Create a Bounded pool.
113      * @param The ObjectFactory to create objects
114      * @param The minimum number of objects to be held in the pool (initial size)
115      * @param The pool limit (maximum number of objects in the pool).
116      * @param The maximum idle time after which the object may be removed from the pool.
117      * @param The initial size of the pool. If this is less than the minSize parameter then this is ignored.
118      */

119     public ObjectPool(ObjectFactory factory, int minSize, int initialSize, int maxLimit,
120                     long maxIdleTime)
121     {
122         super();
123         super.factory = factory;
124         this.minSize = minSize;
125         this.maxIdleTime = maxIdleTime;
126         this.initialSize = initialSize;
127         
128         setMaxLimit(maxLimit);
129         
130         initPool();
131     }
132     
133     
134     public int getMaxLimit() {
135         return this.maxLimit;
136     }
137     
138     public void setMaxLimit(int limit) {
139         if ((limit <= 0) || (limit >= Integer.MAX_VALUE-1)) {
140             this.isBounded = null;
141         } else {
142             this.isBounded = new Boolean JavaDoc(true);
143             this.maxLimit = limit;
144         }
145     }
146         
147     
148     private void initPool() {
149         list = new DList();
150         
151         super.collection = list;
152         super.preload((minSize < initialSize) ? initialSize : minSize);
153         
154         scheduler.addTimeRepeatableTask(this, (int) maxIdleTime);
155     }
156     
157     /**
158      * Since this method would be called only if the pool is empty
159      */

160     protected boolean canCreate() {
161         return (isBounded == null) ? true : (createdCount < maxLimit);
162     }
163     
164     /**
165      * Notification when an object is put back into the pool (checkin).
166      * @param The object to be returned back to the pool.
167      * @return Any non null value can be returned to signal that the object
168      * was indeed added to the pool. This class always adds the object to the
169      * pool (at the end of the list), it returns non-null value.
170      * Subclasses can override this behaviour.
171      */

172     protected Object JavaDoc checkin(Object JavaDoc object) {
173         list.addAsLastNode(new TimeStampedDListNode(object, _clock.getTime()));
174         return this;
175     }
176                 
177     private Object JavaDoc obtainObject(Object JavaDoc param) {
178         SoftReference JavaDoc ref;
179         Object JavaDoc object;
180         
181         TimeStampedDListNode tsNode = (TimeStampedDListNode) list.delinkLastNode();
182         return tsNode.object;
183     }
184                 
185     /**
186      * Notification when an object is given out from the pool (checout).
187      * @return The object that has to be returned to the application. A null
188      * value must be returned if no object can be returned to the application. Since this
189      * class always returns the last node from the list, it returns non-null value.
190      * Subclasses can override this behaviour.
191      */

192     protected Object JavaDoc checkout(Object JavaDoc param) {
193         return obtainObject(param);
194     }
195     
196   
197     
198     //Methods required for periodically schedulable task
199

200     public void prolog() {
201     }
202     
203     public void service() {
204         int killedCount = 0;
205         
206         long now = _clock.getTime();
207         long allowed = now - maxIdleTime;
208         
209         TimeStampedDListNode tsNode = null;
210         FastStack stack = new FastStack();
211         
212         synchronized (super.collection) {
213             Object JavaDoc done = null;
214             while (done == null) {
215                 tsNode = (TimeStampedDListNode) list.getFirstDListNode();
216                 
217                 if (tsNode == null) { //Empty list
218
done = new Object JavaDoc();
219                 } else if (tsNode.timeStamp <= allowed) {
220                     //Need to destroy the contained object
221
list.delink(tsNode);
222                     stack.push(tsNode.object);
223                     killedCount++;
224                 } else {
225                     //This node is not old enough
226
done = new Object JavaDoc();
227                 }
228             } //End of for loop
229

230             super.createdCount -= killedCount;
231             
232             if (createdCount < minSize) {
233                 super.preload(minSize - createdCount);
234             }
235             
236             if (waitCount > 0) {
237                 if (killedCount == 1) {
238                     collection.notify();
239                 } else {
240                     collection.notifyAll();
241                 }
242             }
243             
244         } // end of synchronized
245

246         
247         //Now destroy all collected objects
248
while (! stack.isEmpty()) {
249             Object JavaDoc object = stack.pop();
250             beforeDestroy(object);
251             factory.destroy(object);
252         }
253         
254 //Bug 4677074 System.out.println("Leaving service after killing " + killedCount + " (idle) objects. Now size: " + list.size());
255
//Bug 4677074 begin
256
_logger.log(Level.FINE,"Leaving service after killing " + killedCount + " (idle) objects. Now size: " + list.size());
257 //Bug 4677074 end
258
}
259     
260     public void epilog() {
261     }
262     
263     /**
264     * Get the frequency (time interval) at which service() method will be invoked.
265     * @return time in milli seconds.
266     */

267     public long getFrequency() {
268         return this.maxIdleTime;
269     }
270     
271     /**
272     * Determine to execute the service method of this object even if it has
273     * missed the right schedule.
274     */

275     public boolean getExecuteIfMissed() {
276         return true;
277     }
278     
279     /**
280     * Determine to execute the service method of this object when the
281     * schedule is delayed by 'missedByMillis' milli seconds.
282     */

283     public boolean getExecutionTolerance(long missedByMillis) {
284         return true;
285     }
286     
287     /**
288     * Print an identification for the object.
289     */

290     public String JavaDoc toString() {
291         return "";
292     }
293     
294    //Some helper classes
295

296     class TimeStampedDListNode
297         extends DListNode
298     {
299         long timeStamp;
300         
301         public TimeStampedDListNode(Object JavaDoc object, long ts) {
302             super(object);
303             this.timeStamp = ts;
304                 
305             //System.out.println(this + ": created DListNode at: " + ts);
306
//Bug 4677074 begin
307
//_logger.log(Level.FINE,this + ": created DListNode at: " + ts);
308
//Bug 4677074 end
309
}
310         
311         public String JavaDoc toString() {
312             return "TSDListNode: " + object;
313         }
314                     
315         
316     }
317     
318     
319 }
320
Popular Tags