1 17 package org.apache.excalibur.mpool; 18 19 import org.apache.avalon.framework.activity.Disposable; 20 import org.apache.avalon.framework.activity.Initializable; 21 import org.apache.commons.collections.BoundedFifoBuffer; 22 import org.apache.commons.collections.Buffer; 23 import org.apache.commons.collections.BufferUnderflowException; 24 25 33 public final class BlockingFixedSizePool 34 implements Pool, Disposable, Initializable 35 { 36 private boolean m_disposed = false; 37 private final Buffer m_buffer; 38 private final ObjectFactory m_factory; 39 private final long m_timeout; 40 private final int m_maxSize; 41 42 43 protected final Object m_semaphore = new Object (); 44 45 public BlockingFixedSizePool( ObjectFactory factory, int size ) 46 throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 47 { 48 this( factory, size, 1000 ); 49 } 50 51 public BlockingFixedSizePool( ObjectFactory factory, int size, long timeout ) 52 throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 53 { 54 m_timeout = ( timeout < 1 ) ? 0 : timeout; 55 m_buffer = new BoundedFifoBuffer( size ); 56 m_maxSize = size; 57 m_factory = factory; 58 } 59 60 public void initialize() 61 throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 62 { 63 for( int i = 0; i < m_maxSize; i++ ) 64 { 65 m_buffer.add( newInstance() ); 66 } 67 } 68 69 public Object acquire() 70 { 71 if( m_disposed ) 72 { 73 throw new IllegalStateException ( "Cannot get an object from a disposed pool" ); 74 } 75 76 Object object = null; 77 78 synchronized( m_semaphore ) 79 { 80 if( m_buffer.isEmpty() ) 81 { 82 long blockStart = System.currentTimeMillis(); 83 84 if( m_timeout > 0 ) 85 { 86 long blockWait = m_timeout; 87 88 do 89 { 90 try 91 { 92 m_semaphore.wait( blockWait ); 93 } 94 catch( InterruptedException ie ) 95 { 96 } 97 98 if( m_disposed ) 99 { 100 throw new IllegalStateException ( "Pool disposed of while waiting for resources to free up" ); 101 } 102 103 if( m_buffer.isEmpty() ) 104 { 105 blockWait = m_timeout - 106 ( System.currentTimeMillis() - blockStart ); 107 } 108 } while( m_buffer.isEmpty() && blockWait > 0 ); 109 } 110 else 111 { 112 do 113 { 114 try 115 { 116 m_semaphore.wait(); 117 } 118 catch( InterruptedException ie ) 119 { 120 } 121 122 if( m_disposed ) 123 { 124 throw new IllegalStateException ( "Pool disposed of while waiting for resources to free up" ); 125 } 126 } while( m_buffer.isEmpty() ); 127 } 128 } 129 130 try 131 { 132 object = m_buffer.remove(); 133 } 134 catch( BufferUnderflowException bufe ) 135 { 136 } 138 } 139 140 if( object == null ) 141 { 142 throw new IllegalStateException ( "Timeout exceeded without acquiring resource." ); 143 } 144 145 return object; 146 } 147 148 public void release( Object object ) 149 { 150 synchronized( m_semaphore ) 151 { 152 if( m_disposed ) 153 { 154 try 155 { 156 m_factory.dispose( object ); 157 } 158 catch( Exception e ) 159 { 160 } 162 } 163 else 164 { 165 if( m_buffer.size() < m_maxSize ) 166 { 167 m_buffer.add( PoolUtil.recycle( object ) ); 168 m_semaphore.notify(); 169 } 170 else 171 { 172 try 173 { 174 m_factory.dispose( object ); 175 } 176 catch( Exception e ) 177 { 178 } 180 } 181 } 182 } 183 } 184 185 public Object newInstance() 186 throws Exception ![JavaDoc](../../../../../cmn/javadoc.gif) 187 { 188 return m_factory.newInstance(); 189 } 190 191 public void dispose() 192 { 193 m_disposed = true; 194 195 synchronized( m_semaphore ) 196 { 197 while( !m_buffer.isEmpty() ) 198 { 199 try 200 { 201 m_factory.dispose( m_buffer.remove() ); 202 } 203 catch( Exception e ) 204 { 205 } 207 } 208 209 m_semaphore.notifyAll(); 210 } 211 } 212 } 213 214 | Popular Tags |