KickJava   Java API By Example, From Geeks To Geeks.

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


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.Startable;
11 import org.apache.avalon.framework.activity.Disposable;
12 import org.apache.avalon.framework.component.Component;
13 import org.apache.avalon.framework.component.ComponentManager;
14 import org.apache.avalon.framework.configuration.Configuration;
15 import org.apache.avalon.framework.context.Context;
16 import org.apache.avalon.excalibur.logger.LogKitManager;
17 import org.apache.log.Logger;
18
19 /**
20  * The ThreadSafeComponentHandler 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.8 $ $Date: 2001/12/11 09:53:27 $
25  * @since 4.0
26  */

27 public class ThreadSafeComponentHandler extends ComponentHandler {
28     private Component m_instance;
29     private final DefaultComponentFactory m_factory;
30     private boolean m_initialized = false;
31     private boolean m_disposed = false;
32
33     /**
34      * Create a ComponentHandler that takes care of hiding the details of
35      * whether a Component is ThreadSafe, Poolable, or SingleThreaded.
36      * It falls back to SingleThreaded if not specified.
37      */

38     protected ThreadSafeComponentHandler( final Class JavaDoc componentClass,
39                                           final Configuration config,
40                                           final ComponentManager manager,
41                                           final Context context,
42                                           final RoleManager roles,
43                                           final LogKitManager logkit )
44         throws Exception JavaDoc
45     {
46         m_factory = new DefaultComponentFactory( componentClass, config, manager, context, roles, logkit );
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 ThreadSafeComponentHandler( final Component component )
55         throws Exception JavaDoc
56     {
57         m_instance = component;
58         m_factory = null;
59     }
60
61     public void setLogger(Logger log)
62     {
63         if ( this.m_factory != null )
64         {
65             m_factory.setLogger(log);
66         }
67
68         super.setLogger(log);
69     }
70
71     /**
72      * Initialize the ComponentHandler.
73      */

74     public void initialize()
75     throws Exception JavaDoc
76     {
77         if( m_initialized )
78         {
79             return;
80         }
81
82         if (m_instance == null)
83         {
84             m_instance = (Component) this.m_factory.newInstance();
85         }
86
87         if (getLogger().isDebugEnabled())
88         {
89             if (this.m_factory != null)
90             {
91                 getLogger().debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName());
92             }
93             else
94             {
95                 getLogger().debug("ComponentHandler initialized for: " + this.m_instance.getClass().getName());
96             }
97         }
98
99         m_initialized = true;
100     }
101
102     /**
103      * Get a reference of the desired Component
104      */

105     public final Component get()
106         throws Exception JavaDoc
107     {
108         if( ! m_initialized )
109         {
110             throw new IllegalStateException JavaDoc( "You cannot get a component from an uninitialized holder." );
111         }
112
113         if( m_disposed )
114         {
115             throw new IllegalStateException JavaDoc( "You cannot get a component from a disposed holder" );
116         }
117
118         return m_instance;
119     }
120
121     /**
122      * Return a reference of the desired Component
123      */

124     public void put( final Component component )
125     {
126         if( !m_initialized )
127         {
128             throw new IllegalStateException JavaDoc( "You cannot put a component in an uninitialized holder." );
129         }
130     }
131
132     /**
133      * Dispose of the ComponentHandler and any associated Pools and Factories.
134      */

135     public void dispose()
136     {
137         try {
138             if( null != m_factory )
139             {
140                 m_factory.decommission( m_instance );
141             }
142             else
143             {
144                 if( m_instance instanceof Startable )
145                 {
146                     ((Startable)m_instance).stop();
147                 }
148
149                 if( m_instance instanceof Disposable )
150                 {
151                     ((Disposable)m_instance).dispose();
152                 }
153             }
154
155             m_instance = null;
156         }
157         catch( final Exception JavaDoc e )
158         {
159             if (getLogger().isWarnEnabled())
160             {
161                 getLogger().warn( "Error decommissioning component: " +
162                                   m_factory.getCreatedClass().getName(), e );
163             }
164         }
165
166         m_disposed = true;
167     }
168 }
169
Popular Tags