KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > component > PoolableComponentHandler


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.component;
9
10 import org.apache.avalon.framework.activity.Disposable;
11 import org.apache.avalon.framework.component.Component;
12 import org.apache.avalon.framework.component.ComponentManager;
13 import org.apache.avalon.framework.configuration.Configuration;
14 import org.apache.avalon.framework.context.Context;
15 import org.apache.avalon.excalibur.pool.Poolable;
16 import org.apache.avalon.excalibur.logger.LogKitManager;
17 import org.apache.log.Logger;
18
19 /**
20  * The PoolableComponentHandler to make sure components are initialized
21  * and destroyed correctly.
22  *
23  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
24  * @version CVS $Revision: 1.7 $ $Date: 2001/12/11 09:53:27 $
25  * @since 4.0
26  */

27 public class PoolableComponentHandler extends ComponentHandler {
28     /** The instance of the ComponentFactory that creates and disposes of the Component */
29     private final DefaultComponentFactory m_factory;
30
31     /** The pool of components for <code>Poolable</code> Components */
32     private final DefaultComponentPool m_pool;
33
34     /** State management boolean stating whether the Handler is initialized or not */
35     private boolean m_initialized = false;
36
37     /** State management boolean stating whether the Handler is disposed or not */
38     private boolean m_disposed = false;
39
40     /**
41      * Create a ComponentHandler that takes care of hiding the details of
42      * whether a Component is ThreadSafe, Poolable, or SingleThreaded.
43      * It falls back to SingleThreaded if not specified.
44      */

45     protected PoolableComponentHandler( final Class JavaDoc componentClass,
46                                         final Configuration config,
47                                         final ComponentManager manager,
48                                         final Context context,
49                                         final RoleManager roles,
50                                         final LogKitManager logkit )
51         throws Exception JavaDoc
52     {
53         m_factory =
54             new DefaultComponentFactory( componentClass, config, manager, context, roles, logkit );
55
56         int min = config.getAttributeAsInteger("pool-min", DefaultComponentPool.DEFAULT_POOL_SIZE/4);
57         int max = config.getAttributeAsInteger("pool-max", DefaultComponentPool.DEFAULT_POOL_SIZE);
58         int grow = config.getAttributeAsInteger("pool-grow", min);
59         DefaultComponentPoolController controller =
60             new DefaultComponentPoolController(grow);
61
62         m_pool = new DefaultComponentPool( m_factory, controller, min, max );
63     }
64
65     /**
66      * Sets the logger that the ComponentHandler will use.
67      */

68     public void setLogger( final Logger logger )
69     {
70         m_factory.setLogger( logger );
71         m_pool.setLogger( logger );
72
73         super.setLogger( logger );
74     }
75
76     /**
77      * Initialize the ComponentHandler.
78      */

79     public void initialize()
80     {
81         if( m_initialized )
82         {
83             return;
84         }
85
86         try
87         {
88             m_pool.initialize();
89         }
90         catch( Exception JavaDoc e )
91         {
92             if (getLogger().isErrorEnabled())
93             {
94                 getLogger().error( "Cannot use component: " +
95                                    m_factory.getCreatedClass().getName(), e );
96             }
97         }
98
99         if (getLogger().isDebugEnabled())
100         {
101             getLogger().debug( "ComponentHandler initialized for: " +
102                                m_factory.getCreatedClass().getName() );
103         }
104
105         m_initialized = true;
106     }
107
108     /**
109      * Get a reference of the desired Component
110      */

111     public Component get()
112         throws Exception JavaDoc
113     {
114         if( ! m_initialized )
115         {
116             throw new IllegalStateException JavaDoc( "You cannot get a component from an " +
117                                              "uninitialized holder." );
118         }
119
120         if( m_disposed )
121         {
122             throw new IllegalStateException JavaDoc( "You cannot get a component from " +
123                                              "a disposed holder" );
124         }
125
126         return (Component)m_pool.get();
127     }
128
129     /**
130      * Return a reference of the desired Component
131      */

132     public void put( final Component component )
133     {
134         if( !m_initialized )
135         {
136             throw new IllegalStateException JavaDoc( "You cannot put a component in " +
137                                              "an uninitialized holder." );
138         }
139
140         m_pool.put( (Poolable)component );
141     }
142
143     /**
144      * Dispose of the ComponentHandler and any associated Pools and Factories.
145      */

146     public void dispose()
147     {
148         m_pool.dispose();
149
150         if( m_factory instanceof Disposable )
151         {
152             ((Disposable)m_factory).dispose();
153         }
154
155         m_disposed = true;
156     }
157 }
158
Popular Tags