KickJava   Java API By Example, From Geeks To Geeks.

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


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.activity.Initializable;
13 import org.apache.avalon.framework.component.ComponentManager;
14 import org.apache.avalon.framework.component.Composable;
15 import org.apache.avalon.framework.configuration.Configurable;
16 import org.apache.avalon.framework.configuration.Configuration;
17 import org.apache.avalon.framework.parameters.Parameterizable;
18 import org.apache.avalon.framework.parameters.Parameters;
19 import org.apache.avalon.framework.context.Context;
20 import org.apache.avalon.framework.context.Contextualizable;
21 import org.apache.avalon.framework.logger.AbstractLoggable;
22 import org.apache.avalon.framework.logger.Loggable;
23 import org.apache.avalon.framework.logger.LogEnabled;
24 import org.apache.avalon.framework.logger.LogKitLogger;
25 import org.apache.avalon.framework.thread.ThreadSafe;
26 import org.apache.avalon.excalibur.pool.ObjectFactory;
27 import org.apache.avalon.excalibur.logger.LogKitManager;
28 import org.apache.avalon.excalibur.logger.LogKitManageable;
29
30 /**
31  * Factory for Avalon components.
32  *
33  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
34  * @author <a HREF="mailto:paul@luminas.co.uk">Paul Russell</a>
35  * @version CVS $Revision: 1.10 $ $Date: 2001/12/11 09:53:27 $
36  * @since 4.0
37  */

38 public class DefaultComponentFactory
39     extends AbstractLoggable
40     implements ObjectFactory, ThreadSafe
41 {
42     /** The class which this <code>ComponentFactory</code>
43      * should create.
44      */

45     private Class JavaDoc m_componentClass;
46
47     /** The Context for the component
48      */

49     private Context m_context;
50
51     /** The component manager for this component.
52      */

53     private ComponentManager m_componentManager;
54
55     /** The configuration for this component.
56      */

57     private Configuration m_configuration;
58
59     /** The RoleManager for child ComponentSelectors
60      */

61     private RoleManager m_roles;
62
63     /** The LogKitManager for child ComponentSelectors
64      */

65     private LogKitManager m_logkit;
66
67     /** The org.apache.avalon.framework.logger.Logger instance.
68      */

69     private org.apache.avalon.framework.logger.Logger m_logEnabledLogger;
70
71     /**
72      * Construct a new component factory for the specified component.
73      *
74      * @param componentClass the class to instantiate (must have a default constructor).
75      * @param configuration the <code>Configuration</code> object to pass to new instances.
76      * @param componentManager the component manager to pass to <code>Composable</code>s.
77      * @param context the <code>Context</code> to pass to <code>Contexutalizable</code>s.
78      * @param roles the <code>RoleManager</code> to pass to <code>DefaultComponentSelector</code>s.
79      */

80     public DefaultComponentFactory( final Class JavaDoc componentClass,
81                                     final Configuration configuration,
82                                     final ComponentManager componentManager,
83                                     final Context context,
84                                     final RoleManager roles,
85                                     final LogKitManager logkit )
86     {
87         m_componentClass = componentClass;
88         m_configuration = configuration;
89         m_componentManager = componentManager;
90         m_context = context;
91         m_roles = roles;
92         m_logkit = logkit;
93     }
94
95     public Object JavaDoc newInstance()
96         throws Exception JavaDoc
97     {
98         final Object JavaDoc component = m_componentClass.newInstance();
99
100         if (getLogger().isDebugEnabled())
101         {
102             getLogger().debug( "ComponentFactory creating new instance of " +
103                                m_componentClass.getName() + "." );
104         }
105
106         if( component instanceof LogEnabled )
107         {
108             if( null == m_logkit || null == m_configuration )
109             {
110                 ((LogEnabled)component).enableLogging( getLogEnabledLogger() );
111             }
112             else
113             {
114                 final String JavaDoc logger = m_configuration.getAttribute( "logger", null );
115                 if( null == logger )
116                 {
117                     getLogger().debug( "no logger attribute available, using standard logger" );
118                     ((LogEnabled)component).enableLogging( getLogEnabledLogger() );
119                 }
120                 else
121                 {
122                     getLogger().debug( "logger attribute is " + logger );
123                     ((LogEnabled)component).enableLogging( new LogKitLogger( m_logkit.getLogger( logger ) ) );
124                 }
125             }
126         }
127
128         if( component instanceof Loggable )
129         {
130             if( null == m_logkit || null == m_configuration )
131             {
132                 ((Loggable)component).setLogger( getLogger() );
133             }
134             else
135             {
136                 final String JavaDoc logger = m_configuration.getAttribute( "logger", null );
137                 if( null == logger )
138                 {
139                     getLogger().debug( "no logger attribute available, using standard logger" );
140                     ((Loggable)component).setLogger( getLogger() );
141                 }
142                 else
143                 {
144                     getLogger().debug( "logger attribute is " + logger );
145                     ((Loggable)component).setLogger( m_logkit.getLogger( logger ) );
146                 }
147             }
148         }
149
150         if( component instanceof Contextualizable )
151         {
152             ((Contextualizable)component).contextualize( m_context );
153         }
154
155         if( component instanceof Composable )
156         {
157             ((Composable)component).compose( m_componentManager );
158         }
159
160         if ( component instanceof RoleManageable )
161         {
162             ((RoleManageable)component).setRoleManager( m_roles );
163         }
164
165         if ( component instanceof LogKitManageable )
166         {
167             ((LogKitManageable)component).setLogKitManager( m_logkit );
168         }
169
170         if( component instanceof Configurable )
171         {
172             ((Configurable)component).configure( m_configuration );
173         }
174
175         if( component instanceof Parameterizable )
176         {
177             ((Parameterizable)component).
178                 parameterize( Parameters.fromConfiguration( m_configuration ) );
179         }
180
181         if( component instanceof Initializable )
182         {
183             ((Initializable)component).initialize();
184         }
185
186         if( component instanceof Startable )
187         {
188             ((Startable)component).start();
189         }
190
191         return component;
192     }
193
194     public final Class JavaDoc getCreatedClass()
195     {
196         return m_componentClass;
197     }
198
199     public final void decommission( final Object JavaDoc component )
200         throws Exception JavaDoc
201     {
202         if (getLogger().isDebugEnabled())
203         {
204             getLogger().debug( "ComponentFactory decommissioning instance of " +
205                                m_componentClass.getName() + "." );
206         }
207
208         if( component instanceof Startable )
209         {
210             ((Startable)component).stop();
211         }
212
213         if( component instanceof Disposable )
214         {
215             ((Disposable)component).dispose();
216         }
217     }
218
219     protected org.apache.avalon.framework.logger.Logger getLogEnabledLogger()
220     {
221         if ( null == m_logEnabledLogger )
222         {
223             m_logEnabledLogger = new LogKitLogger( getLogger() );
224         }
225
226         return m_logEnabledLogger;
227     }
228 }
229
Popular Tags