KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > impl > handler > PoolableComponentHandler


1 /*
2  * Copyright 2003-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
18 package org.apache.avalon.fortress.impl.handler;
19
20 import org.apache.avalon.framework.configuration.Configurable;
21 import org.apache.avalon.framework.configuration.Configuration;
22 import org.apache.avalon.framework.configuration.ConfigurationException;
23 import org.apache.avalon.framework.service.ServiceException;
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.excalibur.mpool.Pool;
26 import org.apache.excalibur.mpool.PoolManager;
27
28 /**
29  * The PoolableComponentHandler to make sure components are initialized
30  * and destroyed correctly.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @version CVS $Revision: 1.10 $ $Date: 2004/02/28 15:16:25 $
34  * @since 4.0
35  */

36 public final class PoolableComponentHandler
37     extends AbstractComponentHandler
38     implements Configurable
39 {
40     /** The instance of the PoolManager to create the Pool for the Handler */
41     private PoolManager m_poolManager;
42
43     /** The pool of components for <code>Poolable</code> Components */
44     private Pool m_pool;
45
46     /** The Config element for the poolable */
47     private int m_poolMin;
48
49     /**
50      * Application of suporting services to the handler.
51      * @param serviceManager the service manager
52      * @exception ServiceException if a service related error occurs
53      * @avalon.dependency type="PoolManager"
54      */

55     public void service( final ServiceManager serviceManager )
56         throws ServiceException
57     {
58         super.service( serviceManager );
59         m_poolManager =
60             (PoolManager) serviceManager.lookup( PoolManager.ROLE );
61     }
62
63     /**
64      * Configuration of the handler under which the minimum pool size
65      * is established.
66      * @param configuration the configuration fragment
67      * @exception ConfigurationException if the supplied configuration attribute
68      * for 'pool-min' cannot be resolved to an integer value
69      */

70     public void configure( final Configuration configuration )
71         throws ConfigurationException
72     {
73         m_poolMin = configuration.getAttributeAsInteger( "pool-min", 10 );
74     }
75
76     /**
77      * Initialize the ComponentHandler.
78      * @exception Exception if an error occurs
79      */

80     protected void doPrepare()
81         throws Exception JavaDoc
82     {
83         m_pool = m_poolManager.getManagedPool( m_factory, m_poolMin );
84     }
85
86     /**
87      * Get a reference of the desired Component
88      * @exception Exception if an error occurs
89      */

90     protected Object JavaDoc doGet()
91         throws Exception JavaDoc
92     {
93         return m_pool.acquire();
94     }
95
96     /**
97      * Return a reference of the desired Component
98      * @param component the component to return to the handler
99      */

100     protected void doPut( final Object JavaDoc component )
101     {
102         m_pool.release( component );
103     }
104 }
105
Popular Tags