1 16 17 package org.apache.commons.pool.impl; 18 19 import java.lang.ref.SoftReference ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.NoSuchElementException ; 24 25 import org.apache.commons.pool.BaseObjectPool; 26 import org.apache.commons.pool.ObjectPool; 27 import org.apache.commons.pool.PoolableObjectFactory; 28 29 36 public class SoftReferenceObjectPool extends BaseObjectPool implements ObjectPool { 37 public SoftReferenceObjectPool() { 38 _pool = new ArrayList (); 39 _factory = null; 40 } 41 42 public SoftReferenceObjectPool(PoolableObjectFactory factory) { 43 _pool = new ArrayList (); 44 _factory = factory; 45 } 46 47 public SoftReferenceObjectPool(PoolableObjectFactory factory, int initSize) throws Exception { 48 _pool = new ArrayList (); 49 _factory = factory; 50 if(null != _factory) { 51 for(int i=0;i<initSize;i++) { 52 Object obj = _factory.makeObject(); 53 _factory.passivateObject(obj); 54 _pool.add(new SoftReference (obj)); 55 } 56 } 57 } 58 59 public synchronized Object borrowObject() throws Exception { 60 assertOpen(); 61 Object obj = null; 62 while(null == obj) { 63 if(_pool.isEmpty()) { 64 if(null == _factory) { 65 throw new NoSuchElementException (); 66 } else { 67 obj = _factory.makeObject(); 68 } 69 } else { 70 SoftReference ref = (SoftReference )(_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 obj) throws Exception { 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 e) { 90 success = false; 91 } 92 } 93 94 boolean shouldDestroy = !success; 95 synchronized(this) { 96 _numActive--; 97 if(success) { 98 _pool.add(new SoftReference (obj)); 99 } 100 notifyAll(); } 102 103 if(shouldDestroy) { 104 try { 105 _factory.destroyObject(obj); 106 } catch(Exception e) { 107 } 109 } 110 111 } 112 113 public synchronized void invalidateObject(Object obj) throws Exception { 114 assertOpen(); 115 _numActive--; 116 _factory.destroyObject(obj); 117 notifyAll(); } 119 120 124 public void addObject() throws Exception { 125 Object obj = _factory.makeObject(); 126 synchronized(this) { 127 _numActive++; this.returnObject(obj); 129 } 130 } 131 132 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 iter = _pool.iterator(); 145 while(iter.hasNext()) { 146 try { 147 Object obj = ((SoftReference )iter.next()).get(); 148 if(null != obj) { 149 _factory.destroyObject(obj); 150 } 151 } catch(Exception e) { 152 } 154 } 155 } 156 _pool.clear(); 157 } 158 159 synchronized public void close() throws Exception { 160 clear(); 161 _pool = null; 162 _factory = null; 163 super.close(); 164 } 165 166 synchronized public void setFactory(PoolableObjectFactory factory) throws IllegalStateException { 167 assertOpen(); 168 if(0 < getNumActive()) { 169 throw new IllegalStateException ("Objects are already active"); 170 } else { 171 clear(); 172 _factory = factory; 173 } 174 } 175 176 177 private List _pool = null; 178 179 180 private PoolableObjectFactory _factory = null; 181 182 183 private int _numActive = 0; 184 } 185 | Popular Tags |