KickJava   Java API By Example, From Geeks To Geeks.

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


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.Startable;
13 import org.apache.avalon.framework.activity.Disposable;
14 import org.apache.avalon.framework.component.Component;
15 import org.apache.avalon.framework.component.ComponentManager;
16 import org.apache.avalon.framework.configuration.Configuration;
17 import org.apache.avalon.framework.context.Context;
18 import org.apache.avalon.excalibur.logger.LoggerManager;
19 import org.apache.avalon.framework.logger.Logger;
20
21 /**
22  * The ThreadSafeComponentHandler 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.3 $ $Date: 2002/01/29 16:18:53 $
27  * @since 4.0
28  */

29 public class PerThreadComponentHandler implements ComponentHandler {
30     private ThreadLocalComponent m_instance;
31     private final ComponentFactory m_factory;
32     private boolean m_initialized = false;
33     private boolean m_disposed = false;
34     private final Logger m_logger;
35
36     /**
37      * Create a ComponentHandler that takes care of hiding the details of
38      * whether a Component is ThreadSafe, Poolable, or SingleThreaded.
39      * It falls back to SingleThreaded if not specified.
40      */

41     protected PerThreadComponentHandler ( final Class JavaDoc componentClass,
42                                           final Configuration config,
43                                           final ComponentManager manager,
44                                           final Context context )
45         throws Exception JavaDoc
46     {
47         RoleManager roles = (RoleManager) context.get( Container.ROLE_MANAGER );
48         LoggerManager logkit = (LoggerManager) context.get( Container.LOGGER_MANAGER );
49
50         m_factory = new ComponentFactory( componentClass, config, manager, context, roles, logkit );
51         m_instance = new ThreadLocalComponent( m_factory );
52         m_logger = logkit.getLoggerForCategory("system.handler.perthread");
53     }
54
55     public boolean isInitialized()
56     {
57         return m_initialized;
58     }
59
60     /**
61      * Initialize the ComponentHandler.
62      */

63     public void initialize()
64     throws Exception JavaDoc
65     {
66         if( m_initialized )
67         {
68             return;
69         }
70
71         if (m_logger.isDebugEnabled())
72         {
73             if (this.m_factory != null)
74             {
75                 m_logger.debug("ComponentHandler initialized for: " + this.m_factory.getCreatedClass().getName());
76             }
77             else
78             {
79                m_logger.debug("ComponentHandler initialized for: " + this.m_instance.getClass().getName());
80             }
81         }
82
83         m_initialized = true;
84     }
85
86     /**
87      * Get a reference of the desired Component
88      */

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

108     public void put( final Component component )
109     {
110         if( !m_initialized )
111         {
112             throw new IllegalStateException JavaDoc( "You cannot put a component in an uninitialized holder." );
113         }
114     }
115
116     /**
117      * Dispose of the ComponentHandler and any associated Pools and Factories.
118      */

119     public void dispose()
120     {
121         try {
122             m_factory.decommission( m_instance.getComponent() );
123
124             m_instance = null;
125         }
126         catch( final Exception JavaDoc e )
127         {
128             if (m_logger.isWarnEnabled())
129             {
130                 m_logger.warn( "Error decommissioning component: " +
131                                 m_factory.getCreatedClass().getName(), e );
132             }
133         }
134
135         m_disposed = true;
136     }
137
138     private final static class ThreadLocalComponent extends ThreadLocal JavaDoc
139     {
140         private final ComponentFactory m_factory;
141
142         protected ThreadLocalComponent(ComponentFactory factory)
143         {
144             m_factory = factory;
145         }
146
147         protected Object JavaDoc initialValue()
148         {
149             try
150             {
151                 return m_factory.newInstance();
152             }
153             catch (Exception JavaDoc e)
154             {
155                 return null;
156             }
157         }
158
159         public Component getComponent()
160         {
161             return (Component) this.get();
162         }
163     }
164 }
165
Popular Tags