KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > application > BlockResourceProvider


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.application;
88
89 import java.lang.reflect.Array JavaDoc;
90 import java.util.ArrayList JavaDoc;
91 import java.util.Collections JavaDoc;
92 import java.util.HashMap JavaDoc;
93 import java.util.Iterator JavaDoc;
94 import java.util.List JavaDoc;
95 import java.util.Map JavaDoc;
96 import org.apache.avalon.framework.component.ComponentManager;
97 import org.apache.avalon.framework.component.WrapperComponentManager;
98 import org.apache.avalon.framework.configuration.Configuration;
99 import org.apache.avalon.framework.context.Context;
100 import org.apache.avalon.framework.logger.Logger;
101 import org.apache.avalon.framework.parameters.Parameters;
102 import org.apache.avalon.framework.service.DefaultServiceManager;
103 import org.apache.avalon.framework.service.ServiceManager;
104 import org.apache.excalibur.instrument.InstrumentManager;
105 import org.codehaus.dna.AbstractLogEnabled;
106 import org.codehaus.loom.components.util.info.ComponentInfo;
107 import org.codehaus.loom.components.util.info.DependencyDescriptor;
108 import org.codehaus.loom.components.util.lifecycle.ResourceProvider;
109 import org.codehaus.loom.components.util.metadata.ComponentTemplate;
110 import org.codehaus.loom.components.util.metadata.DependencyDirective;
111 import org.codehaus.loom.interfaces.Application;
112 import org.codehaus.loom.interfaces.ApplicationContext;
113 import org.codehaus.spice.alchemist.configuration.ConfigurationAlchemist;
114
115 /**
116  * The accessor used to access resources for a particular Block or Listener.
117  *
118  * @author Peter Donald
119  * @version $Revision: 1.4 $ $Date: 2004/08/17 23:14:32 $
120  */

121 class BlockResourceProvider
122     extends AbstractLogEnabled
123     implements ResourceProvider
124 {
125     /** Context in which Blocks/Listeners operate. */
126     private final ApplicationContext m_context;
127
128     /**
129      * The Application which this phase is associated with. Required to build a
130      * ComponentManager.
131      */

132     private final Application m_application;
133
134     public BlockResourceProvider( final ApplicationContext context,
135                                   final Application application )
136     {
137         if( null == context )
138         {
139             throw new NullPointerException JavaDoc( "context" );
140         }
141
142         if( null == application )
143         {
144             throw new NullPointerException JavaDoc( "application" );
145         }
146
147         m_context = context;
148         m_application = application;
149     }
150
151     /**
152      * Create Block for specified entry.
153      *
154      * @param entry the entry
155      * @return a new object
156      */

157     public Object JavaDoc createObject( final Object JavaDoc entry )
158         throws Exception JavaDoc
159     {
160         final Class JavaDoc clazz = getBlockEntry( entry ).getInfo().getType();
161         return clazz.newInstance();
162     }
163
164     /**
165      * Retrieve Logger for specified block.
166      *
167      * @param entry the entry representing block
168      * @return the new Logger object
169      * @throws Exception if an error occurs
170      */

171     public Logger createLogger( final Object JavaDoc entry )
172         throws Exception JavaDoc
173     {
174         final String JavaDoc name = getBlockEntry( entry ).getName();
175         return m_context.getLogger( name );
176     }
177
178     /**
179      * Create a new InstrumentMaanger object for component.
180      *
181      * @param entry the entry
182      * @return a new InstrumentManager object for component
183      * @throws Exception if unable to create resource
184      */

185     public InstrumentManager createInstrumentManager( Object JavaDoc entry )
186         throws Exception JavaDoc
187     {
188         return m_context.getInstrumentManager();
189     }
190
191     /**
192      * Create a name for this components instrumentables.
193      *
194      * @param entry the entry
195      * @return the String to use as the instrumentable name
196      * @throws Exception if unable to create resource
197      */

198     public String JavaDoc createInstrumentableName( Object JavaDoc entry )
199         throws Exception JavaDoc
200     {
201         final String JavaDoc name = getBlockEntry( entry ).getName();
202         return m_context.getInstrumentableName( name );
203     }
204
205     /**
206      * Create a BlockContext object for Block.
207      *
208      * @param entry the entry representing block
209      * @return the created BlockContext
210      */

211     public Context createContext( final Object JavaDoc entry )
212         throws Exception JavaDoc
213     {
214         return new DefaultBlockContext( getBlockEntry( entry ).getName(),
215                                         m_context );
216     }
217
218     /**
219      * Create a ComponentManager object for a specific Block. This requires that
220      * for each dependency a reference to providing Block is aaqiured from the
221      * Application and placing it in {@link ComponentManager} under the correct
222      * name.
223      *
224      * @param entry the entry representing block
225      * @return the created ComponentManager
226      */

227     public ComponentManager createComponentManager( final Object JavaDoc entry )
228         throws Exception JavaDoc
229     {
230         final ServiceManager serviceManager = createServiceManager( entry );
231         return new WrapperComponentManager( serviceManager );
232     }
233
234     /**
235      * Create a {@link ServiceManager} object for a specific Block. This
236      * requires that for each dependency a reference to providing Block is
237      * aaqiured from the Application and placing it in {@link ServiceManager}
238      * under the correct name.
239      *
240      * @param entry the entry representing block
241      * @return the created ServiceManager
242      */

243     public ServiceManager createServiceManager( final Object JavaDoc entry )
244         throws Exception JavaDoc
245     {
246         final Map JavaDoc serviceMap = createServiceMap( entry );
247         final DefaultServiceManager manager = new DefaultServiceManager();
248
249         final Iterator JavaDoc iterator = serviceMap.keySet().iterator();
250         while( iterator.hasNext() )
251         {
252             final String JavaDoc key = (String JavaDoc)iterator.next();
253             final Object JavaDoc value = serviceMap.get( key );
254             manager.put( key, value );
255         }
256
257         return manager;
258     }
259
260     private Map JavaDoc createServiceMap( final Object JavaDoc entry )
261         throws Exception JavaDoc
262     {
263         final BlockEntry blockEntry = getBlockEntry( entry );
264         final HashMap JavaDoc map = new HashMap JavaDoc();
265         final HashMap JavaDoc sets = new HashMap JavaDoc();
266
267         final ComponentInfo info = blockEntry.getInfo();
268         final DependencyDirective[] roles = blockEntry.getTemplate()
269             .getDependencies();
270
271         for( int i = 0; i < roles.length; i++ )
272         {
273             final DependencyDirective role = roles[ i ];
274             final Object JavaDoc dependency = m_application.getBlock(
275                 role.getProviderName() );
276
277             final String JavaDoc key = role.getKey();
278             final DependencyDescriptor candidate =
279                 info.getDependency( key );
280
281             if( candidate.isArray() )
282             {
283                 ArrayList JavaDoc list = (ArrayList JavaDoc)sets.get( key );
284                 if( null == list )
285                 {
286                     list = new ArrayList JavaDoc();
287                     sets.put( key, list );
288                 }
289
290                 list.add( dependency );
291             }
292             else if( candidate.isMap() )
293             {
294                 HashMap JavaDoc smap = (HashMap JavaDoc)sets.get( key );
295                 if( null == smap )
296                 {
297                     smap = new HashMap JavaDoc();
298                     sets.put( key, smap );
299                 }
300
301                 smap.put( role.getAlias(), dependency );
302             }
303             else
304             {
305                 map.put( key, dependency );
306             }
307         }
308
309         final Iterator JavaDoc iterator = sets.keySet().iterator();
310         while( iterator.hasNext() )
311         {
312             final String JavaDoc key = (String JavaDoc)iterator.next();
313             final Object JavaDoc value = sets.get( key );
314             if( value instanceof List JavaDoc )
315             {
316                 final List JavaDoc list = (List JavaDoc)value;
317                 final DependencyDescriptor dependency = info.getDependency(
318                     key );
319
320                 final Object JavaDoc[] result = toArray( list,
321                                                  dependency.getComponentType() );
322                 map.put( key, result );
323
324                 if( key.equals( dependency.getType() ) )
325                 {
326                     final String JavaDoc classname =
327                         "[L" + dependency.getComponentType() + ";";
328                     map.put( classname, result );
329                 }
330             }
331             else
332             {
333                 final Map JavaDoc smap =
334                     Collections.unmodifiableMap( (Map JavaDoc)value );
335                 map.put( key, smap );
336             }
337         }
338
339         return map;
340     }
341
342     /**
343      * Convert specified list into array of specified type. Note that the class
344      * for the type must be loaded from same classloader as the elements in the
345      * list are loaded from.
346      *
347      * @param list the list
348      * @param type the classname of type
349      * @return array of objects that are in list
350      * @throws ClassNotFoundException if unable to find correct type
351      */

352     private Object JavaDoc[] toArray( final List JavaDoc list, final String JavaDoc type )
353         throws ClassNotFoundException JavaDoc
354     {
355         final ClassLoader JavaDoc classLoader =
356             list.get( 0 ).getClass().getClassLoader();
357         final Class JavaDoc clazz = classLoader.loadClass( type );
358         final Object JavaDoc[] elements =
359             (Object JavaDoc[])Array.newInstance( clazz, list.size() );
360         return list.toArray( elements );
361     }
362
363     public Configuration createConfiguration( final Object JavaDoc entry )
364         throws Exception JavaDoc
365     {
366         final ComponentTemplate template = getBlockEntry( entry ).getTemplate();
367         return ConfigurationAlchemist.toAvalonConfiguration(
368             template.getConfiguration() );
369     }
370
371     public Parameters createParameters( final Object JavaDoc entry )
372         throws Exception JavaDoc
373     {
374         final Configuration configuration =
375             createConfiguration( entry );
376         final Parameters parameters =
377             Parameters.fromConfiguration( configuration );
378         parameters.makeReadOnly();
379         return parameters;
380     }
381
382     private BlockEntry getBlockEntry( final Object JavaDoc entry )
383     {
384         return (BlockEntry)entry;
385     }
386 }
387
Popular Tags