KickJava   Java API By Example, From Geeks To Geeks.

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


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
54 /**
55  * This is an <code>Pool</code> that caches Poolable objects for reuse.
56  * Please note that this pool offers no resource limiting whatsoever.
57  *
58  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
59  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
60  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
61  * @version CVS $Revision: 1.3 $ $Date: 2003/02/20 17:09:24 $
62  * @since 4.0
63  */

64 public class DefaultPool
65     extends AbstractPool
66     implements Disposable
67 {
68     protected int m_min;
69     protected int m_max;
70     protected PoolController m_controller;
71     protected boolean m_disposed = false;
72     protected boolean m_quickFail = false;
73
74     public DefaultPool( final ObjectFactory factory,
75                         final PoolController controller )
76         throws Exception JavaDoc
77     {
78         this( factory, controller, AbstractPool.DEFAULT_POOL_SIZE, AbstractPool.DEFAULT_POOL_SIZE );
79     }
80
81     public DefaultPool( final ObjectFactory factory,
82                         final PoolController controller,
83                         final int initial,
84                         final int maximum )
85         throws Exception JavaDoc
86     {
87         super( factory );
88
89         int t_max = maximum;
90         int t_min = initial;
91
92         if( t_min < 0 )
93         {
94             if( null != getLogger() && getLogger().isWarnEnabled() )
95             {
96                 getLogger().warn( "Minumum number of poolables specified is " +
97                                   "less than 0, using 0" );
98             }
99
100             t_min = 0;
101         }
102
103         if( ( t_max < t_min ) || ( t_max < 1 ) )
104         {
105             if( null != getLogger() && getLogger().isWarnEnabled() )
106             {
107                 getLogger().warn( "Maximum number of poolables specified must be at " +
108                                   "least 1 and must be greater than the minumum number " +
109                                   "of connections" );
110             }
111             t_max = ( t_min > 1 ) ? t_min : 1;
112         }
113
114         m_max = t_max;
115         m_min = t_min;
116
117         if( null != controller )
118         {
119             m_controller = controller;
120         }
121         else
122         {
123             m_controller = new DefaultPoolController( t_min / 2 );
124         }
125     }
126
127     public DefaultPool( final ObjectFactory factory )
128         throws Exception JavaDoc
129     {
130         this( factory, null, AbstractPool.DEFAULT_POOL_SIZE, AbstractPool.DEFAULT_POOL_SIZE );
131     }
132
133     public DefaultPool( final Class JavaDoc clazz, final int initial, final int maximum )
134         throws NoSuchMethodException JavaDoc, Exception JavaDoc
135     {
136         this( new DefaultObjectFactory( clazz ), null, initial, maximum );
137     }
138
139     public DefaultPool( final Class JavaDoc clazz, final int initial )
140         throws NoSuchMethodException JavaDoc, Exception JavaDoc
141     {
142         this( clazz, initial, initial );
143     }
144
145     public Poolable get() throws Exception JavaDoc
146     {
147         Poolable obj = null;
148
149         if( !m_initialized )
150         {
151             throw new IllegalStateException JavaDoc( "You cannot get a Poolable before the pool is initialized" );
152         }
153
154         if( m_disposed )
155         {
156             throw new IllegalStateException JavaDoc( "You cannot get a Poolable after the pool is disposed" );
157         }
158
159         m_mutex.acquire();
160         try
161         {
162             if( m_ready.size() == 0 )
163             {
164                 if( this instanceof Resizable )
165                 {
166                     this.internalGrow( m_controller.grow() );
167
168                     if( m_ready.size() > 0 )
169                     {
170                         obj = (Poolable)m_ready.remove();
171                     }
172                     else
173                     {
174                         final String JavaDoc message =
175                             "Could not create enough Components to service " +
176                             "your request.";
177                         throw new Exception JavaDoc( message );
178                     }
179                 }
180                 else
181                 {
182                     obj = newPoolable();
183                 }
184             }
185             else
186             {
187                 obj = (Poolable)m_ready.remove();
188             }
189
190             m_active.add( obj );
191
192             if( getLogger().isDebugEnabled() )
193             {
194                 final String JavaDoc message = "Retrieving a " +
195                     m_factory.getCreatedClass().getName() + " from the pool";
196                 getLogger().debug( message );
197             }
198             return obj;
199         }
200         finally
201         {
202             m_mutex.release();
203         }
204     }
205
206     public void put( final Poolable obj )
207     {
208         if( !m_initialized )
209         {
210             final String JavaDoc message = "You cannot get a Poolable before " +
211                 "the pool is initialized";
212             throw new IllegalStateException JavaDoc( message );
213         }
214
215         try
216         {
217             if( obj instanceof Recyclable )
218             {
219                 ( (Recyclable)obj ).recycle();
220             }
221
222             m_mutex.acquire();
223             try
224             {
225                 m_active.remove( m_active.indexOf( obj ) );
226
227                 if( getLogger().isDebugEnabled() )
228                 {
229                     final String JavaDoc message =
230                         "Returning a " + m_factory.getCreatedClass().getName() +
231                         " to the pool";
232                     getLogger().debug( message );
233                 }
234
235                 if( m_disposed == false )
236                 {
237                     m_ready.add( obj );
238
239                     if( ( this.size() > m_max ) && ( this instanceof Resizable ) )
240                     {
241                         this.internalShrink( m_controller.shrink() );
242                     }
243                 }
244                 else
245                 {
246                     this.removePoolable( obj );
247                 }
248             }
249             finally
250             {
251                 m_mutex.release();
252             }
253         }
254         catch( Exception JavaDoc e )
255         {
256             if( getLogger().isWarnEnabled() )
257             {
258                 getLogger().warn( "Pool interrupted while waiting for lock.", e );
259             }
260         }
261     }
262
263     public final void dispose()
264     {
265         try
266         {
267             m_mutex.acquire();
268             try
269             {
270                 while( m_ready.size() > 0 )
271                 {
272                     this.removePoolable( (Poolable)m_ready.remove() );
273                 }
274             }
275             finally
276             {
277                 m_mutex.release();
278             }
279         }
280         catch( Exception JavaDoc e )
281         {
282             if( getLogger().isWarnEnabled() )
283             {
284                 getLogger().warn( "Caught an exception disposing of pool", e );
285             }
286         }
287
288         this.m_disposed = true;
289     }
290 }
291
Popular Tags