KickJava   Java API By Example, From Geeks To Geeks.

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


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

36 public class ComponentFactory
37     implements ObjectFactory, ThreadSafe
38 {
39     /** The class which this <code>ComponentFactory</code>
40      * should create.
41      */

42     private Class JavaDoc m_componentClass;
43
44     /** The Context for the component
45      */

46     private Context m_context;
47
48     /** The component manager for this component.
49      */

50     private ComponentManager m_componentManager;
51
52     /** The configuration for this component.
53      */

54     private Configuration m_configuration;
55
56     /** The RoleManager for child ComponentSelectors
57      */

58     private RoleManager m_roles;
59
60     /** The LogKitManager for child ComponentSelectors
61      */

62     private LoggerManager m_logManager;
63
64     /** The logger for the ComponentFactory
65      */

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

77     public ComponentFactory( final Class JavaDoc componentClass,
78                              final Configuration configuration,
79                              final ComponentManager componentManager,
80                              final Context context,
81                              final RoleManager roles,
82                              final LoggerManager logkit )
83     {
84         m_componentClass = componentClass;
85         m_configuration = configuration;
86         m_componentManager = componentManager;
87         m_context = context;
88         m_roles = roles;
89         m_logManager = logkit;
90         m_logger = m_logManager.getLoggerForCategory("system.factory");
91     }
92
93     public Object JavaDoc newInstance()
94         throws Exception JavaDoc
95     {
96         final Object JavaDoc component = m_componentClass.newInstance();
97
98         if (m_logger.isDebugEnabled())
99         {
100             m_logger.debug( "ComponentFactory creating new instance of " +
101                                m_componentClass.getName() + "." );
102         }
103
104         if( component instanceof LogEnabled )
105         {
106             final String JavaDoc logger = m_configuration.getAttribute( "logger", null );
107             if( null == logger )
108             {
109                 m_logger.debug( "no logger attribute available, using standard logger" );
110                 ((LogEnabled)component).enableLogging( m_logManager.getDefaultLogger() );
111             }
112             else
113             {
114                 m_logger.debug( "logger attribute is " + logger );
115                 ((LogEnabled)component).enableLogging( m_logManager.getLoggerForCategory( logger ) );
116             }
117         }
118
119         if( component instanceof Contextualizable )
120         {
121             ((Contextualizable)component).contextualize( m_context );
122         }
123
124         if( component instanceof Composable )
125         {
126             ((Composable)component).compose( m_componentManager );
127         }
128
129         if( component instanceof Configurable )
130         {
131             ((Configurable)component).configure( m_configuration );
132         }
133
134         if( component instanceof Parameterizable )
135         {
136             ((Parameterizable)component).
137                 parameterize( Parameters.fromConfiguration( m_configuration ) );
138         }
139
140         if( component instanceof Initializable )
141         {
142             ((Initializable)component).initialize();
143         }
144
145         if( component instanceof Startable )
146         {
147             ((Startable)component).start();
148         }
149
150         return component;
151     }
152
153     public final Class JavaDoc getCreatedClass()
154     {
155         return m_componentClass;
156     }
157
158     public final void decommission( final Object JavaDoc component )
159         throws Exception JavaDoc
160     {
161         if (m_logger.isDebugEnabled())
162         {
163             m_logger.debug( "ComponentFactory decommissioning instance of " +
164                                m_componentClass.getName() + "." );
165         }
166
167         if( component instanceof Startable )
168         {
169             ((Startable)component).stop();
170         }
171
172         if( component instanceof Disposable )
173         {
174             ((Disposable)component).dispose();
175         }
176     }
177 }
178
Popular Tags