KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
53  * This is an <code>Pool</code> that caches Poolable objects for reuse.
54  *
55  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
56  * @version CVS $Revision: 1.2 $ $Date: 2003/02/20 17:09:24 $
57  * @since 4.0
58  */

59 public class SoftResourceLimitingPool
60     extends DefaultPool
61     implements Resizable
62 {
63     /**
64      * Create an SoftResourceLimitingPool. The pool requires a factory.
65      */

66     public SoftResourceLimitingPool( final ObjectFactory factory )
67         throws Exception JavaDoc
68     {
69         this( factory, AbstractPool.DEFAULT_POOL_SIZE / 2 );
70     }
71
72     /**
73      * Create an SoftResourceLimitingPool. The pool requires a factory,
74      * and can optionally have a controller.
75      */

76     public SoftResourceLimitingPool( final ObjectFactory factory,
77                                      final int min )
78         throws Exception JavaDoc
79     {
80         this( factory, null, min, min * 2 );
81     }
82
83     /**
84      * Create an SoftResourceLimitingPool. The pool requires a factory,
85      * and can optionally have a controller.
86      */

87     public SoftResourceLimitingPool( final ObjectFactory factory,
88                                      final int min,
89                                      final int max ) throws Exception JavaDoc
90     {
91         this( factory, null, min, max );
92     }
93
94     /**
95      * Create an SoftResourceLimitingPool. The pool requires a factory,
96      * and can optionally have a controller.
97      */

98     public SoftResourceLimitingPool( final ObjectFactory factory,
99                                      final PoolController controller,
100                                      final int min,
101                                      final int max )
102         throws Exception JavaDoc
103     {
104         super( factory, controller, min, max );
105     }
106
107     public SoftResourceLimitingPool( final Class JavaDoc clazz, final int initial, final int maximum )
108         throws NoSuchMethodException JavaDoc, Exception JavaDoc
109     {
110         this( new DefaultObjectFactory( clazz ), initial, maximum );
111     }
112
113     public SoftResourceLimitingPool( final Class JavaDoc clazz, final int initial )
114         throws NoSuchMethodException JavaDoc, Exception JavaDoc
115     {
116         this( clazz, initial, initial );
117     }
118
119     public void initialize()
120         throws Exception JavaDoc
121     {
122         this.grow( this.m_min );
123
124         this.m_initialized = true;
125     }
126
127     public void grow( final int amount )
128     {
129         try
130         {
131             m_mutex.acquire();
132
133             this.internalGrow( amount );
134         }
135         catch( final InterruptedException JavaDoc ie )
136         {
137             if( getLogger().isWarnEnabled() )
138             {
139                 getLogger().warn( "Interrupted while waiting on lock", ie );
140             }
141         }
142         catch( final Exception JavaDoc e )
143         {
144             if( getLogger().isWarnEnabled() )
145             {
146                 getLogger().warn( "Could not grow the pool properly, an exception was caught", e );
147             }
148         }
149         finally
150         {
151             m_mutex.release();
152         }
153     }
154
155     public void shrink( final int amount )
156     {
157         try
158         {
159             m_mutex.acquire();
160
161             this.internalShrink( amount );
162         }
163         catch( final InterruptedException JavaDoc ie )
164         {
165             if( getLogger().isWarnEnabled() )
166             {
167                 getLogger().warn( "Interrupted while waiting on lock", ie );
168             }
169         }
170         catch( final Exception JavaDoc e )
171         {
172             if( getLogger().isWarnEnabled() )
173             {
174                 getLogger().warn( "Could not shrink the pool properly, an exception was caught", e );
175             }
176         }
177         finally
178         {
179             m_mutex.release();
180         }
181     }
182 }
183
Popular Tags