KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > meta > info > builder > XMLLegacyCreator


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6
7  Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
8
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24
25  4. The names "Jakarta", "Apache Avalon", "Avalon Framework" and
26     "Apache Software Foundation" must not be used to endorse or promote
27     products derived from this software without prior written
28     permission. For written permission, please contact apache@apache.org.
29
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48
49 */

50
51 package org.apache.avalon.meta.info.builder;
52
53 import java.io.InputStream JavaDoc;
54 import java.util.ArrayList JavaDoc;
55 import java.util.Properties JavaDoc;
56 import org.apache.avalon.excalibur.i18n.ResourceManager;
57 import org.apache.avalon.excalibur.i18n.Resources;
58 import org.apache.avalon.framework.Version;
59 import org.apache.avalon.framework.configuration.Configuration;
60 import org.apache.avalon.framework.configuration.ConfigurationException;
61 import org.apache.avalon.meta.ConfigurationBuilder;
62 import org.apache.avalon.meta.info.InfoDescriptor;
63 import org.apache.avalon.meta.info.ContextDescriptor;
64 import org.apache.avalon.meta.info.EntryDescriptor;
65 import org.apache.avalon.meta.info.DependencyDescriptor;
66 import org.apache.avalon.meta.info.ExtensionDescriptor;
67 import org.apache.avalon.meta.info.CategoryDescriptor;
68 import org.apache.avalon.meta.info.ReferenceDescriptor;
69 import org.apache.avalon.meta.info.ServiceDescriptor;
70 import org.apache.avalon.meta.info.StageDescriptor;
71 import org.apache.avalon.meta.info.Type;
72 import org.apache.excalibur.configuration.ConfigurationUtil;
73 import org.xml.sax.InputSource JavaDoc;
74
75 /**
76  * Handles internalization of a legacy Phoenix XML based description of a {@link Type}
77  * from a Configuration object.
78  *
79  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
80  * @version $Revision: 1.11 $ $Date: 2003/08/26 11:38:51 $
81  */

82 public class XMLLegacyCreator
83     extends XMLTypeCreator
84     implements TypeCreator
85 {
86     private static final Resources REZ =
87         ResourceManager.getPackageResources( XMLLegacyCreator.class );
88
89     /**
90      * Create a {@link Type} object for specified
91      * classname, loaded from specified {@link InputStream}.
92      *
93      * @param implementationKey The classname of Component
94      * @param inputStream the InputStream to load Type from
95      * @return the created Type
96      * @throws Exception if an error occurs
97      */

98     public Type createType( String JavaDoc implementationKey,
99                             InputStream JavaDoc inputStream )
100         throws Exception JavaDoc
101     {
102         if( inputStream == null )
103         {
104             throw new NullPointerException JavaDoc( "input" );
105         }
106         final InputSource JavaDoc input = new InputSource JavaDoc( inputStream );
107         final String JavaDoc classname = implementationKey;
108         final Configuration xinfo = ConfigurationBuilder.build( input );
109         return build( classname, xinfo );
110     }
111
112     /**
113      * Create a {@link Type} object for specified
114      * classname and configuration.
115      *
116      * @param classname The classname of the component
117      * @param config the meta info configuration fragment
118      * @return the created Type
119      * @throws ConfigurationException if an error occurs
120      */

121     public Type createType( String JavaDoc classname, Configuration config )
122         throws BuildException
123     {
124         return build( classname, config );
125     }
126
127     /**
128      * Create a {@link Type} object for specified classname from
129      * specified configuration data.
130      *
131      * @param classname The classname of Component
132      * @param info the Type configuration
133      * @return the created Type
134      * @throws BuildException if an error occurs
135      */

136     private Type build( final String JavaDoc classname, final Configuration info )
137         throws BuildException
138     {
139         final String JavaDoc topLevelName = info.getName();
140         if( !topLevelName.equals( "blockinfo" ) )
141         {
142             final String JavaDoc message =
143               REZ.getString( "builder.bad-toplevel-block-element.error",
144                    classname,
145                    topLevelName );
146             throw new BuildException( message );
147         }
148
149         Configuration configuration = null;
150
151         configuration = info.getChild( "block" );
152
153         final InfoDescriptor descriptor =
154           buildInfoDescriptor( classname, configuration );
155
156         configuration = info.getChild( "loggers" );
157         final CategoryDescriptor[] loggers = new CategoryDescriptor[0];
158         final ContextDescriptor context =
159           buildPhoenixContext();
160
161         configuration = info.getChild( "services" );
162         final ServiceDescriptor[] services =
163           buildBlockServices( configuration );
164
165         configuration = info.getChild( "dependencies" );
166         final DependencyDescriptor[] dependencies =
167           buildBlockDependencies( configuration );
168
169         configuration = info.getChild( "stages" );
170         final StageDescriptor[] phases =
171           buildStages( configuration );
172
173         configuration = info.getChild( "extensions" );
174         final ExtensionDescriptor[] extensions =
175           buildExtensions( configuration );
176
177         return new Type(
178             descriptor, loggers, context, services, dependencies,
179             phases, extensions );
180     }
181
182     /**
183      * A utility method to build a {@link InfoDescriptor}
184      * object from specified configuration data and classname.
185      *
186      * @param classname The classname of Component (used to create descriptor)
187      * @param info the component info configuration fragment
188      * @return the created InfoDescriptor
189      * @throws ConfigurationException if an error occurs
190      */

191     public InfoDescriptor buildInfoDescriptor(
192       final String JavaDoc classname, final Configuration info )
193       throws BuildException
194     {
195         final String JavaDoc name =
196           info.getChild( "name" ).getValue( null );
197         final String JavaDoc schema =
198           info.getChild( "schema-type" ).getValue( null );
199         final Version version =
200           buildVersion( info.getChild( "version" ).getValue( "1.0" ) );
201         final Properties JavaDoc attributes =
202             buildAttributes( info.getChild( "attributes" ) );
203         final String JavaDoc lifestyle = InfoDescriptor.SINGLETON;
204         return new InfoDescriptor(
205           name, classname, version, lifestyle, schema, attributes );
206     }
207
208     private ContextDescriptor buildPhoenixContext()
209     {
210         EntryDescriptor name =
211           new EntryDescriptor(
212             ContextDescriptor.NAME_KEY,
213             "java.lang.String", false, false, "block.name" );
214         EntryDescriptor partition =
215           new EntryDescriptor(
216             ContextDescriptor.PARTITION_KEY,
217             "java.lang.String", false, false, "app.name" );
218         EntryDescriptor home =
219           new EntryDescriptor(
220             ContextDescriptor.HOME_KEY,
221             "java.io.File", false, false, "app.home" );
222
223         return new ContextDescriptor( new EntryDescriptor[]{ name, partition, home } );
224     }
225
226     /**
227      * A utility method to build an array of {@link ServiceDescriptor}
228      * objects from specified configuration.
229      *
230      * @param depSet the set of dependency configurations
231      * @return the created dependency descriptor
232      * @throws ConfigurationException if an error occurs
233      */

234     protected DependencyDescriptor[] buildBlockDependencies( final Configuration depSet )
235         throws BuildException
236     {
237         final Configuration[] deps = depSet.getChildren( "dependency" );
238         final ArrayList JavaDoc dependencies = new ArrayList JavaDoc();
239         for( int i = 0; i < deps.length; i++ )
240         {
241             final DependencyDescriptor dependency =
242               buildBlockDependency( deps[ i ] );
243             dependencies.add( dependency );
244         }
245         return (DependencyDescriptor[])dependencies.toArray(
246           new DependencyDescriptor[ 0 ] );
247     }
248
249
250     /**
251      * A utility method to build an array of {@link ServiceDescriptor}
252      * objects from specified configuraiton.
253      *
254      * @param servicesSet the services configuration
255      * @return the created ServiceDescriptor
256      * @throws BuildException if an error occurs
257      */

258     protected ServiceDescriptor[] buildBlockServices( final Configuration servicesSet )
259         throws BuildException
260     {
261         final Configuration[] elements = servicesSet.getChildren( "service" );
262         final ArrayList JavaDoc services = new ArrayList JavaDoc();
263
264         for( int i = 0; i < elements.length; i++ )
265         {
266             final ServiceDescriptor service = buildBlockService( elements[ i ] );
267             services.add( service );
268         }
269
270         return (ServiceDescriptor[])services.toArray( new ServiceDescriptor[ 0 ] );
271     }
272
273     /**
274      * A utility method to build a <code>ServiceDescriptor</code>
275      * object from specified configuraiton data.
276      *
277      * @param service the service Configuration
278      * @return the created ServiceDescriptor
279      * @throws BuildException if an error occurs
280      */

281     protected ServiceDescriptor buildBlockService( final Configuration service )
282         throws BuildException
283     {
284         final ReferenceDescriptor designator = buildReferenceDescriptor( service );
285         final Properties JavaDoc attributes =
286             buildAttributes( service.getChild( "attributes" ) );
287         return new ServiceDescriptor( designator, attributes );
288     }
289
290     /**
291      * A utility method to build a {@link ReferenceDescriptor}
292      * object from specified configuraiton data.
293      *
294      * @param service the service Configuration
295      * @return the created ReferenceDescriptor
296      * @throws ConfigurationException if an error occurs
297      */

298     protected ReferenceDescriptor buildReferenceDescriptor( final Configuration service )
299         throws BuildException
300     {
301         try
302         {
303             final String JavaDoc type = service.getAttribute( "name" );
304             final String JavaDoc versionString = service.getAttribute( "version", "1.0" );
305             final Version version = buildVersion( versionString );
306             return new ReferenceDescriptor( type, version );
307         }
308         catch( Throwable JavaDoc e )
309         {
310             final String JavaDoc error =
311               "Error occured while attempting to build reference descriptor from legacy blockinfo configuration: "
312               + ConfigurationUtil.list( service );
313             throw new BuildException( error, e );
314         }
315     }
316
317     /**
318      * A utility method to build a {@link DependencyDescriptor}
319      * object from specified configuraiton.
320      *
321      * @param dependency the dependency configuration
322      * @return the created DependencyDescriptor
323      * @throws ConfigurationException if an error occurs
324      */

325     protected DependencyDescriptor buildBlockDependency( final Configuration dependency )
326         throws BuildException
327     {
328         try
329         {
330             Configuration serviceRef = dependency.getChild( "service" );
331             final ReferenceDescriptor service =
332               buildReferenceDescriptor( serviceRef );
333             final boolean optional =
334               dependency.getAttributeAsBoolean( "optional", false );
335             final Properties JavaDoc attributes =
336               buildAttributes( dependency.getChild( "attributes" ) );
337             String JavaDoc role = dependency.getChild( "role" ).getValue( null );
338
339             // default to name of service if role unspecified
340
if( null == role ) role = service.getClassname();
341             return new DependencyDescriptor( role, service, optional, attributes );
342         }
343         catch( Throwable JavaDoc e )
344         {
345             final String JavaDoc error =
346               "Error occured while attempting to build dependency descriptor from legacy blockinfo descriptor: "
347               + ConfigurationUtil.list( dependency );
348             throw new BuildException( error, e );
349         }
350     }
351
352 }
353
Popular Tags