KickJava   Java API By Example, From Geeks To Geeks.

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


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.avalon.framework.activity.Initializable;
21 import org.apache.commons.collections.BoundedFifoBuffer;
22 import org.apache.commons.collections.Buffer;
23 import org.apache.commons.collections.BufferUnderflowException;
24
25 /**
26  * This is an <code>Pool</code> that caches Poolable objects for reuse.
27  * Please note that this pool offers no resource limiting whatsoever.
28  *
29  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
30  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:34 $
31  * @since 4.1
32  */

33 public final class BlockingFixedSizePool
34     implements Pool, Disposable, Initializable
35 {
36     private boolean m_disposed = false;
37     private final Buffer m_buffer;
38     private final ObjectFactory m_factory;
39     private final long m_timeout;
40     private final int m_maxSize;
41
42     /** The semaphor we synchronize on */
43     protected final Object JavaDoc m_semaphore = new Object JavaDoc();
44
45     public BlockingFixedSizePool( ObjectFactory factory, int size )
46         throws Exception JavaDoc
47     {
48         this( factory, size, 1000 );
49     }
50
51     public BlockingFixedSizePool( ObjectFactory factory, int size, long timeout )
52         throws Exception JavaDoc
53     {
54         m_timeout = ( timeout < 1 ) ? 0 : timeout;
55         m_buffer = new BoundedFifoBuffer( size );
56         m_maxSize = size;
57         m_factory = factory;
58     }
59
60     public void initialize()
61         throws Exception JavaDoc
62     {
63         for( int i = 0; i < m_maxSize; i++ )
64         {
65             m_buffer.add( newInstance() );
66         }
67     }
68
69     public Object JavaDoc acquire()
70     {
71         if( m_disposed )
72         {
73             throw new IllegalStateException JavaDoc( "Cannot get an object from a disposed pool" );
74         }
75
76         Object JavaDoc object = null;
77
78         synchronized( m_semaphore )
79         {
80             if( m_buffer.isEmpty() )
81             {
82                 long blockStart = System.currentTimeMillis();
83
84                 if( m_timeout > 0 )
85                 {
86                     long blockWait = m_timeout;
87
88                     do
89                     {
90                         try
91                         {
92                             m_semaphore.wait( blockWait );
93                         }
94                         catch( InterruptedException JavaDoc ie )
95                         {
96                         }
97
98                         if( m_disposed )
99                         {
100                             throw new IllegalStateException JavaDoc( "Pool disposed of while waiting for resources to free up" );
101                         }
102
103                         if( m_buffer.isEmpty() )
104                         {
105                             blockWait = m_timeout -
106                                 ( System.currentTimeMillis() - blockStart );
107                         }
108                     } while( m_buffer.isEmpty() && blockWait > 0 );
109                 }
110                 else
111                 {
112                     do
113                     {
114                         try
115                         {
116                             m_semaphore.wait();
117                         }
118                         catch( InterruptedException JavaDoc ie )
119                         {
120                         }
121
122                         if( m_disposed )
123                         {
124                             throw new IllegalStateException JavaDoc( "Pool disposed of while waiting for resources to free up" );
125                         }
126                     } while( m_buffer.isEmpty() );
127                 }
128             }
129
130             try
131             {
132                 object = m_buffer.remove();
133             }
134             catch( BufferUnderflowException bufe )
135             {
136                 // ignore exception and leave object as null
137
}
138         }
139
140         if( object == null )
141         {
142             throw new IllegalStateException JavaDoc( "Timeout exceeded without acquiring resource." );
143         }
144
145         return object;
146     }
147
148     public void release( Object JavaDoc object )
149     {
150         synchronized( m_semaphore )
151         {
152             if( m_disposed )
153             {
154                 try
155                 {
156                     m_factory.dispose( object );
157                 }
158                 catch( Exception JavaDoc e )
159                 {
160                     // We should never get here, but ignore the exception if it happens
161
}
162             }
163             else
164             {
165                 if( m_buffer.size() < m_maxSize )
166                 {
167                     m_buffer.add( PoolUtil.recycle( object ) );
168                     m_semaphore.notify();
169                 }
170                 else
171                 {
172                     try
173                     {
174                         m_factory.dispose( object );
175                     }
176                     catch( Exception JavaDoc e )
177                     {
178                         // We should never get here, but ignore the exception if it happens
179
}
180                 }
181             }
182         }
183     }
184
185     public Object JavaDoc newInstance()
186         throws Exception JavaDoc
187     {
188         return m_factory.newInstance();
189     }
190
191     public void dispose()
192     {
193         m_disposed = true;
194
195         synchronized( m_semaphore )
196         {
197             while( !m_buffer.isEmpty() )
198             {
199                 try
200                 {
201                     m_factory.dispose( m_buffer.remove() );
202                 }
203                 catch( Exception JavaDoc e )
204                 {
205                     // We should never get here, but ignore the exception if it happens
206
}
207             }
208
209             m_semaphore.notifyAll();
210         }
211     }
212 }
213
214
Popular Tags