KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > mpool > FixedSizePool


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.mpool;
18
19 import org.apache.avalon.framework.activity.Disposable;
20 import org.apache.commons.collections.BoundedFifoBuffer;
21 import org.apache.commons.collections.Buffer;
22
23 /**
24  * This is an <code>Pool</code> that caches Poolable objects for reuse.
25  * Please note that this pool offers no resource limiting whatsoever.
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:34 $
29  * @since 4.1
30  */

31 public final class FixedSizePool
32     implements Pool, Disposable
33 {
34     private boolean m_disposed = false;
35     private final Buffer m_buffer;
36     private final ObjectFactory m_factory;
37
38     public FixedSizePool( ObjectFactory factory, int size )
39         throws Exception JavaDoc
40     {
41         m_buffer = new BoundedFifoBuffer( size );
42         m_factory = factory;
43
44         for( int i = 0; i < size; i++ )
45         {
46             m_buffer.add( newInstance() );
47         }
48     }
49
50     public Object JavaDoc acquire()
51     {
52         if( m_disposed )
53         {
54             throw new IllegalStateException JavaDoc( "Cannot get an object from a disposed pool" );
55         }
56
57         Object JavaDoc object = null;
58
59         synchronized( m_buffer )
60         {
61             object = m_buffer.remove();
62         }
63
64         return object;
65     }
66
67     public void release( Object JavaDoc object )
68     {
69         if( m_disposed )
70         {
71             try
72             {
73                 m_factory.dispose( object );
74             }
75             catch( Exception JavaDoc e )
76             {
77                 // We should never get here, but ignore the exception if it happens
78
}
79         }
80         else
81         {
82             synchronized( m_buffer )
83             {
84                 m_buffer.add( PoolUtil.recycle( object ) );
85                 m_buffer.notifyAll();
86             }
87         }
88     }
89
90     public Object JavaDoc newInstance()
91         throws Exception JavaDoc
92     {
93         return m_factory.newInstance();
94     }
95
96     public void dispose()
97     {
98         m_disposed = true;
99
100         synchronized( m_buffer )
101         {
102             while( !m_buffer.isEmpty() )
103             {
104                 try
105                 {
106                     m_factory.dispose( m_buffer.remove() );
107                 }
108                 catch( Exception JavaDoc e )
109                 {
110                     // We should never get here, but ignore the exception if it happens
111
}
112             }
113         }
114     }
115 }
116
117
Popular Tags