KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > tools > infobuilder > BlockInfoBuilder


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.infobuilder;
9
10 import java.util.ArrayList JavaDoc;
11 import org.apache.avalon.excalibur.i18n.ResourceManager;
12 import org.apache.avalon.excalibur.i18n.Resources;
13 import org.apache.avalon.framework.Version;
14 import org.apache.avalon.framework.configuration.Configuration;
15 import org.apache.avalon.framework.configuration.ConfigurationException;
16 import org.apache.avalon.framework.logger.AbstractLogEnabled;
17 import org.apache.avalon.phoenix.metainfo.BlockDescriptor;
18 import org.apache.avalon.phoenix.metainfo.BlockInfo;
19 import org.apache.avalon.phoenix.metainfo.DependencyDescriptor;
20 import org.apache.avalon.phoenix.metainfo.ServiceDescriptor;
21
22 /**
23  * A BlockInfoBuilder is responsible for building {@link BlockInfo}
24  * objects from Configuration objects. The format for Configuration object
25  * is specified in the BlockInfo specification.
26  *
27  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
28  * @version $Revision: 1.24 $ $Date: 2002/09/06 11:18:59 $
29  */

30 public final class BlockInfoBuilder
31     extends AbstractLogEnabled
32 {
33     private static final Resources REZ =
34         ResourceManager.getPackageResources( BlockInfoBuilder.class );
35
36     /**
37      * Create a {@link BlockInfo} object for specified classname from
38      * specified configuration data.
39      *
40      * @param classname The classname of Block
41      * @param info the BlockInfo configuration
42      * @return the created BlockInfo
43      * @throws ConfigurationException if an error occurs
44      */

45     public BlockInfo build( final String JavaDoc classname, final Configuration info )
46         throws Exception JavaDoc
47     {
48         if( getLogger().isDebugEnabled() )
49         {
50             final String JavaDoc message = REZ.getString( "creating-blockinfo", classname );
51             getLogger().debug( message );
52         }
53
54         Configuration configuration = null;
55
56         configuration = info.getChild( "services" );
57         final ServiceDescriptor[] services = buildServices( configuration );
58
59         configuration = info.getChild( "management-access-points" );
60         final ServiceDescriptor[] management = buildServices( configuration );
61
62         configuration = info.getChild( "dependencies" );
63         final DependencyDescriptor[] dependencies = buildDependencies( classname, configuration );
64
65         configuration = info.getChild( "block" );
66         final BlockDescriptor descriptor = buildBlockDescriptor( classname, configuration );
67
68         if( getLogger().isDebugEnabled() )
69         {
70             final String JavaDoc message = REZ.getString( "blockinfo-created",
71                                                   classname,
72                                                   new Integer JavaDoc( services.length ),
73                                                   new Integer JavaDoc( dependencies.length ) );
74             getLogger().debug( message );
75         }
76
77         return new BlockInfo( descriptor, services, management, dependencies );
78     }
79
80     /**
81      * A utility method to build an array of {@link DependencyDescriptor}
82      * objects from specified configuraiton and classname.
83      *
84      * @param classname The classname of Block (used for logging purposes)
85      * @param configuration the dependencies configuration
86      * @return the created DependencyDescriptor
87      * @throws ConfigurationException if an error occurs
88      */

89     private DependencyDescriptor[] buildDependencies( final String JavaDoc classname,
90                                                       final Configuration configuration )
91         throws ConfigurationException
92     {
93         final Configuration[] elements = configuration.getChildren( "dependency" );
94         final ArrayList JavaDoc dependencies = new ArrayList JavaDoc();
95
96         for( int i = 0; i < elements.length; i++ )
97         {
98             final DependencyDescriptor dependency =
99                 buildDependency( classname, elements[ i ] );
100             dependencies.add( dependency );
101         }
102
103         return (DependencyDescriptor[])dependencies.toArray( new DependencyDescriptor[ 0 ] );
104     }
105
106     /**
107      * A utility method to build a {@link DependencyDescriptor}
108      * object from specified configuraiton.
109      *
110      * @param classname The classname of Block (used for logging purposes)
111      * @param dependency the dependency configuration
112      * @return the created DependencyDescriptor
113      * @throws ConfigurationException if an error occurs
114      */

115     private DependencyDescriptor buildDependency( final String JavaDoc classname,
116                                                   final Configuration dependency )
117         throws ConfigurationException
118     {
119         final ServiceDescriptor service = buildService( dependency.getChild( "service" ) );
120         String JavaDoc role = dependency.getChild( "role" ).getValue( null );
121
122         //default to name of service if role unspecified
123
if( null == role )
124         {
125             role = service.getName();
126         }
127         else
128         {
129             //If role is specified and it is the same as
130
//service name then warn that it is redundent.
131
if( role.equals( service.getName() ) )
132             {
133                 final String JavaDoc message = REZ.getString( "redundent-role", classname, role );
134                 getLogger().warn( message );
135             }
136         }
137
138         return new DependencyDescriptor( role, service );
139     }
140
141     /**
142      * A utility method to build an array of {@link ServiceDescriptor}
143      * objects from specified configuraiton.
144      *
145      * @param servicesSet the services configuration
146      * @return the created ServiceDescriptor
147      * @throws ConfigurationException if an error occurs
148      */

149     private ServiceDescriptor[] buildServices( final Configuration servicesSet )
150         throws ConfigurationException
151     {
152         final Configuration[] elements = servicesSet.getChildren( "service" );
153         final ArrayList JavaDoc services = new ArrayList JavaDoc();
154
155         for( int i = 0; i < elements.length; i++ )
156         {
157             final ServiceDescriptor service = buildService( elements[ i ] );
158             services.add( service );
159         }
160
161         return (ServiceDescriptor[])services.toArray( new ServiceDescriptor[ 0 ] );
162     }
163
164     /**
165      * A utility method to build a {@link ServiceDescriptor}
166      * object from specified configuraiton data.
167      *
168      * @param service the service Configuration
169      * @return the created ServiceDescriptor
170      * @throws ConfigurationException if an error occurs
171      */

172     private ServiceDescriptor buildService( final Configuration service )
173         throws ConfigurationException
174     {
175         final String JavaDoc name = service.getAttribute( "name" );
176         final String JavaDoc versionString = service.getAttribute( "version", "1.0" );
177         final Version version = buildVersion( versionString );
178         return new ServiceDescriptor( name, version );
179     }
180
181     /**
182      * A utility method to build a {@link BlockDescriptor}
183      * object from specified configuraiton data and classname.
184      *
185      * <p>Note that if a &lt;block/&gt; section is not specified then a warning
186      * is generated as previous versions of Phoenix did not require such sections.
187      * In the future this section will be required.</p>
188      *
189      * @param classname The classname of Block (used to create descriptor)
190      * @param block the Block Configuration
191      * @return the created BlockDescriptor
192      * @throws ConfigurationException if an error occurs
193      */

194     private BlockDescriptor buildBlockDescriptor( final String JavaDoc classname,
195                                                   final Configuration block )
196         throws ConfigurationException
197     {
198         if( 0 == block.getChildren().length )
199         {
200             final String JavaDoc message =
201                 REZ.getString( "missing-block", classname );
202             getLogger().warn( message );
203             System.err.println( message );
204             return null;
205         }
206
207         final String JavaDoc name = block.getChild( "name" ).getValue( null );
208         final Version version = buildVersion( block.getChild( "version" ).getValue() );
209         final String JavaDoc schemaType = block.getChild( "schema-type" ).getValue( null );
210
211         return new BlockDescriptor( name, classname, schemaType, version );
212     }
213
214     /**
215      * A utility method to parse a Version object from specified string.
216      *
217      * @param version the version string
218      * @return the created Version object
219      */

220     private Version buildVersion( final String JavaDoc version )
221     {
222         return Version.getVersion( version );
223     }
224 }
225
Popular Tags