KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > tools > assembler > Assembler


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.tools.assembler;
9
10 import java.io.File JavaDoc;
11 import java.net.URL JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import org.apache.avalon.excalibur.i18n.ResourceManager;
14 import org.apache.avalon.excalibur.i18n.Resources;
15 import org.apache.avalon.framework.configuration.Configuration;
16 import org.apache.avalon.framework.configuration.ConfigurationException;
17 import org.apache.avalon.framework.logger.AbstractLogEnabled;
18 import org.apache.avalon.framework.logger.Logger;
19 import org.apache.avalon.phoenix.metadata.BlockListenerMetaData;
20 import org.apache.avalon.phoenix.metadata.BlockMetaData;
21 import org.apache.avalon.phoenix.metadata.DependencyMetaData;
22 import org.apache.avalon.phoenix.metadata.SarMetaData;
23 import org.apache.avalon.phoenix.metainfo.BlockInfo;
24 import org.apache.avalon.phoenix.tools.configuration.ConfigurationBuilder;
25 import org.apache.avalon.phoenix.tools.infobuilder.BlockInfoBuilder;
26
27 /**
28  * Assemble a {@link SarMetaData} object from a Configuration
29  * object. The Configuration object represents the assembly descriptor
30  * and is in the format specified for <tt>assembly.xml</tt> files.
31  *
32  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
33  * @version $Revision: 1.20 $ $Date: 2002/09/06 11:20:22 $
34  */

35 public class Assembler
36     extends AbstractLogEnabled
37 {
38     private static final Resources REZ =
39         ResourceManager.getPackageResources( Assembler.class );
40
41     private final BlockInfoBuilder m_builder = new BlockInfoBuilder();
42
43     /**
44      * Overidden setLogger() method to setup BlockInfoBuilder
45      * logger simultaneously.
46      *
47      * @param logger the logger to use
48      */

49     public void enableLogging( final Logger logger )
50     {
51         super.enableLogging( logger );
52         setupLogger( m_builder );
53     }
54
55     /**
56      * Create a {@link SarMetaData} object based on specified
57      * name and assembly configuration.
58      *
59      * @param name the name of Sar
60      * @param assembly the assembly configuration object
61      * @param directory the directory Sar installed to
62      * @param classLoader the ClassLoader from which resources
63      * are loaded (such as meta-data).
64      * @return the new SarMetaData
65      * @throws AssemblyException if an error occurs
66      */

67     public SarMetaData assembleSar( final String JavaDoc name,
68                                     final Configuration assembly,
69                                     final File JavaDoc directory,
70                                     final ClassLoader JavaDoc classLoader )
71         throws AssemblyException
72     {
73         final Configuration[] blockConfig = assembly.getChildren( "block" );
74         final BlockMetaData[] blocks = buildBlocks( blockConfig, classLoader );
75
76         final Configuration[] listenerConfig = assembly.getChildren( "listener" );
77         final BlockListenerMetaData[] listeners = buildBlockListeners( listenerConfig );
78
79         return new SarMetaData( name, directory, blocks, listeners );
80     }
81
82     /**
83      * Create an array of {@link BlockMetaData} objects to represent
84      * the &lt;block .../&gt; sections in <tt>assembly.xml</tt>.
85      *
86      * @param blocks the list of Configuration objects for blocks
87      * @return the BlockMetaData array
88      * @throws AssemblyException if an error occurs
89      */

90     private BlockMetaData[] buildBlocks( final Configuration[] blocks,
91                                          final ClassLoader JavaDoc classLoader )
92         throws AssemblyException
93     {
94         final ArrayList JavaDoc blockSet = new ArrayList JavaDoc();
95         for( int i = 0; i < blocks.length; i++ )
96         {
97             final BlockMetaData blockMetaData =
98                 buildBlock( blocks[ i ], classLoader );
99             blockSet.add( blockMetaData );
100         }
101
102         return (BlockMetaData[])blockSet.toArray( new BlockMetaData[ 0 ] );
103     }
104
105     /**
106      * Create a single {@link BlockMetaData} object to represent
107      * specified &lt;block .../&gt; section.
108      *
109      * @param block the Configuration object for block
110      * @return the BlockMetaData object
111      * @throws AssemblyException if an error occurs
112      */

113     private BlockMetaData buildBlock( final Configuration block,
114                                       final ClassLoader JavaDoc classLoader )
115         throws AssemblyException
116     {
117         try
118         {
119             final String JavaDoc name = block.getAttribute( "name" );
120             final String JavaDoc classname = block.getAttribute( "class" );
121             final Configuration[] provides = block.getChildren( "provide" );
122             final Configuration proxy = block.getChild( "proxy" );
123             final boolean disableProxy =
124                 proxy.getAttributeAsBoolean( "disable", false );
125
126             final DependencyMetaData[] roles = buildDependencyMetaDatas( provides );
127             final BlockInfo info = getBlockInfo( name, classname, classLoader );
128
129
130             return new BlockMetaData( name, roles, disableProxy, info );
131         }
132         catch( final ConfigurationException ce )
133         {
134             final String JavaDoc message =
135                 REZ.getString( "block-entry-malformed", block.getLocation(), ce.getMessage() );
136             throw new AssemblyException( message );
137         }
138     }
139
140     /**
141      * Get a BlockInfo for Block with specified name and classname.
142      * The BlockInfo may be loaded from the specified cache otherwise it must be
143      * loaded from specified ClassLoader.
144      *
145      * @param name the name of Block
146      * @param classname the name of Blocks class
147      * @return the BlockInfo for specified block
148      * @throws AssemblyException if an error occurs
149      */

150     private BlockInfo getBlockInfo( final String JavaDoc name,
151                                     final String JavaDoc classname,
152                                     final ClassLoader JavaDoc classLoader )
153         throws AssemblyException
154     {
155         final String JavaDoc resourceName = classname.replace( '.', '/' ) + ".xinfo";
156
157         final String JavaDoc notice = REZ.getString( "loading-blockinfo", resourceName );
158         getLogger().debug( notice );
159
160         final URL JavaDoc resource = classLoader.getResource( resourceName );
161         if( null == resource )
162         {
163             final String JavaDoc message = REZ.getString( "blockinfo-missing", name, resourceName );
164             throw new AssemblyException( message );
165         }
166
167         try
168         {
169             final Configuration info = ConfigurationBuilder.build( resource.toString() );
170
171             return m_builder.build( classname, info );
172         }
173         catch( final Exception JavaDoc e )
174         {
175             final String JavaDoc message =
176                 REZ.getString( "blockinfo-nocreate", name, resourceName, e.getMessage() );
177             throw new AssemblyException( message, e );
178         }
179     }
180
181     /**
182      * Create an array of {@link BlockListenerMetaData} objects to represent
183      * the &lt;listener .../&gt; sections in <tt>assembly.xml</tt>.
184      *
185      * @param listeners the list of Configuration objects for listeners
186      * @return the BlockListenerMetaData array
187      * @throws AssemblyException if an error occurs
188      */

189     private BlockListenerMetaData[] buildBlockListeners( final Configuration[] listeners )
190         throws AssemblyException
191     {
192         final ArrayList JavaDoc listenersMetaData = new ArrayList JavaDoc();
193         for( int i = 0; i < listeners.length; i++ )
194         {
195             final BlockListenerMetaData listener = buildBlockListener( listeners[ i ] );
196             listenersMetaData.add( listener );
197         }
198
199         return (BlockListenerMetaData[])listenersMetaData.toArray( new BlockListenerMetaData[ 0 ] );
200     }
201
202     /**
203      * Create a {@link BlockListenerMetaData} object to represent
204      * the specified &lt;listener .../&gt; section.
205      *
206      * @param listener the Configuration object for listener
207      * @return the BlockListenerMetaData object
208      * @throws AssemblyException if an error occurs
209      */

210     private BlockListenerMetaData buildBlockListener( final Configuration listener )
211         throws AssemblyException
212     {
213         try
214         {
215             final String JavaDoc name = listener.getAttribute( "name" );
216             final String JavaDoc className = listener.getAttribute( "class" );
217
218             return new BlockListenerMetaData( name, className );
219         }
220         catch( final ConfigurationException ce )
221         {
222             final String JavaDoc message =
223                 REZ.getString( "listener-entry-malformed",
224                                listener.getLocation(),
225                                ce.getMessage() );
226             throw new AssemblyException( message );
227         }
228     }
229
230     /**
231      * Helper method to build an array of DependencyMetaDatas from input config data.
232      *
233      * @param provides the set of provides elements for block
234      * @return the created DependencyMetaData array
235      * @throws ConfigurationException if config data is malformed
236      */

237     private DependencyMetaData[] buildDependencyMetaDatas( final Configuration[] provides )
238         throws ConfigurationException
239     {
240         final ArrayList JavaDoc dependencies = new ArrayList JavaDoc();
241         for( int j = 0; j < provides.length; j++ )
242         {
243             final Configuration provide = provides[ j ];
244             final String JavaDoc requiredName = provide.getAttribute( "name" );
245             final String JavaDoc role = provide.getAttribute( "role" );
246
247             dependencies.add( new DependencyMetaData( requiredName, role ) );
248         }
249
250         return (DependencyMetaData[])dependencies.toArray( new DependencyMetaData[ 0 ] );
251     }
252 }
253
Popular Tags