KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > system > handler > 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.system.handler;
9
10 import org.apache.avalon.excalibur.system.Container;
11 import org.apache.avalon.excalibur.system.RoleManager;
12 import org.apache.avalon.framework.activity.Disposable;
13 import org.apache.avalon.framework.component.Component;
14 import org.apache.avalon.framework.component.ComponentManager;
15 import org.apache.avalon.framework.configuration.Configuration;
16 import org.apache.avalon.framework.context.Context;
17 import org.apache.avalon.excalibur.pool.Pool;
18 import org.apache.avalon.excalibur.pool.Poolable;
19 import org.apache.avalon.excalibur.pool.PoolManager;
20 import org.apache.avalon.excalibur.logger.LoggerManager;
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.log.Hierarchy;
23
24 /**
25  * The PoolableComponentHandler to make sure components are initialized
26  * and destroyed correctly.
27  *
28  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
29  * @version CVS $Revision: 1.3 $ $Date: 2002/01/29 16:18:53 $
30  * @since 4.0
31  */

32 public class PoolableComponentHandler implements ComponentHandler {
33     /** The instance of the ComponentFactory that creates and disposes of the Component */
34     private final ComponentFactory m_factory;
35
36     /** The pool of components for <code>Poolable</code> Components */
37     private final Pool m_pool;
38
39     /** State management boolean stating whether the Handler is initialized or not */
40     private boolean m_initialized = false;
41
42     /** State management boolean stating whether the Handler is disposed or not */
43     private boolean m_disposed = false;
44
45     /** Logger for PoolableComponentHandler */
46     private final Logger m_logger;
47
48
49     /**
50      * Create a ComponentHandler that takes care of hiding the details of
51      * whether a Component is ThreadSafe, Poolable, or SingleThreaded.
52      * It falls back to SingleThreaded if not specified.
53      */

54     protected PoolableComponentHandler( final Class JavaDoc componentClass,
55                                         final Configuration config,
56                                         final ComponentManager manager,
57                                         final Context context )
58         throws Exception JavaDoc
59     {
60         RoleManager roles = (RoleManager) context.get( Container.ROLE_MANAGER );
61         LoggerManager logkit = (LoggerManager) context.get( Container.LOGGER_MANAGER );
62         m_factory =
63             new ComponentFactory( componentClass, config, manager, context, roles, logkit );
64
65         PoolManager poolManager = (PoolManager) context.get( Container.POOL_MANAGER );
66         m_pool = poolManager.getManagedPool( m_factory, config.getAttributeAsInteger( "pool-min", 4 ) );
67         m_logger = logkit.getLoggerForCategory("system.handler.poolable");
68     }
69
70     public boolean isInitialized()
71     {
72         return m_initialized;
73     }
74
75     /**
76      * Initialize the ComponentHandler.
77      */

78     public void initialize()
79     {
80         if( m_initialized )
81         {
82             return;
83         }
84
85         if (m_logger.isDebugEnabled())
86         {
87             m_logger.debug( "ComponentHandler initialized for: " +
88                                m_factory.getCreatedClass().getName() );
89         }
90
91         m_initialized = true;
92     }
93
94     /**
95      * Get a reference of the desired Component
96      */

97     public Component get()
98         throws Exception JavaDoc
99     {
100         if( ! m_initialized )
101         {
102             throw new IllegalStateException JavaDoc( "You cannot get a component from an " +
103                                              "uninitialized holder." );
104         }
105
106         if( m_disposed )
107         {
108             throw new IllegalStateException JavaDoc( "You cannot get a component from " +
109                                              "a disposed holder" );
110         }
111
112         return (Component)m_pool.get();
113     }
114
115     /**
116      * Return a reference of the desired Component
117      */

118     public void put( final Component component )
119     {
120         if( !m_initialized )
121         {
122             throw new IllegalStateException JavaDoc( "You cannot put a component in " +
123                                              "an uninitialized holder." );
124         }
125
126         m_pool.put( (Poolable)component );
127     }
128
129     /**
130      * Dispose of the ComponentHandler and any associated Pools and Factories.
131      */

132     public void dispose()
133     {
134         if( m_factory instanceof Disposable )
135         {
136             ((Disposable)m_factory).dispose();
137         }
138
139         m_disposed = true;
140     }
141 }
142
Popular Tags