KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > system > handler > 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.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 ThreadSafeComponentHandler implements ComponentHandler {
30     private Component 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 ThreadSafeComponentHandler( 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_logger = logkit.getLoggerForCategory("system.handler.threadsafe");
52     }
53
54     public boolean isInitialized()
55     {
56         return m_initialized;
57     }
58
59     /**
60      * Initialize the ComponentHandler.
61      */

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

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

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

123     public void dispose()
124     {
125         try {
126             if( null != m_factory )
127             {
128                 m_factory.decommission( m_instance );
129             }
130             else
131             {
132                 if( m_instance instanceof Startable )
133                 {
134                     ((Startable)m_instance).stop();
135                 }
136
137                 if( m_instance instanceof Disposable )
138                 {
139                     ((Disposable)m_instance).dispose();
140                 }
141             }
142
143             m_instance = null;
144         }
145         catch( final Exception JavaDoc e )
146         {
147             if (m_logger.isWarnEnabled())
148             {
149                 m_logger.warn( "Error decommissioning component: " +
150                                   m_factory.getCreatedClass().getName(), e );
151             }
152         }
153
154         m_disposed = true;
155     }
156 }
157
Popular Tags