1 50 package org.apache.avalon.excalibur.pool; 51 52 import org.apache.avalon.framework.activity.Disposable; 53 import org.apache.avalon.framework.activity.Initializable; 54 import org.apache.avalon.framework.logger.AbstractLogEnabled; 55 import org.apache.avalon.framework.thread.SingleThreaded; 56 57 66 public class SingleThreadedPool 67 extends AbstractLogEnabled 68 implements Pool, Initializable, SingleThreaded, Resizable, Disposable 69 { 70 protected boolean m_initialized; 71 protected int m_count; 72 protected Poolable[] m_pool; 73 protected ObjectFactory m_factory; 74 protected PoolController m_controller; 75 protected int m_maximum; 76 protected int m_initial; 77 78 public SingleThreadedPool( final Class clazz, 79 final int initial, 80 final int maximum ) throws Exception 81 { 82 this( new DefaultObjectFactory( clazz ), initial, maximum ); 83 } 84 85 public SingleThreadedPool( final ObjectFactory factory, 86 final int initial, 87 final int maximum ) throws Exception 88 { 89 this( factory, null, initial, maximum ); 90 } 91 92 public SingleThreadedPool( final ObjectFactory factory, 93 final PoolController controller, 94 final int initial, 95 final int maximum ) throws Exception 96 { 97 m_count = 0; 98 m_factory = factory; 99 m_controller = controller; 100 m_maximum = maximum; 101 m_initial = initial; 102 } 103 104 public void initialize() 105 throws Exception 106 { 107 m_initialized = true; 108 109 grow( m_maximum ); 110 fill( m_initial ); 111 } 112 113 118 public Poolable get() throws Exception 119 { 120 if( !m_initialized ) 122 { 123 initialize(); 124 } 125 126 if( null == m_pool && null != m_controller ) 127 { 128 final int increase = m_controller.grow(); 129 if( increase > 0 ) 130 { 131 grow( increase ); 132 } 133 } 134 135 if( 0 > m_count ) 136 { 137 m_count = -1; 138 return (Poolable)m_factory.newInstance(); 139 } 140 else if( 0 == m_count ) 141 { 142 m_count--; 143 return m_pool[ 0 ]; 144 } 145 146 final Poolable poolable = m_pool[ m_count ]; 147 m_pool[ m_count ] = null; 148 m_count--; 149 return poolable; 150 } 151 152 157 public void put( final Poolable poolable ) 158 { 159 if( poolable instanceof Recyclable ) 160 { 161 ( (Recyclable)poolable ).recycle(); 162 } 163 164 if( m_pool.length == ( m_count + 1 ) && null != m_controller ) 165 { 166 final int decrease = m_controller.shrink(); 167 if( decrease > 0 ) 168 { 169 shrink( decrease ); 170 } 171 } 172 173 if( m_pool.length > m_count + 1 ) 174 { 175 m_count++; 176 m_pool[ m_count ] = poolable; 177 } 178 else 179 { 180 try 181 { 182 m_factory.decommission( poolable ); 183 } 184 catch( Exception e ) 185 { 186 if( ( getLogger() != null ) && ( getLogger().isDebugEnabled() ) ) 188 { 189 getLogger().debug( "Error decommissioning object", e ); 190 } 191 } 192 } 193 } 194 195 200 public final int getCapacity() 201 { 202 return m_pool.length; 203 } 204 205 211 public final int getSize() 212 { 213 return m_count; 214 } 215 216 221 public final int size() 222 { 223 return getSize(); 224 } 225 226 229 public final void fill( final int fillSize ) throws Exception 230 { 231 final int size = Math.min( m_pool.length, fillSize ); 232 233 for( int i = m_count; i < size; i++ ) 234 { 235 m_pool[ i ] = (Poolable)m_factory.newInstance(); 236 } 237 238 m_count = size - 1; 239 } 240 241 244 public final void grow( final int increase ) 245 { 246 if( null == m_pool ) 247 { 248 m_pool = new Poolable[ increase ]; 249 return; 250 } 251 252 final Poolable[] poolables = new Poolable[ increase + m_pool.length ]; 253 System.arraycopy( m_pool, 0, poolables, 0, m_pool.length ); 254 m_pool = poolables; 255 } 256 257 260 public final void shrink( final int decrease ) 261 { 262 final Poolable[] poolables = new Poolable[ m_pool.length - decrease ]; 263 System.arraycopy( m_pool, 0, poolables, 0, poolables.length ); 264 m_pool = poolables; 265 } 266 267 270 public void dispose() 271 { 272 while( m_count > 0 ) 273 { 274 int i = m_count - 1; 275 try 276 { 277 m_factory.decommission( m_pool[ i ] ); 278 } 279 catch( Exception e ) 280 { 281 if( ( getLogger() != null ) && ( getLogger().isDebugEnabled() ) ) 283 { 284 getLogger().debug( "Error decommissioning object", e ); 285 } 286 } 287 m_pool[ i ] = null; 288 m_count--; 289 } 290 } 291 } 292 | Popular Tags |