1 10 11 package org.mule.config.pool; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.apache.commons.pool.PoolableObjectFactory; 16 import org.apache.commons.pool.impl.GenericObjectPool; 17 import org.mule.impl.MuleDescriptor; 18 import org.mule.umo.UMOException; 19 import org.mule.umo.lifecycle.Disposable; 20 import org.mule.umo.lifecycle.Startable; 21 import org.mule.umo.lifecycle.Stoppable; 22 import org.mule.util.ObjectFactory; 23 import org.mule.util.ObjectPool; 24 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 33 public class CommonsPoolProxyPool implements ObjectPool 34 { 35 38 protected static final Log logger = LogFactory.getLog(CommonsPoolProxyPool.class); 39 40 43 protected GenericObjectPool pool; 44 45 48 protected ObjectFactory factory; 49 50 private List components; 51 52 58 public CommonsPoolProxyPool(MuleDescriptor descriptor, ObjectFactory factory) 59 { 60 this.factory = factory; 61 62 GenericObjectPool.Config config = new GenericObjectPool.Config(); 63 config.maxIdle = descriptor.getPoolingProfile().getMaxIdle(); 64 config.maxActive = descriptor.getPoolingProfile().getMaxActive(); 65 config.maxWait = descriptor.getPoolingProfile().getMaxWait(); 66 config.whenExhaustedAction = (byte)descriptor.getPoolingProfile().getExhaustedAction(); 67 68 init(descriptor, config); 69 } 70 71 75 public CommonsPoolProxyPool(MuleDescriptor descriptor, GenericObjectPool.Config config) 76 { 77 init(descriptor, config); 78 } 79 80 84 private void init(MuleDescriptor descriptor, GenericObjectPool.Config config) 85 { 86 components = new ArrayList (); 87 88 if (factory == null) 89 { 90 setFactory(new CommonsPoolProxyFactory(descriptor)); 91 } 92 93 pool = new GenericObjectPool((PoolableObjectFactory)factory, config); 94 95 if (factory instanceof CommonsPoolProxyFactory) 96 { 97 ((CommonsPoolProxyFactory)factory).setPool(this); 98 } 99 } 100 101 106 public Object borrowObject() throws Exception 107 { 108 return pool.borrowObject(); 109 } 110 111 116 public void returnObject(Object object) throws Exception 117 { 118 pool.returnObject(object); 119 } 120 121 126 public int getSize() 127 { 128 return pool.getNumActive(); 129 } 130 131 136 public int getMaxSize() 137 { 138 return pool.getMaxActive(); 139 } 140 141 146 public void setFactory(ObjectFactory factory) 147 { 148 this.factory = factory; 149 } 150 151 156 public void clearPool() 157 { 158 synchronized (components) 159 { 160 for (Iterator i = components.iterator(); i.hasNext();) 161 { 162 ((Disposable)i.next()).dispose(); 163 } 164 components.clear(); 165 } 166 pool.clear(); 167 } 168 169 public void onAdd(Object proxy) 170 { 171 synchronized (components) 172 { 173 components.add(proxy); 174 } 175 } 176 177 public void onRemove(Object proxy) 178 { 179 synchronized (components) 180 { 181 final boolean wasRemoved = components.remove(proxy); 182 if (wasRemoved) 183 { 184 ((Disposable)proxy).dispose(); 185 } 186 } 187 } 188 189 public void start() throws UMOException 190 { 191 synchronized (components) 192 { 193 for (Iterator i = components.iterator(); i.hasNext();) 194 { 195 ((Startable)i.next()).start(); 196 } 197 } 198 } 199 200 public void stop() throws UMOException 201 { 202 synchronized (components) 203 { 204 for (Iterator i = components.iterator(); i.hasNext();) 205 { 206 ((Stoppable)i.next()).stop(); 207 } 208 } 209 } 210 211 } 212 | Popular Tags |