1 50 package org.apache.avalon.excalibur.pool; 51 52 import java.util.ArrayList ; 53 import java.util.List ; 54 55 import org.apache.avalon.framework.activity.Initializable; 56 import org.apache.avalon.framework.logger.AbstractLogEnabled; 57 import org.apache.avalon.framework.thread.ThreadSafe; 58 import org.apache.commons.collections.Buffer; 59 import org.apache.commons.collections.UnboundedFifoBuffer; 60 61 68 public abstract class AbstractPool 69 extends AbstractLogEnabled 70 implements Pool, ThreadSafe 71 { 72 public static final int DEFAULT_POOL_SIZE = 8; 73 protected final ObjectFactory m_factory; 74 protected List m_active = new ArrayList (); 75 protected Buffer m_ready = new UnboundedFifoBuffer(); 76 protected Mutex m_mutex = new Mutex(); 77 protected boolean m_initialized = false; 78 protected int m_min; 79 80 84 public AbstractPool( final ObjectFactory factory ) throws Exception 85 { 86 m_factory = factory; 87 88 if( !( this instanceof Initializable ) ) 89 { 90 initialize(); 91 } 92 } 93 94 protected void initialize() 95 throws Exception 96 { 97 lock(); 98 99 for( int i = 0; i < AbstractPool.DEFAULT_POOL_SIZE; i++ ) 100 { 101 this.m_ready.add( this.newPoolable() ); 102 } 103 104 m_initialized = true; 105 106 unlock(); 107 } 108 109 protected final void lock() 110 throws InterruptedException 111 { 112 m_mutex.acquire(); 113 } 114 115 protected final void unlock() 116 throws InterruptedException 117 { 118 m_mutex.release(); 119 } 120 121 125 protected Poolable newPoolable() throws Exception 126 { 127 Object obj = m_factory.newInstance(); 128 return (Poolable)obj; 129 } 130 131 135 protected void removePoolable( Poolable poolable ) 136 { 137 try 138 { 139 m_factory.decommission( poolable ); 140 } 141 catch( Exception e ) 142 { 143 if( getLogger().isDebugEnabled() ) 144 { 145 getLogger().debug( "Error decommissioning object", e ); 146 } 147 } 148 } 149 150 public final int size() 151 { 152 synchronized( this ) 153 { 154 return this.m_active.size() + this.m_ready.size(); 158 } 159 } 160 161 public abstract Poolable get() throws Exception ; 162 163 public abstract void put( Poolable object ); 164 165 protected void internalGrow( final int amount ) 166 throws Exception 167 { 168 for( int i = 0; i < amount; i++ ) 169 { 170 try 171 { 172 m_ready.add( newPoolable() ); 173 } 174 catch( final Exception e ) 175 { 176 if( null != getLogger() && getLogger().isDebugEnabled() ) 177 { 178 Class createdClass = m_factory.getCreatedClass(); 179 if( createdClass == null ) 180 { 181 getLogger().debug( "factory created class was null so a new " 182 + "instance could not be created.", e ); 183 } 184 else 185 { 186 getLogger().debug( createdClass.getName() + 187 ": could not be instantiated.", e ); 188 } 189 } 190 191 throw e; 192 } 193 } 194 } 195 196 protected void internalShrink( final int amount ) 197 throws Exception 198 { 199 for( int i = 0; i < amount; i++ ) 200 { 201 if( m_ready.size() > m_min ) 202 { 203 try 204 { 205 this.removePoolable( (Poolable)m_ready.remove() ); 206 } 207 catch( final Exception e ) 208 { 209 if( null != getLogger() && getLogger().isDebugEnabled() ) 210 { 211 getLogger().debug( m_factory.getCreatedClass().getName() + 212 ": improperly decommissioned.", e ); 213 } 214 } 215 } 216 } 217 } 218 } 219 | Popular Tags |