KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > pool > impl > SoftReferenceObjectPool


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 package org.apache.commons.pool.impl;
18
19 import java.lang.ref.SoftReference JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24
25 import org.apache.commons.pool.BaseObjectPool;
26 import org.apache.commons.pool.ObjectPool;
27 import org.apache.commons.pool.PoolableObjectFactory;
28
29 /**
30  * A {@link java.lang.ref.SoftReference SoftReference} based
31  * {@link ObjectPool}.
32  *
33  * @author Rodney Waldhoff
34  * @version $Revision$ $Date: 2005-02-26 05:13:28 -0800 (Sat, 26 Feb 2005) $
35  */

36 public class SoftReferenceObjectPool extends BaseObjectPool implements ObjectPool {
37     public SoftReferenceObjectPool() {
38         _pool = new ArrayList JavaDoc();
39         _factory = null;
40     }
41
42     public SoftReferenceObjectPool(PoolableObjectFactory factory) {
43         _pool = new ArrayList JavaDoc();
44         _factory = factory;
45     }
46
47     public SoftReferenceObjectPool(PoolableObjectFactory factory, int initSize) throws Exception JavaDoc {
48         _pool = new ArrayList JavaDoc();
49         _factory = factory;
50         if(null != _factory) {
51             for(int i=0;i<initSize;i++) {
52                 Object JavaDoc obj = _factory.makeObject();
53                 _factory.passivateObject(obj);
54                 _pool.add(new SoftReference JavaDoc(obj));
55             }
56         }
57     }
58
59     public synchronized Object JavaDoc borrowObject() throws Exception JavaDoc {
60         assertOpen();
61         Object JavaDoc obj = null;
62         while(null == obj) {
63             if(_pool.isEmpty()) {
64                 if(null == _factory) {
65                     throw new NoSuchElementException JavaDoc();
66                 } else {
67                     obj = _factory.makeObject();
68                 }
69             } else {
70                 SoftReference JavaDoc ref = (SoftReference JavaDoc)(_pool.remove(_pool.size() - 1));
71                 obj = ref.get();
72             }
73         }
74         if(null != _factory && null != obj) {
75             _factory.activateObject(obj);
76         }
77         _numActive++;
78         return obj;
79     }
80
81     public void returnObject(Object JavaDoc obj) throws Exception JavaDoc {
82         assertOpen();
83         boolean success = true;
84         if(!(_factory.validateObject(obj))) {
85             success = false;
86         } else {
87             try {
88                 _factory.passivateObject(obj);
89             } catch(Exception JavaDoc e) {
90                 success = false;
91             }
92         }
93
94         boolean shouldDestroy = !success;
95         synchronized(this) {
96             _numActive--;
97             if(success) {
98                 _pool.add(new SoftReference JavaDoc(obj));
99             }
100             notifyAll(); // _numActive has changed
101
}
102
103         if(shouldDestroy) {
104             try {
105                 _factory.destroyObject(obj);
106             } catch(Exception JavaDoc e) {
107                 // ignored
108
}
109         }
110
111     }
112
113     public synchronized void invalidateObject(Object JavaDoc obj) throws Exception JavaDoc {
114         assertOpen();
115         _numActive--;
116         _factory.destroyObject(obj);
117         notifyAll(); // _numActive has changed
118
}
119
120     /**
121      * Create an object, and place it into the pool.
122      * addObject() is useful for "pre-loading" a pool with idle objects.
123      */

124     public void addObject() throws Exception JavaDoc {
125         Object JavaDoc obj = _factory.makeObject();
126         synchronized(this) {
127             _numActive++; // A little slimy - must do this because returnObject decrements it.
128
this.returnObject(obj);
129         }
130     }
131
132     /** Returns an approximation not less than the of the number of idle instances in the pool. */
133     public int getNumIdle() {
134         return _pool.size();
135     }
136
137     public int getNumActive() {
138         return _numActive;
139     }
140
141     public synchronized void clear() {
142         assertOpen();
143         if(null != _factory) {
144             Iterator JavaDoc iter = _pool.iterator();
145             while(iter.hasNext()) {
146                 try {
147                     Object JavaDoc obj = ((SoftReference JavaDoc)iter.next()).get();
148                     if(null != obj) {
149                         _factory.destroyObject(obj);
150                     }
151                 } catch(Exception JavaDoc e) {
152                     // ignore error, keep destroying the rest
153
}
154             }
155         }
156         _pool.clear();
157     }
158
159     synchronized public void close() throws Exception JavaDoc {
160         clear();
161         _pool = null;
162         _factory = null;
163         super.close();
164     }
165
166     synchronized public void setFactory(PoolableObjectFactory factory) throws IllegalStateException JavaDoc {
167         assertOpen();
168         if(0 < getNumActive()) {
169             throw new IllegalStateException JavaDoc("Objects are already active");
170         } else {
171             clear();
172             _factory = factory;
173         }
174     }
175
176     /** My pool. */
177     private List JavaDoc _pool = null;
178
179     /** My {@link PoolableObjectFactory}. */
180     private PoolableObjectFactory _factory = null;
181
182     /** Number of active objects. */
183     private int _numActive = 0;
184 }
185
Popular Tags