KickJava   Java API By Example, From Geeks To Geeks.

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


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.framework.logger.AbstractLoggable;
16 import org.apache.avalon.excalibur.logger.LogKitManager;
17 import org.apache.log.Logger;
18
19 /**
20  * The DefaultComponentHandler 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 class DefaultComponentHandler
28     extends ComponentHandler
29 {
30     /** The instance of the ComponentFactory that creates and disposes of the Component */
31     private final DefaultComponentFactory m_factory;
32
33     /** State management boolean stating whether the Handler is initialized or not */
34     private boolean m_initialized = false;
35
36     /** State management boolean stating whether the Handler is disposed or not */
37     private boolean m_disposed = false;
38
39     /**
40      * Create a ComponentHandler that takes care of hiding the details of
41      * whether a Component is ThreadSafe, Poolable, or SingleThreaded.
42      * It falls back to SingleThreaded if not specified.
43      */

44     protected DefaultComponentHandler( final Class JavaDoc componentClass,
45                                        final Configuration config,
46                                        final ComponentManager manager,
47                                        final Context context,
48                                        final RoleManager roles,
49                                        final LogKitManager logkit )
50         throws Exception JavaDoc
51     {
52         m_factory = new DefaultComponentFactory( componentClass, config, manager, context, roles, logkit );
53     }
54
55     /**
56      * Sets the logger that the ComponentHandler will use.
57      */

58     public void setLogger( final Logger logger )
59     {
60         m_factory.setLogger( logger );
61
62         super.setLogger( logger );
63     }
64
65     /**
66      * Initialize the ComponentHandler.
67      */

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

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

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

128     public void dispose()
129     {
130         try
131         {
132             // do nothing here
133

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