KickJava   Java API By Example, From Geeks To Geeks.

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


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.Properties JavaDoc;
55 import java.util.ArrayList 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.Service;
63 import org.apache.avalon.meta.info.EntryDescriptor;
64 import org.apache.avalon.meta.info.ReferenceDescriptor;
65 import org.apache.avalon.meta.info.builder.BuildException;
66 import org.apache.excalibur.configuration.ConfigurationUtil;
67 import org.xml.sax.InputSource JavaDoc;
68
69 /**
70  * Utility class the handles the internalization of an XML description
71  * of a service into a {@link Service} instance.
72  *
73  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
74  * @version $Revision: 1.5 $ $Date: 2003/09/09 22:00:43 $
75  */

76 public class XMLServiceCreator
77     implements ServiceCreator
78 {
79     private static final Resources REZ =
80         ResourceManager.getPackageResources( XMLServiceCreator.class );
81
82     /**
83      * Create a {@link Service} from stream
84      *
85      * @param key the name of the service type
86      * @param input the input stream that the resource is loaded from
87      * @return the created {@link Service}
88      * @exception Exception if an error occurs
89      */

90     public Service createService( String JavaDoc key, InputStream JavaDoc input )
91         throws Exception JavaDoc
92     {
93         if( input == null )
94         {
95             throw new NullPointerException JavaDoc( "input" );
96         }
97
98         final InputSource JavaDoc source = new InputSource JavaDoc( input );
99         final Configuration xservice = ConfigurationBuilder.build( source );
100         return build( key, xservice );
101     }
102
103     /**
104      * Create a {@link Service} object for a supplied classname from
105      * configuration data.
106      *
107      * @param classname the classname of the service
108      * @param info the service defintion
109      * @return the created Service
110      * @throws BuildException if an error occurs
111      */

112     private Service build( final String JavaDoc classname, final Configuration info )
113         throws BuildException
114     {
115         final String JavaDoc topLevelName = info.getName();
116         if( !topLevelName.equals( "service" ) )
117         {
118             final String JavaDoc message =
119                 REZ.getString( "builder.bad-toplevel-service-element.error",
120                                classname,
121                                topLevelName );
122             throw new BuildException( message );
123         }
124
125         final Properties JavaDoc attributes =
126             buildAttributes( info.getChild( "attributes" ) );
127         final EntryDescriptor[] entries =
128             buildEntries( info.getChild( "entries" ).getChildren("entry") );
129         final String JavaDoc versionString = info.getChild( "version" ).getValue( "1.0" );
130         final Version version = buildVersion( versionString );
131
132         return new Service( new ReferenceDescriptor( classname, version ), entries, attributes );
133     }
134
135     /**
136      * Build up a list of attributes from specific config tree.
137      *
138      * @param config the attributes config
139      * @return the Properties object representing attributes
140      * @throws ConfigurationException if an error occurs
141      */

142     public Properties JavaDoc buildAttributes( final Configuration config )
143         throws BuildException
144     {
145         final Properties JavaDoc attributes = new Properties JavaDoc();
146         final Configuration[] children = config.getChildren( "attribute" );
147         for( int i = 0; i < children.length; i++ )
148         {
149             Configuration child = children[ i ];
150             String JavaDoc key = null;
151             try
152             {
153                 key = child.getAttribute( "key" );
154             }
155             catch( ConfigurationException ce )
156             {
157                 final String JavaDoc error =
158                   "Missing 'key' attribute in 'attribute' element.\n"
159                   + ConfigurationUtil.list( child );
160                 throw new BuildException( error, ce );
161             }
162
163             String JavaDoc value = null;
164             try
165             {
166                 value = child.getAttribute( "value" );
167             }
168             catch( Throwable JavaDoc e )
169             {
170                 value = child.getValue( "" );
171             }
172             attributes.setProperty( key, value );
173         }
174         return attributes;
175     }
176
177     /**
178      * A utility method to build an array of {@link EntryDescriptor}
179      * objects from specified configuration.
180      *
181      * @param entrySet the set of entrys to build
182      * @return the created {@link EntryDescriptor}s
183      * @throws ConfigurationException if an error occurs
184      */

185     protected EntryDescriptor[] buildEntries( final Configuration[] entrySet )
186         throws BuildException
187     {
188         final ArrayList JavaDoc entrys = new ArrayList JavaDoc();
189
190         for( int i = 0; i < entrySet.length; i++ )
191         {
192             final EntryDescriptor service = buildEntry( entrySet[ i ] );
193             entrys.add( service );
194         }
195
196         return (EntryDescriptor[])entrys.toArray( new EntryDescriptor[ entrys.size() ] );
197     }
198
199     /**
200      * Create a {@link EntryDescriptor} from configuration.
201      *
202      * @param config the configuration
203      * @return the created {@link EntryDescriptor}
204      * @throws ConfigurationException if an error occurs
205      */

206     protected EntryDescriptor buildEntry( final Configuration config )
207         throws BuildException
208     {
209         try
210         {
211             final String JavaDoc key = config.getAttribute( "key" );
212             final String JavaDoc type = config.getAttribute( "type", "java.lang.String" );
213             final boolean isVolatile = config.getAttributeAsBoolean( "volatile", false );
214             final boolean optional =
215               config.getAttributeAsBoolean( "optional", false );
216             final String JavaDoc alias = config.getAttribute( "alias", null );
217             return new EntryDescriptor( key, type, optional, isVolatile, alias );
218         }
219         catch( Throwable JavaDoc e )
220         {
221             final String JavaDoc error =
222               "Unable to build entry descriptor."
223               + ConfigurationUtil.list( config );
224             throw new BuildException( error, e );
225         }
226     }
227
228     /**
229      * A utility method to parse a Version object from specified string.
230      *
231      * @param version the version string
232      * @return the created Version object
233      */

234     protected Version buildVersion( final String JavaDoc version )
235       throws BuildException
236     {
237         try
238         {
239             return Version.getVersion( version );
240         }
241         catch( Throwable JavaDoc e )
242         {
243             final String JavaDoc error =
244               REZ.getString( "builder.bad-version", version );
245             throw new BuildException( error, e );
246         }
247     }
248 }
249
Popular Tags