KickJava   Java API By Example, From Geeks To Geeks.

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


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.Buffer;
21 import org.apache.commons.collections.UnboundedFifoBuffer;
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 VariableSizePool
32     implements Pool, Disposable, ManagablePool
33 {
34     private boolean m_disposed = false;
35     private final Buffer m_buffer;
36     private final ObjectFactory m_factory;
37     private final long m_key;
38
39     /**
40      * Constructor for an unmanaged pool
41      */

42     public VariableSizePool( ObjectFactory factory, int size )
43         throws Exception JavaDoc
44     {
45         this( factory, size, -1 );
46     }
47
48     /**
49      * Constructor for a managed pool
50      */

51     public VariableSizePool( ObjectFactory factory, int size, long key )
52         throws Exception JavaDoc
53     {
54         m_buffer = new UnboundedFifoBuffer( size );
55         m_factory = factory;
56         m_key = key;
57
58         synchronized( m_factory )
59         {
60             for( int i = 0; i < size; i++ )
61             {
62                 m_buffer.add( newInstance() );
63             }
64         }
65     }
66
67     public Object JavaDoc acquire()
68         throws Exception JavaDoc
69     {
70         if( m_disposed )
71         {
72             throw new IllegalStateException JavaDoc( "Cannot get an object from a disposed pool" );
73         }
74
75         Object JavaDoc object = null;
76
77         synchronized( m_buffer )
78         {
79             if( m_buffer.size() > 0 )
80             {
81                 object = m_buffer.remove();
82             }
83         }
84
85         if( null == object )
86         {
87             object = newInstance();
88         }
89
90         return object;
91     }
92
93     public void release( Object JavaDoc pooledObject )
94     {
95         if( m_disposed )
96         {
97             try
98             {
99                 m_factory.dispose( pooledObject );
100             }
101             catch( Exception JavaDoc e )
102             {
103                 // We should never get here, but ignore the exception if it happens
104
}
105         }
106         else
107         {
108             synchronized( m_buffer )
109             {
110                 m_buffer.add( PoolUtil.recycle( pooledObject ) );
111             }
112         }
113     }
114
115     public Object JavaDoc newInstance()
116         throws Exception JavaDoc
117     {
118         synchronized( m_factory )
119         {
120             return m_factory.newInstance();
121         }
122     }
123
124     public void dispose()
125     {
126         m_disposed = true;
127
128         synchronized( m_buffer )
129         {
130             while( !m_buffer.isEmpty() )
131             {
132                 try
133                 {
134                     m_factory.dispose( m_buffer.remove() );
135                 }
136                 catch( Exception JavaDoc e )
137                 {
138                     // We should never get here, but ignore the exception if it happens
139
}
140             }
141         }
142     }
143
144     public void shrink( final int byNum, final long key )
145         throws IllegalAccessException JavaDoc
146     {
147         if( m_key < 0 || m_key != key )
148         {
149             throw new IllegalAccessException JavaDoc();
150         }
151
152         synchronized( m_buffer )
153         {
154             final int num = Math.min( byNum, m_buffer.size() );
155
156             for( int i = 0; i < num; i++ )
157             {
158                 try
159                 {
160                     m_factory.dispose( m_buffer.remove() );
161                 }
162                 catch( Exception JavaDoc e )
163                 {
164                     // ignore exception
165
}
166             }
167         }
168     }
169
170     public void grow( final int byNum, final long key )
171         throws IllegalAccessException JavaDoc
172     {
173         if( m_key < 0 || m_key != key )
174         {
175             throw new IllegalAccessException JavaDoc();
176         }
177
178         synchronized( m_buffer )
179         {
180             for( int i = 0; i < byNum; i++ )
181             {
182                 try
183                 {
184                     m_buffer.add( newInstance() );
185                 }
186                 catch( Exception JavaDoc e )
187                 {
188                     // ignore exception
189
}
190             }
191         }
192     }
193
194     public int size( final long key )
195         throws IllegalAccessException JavaDoc
196     {
197         if( m_key < 0 || m_key != key )
198         {
199             throw new IllegalAccessException JavaDoc();
200         }
201
202         synchronized( m_buffer )
203         {
204             return m_buffer.size();
205         }
206     }
207 }
208
Popular Tags