KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > pool > SingleThreadedPool


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6  
7  Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11  
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18  
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24  
25  4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
26     must not be used to endorse or promote products derived from this software
27     without prior written permission. For written permission, please contact
28     apache@apache.org.
29  
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33  
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48  
49 */

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 /**
58  * This is an <code>Pool</code> that caches Poolable objects for reuse.
59  *
60  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
61  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
62  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
63  * @version CVS $Revision: 1.2 $ $Date: 2003/02/20 17:09:24 $
64  * @since 4.0
65  */

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 JavaDoc clazz,
79                                final int initial,
80                                final int maximum ) throws Exception JavaDoc
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 JavaDoc
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 JavaDoc
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 JavaDoc
106     {
107         m_initialized = true;
108
109         grow( m_maximum );
110         fill( m_initial );
111     }
112
113     /**
114      * Retrieve an object from pool.
115      *
116      * @return an object from Pool
117      */

118     public Poolable get() throws Exception JavaDoc
119     {
120         // To make this class backwards compatible, it has to auto initialize if necessary
121
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     /**
153      * Place an object in pool.
154      *
155      * @param poolable the object to be placed in pool
156      */

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 JavaDoc e )
185             {
186                 // To be backwards compatible, we have to support the logger having not been set.
187
if( ( getLogger() != null ) && ( getLogger().isDebugEnabled() ) )
188                 {
189                     getLogger().debug( "Error decommissioning object", e );
190                 }
191             }
192         }
193     }
194
195     /**
196      * Return the total number of slots in Pool
197      *
198      * @return the total number of slots
199      */

200     public final int getCapacity()
201     {
202         return m_pool.length;
203     }
204
205     /**
206      * Get the number of used slots in Pool
207      *
208      * @return the number of used slots
209      * @deprecated use the official size() method instead
210      */

211     public final int getSize()
212     {
213         return m_count;
214     }
215
216     /**
217      * Get the number of used slots in Pool
218      *
219      * @return the number of used slots
220      */

221     public final int size()
222     {
223         return getSize();
224     }
225
226     /**
227      * This fills the pool to the size specified in parameter.
228      */

229     public final void fill( final int fillSize ) throws Exception JavaDoc
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     /**
242      * This fills the pool by the size specified in parameter.
243      */

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     /**
258      * This shrinks the pool by parameter size.
259      */

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     /**
268      * Dispose the pool and decommission any Poolables.
269      */

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 JavaDoc e )
280             {
281                 // To be backwards compatible, we have to support the logger having not been set.
282
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