1 17 package org.apache.excalibur.mpool; 18 19 import org.apache.avalon.framework.activity.Disposable; 20 import org.apache.commons.collections.Buffer; 21 import org.apache.commons.collections.UnboundedFifoBuffer; 22 23 31 public final class VariableSizePool 32 implements Pool, Disposable, ManagablePool 33 { 34 private boolean m_disposed = false; 35 private final Buffer m_buffer; 36 private final ObjectFactory m_factory; 37 private final long m_key; 38 39 42 public VariableSizePool( ObjectFactory factory, int size ) 43 throws Exception 44 { 45 this( factory, size, -1 ); 46 } 47 48 51 public VariableSizePool( ObjectFactory factory, int size, long key ) 52 throws Exception 53 { 54 m_buffer = new UnboundedFifoBuffer( size ); 55 m_factory = factory; 56 m_key = key; 57 58 synchronized( m_factory ) 59 { 60 for( int i = 0; i < size; i++ ) 61 { 62 m_buffer.add( newInstance() ); 63 } 64 } 65 } 66 67 public Object acquire() 68 throws Exception 69 { 70 if( m_disposed ) 71 { 72 throw new IllegalStateException ( "Cannot get an object from a disposed pool" ); 73 } 74 75 Object object = null; 76 77 synchronized( m_buffer ) 78 { 79 if( m_buffer.size() > 0 ) 80 { 81 object = m_buffer.remove(); 82 } 83 } 84 85 if( null == object ) 86 { 87 object = newInstance(); 88 } 89 90 return object; 91 } 92 93 public void release( Object pooledObject ) 94 { 95 if( m_disposed ) 96 { 97 try 98 { 99 m_factory.dispose( pooledObject ); 100 } 101 catch( Exception e ) 102 { 103 } 105 } 106 else 107 { 108 synchronized( m_buffer ) 109 { 110 m_buffer.add( PoolUtil.recycle( pooledObject ) ); 111 } 112 } 113 } 114 115 public Object newInstance() 116 throws Exception 117 { 118 synchronized( m_factory ) 119 { 120 return m_factory.newInstance(); 121 } 122 } 123 124 public void dispose() 125 { 126 m_disposed = true; 127 128 synchronized( m_buffer ) 129 { 130 while( !m_buffer.isEmpty() ) 131 { 132 try 133 { 134 m_factory.dispose( m_buffer.remove() ); 135 } 136 catch( Exception e ) 137 { 138 } 140 } 141 } 142 } 143 144 public void shrink( final int byNum, final long key ) 145 throws IllegalAccessException 146 { 147 if( m_key < 0 || m_key != key ) 148 { 149 throw new IllegalAccessException (); 150 } 151 152 synchronized( m_buffer ) 153 { 154 final int num = Math.min( byNum, m_buffer.size() ); 155 156 for( int i = 0; i < num; i++ ) 157 { 158 try 159 { 160 m_factory.dispose( m_buffer.remove() ); 161 } 162 catch( Exception e ) 163 { 164 } 166 } 167 } 168 } 169 170 public void grow( final int byNum, final long key ) 171 throws IllegalAccessException 172 { 173 if( m_key < 0 || m_key != key ) 174 { 175 throw new IllegalAccessException (); 176 } 177 178 synchronized( m_buffer ) 179 { 180 for( int i = 0; i < byNum; i++ ) 181 { 182 try 183 { 184 m_buffer.add( newInstance() ); 185 } 186 catch( Exception e ) 187 { 188 } 190 } 191 } 192 } 193 194 public int size( final long key ) 195 throws IllegalAccessException 196 { 197 if( m_key < 0 || m_key != key ) 198 { 199 throw new IllegalAccessException (); 200 } 201 202 synchronized( m_buffer ) 203 { 204 return m_buffer.size(); 205 } 206 } 207 } 208 | Popular Tags |