KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > system > handler > FactoryComponentHandler


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.framework.logger.AbstractLogEnabled;
18 import org.apache.avalon.framework.logger.Logger;
19 import org.apache.avalon.excalibur.logger.LoggerManager;
20
21 /**
22  * The DefaultComponentHandler to make sure components are initialized
23  * and destroyed correctly.
24  *
25  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
26  * @version CVS $Revision: 1.2 $ $Date: 2002/01/29 16:18:53 $
27  * @since 4.0
28  */

29 class DefaultComponentHandler
30     implements ComponentHandler
31 {
32     /** The instance of the ComponentFactory that creates and disposes of the Component */
33     private final ComponentFactory m_factory;
34
35     /** State management boolean stating whether the Handler is initialized or not */
36     private boolean m_initialized = false;
37
38     /** State management boolean stating whether the Handler is disposed or not */
39     private boolean m_disposed = false;
40
41     /** Logger for factory */
42     private final Logger m_logger;
43
44     /**
45      * Create a ComponentHandler that takes care of hiding the details of
46      * whether a Component is ThreadSafe, Poolable, or SingleThreaded.
47      * It falls back to SingleThreaded if not specified.
48      */

49     protected DefaultComponentHandler( final Class JavaDoc componentClass,
50                                        final Configuration config,
51                                        final ComponentManager manager,
52                                        final Context context )
53         throws Exception JavaDoc
54     {
55         RoleManager roles = (RoleManager) context.get( Container.ROLE_MANAGER );
56         LoggerManager logkit = (LoggerManager) context.get( Container.LOGGER_MANAGER );
57
58         m_factory = new ComponentFactory( componentClass, config, manager, context, roles, logkit );
59         m_logger = logkit.getLoggerForCategory("system.handler.factory");
60     }
61
62     public boolean isInitialized()
63     {
64         return m_initialized;
65     }
66
67     /**
68      * Initialize the ComponentHandler.
69      */

70     public void initialize()
71     {
72         if( m_initialized )
73         {
74             return;
75         }
76
77         if (m_logger.isDebugEnabled())
78         {
79             m_logger.debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName());
80         }
81         m_initialized = true;
82     }
83
84     /**
85      * Get a reference of the desired Component
86      */

87     public Component get()
88         throws Exception JavaDoc
89     {
90         if( ! m_initialized )
91         {
92             throw new IllegalStateException JavaDoc( "You cannot get a component from an uninitialized holder." );
93         }
94
95         if( m_disposed )
96         {
97             throw new IllegalStateException JavaDoc( "You cannot get a component from a disposed holder" );
98         }
99
100         return (Component)m_factory.newInstance();
101     }
102
103     /**
104      * Return a reference of the desired Component
105      */

106     public void put( final Component component )
107     {
108         if( !m_initialized )
109         {
110             throw new IllegalStateException JavaDoc( "You cannot put a component in an uninitialized holder." );
111         }
112
113         try
114         {
115             m_factory.decommission( component );
116         }
117         catch( final Exception JavaDoc e )
118         {
119             if (m_logger.isWarnEnabled())
120             {
121                 m_logger.warn( "Error decommissioning component: " +
122                                   m_factory.getCreatedClass().getName(), e);
123             }
124         }
125     }
126
127     /**
128      * Dispose of the ComponentHandler and any associated Pools and Factories.
129      */

130     public void dispose()
131     {
132         try
133         {
134             // do nothing here
135

136             if( m_factory instanceof Disposable )
137             {
138                 ((Disposable)m_factory).dispose();
139             }
140         }
141         catch( final Exception JavaDoc e )
142         {
143             if (m_logger.isWarnEnabled())
144             {
145                 m_logger.warn( "Error decommissioning component: " +
146                                   m_factory.getCreatedClass().getName(), e );
147             }
148         }
149
150         m_disposed = true;
151     }
152 }
153
Popular Tags