KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > application > BlockResourceProvider


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.phoenix.components.application;
9
10 import org.apache.avalon.excalibur.i18n.ResourceManager;
11 import org.apache.avalon.excalibur.i18n.Resources;
12 import org.apache.avalon.framework.component.Component;
13 import org.apache.avalon.framework.component.ComponentManager;
14 import org.apache.avalon.framework.component.DefaultComponentManager;
15 import org.apache.avalon.framework.configuration.Configuration;
16 import org.apache.avalon.framework.configuration.ConfigurationException;
17 import org.apache.avalon.framework.context.Context;
18 import org.apache.avalon.framework.logger.AbstractLogEnabled;
19 import org.apache.avalon.framework.logger.Logger;
20 import org.apache.avalon.framework.parameters.Parameters;
21 import org.apache.avalon.framework.service.DefaultServiceManager;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.avalon.phoenix.interfaces.Application;
24 import org.apache.avalon.phoenix.interfaces.ApplicationContext;
25 import org.apache.avalon.phoenix.metadata.BlockMetaData;
26 import org.apache.avalon.phoenix.metadata.DependencyMetaData;
27 import org.apache.excalibur.containerkit.lifecycle.ResourceProvider;
28
29 /**
30  * The accessor used to access resources for a particular
31  * Block or Listener.
32  *
33  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
34  * @version $Revision: 1.6 $ $Date: 2002/09/06 12:01:07 $
35  */

36 class BlockResourceProvider
37     extends AbstractLogEnabled
38     implements ResourceProvider
39 {
40     private static final Resources REZ =
41         ResourceManager.getPackageResources( BlockResourceProvider.class );
42
43     /**
44      * Context in which Blocks/Listeners operate.
45      */

46     private final ApplicationContext m_context;
47
48     /**
49      * The Application which this phase is associated with.
50      * Required to build a ComponentManager.
51      */

52     private final Application m_application;
53
54     public BlockResourceProvider( final ApplicationContext context,
55                                   final Application application )
56     {
57         if( null == context )
58         {
59             throw new NullPointerException JavaDoc( "context" );
60         }
61
62         if( null == application )
63         {
64             throw new NullPointerException JavaDoc( "application" );
65         }
66
67         m_context = context;
68         m_application = application;
69     }
70
71     /**
72      * Create Block for specified entry.
73      *
74      * @param entry the entry
75      * @return a new object
76      * @throws Exception
77      */

78     public Object JavaDoc createObject( final Object JavaDoc entry )
79         throws Exception JavaDoc
80     {
81         final BlockMetaData metaData = getMetaDataFor( entry );
82         final ClassLoader JavaDoc classLoader = m_context.getClassLoader();
83         String JavaDoc classname = metaData.getBlockInfo().getBlockDescriptor().getImplementationKey();
84         final Class JavaDoc clazz = classLoader.loadClass( classname );
85         return clazz.newInstance();
86     }
87
88     /**
89      * Retrieve Logger for specified block.
90      *
91      * @param entry the entry representing block
92      * @return the new Logger object
93      * @throws Exception if an error occurs
94      */

95     public Logger createLogger( final Object JavaDoc entry )
96         throws Exception JavaDoc
97     {
98         final BlockMetaData metaData = getMetaDataFor( entry );
99         final String JavaDoc name = metaData.getName();
100         return m_context.getLogger( name );
101     }
102
103     /**
104      * Create a BlockContext object for Block.
105      *
106      * @param entry the entry representing block
107      * @return the created BlockContext
108      */

109     public Context createContext( final Object JavaDoc entry )
110         throws Exception JavaDoc
111     {
112         final BlockMetaData metaData = getMetaDataFor( entry );
113         return new DefaultBlockContext( metaData.getName(),
114                                         m_context );
115     }
116
117     /**
118      * Create a {@link ComponentManager} object for a
119      * specific Block. This requires that for
120      * each dependency a reference to providing Block
121      * is aaqiured from the Application and placing it in
122      * {@link ComponentManager} under the correct name.
123      *
124      * @param entry the entry representing block
125      * @return the created ComponentManager
126      */

127     public ComponentManager createComponentManager( final Object JavaDoc entry )
128         throws Exception JavaDoc
129     {
130         final BlockMetaData metaData = getMetaDataFor( entry );
131         final DefaultComponentManager componentManager = new DefaultComponentManager();
132         final DependencyMetaData[] roles = metaData.getDependencies();
133
134         for( int i = 0; i < roles.length; i++ )
135         {
136             final DependencyMetaData role = roles[ i ];
137             final Object JavaDoc dependency = m_application.getBlock( role.getName() );
138             if( dependency instanceof Component )
139             {
140                 componentManager.put( role.getRole(), (Component)dependency );
141             }
142             else
143             {
144                 final String JavaDoc message =
145                     REZ.getString( "lifecycle.nota-component.error",
146                                    metaData.getName(),
147                                    role.getRole(),
148                                    role.getName() );
149                 throw new Exception JavaDoc( message );
150             }
151         }
152
153         return componentManager;
154     }
155
156     /**
157      * Create a {@link ServiceManager} object for a
158      * specific Block. This requires that for
159      * each dependency a reference to providing Block
160      * is aaqiured from the Application and placing it in
161      * {@link ServiceManager} under the correct name.
162      *
163      * @param entry the entry representing block
164      * @return the created ServiceManager
165      */

166     public ServiceManager createServiceManager( final Object JavaDoc entry )
167         throws Exception JavaDoc
168     {
169         final BlockMetaData metaData = getMetaDataFor( entry );
170         final DefaultServiceManager manager = new DefaultServiceManager();
171         final DependencyMetaData[] roles = metaData.getDependencies();
172
173         for( int i = 0; i < roles.length; i++ )
174         {
175             final DependencyMetaData role = roles[ i ];
176             final Object JavaDoc dependency = m_application.getBlock( role.getName() );
177             manager.put( role.getRole(), dependency );
178         }
179
180         return manager;
181     }
182
183     public Configuration createConfiguration( final Object JavaDoc entry )
184         throws Exception JavaDoc
185     {
186         final BlockMetaData metaData = getMetaDataFor( entry );
187         final String JavaDoc name = metaData.getName();
188         try
189         {
190             return m_context.getConfiguration( name );
191         }
192         catch( final ConfigurationException ce )
193         {
194             //Note that this shouldn't ever happen once we
195
//create a Config validator
196
final String JavaDoc message =
197                 REZ.getString( "missing-block-configuration",
198                                name );
199             throw new ConfigurationException( message, ce );
200         }
201     }
202
203     public Parameters createParameters( final Object JavaDoc entry )
204         throws Exception JavaDoc
205     {
206         final Configuration configuration =
207             createConfiguration( entry );
208         final Parameters parameters =
209             Parameters.fromConfiguration( configuration );
210         parameters.makeReadOnly();
211         return parameters;
212     }
213
214     /**
215      * Retrieve metadata for entry.
216      *
217      * @param entry the entry
218      * @return the MetaData for entry
219      */

220     private BlockMetaData getMetaDataFor( final Object JavaDoc entry )
221     {
222         return ((BlockEntry)entry).getMetaData();
223     }
224 }
225
Popular Tags