KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > assembler > Assembler


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.assembler;
88
89 import java.util.ArrayList JavaDoc;
90 import java.util.List JavaDoc;
91 import java.util.Map JavaDoc;
92
93 import org.codehaus.loom.components.util.metadata.ComponentTemplate;
94 import org.codehaus.loom.components.util.metadata.DependencyDirective;
95 import org.codehaus.loom.components.util.metadata.MetaDataBuilder;
96 import org.codehaus.loom.components.util.metadata.PartitionTemplate;
97 import org.codehaus.loom.interfaces.ContainerConstants;
98 import org.codehaus.loom.interfaces.LoomException;
99 import org.codehaus.spice.salt.i18n.ResourceManager;
100 import org.codehaus.spice.salt.i18n.Resources;
101 import org.codehaus.dna.Configuration;
102 import org.codehaus.dna.ConfigurationException;
103
104 /**
105  * Assemble a {@link PartitionTemplate} object from a Configuration object. The
106  * Configuration object represents the assembly descriptor and is in the format
107  * specified for <tt>assembly.xml</tt> files.
108  *
109  * @author Peter Donald
110  * @version $Revision: 1.3 $ $Date: 2004/08/17 23:14:32 $
111  */

112 public class Assembler
113     implements MetaDataBuilder
114 {
115     private static final Resources REZ =
116         ResourceManager.getPackageResources( Assembler.class );
117
118     /**
119      * Create a {@link PartitionTemplate} object based on specified name and
120      * assembly configuration. This implementation takes two parameters. {@link
121      * ContainerConstants#ASSEMBLY_NAME} specifies the name of the assembly and
122      * {@link ContainerConstants#ASSEMBLY_DESCRIPTOR} specifies the
123      * configuration tree to use when assembling Partition.
124      *
125      * @param parameters the parameters for constructing assembly
126      * @return the new PartitionTemplate
127      * @throws LoomException if an error occurs
128      */

129     public PartitionTemplate buildAssembly( final Map JavaDoc parameters )
130         throws Exception JavaDoc
131     {
132         final String JavaDoc name =
133             (String JavaDoc)parameters.get( ContainerConstants.ASSEMBLY_NAME );
134         final Configuration assembly =
135             (Configuration)parameters.get(
136                 ContainerConstants.ASSEMBLY_DESCRIPTOR );
137         final Configuration config =
138             (Configuration)parameters.get(
139                 ContainerConstants.CONFIG_DESCRIPTOR );
140         return assembleSar( name, config, assembly );
141     }
142
143     /**
144      * Create a {@link PartitionTemplate} object based on specified name and
145      * assembly configuration.
146      *
147      * @param name the name of Sar
148      * @param assembly the assembly configuration object
149      * @return the new PartitionTemplate
150      * @throws LoomException if an error occurs
151      */

152     private PartitionTemplate assembleSar( final String JavaDoc name,
153                                            final Configuration config,
154                                            final Configuration assembly )
155         throws LoomException
156     {
157         final Configuration[] blockConfig = assembly.getChildren( "block" );
158         final ComponentTemplate[] blocks = buildBlocks( blockConfig, config );
159         final PartitionTemplate blockPartition =
160             new PartitionTemplate( ContainerConstants.BLOCK_PARTITION,
161                                    new String JavaDoc[]{
162                                        ContainerConstants.LISTENER_PARTITION},
163                                    PartitionTemplate.EMPTY_SET,
164                                    blocks );
165
166         final Configuration[] listenerConfig = assembly.getChildren(
167             "listener" );
168         final ComponentTemplate[] listeners = buildBlockListeners(
169             listenerConfig, config );
170         final PartitionTemplate listenerPartition =
171             new PartitionTemplate( ContainerConstants.LISTENER_PARTITION,
172                                    new String JavaDoc[ 0 ],
173                                    PartitionTemplate.EMPTY_SET,
174                                    listeners );
175
176         final PartitionTemplate[] partitions =
177             new PartitionTemplate[]{blockPartition, listenerPartition};
178
179         return new PartitionTemplate( name,
180                                       new String JavaDoc[ 0 ],
181                                       partitions,
182                                       ComponentTemplate.EMPTY_SET );
183     }
184
185     /**
186      * Create an array of {@link ComponentTemplate} objects to represent the
187      * &lt;block .../&gt; sections in <tt>assembly.xml</tt>.
188      *
189      * @param blocks the list of Configuration objects for blocks
190      * @return the BlockMetaData array
191      * @throws LoomException if an error occurs
192      */

193     private ComponentTemplate[] buildBlocks( final Configuration[] blocks,
194                                              final Configuration config )
195         throws LoomException
196     {
197         final ArrayList JavaDoc blockSet = new ArrayList JavaDoc();
198         for( int i = 0; i < blocks.length; i++ )
199         {
200             blockSet.add( buildBlock( blocks[ i ], config ) );
201         }
202
203         return (ComponentTemplate[])blockSet.toArray(
204             new ComponentTemplate[ blockSet.size() ] );
205     }
206
207     /**
208      * Create a single {@link ComponentTemplate} object to represent specified
209      * &lt;block .../&gt; section.
210      *
211      * @param block the Configuration object for block
212      * @return the BlockMetaData object
213      * @throws LoomException if an error occurs
214      */

215     private ComponentTemplate buildBlock( final Configuration block,
216                                           final Configuration config )
217         throws LoomException
218     {
219         try
220         {
221             final String JavaDoc name = block.getAttribute( "name" );
222             final String JavaDoc classname = block.getAttribute( "class" );
223             final Configuration proxy = block.getChild( "proxy" );
224
225             final boolean disableProxy =
226                 proxy.getAttributeAsBoolean( "disable", false );
227
228             final Configuration[] provides = block.getChildren( "provide" );
229             final DependencyDirective[] dependencys = buildDependencies(
230                 provides );
231
232             final Configuration configuration = config.getChild( name );
233
234             return new ComponentTemplate( name, classname,
235                                           dependencys, null,
236                                           configuration,
237                                           disableProxy );
238         }
239         catch( final ConfigurationException ce )
240         {
241             final String JavaDoc message =
242                 REZ.format( "block-entry-malformed",
243                             block.getLocation(),
244                             ce.getMessage() );
245             throw new LoomException( message, ce );
246         }
247     }
248
249     /**
250      * Create an array of {@link ComponentTemplate} objects to represent the
251      * &lt;listener .../&gt; sections in <tt>assembly.xml</tt>.
252      *
253      * @param listenerConfigs the list of Configuration objects for
254      * listenerConfigs
255      * @return the array of listeners
256      * @throws LoomException if an error occurs
257      */

258     private ComponentTemplate[] buildBlockListeners(
259         final Configuration[] listenerConfigs,
260         final Configuration config )
261         throws LoomException
262     {
263         final List JavaDoc listeners = new ArrayList JavaDoc();
264         for( int i = 0; i < listenerConfigs.length; i++ )
265         {
266             final ComponentTemplate listener = buildBlockListener(
267                 listenerConfigs[ i ], config );
268             listeners.add( listener );
269         }
270         return (ComponentTemplate[])listeners.
271             toArray( new ComponentTemplate[ listeners.size() ] );
272     }
273
274     /**
275      * Create a {@link ComponentTemplate} object to represent the specified
276      * &lt;listener .../&gt; section.
277      *
278      * @param listener the Configuration object for listener
279      * @return the BlockListenerMetaData object
280      * @throws LoomException if an error occurs
281      */

282     ComponentTemplate buildBlockListener( final Configuration listener,
283                                           final Configuration config )
284         throws LoomException
285     {
286         try
287         {
288             final String JavaDoc name = listener.getAttribute( "name" );
289             final String JavaDoc classname = listener.getAttribute( "class" );
290             final Configuration configuration = config.getChild( name );
291             return new ComponentTemplate( name, classname,
292                                           DependencyDirective.EMPTY_SET,
293                                           null,
294                                           configuration,
295                                           false );
296         }
297         catch( final ConfigurationException ce )
298         {
299             final String JavaDoc message =
300                 REZ.format( "listener-entry-malformed",
301                             listener.getLocation(),
302                             ce.getMessage() );
303             throw new LoomException( message, ce );
304         }
305     }
306
307     /**
308      * Helper method to build an array of DependencyMetaDatas from input config
309      * data.
310      *
311      * @param provides the set of provides elements for block
312      * @return the created DependencyDirective array
313      * @throws ConfigurationException if config data is malformed
314      */

315     DependencyDirective[] buildDependencies( final Configuration[] provides )
316         throws ConfigurationException
317     {
318         final ArrayList JavaDoc dependencies = new ArrayList JavaDoc();
319         for( int j = 0; j < provides.length; j++ )
320         {
321             final DependencyDirective directive = buildDependency(
322                 provides[ j ] );
323             dependencies.add( directive );
324         }
325
326         return (DependencyDirective[])dependencies.toArray(
327             new DependencyDirective[ dependencies.size() ] );
328     }
329
330     /**
331      * Parse a dependency directive from provide.
332      *
333      * @param provide the provide element
334      * @return the directive
335      * @throws ConfigurationException if element malformed
336      */

337     DependencyDirective buildDependency( final Configuration provide )
338         throws ConfigurationException
339     {
340         final String JavaDoc requiredName = provide.getAttribute( "name" );
341         final String JavaDoc alias = provide.getAttribute( "alias", requiredName );
342         final String JavaDoc key = provide.getAttribute( "role" );
343         return new DependencyDirective( key, requiredName, alias );
344     }
345 }
346
Popular Tags