1 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 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 { 50 if(config.maxIdle==-1 || config.maxIdle > getNumIdle()) 51 idleObjects.add(factory.makeObject()); 52 else 53 maintain(); 54 } 55 56 public Object borrowObject() throws Exception { 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 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 77 public synchronized void clear() { 78 Iterator iterator = idleObjects.iterator(); 79 while(iterator.hasNext()) { 80 try { 81 invalidateObject(iterator.next()); 82 } catch(Exception e) { 83 logger.warning("Error in BasicObjectPool.clear : "+e); 84 } 85 } 86 idleObjects.clear(); 87 } 88 89 90 public void close() throws Exception { 91 clear(); 92 102 activeObjects.clear(); 103 } 104 105 106 public int getNumActive() { 107 return activeObjects.size(); 108 } 109 110 public int getNumIdle() { 111 return idleObjects.size(); 112 } 113 114 115 public void invalidateObject(Object obj) throws Exception { 116 factory.destroyObject(obj); 117 } 118 119 120 public synchronized void returnObject(Object obj) throws Exception { 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 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 obj = idleObjects.iterator().next(); 148 idleObjects.remove(obj); 149 invalidateObject(obj); 150 } 151 } catch(Exception 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 168 public Iterator getAllActiveObjects() { 169 List _list = new LinkedList(); 170 _list.addAll(activeObjects); 171 return _list.iterator(); } 173 174 public Object getObjectToSynchronize() { 175 return activeObjects; 176 } 177 } 178 | Popular Tags |