KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > util > infobuilder > LegacyBlockInfoReader


1 /*
2  * Copyright (C) The Loom Group. All rights reserved.
3  *
4  * This software is published under the terms of the Loom
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.loom.components.util.infobuilder;
9
10 import java.io.InputStream JavaDoc;
11 import java.util.ArrayList JavaDoc;
12
13 import org.codehaus.loom.components.util.ConfigurationBuilder;
14 import org.codehaus.loom.components.util.info.ComponentInfo;
15 import org.codehaus.loom.components.util.info.DependencyDescriptor;
16 import org.codehaus.loom.components.util.info.SchemaDescriptor;
17 import org.codehaus.loom.components.util.info.ServiceDescriptor;
18 import org.codehaus.spice.salt.i18n.ResourceManager;
19 import org.codehaus.spice.salt.i18n.Resources;
20 import org.codehaus.dna.AbstractLogEnabled;
21 import org.codehaus.dna.Configuration;
22 import org.codehaus.dna.ConfigurationException;
23 import org.codehaus.dna.Logger;
24 import org.xml.sax.InputSource JavaDoc;
25
26 /**
27  * A LegacyBlockInfoReader is responsible for building {@link
28  * org.codehaus.loom.components.util.info.ComponentInfo} objects from <a
29  * HREF="http://jakarta.apache.org/avalon/phoenix">Phoenixs</a> BlockInfo
30  * descriptors. The format for descriptor is specified in the <a
31  * HREF="package-summary.html#external">package summary</a>.
32  *
33  * @author Peter Donald
34  * @version $Revision: 1.3 $ $Date: 2004/07/02 23:59:14 $
35  */

36 public final class LegacyBlockInfoReader
37     extends AbstractLogEnabled
38     implements BlockInfoReader
39 {
40     /**
41      * I18n resources.
42      */

43     private static final Resources REZ = ResourceManager.getPackageResources(
44         LegacyBlockInfoReader.class );
45     private static final String JavaDoc BLOCKINFO_SCHEMA = "-//PHOENIX/Block Info DTD Version 1.0//EN";
46     private final ClassLoader JavaDoc m_classLoader;
47
48     public LegacyBlockInfoReader( final ClassLoader JavaDoc classLoader,
49                                   final Logger logger )
50     {
51         if( null == classLoader )
52         {
53             throw new NullPointerException JavaDoc( "classLoader" );
54         }
55         else if( null == logger )
56         {
57             throw new NullPointerException JavaDoc( "logger" );
58         }
59
60         enableLogging( logger );
61
62         m_classLoader = classLoader;
63     }
64
65     public ComponentInfo buildComponentInfo( final Class JavaDoc type )
66         throws Exception JavaDoc
67     {
68         final String JavaDoc xinfo = type.getName().replace( '.', '/' ) + ".xinfo";
69         final InputStream JavaDoc inputStream = m_classLoader.getResourceAsStream(
70             xinfo );
71
72         if( null == inputStream )
73         {
74             return null;
75         }
76         else
77         {
78             final InputSource JavaDoc input = new InputSource JavaDoc( inputStream );
79             final Configuration configuration = ConfigurationBuilder.build(
80                 input, BLOCKINFO_SCHEMA, getLogger() );
81
82             return build( type, configuration );
83         }
84     }
85
86     /**
87      * Create a {@link org.codehaus.loom.components.util.info.ComponentInfo}
88      * object for specified classname from specified configuration data.
89      *
90      * @param type The Components type
91      * @param info the ComponentInfo configuration
92      * @return the created ComponentInfo
93      * @throws ConfigurationException if an error occurs
94      */

95     private ComponentInfo build( final Class JavaDoc type, final Configuration info )
96         throws Exception JavaDoc
97     {
98         if( getLogger().isDebugEnabled() )
99         {
100             final String JavaDoc message = REZ.format(
101                 "legacy.builder.creating-info.notice", type.getName() );
102             getLogger().debug( message );
103         }
104
105         final String JavaDoc topLevelName = info.getName();
106         if( !topLevelName.equals( "blockinfo" ) )
107         {
108             final String JavaDoc message = REZ.format(
109                 "legacy.bad-toplevel-element.error",
110                 type.getName(),
111                 topLevelName );
112             throw new ConfigurationException( message,
113                                               info.getPath(),
114                                               info.getLocation() );
115         }
116
117         Configuration configuration;
118
119         final ServiceDescriptor[] services = buildServices( info );
120
121         configuration = info.getChild( "dependencies" );
122         final DependencyDescriptor[] dependencies = buildDependencies(
123             type.getName(), configuration );
124
125         if( getLogger().isDebugEnabled() )
126         {
127             final String JavaDoc message = REZ.format( "legacy.created-info.notice",
128                                                type.getName(),
129                                                new Integer JavaDoc( services.length ),
130                                                new Integer JavaDoc(
131                                                    dependencies.length ) );
132             getLogger().debug( message );
133         }
134
135         configuration = info.getChild( "block" );
136         final SchemaDescriptor schema = buildConfigurationSchema(
137             type.getName(), configuration );
138
139         return new ComponentInfo( type, services, dependencies, schema );
140     }
141
142     /**
143      * A utility method to build a descriptor for SchemaDescriptor,
144      * <br/>
145      * Schema type declarations of type 'relax-ng' will be substituted with
146      * the full name space 'http://relaxng.org/ns/structure/1.0'. The 'relax-ng'
147      * schema-type is deprecated and shouldn't be used anymore.
148      *
149      * @return the a descriptor for the SchemaDescriptor,
150      */

151     private SchemaDescriptor buildConfigurationSchema( final String JavaDoc classname,
152       final Configuration configuration )
153     {
154         String JavaDoc schemaType = configuration.getChild( "schema-type" ).getValue( "" );
155         if( "".equals( schemaType ) )
156         {
157             return null;
158         }
159         if( "relax-ng".equals( schemaType ) )
160         {
161              schemaType = "http://relaxng.org/ns/structure/1.0";
162         }
163         final String JavaDoc location = LegacyUtil.getSchemaLocationFor( classname );
164         return new SchemaDescriptor( location, schemaType );
165     }
166
167     /**
168      * A utility method to build an array of {@link DependencyDescriptor}
169      * objects from specified configuration and classname.
170      *
171      * @param classname The classname of Component (used for logging
172      * purposes)
173      * @param configuration the dependencies configuration
174      * @return the created DependencyDescriptor
175      * @throws ConfigurationException if an error occurs
176      */

177     private DependencyDescriptor[] buildDependencies( final String JavaDoc classname,
178                                                       final Configuration configuration )
179         throws ConfigurationException
180     {
181         final Configuration[] elements = configuration.getChildren(
182             "dependency" );
183         final ArrayList JavaDoc dependencies = new ArrayList JavaDoc();
184
185         for( int i = 0; i < elements.length; i++ )
186         {
187             final DependencyDescriptor dependency = buildDependency( classname,
188                                                                      elements[ i ] );
189             dependencies.add( dependency );
190         }
191
192         return (DependencyDescriptor[]) dependencies.toArray(
193             DependencyDescriptor.EMPTY_SET );
194     }
195
196     /**
197      * A utility method to build a {@link DependencyDescriptor} object from
198      * specified configuraiton.
199      *
200      * @param classname The classname of Component (used for logging purposes)
201      * @param dependency the dependency configuration
202      * @return the created DependencyDescriptor
203      * @throws ConfigurationException if an error occurs
204      */

205     private DependencyDescriptor buildDependency( final String JavaDoc classname,
206                                                   final Configuration dependency )
207         throws ConfigurationException
208     {
209         final String JavaDoc implementationKey = dependency.getChild( "service" ).getAttribute(
210             "name" );
211         String JavaDoc key = dependency.getChild( "role" ).getValue( null );
212
213         //default to name of service if key unspecified
214
if( null == key )
215         {
216             key = implementationKey;
217         }
218         else
219         {
220             //If key is specified and it is the same as
221
//service name then warn that it is redundent.
222
if( key.equals( implementationKey ) )
223             {
224                 final String JavaDoc message = REZ.format(
225                     "legacy.builder.redundent-key.notice", classname, key );
226                 getLogger().warn( message );
227             }
228         }
229
230         return new DependencyDescriptor( key, implementationKey, false );
231     }
232
233     /**
234      * A utility method to build an array of {@link ServiceDescriptor} objects
235      * from specified configuraiton.
236      *
237      * @param info the services configuration
238      * @return the created ServiceDescriptor
239      * @throws ConfigurationException if an error occurs
240      */

241     private ServiceDescriptor[] buildServices( final Configuration info )
242         throws ConfigurationException
243     {
244         final ArrayList JavaDoc services = new ArrayList JavaDoc();
245
246         Configuration[] elements = info.getChild( "services" ).getChildren(
247             "service" );
248         for( int i = 0; i < elements.length; i++ )
249         {
250             final ServiceDescriptor service = buildService( elements[ i ],
251                                                             false );
252             services.add( service );
253         }
254         elements =
255         info.getChild( "management-access-points" ).getChildren( "service" );
256         for( int i = 0; i < elements.length; i++ )
257         {
258             final ServiceDescriptor service = buildService( elements[ i ],
259                                                             true );
260             services.add( service );
261         }
262
263         return (ServiceDescriptor[]) services.toArray(
264             ServiceDescriptor.EMPTY_SET );
265     }
266
267     /**
268      * A utility method to build a {@link ServiceDescriptor} object from
269      * specified configuraiton data.
270      *
271      * @param service the service Configuration
272      * @return the created ServiceDescriptor
273      * @throws ConfigurationException if an error occurs
274      */

275     private ServiceDescriptor buildService( final Configuration service,
276                                             final boolean isManagement )
277         throws ConfigurationException
278     {
279         //TODO need to do something if its a management service. Ideally this is what gets looked at later on
280
//when plugging everything back in
281
return new ServiceDescriptor( service.getAttribute( "name" ) );
282     }
283 }
284
Popular Tags