KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
53 import java.util.List JavaDoc;
54
55 import org.apache.avalon.framework.activity.Initializable;
56 import org.apache.avalon.framework.logger.AbstractLogEnabled;
57 import org.apache.avalon.framework.thread.ThreadSafe;
58 import org.apache.commons.collections.Buffer;
59 import org.apache.commons.collections.UnboundedFifoBuffer;
60
61 /**
62  * This is an <code>Pool</code> that caches Poolable objects for reuse.
63  *
64  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
65  * @version CVS $Revision: 1.9 $ $Date: 2003/03/07 13:36:06 $
66  * @since 4.0
67  */

68 public abstract class AbstractPool
69     extends AbstractLogEnabled
70     implements Pool, ThreadSafe
71 {
72     public static final int DEFAULT_POOL_SIZE = 8;
73     protected final ObjectFactory m_factory;
74     protected List JavaDoc m_active = new ArrayList JavaDoc();
75     protected Buffer m_ready = new UnboundedFifoBuffer();
76     protected Mutex m_mutex = new Mutex();
77     protected boolean m_initialized = false;
78     protected int m_min;
79
80     /**
81      * Create an AbstractPool. The pool requires a factory, and can
82      * optionally have a controller.
83      */

84     public AbstractPool( final ObjectFactory factory ) throws Exception JavaDoc
85     {
86         m_factory = factory;
87
88         if( !( this instanceof Initializable ) )
89         {
90             initialize();
91         }
92     }
93
94     protected void initialize()
95         throws Exception JavaDoc
96     {
97         lock();
98
99         for( int i = 0; i < AbstractPool.DEFAULT_POOL_SIZE; i++ )
100         {
101             this.m_ready.add( this.newPoolable() );
102         }
103
104         m_initialized = true;
105
106         unlock();
107     }
108
109     protected final void lock()
110         throws InterruptedException JavaDoc
111     {
112         m_mutex.acquire();
113     }
114
115     protected final void unlock()
116         throws InterruptedException JavaDoc
117     {
118         m_mutex.release();
119     }
120
121     /**
122      * This is the method to override when you need to enforce creational
123      * policies.
124      */

125     protected Poolable newPoolable() throws Exception JavaDoc
126     {
127         Object JavaDoc obj = m_factory.newInstance();
128         return (Poolable)obj;
129     }
130
131     /**
132      * This is the method to override when you need to enforce destructional
133      * policies.
134      */

135     protected void removePoolable( Poolable poolable )
136     {
137         try
138         {
139             m_factory.decommission( poolable );
140         }
141         catch( Exception JavaDoc e )
142         {
143             if( getLogger().isDebugEnabled() )
144             {
145                 getLogger().debug( "Error decommissioning object", e );
146             }
147         }
148     }
149
150     public final int size()
151     {
152         synchronized( this )
153         {
154             // this is actually not 100% correct as the pool should always
155
// reflect the current size (i.e. m_ready.size()) and not the
156
// total size.
157
return this.m_active.size() + this.m_ready.size();
158         }
159     }
160
161     public abstract Poolable get() throws Exception JavaDoc;
162
163     public abstract void put( Poolable object );
164
165     protected void internalGrow( final int amount )
166         throws Exception JavaDoc
167     {
168         for( int i = 0; i < amount; i++ )
169         {
170             try
171             {
172                 m_ready.add( newPoolable() );
173             }
174             catch( final Exception JavaDoc e )
175             {
176                 if( null != getLogger() && getLogger().isDebugEnabled() )
177                 {
178                     Class JavaDoc createdClass = m_factory.getCreatedClass();
179                     if( createdClass == null )
180                     {
181                         getLogger().debug( "factory created class was null so a new "
182                                            + "instance could not be created.", e );
183                     }
184                     else
185                     {
186                         getLogger().debug( createdClass.getName() +
187                                            ": could not be instantiated.", e );
188                     }
189                 }
190                 
191                 throw e;
192             }
193         }
194     }
195
196     protected void internalShrink( final int amount )
197         throws Exception JavaDoc
198     {
199         for( int i = 0; i < amount; i++ )
200         {
201             if( m_ready.size() > m_min )
202             {
203                 try
204                 {
205                     this.removePoolable( (Poolable)m_ready.remove() );
206                 }
207                 catch( final Exception JavaDoc e )
208                 {
209                     if( null != getLogger() && getLogger().isDebugEnabled() )
210                     {
211                         getLogger().debug( m_factory.getCreatedClass().getName() +
212                                            ": improperly decommissioned.", e );
213                     }
214                 }
215             }
216         }
217     }
218 }
219
Popular Tags