1 50 package org.apache.avalon.excalibur.pool; 51 52 import org.apache.avalon.framework.activity.Disposable; 53 54 64 public class DefaultPool 65 extends AbstractPool 66 implements Disposable 67 { 68 protected int m_min; 69 protected int m_max; 70 protected PoolController m_controller; 71 protected boolean m_disposed = false; 72 protected boolean m_quickFail = false; 73 74 public DefaultPool( final ObjectFactory factory, 75 final PoolController controller ) 76 throws Exception 77 { 78 this( factory, controller, AbstractPool.DEFAULT_POOL_SIZE, AbstractPool.DEFAULT_POOL_SIZE ); 79 } 80 81 public DefaultPool( final ObjectFactory factory, 82 final PoolController controller, 83 final int initial, 84 final int maximum ) 85 throws Exception 86 { 87 super( factory ); 88 89 int t_max = maximum; 90 int t_min = initial; 91 92 if( t_min < 0 ) 93 { 94 if( null != getLogger() && getLogger().isWarnEnabled() ) 95 { 96 getLogger().warn( "Minumum number of poolables specified is " + 97 "less than 0, using 0" ); 98 } 99 100 t_min = 0; 101 } 102 103 if( ( t_max < t_min ) || ( t_max < 1 ) ) 104 { 105 if( null != getLogger() && getLogger().isWarnEnabled() ) 106 { 107 getLogger().warn( "Maximum number of poolables specified must be at " + 108 "least 1 and must be greater than the minumum number " + 109 "of connections" ); 110 } 111 t_max = ( t_min > 1 ) ? t_min : 1; 112 } 113 114 m_max = t_max; 115 m_min = t_min; 116 117 if( null != controller ) 118 { 119 m_controller = controller; 120 } 121 else 122 { 123 m_controller = new DefaultPoolController( t_min / 2 ); 124 } 125 } 126 127 public DefaultPool( final ObjectFactory factory ) 128 throws Exception 129 { 130 this( factory, null, AbstractPool.DEFAULT_POOL_SIZE, AbstractPool.DEFAULT_POOL_SIZE ); 131 } 132 133 public DefaultPool( final Class clazz, final int initial, final int maximum ) 134 throws NoSuchMethodException , Exception 135 { 136 this( new DefaultObjectFactory( clazz ), null, initial, maximum ); 137 } 138 139 public DefaultPool( final Class clazz, final int initial ) 140 throws NoSuchMethodException , Exception 141 { 142 this( clazz, initial, initial ); 143 } 144 145 public Poolable get() throws Exception 146 { 147 Poolable obj = null; 148 149 if( !m_initialized ) 150 { 151 throw new IllegalStateException ( "You cannot get a Poolable before the pool is initialized" ); 152 } 153 154 if( m_disposed ) 155 { 156 throw new IllegalStateException ( "You cannot get a Poolable after the pool is disposed" ); 157 } 158 159 m_mutex.acquire(); 160 try 161 { 162 if( m_ready.size() == 0 ) 163 { 164 if( this instanceof Resizable ) 165 { 166 this.internalGrow( m_controller.grow() ); 167 168 if( m_ready.size() > 0 ) 169 { 170 obj = (Poolable)m_ready.remove(); 171 } 172 else 173 { 174 final String message = 175 "Could not create enough Components to service " + 176 "your request."; 177 throw new Exception ( message ); 178 } 179 } 180 else 181 { 182 obj = newPoolable(); 183 } 184 } 185 else 186 { 187 obj = (Poolable)m_ready.remove(); 188 } 189 190 m_active.add( obj ); 191 192 if( getLogger().isDebugEnabled() ) 193 { 194 final String message = "Retrieving a " + 195 m_factory.getCreatedClass().getName() + " from the pool"; 196 getLogger().debug( message ); 197 } 198 return obj; 199 } 200 finally 201 { 202 m_mutex.release(); 203 } 204 } 205 206 public void put( final Poolable obj ) 207 { 208 if( !m_initialized ) 209 { 210 final String message = "You cannot get a Poolable before " + 211 "the pool is initialized"; 212 throw new IllegalStateException ( message ); 213 } 214 215 try 216 { 217 if( obj instanceof Recyclable ) 218 { 219 ( (Recyclable)obj ).recycle(); 220 } 221 222 m_mutex.acquire(); 223 try 224 { 225 m_active.remove( m_active.indexOf( obj ) ); 226 227 if( getLogger().isDebugEnabled() ) 228 { 229 final String message = 230 "Returning a " + m_factory.getCreatedClass().getName() + 231 " to the pool"; 232 getLogger().debug( message ); 233 } 234 235 if( m_disposed == false ) 236 { 237 m_ready.add( obj ); 238 239 if( ( this.size() > m_max ) && ( this instanceof Resizable ) ) 240 { 241 this.internalShrink( m_controller.shrink() ); 242 } 243 } 244 else 245 { 246 this.removePoolable( obj ); 247 } 248 } 249 finally 250 { 251 m_mutex.release(); 252 } 253 } 254 catch( Exception e ) 255 { 256 if( getLogger().isWarnEnabled() ) 257 { 258 getLogger().warn( "Pool interrupted while waiting for lock.", e ); 259 } 260 } 261 } 262 263 public final void dispose() 264 { 265 try 266 { 267 m_mutex.acquire(); 268 try 269 { 270 while( m_ready.size() > 0 ) 271 { 272 this.removePoolable( (Poolable)m_ready.remove() ); 273 } 274 } 275 finally 276 { 277 m_mutex.release(); 278 } 279 } 280 catch( Exception e ) 281 { 282 if( getLogger().isWarnEnabled() ) 283 { 284 getLogger().warn( "Caught an exception disposing of pool", e ); 285 } 286 } 287 288 this.m_disposed = true; 289 } 290 } 291 | Popular Tags |