KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 /**
21  * This class that creates QSObjectPool from ObjectPool passed to it.
22  * @since 1.4.5
23  */

24 public class MakeQSObjectPool implements QSObjectPool, QSObjectPoolMaker {
25     protected ObjectPool objectPool = null;
26     protected List list = null;
27
28     public QSObjectPool getQSObjectPool(ObjectPool objectPool) {
29         return new MakeQSObjectPool(objectPool);
30     }
31
32     public MakeQSObjectPool() {
33     }
34
35     public MakeQSObjectPool(ObjectPool objectPool) {
36         setObjectPool(objectPool);
37     }
38
39     protected void setObjectPool(ObjectPool objectPool) {
40         this.objectPool = objectPool;
41         list = Collections.synchronizedList(new LinkedList());
42     }
43
44     public void returnObject(Object JavaDoc obj) throws Exception JavaDoc {
45         list.remove(obj);
46         objectPool.returnObject(obj);
47     }
48
49     public Object JavaDoc borrowObject() throws Exception JavaDoc {
50         Object JavaDoc obj = objectPool.borrowObject();
51         list.add(obj);
52         return obj;
53     }
54
55     public synchronized void close() throws Exception JavaDoc {
56         list.clear();
57         objectPool.close();
58     }
59
60     /**
61      * Returns the iterator of all active objects
62      */

63     public Iterator getAllActiveObjects() {
64         return list.iterator();
65     }
66
67     public Object JavaDoc getObjectToSynchronize() {
68         return list;
69     }
70
71     public void addObject() throws Exception JavaDoc {
72         objectPool.addObject();
73     }
74
75     public void clear() throws Exception JavaDoc {
76         objectPool.clear();
77     }
78
79     public int getNumActive() {
80         return objectPool.getNumActive();
81     }
82
83     public int getNumIdle() {
84         return objectPool.getNumIdle();
85     }
86
87     public void invalidateObject(Object JavaDoc obj) throws Exception JavaDoc {
88         objectPool.invalidateObject(obj);
89     }
90
91     public void setFactory(PoolableObjectFactory factory) {
92         objectPool.setFactory(factory);
93     }
94 }
95
Popular Tags