KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > pool > BasicObjectPool


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.util.pool;
16
17 import java.util.*;
18 import org.apache.commons.pool.*;
19 import org.apache.commons.pool.impl.*;
20 import java.util.logging.*;
21
22 /**
23  * This class will maintain a simple pool of object instances.
24  * It internally used a <code>HashSet</code>
25  * @author Akshathkumar Shetty
26  * @since 1.3
27  */

28 public class BasicObjectPool implements QSObjectPool {
29     private static final Logger logger =
30             Logger.getLogger(BasicObjectPool.class.getName());
31
32     private PoolableObjectFactory factory;
33     private Config config;
34     private Set activeObjects, idleObjects;
35     private volatile boolean inMaintain = false;
36
37     public BasicObjectPool() {
38         activeObjects = Collections.synchronizedSet(new HashSet());
39         idleObjects = Collections.synchronizedSet(new HashSet());
40         config = new Config();
41     }
42     public BasicObjectPool(PoolableObjectFactory factory,
43             BasicObjectPool.Config config) {
44         this();
45         this.factory = factory;
46         if(config!=null) this.config = config;
47     }
48
49     public void addObject() throws Exception JavaDoc {
50         if(config.maxIdle==-1 || config.maxIdle > getNumIdle())
51             idleObjects.add(factory.makeObject());
52         else
53             maintain();
54     }
55     
56     public Object JavaDoc borrowObject() throws Exception JavaDoc {
57         if(getNumIdle()<=0 &&
58             (config.maxActive==-1 || config.maxActive > getNumActive()) ) {
59             addObject();
60         }
61         if(getNumIdle()<=0) {
62             throw new NoSuchElementException("No free objects! MaxActive:"+
63                 config.maxActive+", NumActive:"+getNumActive());
64         }
65         
66         Object JavaDoc obj = null;
67         synchronized(this) {
68             obj = idleObjects.iterator().next();
69             idleObjects.remove(obj);
70             factory.activateObject(obj);
71             activeObjects.add(obj);
72         }
73         return obj;
74     }
75
76     /**Clears any objects sitting idle in the pool*/
77     public synchronized void clear() {
78         Iterator iterator = idleObjects.iterator();
79         while(iterator.hasNext()) {
80             try {
81                 invalidateObject(iterator.next());
82             } catch(Exception JavaDoc e) {
83                 logger.warning("Error in BasicObjectPool.clear : "+e);
84             }
85         }
86         idleObjects.clear();
87     }
88
89     /**Close this pool, and free any resources associated with it.*/
90     public void close() throws Exception JavaDoc {
91         clear();
92         /*
93         Iterator iterator = activeObjects.iterator();
94         while(iterator.hasNext()) {
95             try {
96                 invalidateObject(iterator.next());
97             } catch(Exception e) {
98                 logger.warning("Error in BasicObjectPool.close : "+e);
99             }
100         }
101         */

102         activeObjects.clear();
103     }
104
105     /**Return the number of instances currently borrowed from my pool */
106     public int getNumActive() {
107         return activeObjects.size();
108     }
109     /**Return the number of instances currently idle in my pool */
110     public int getNumIdle() {
111         return idleObjects.size();
112     }
113     
114     /**Invalidates an object from the pool */
115     public void invalidateObject(Object JavaDoc obj) throws Exception JavaDoc {
116         factory.destroyObject(obj);
117     }
118
119     /**Return an instance to my pool*/
120     public synchronized void returnObject(Object JavaDoc obj) throws Exception JavaDoc {
121         activeObjects.remove(obj);
122         if(factory.validateObject(obj)==false) {
123             logger.finer("Object not good for return: "+obj);
124             return;
125         }
126         factory.passivateObject(obj);
127         idleObjects.add(obj);
128         if(config.maxIdle!=-1 && config.maxIdle < getNumIdle()) {
129             maintain();
130         }
131     }
132     
133     /**Sets the factory I use to create new instances */
134     public void setFactory(PoolableObjectFactory factory) {
135         this.factory = factory;
136     }
137
138     private void maintain() {
139         if(inMaintain==true) {
140             return;
141         }
142         inMaintain = true;
143         logger.finest("Starting maintain: "+getNumIdle());
144         while(getNumIdle()>config.maxIdle) {
145             try {
146                 synchronized(idleObjects) {
147                     Object JavaDoc obj = idleObjects.iterator().next();
148                     idleObjects.remove(obj);
149                     invalidateObject(obj);
150                 }
151             } catch(Exception JavaDoc e) {
152                 logger.warning("Error in BasicObjectPool.maintain : "+e);
153             }
154         }
155         inMaintain = false;
156         logger.finest("Finished maintain: "+getNumIdle());
157     }
158
159     public static class Config {
160         public int maxActive = -1;
161         public int maxIdle = 10;
162     }
163
164     /**
165      * Returns the iterator of all active objects
166      * @since 1.3.1
167      */

168     public Iterator getAllActiveObjects() {
169         List _list = new LinkedList();
170         _list.addAll(activeObjects);
171         return _list.iterator(); //*/activeObjects.iterator();
172
}
173
174     public Object JavaDoc getObjectToSynchronize() {
175         return activeObjects;
176     }
177 }
178
Popular Tags