KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.excalibur.i18n.ResourceManager;
55 import org.apache.avalon.excalibur.i18n.Resources;
56 import org.apache.avalon.meta.info.Service;
57
58 /**
59  * A ServiceBuilder is responsible for building {@link Service}
60  * objects from Configuration objects.
61  *
62  * <p><b>UML</b></p>
63  * <p><image SRC="doc-files/ServiceBuilder.gif" border="0"/></p>
64  *
65  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
66  * @version $Revision: 1.1.1.1 $ $Date: 2003/07/10 12:10:04 $
67  */

68 public final class ServiceBuilder
69 {
70     private static final Resources REZ =
71         ResourceManager.getPackageResources( ServiceBuilder.class );
72
73     private final ServiceCreator m_xmlServiceCreator = createXMLServiceCreator();
74     private final ServiceCreator m_serialServiceCreator = new SerializedServiceCreator();
75
76     /**
77      * Create a {@link Service} object for specified Class.
78      *
79      * @param clazz The class of Component
80      * @return the created Service
81      * @throws Exception if an error occurs
82      */

83     public Service build( final Class JavaDoc clazz )
84         throws Exception JavaDoc
85     {
86         return build( clazz.getName(), clazz.getClassLoader() );
87     }
88
89     /**
90      * Create a {@link Service} object for specified
91      * classname, in specified ClassLoader.
92      *
93      * @param classname The classname of Component
94      * @param classLoader the ClassLoader to load info from
95      * @return the created Service
96      * @throws Exception if an error occurs
97      */

98     public Service build( final String JavaDoc classname,
99                                     final ClassLoader JavaDoc classLoader )
100         throws Exception JavaDoc
101     {
102         final Service info = buildFromSerDescriptor( classname, classLoader );
103         if( null != info )
104         {
105             return info;
106         }
107         else
108         {
109             return buildFromXMLDescriptor( classname, classLoader );
110         }
111     }
112
113     /**
114      * Build Service from the XML descriptor format.
115      *
116      * @param classname The classname of Component
117      * @param classLoader the ClassLoader to load info from
118      * @return the created Service
119      * @throws Exception if an error occurs
120      */

121     private Service buildFromSerDescriptor( final String JavaDoc classname,
122                                                       final ClassLoader JavaDoc classLoader )
123         throws Exception JavaDoc
124     {
125         final String JavaDoc xinfo =
126             classname.replace( '.', '/' ) + ".sinfo";
127         final InputStream JavaDoc inputStream =
128             classLoader.getResourceAsStream( xinfo );
129         if( null == inputStream )
130         {
131             return null;
132         }
133
134         return m_serialServiceCreator.createService( classname, inputStream );
135     }
136
137     /**
138      * Build Service from the XML descriptor format.
139      *
140      * @param classname The classname of Component
141      * @param classLoader the ClassLoader to load info from
142      * @return the created Service
143      * @throws Exception if an error occurs
144      */

145     private Service buildFromXMLDescriptor( final String JavaDoc classname,
146                                                       final ClassLoader JavaDoc classLoader )
147         throws Exception JavaDoc
148     {
149         //
150
// get the input stream for the .xservice resource
151
//
152

153         final String JavaDoc xservice =
154             classname.replace( '.', '/' ) + ".xservice";
155         final InputStream JavaDoc inputStream =
156             classLoader.getResourceAsStream( xservice );
157
158         if( null == inputStream )
159         {
160             final String JavaDoc message =
161                 REZ.getString( "builder.missing-info.error",
162                                classname );
163             throw new Exception JavaDoc( message );
164         }
165
166         //
167
// build the type
168
//
169

170         final ServiceCreator xmlServiceCreator = getXMLServiceCreator( classname );
171         return xmlServiceCreator.createService( classname, inputStream );
172     }
173
174     /**
175      * Utility to get xml info builder, else throw
176      * an exception if missing descriptor.
177      *
178      * @return the ServiceCreator
179      */

180     private ServiceCreator getXMLServiceCreator( final String JavaDoc classname )
181         throws Exception JavaDoc
182     {
183         if( null != m_xmlServiceCreator )
184         {
185             return m_xmlServiceCreator;
186         }
187         else
188         {
189             final String JavaDoc message =
190                 REZ.getString( "builder.missing-xml-creator.error",
191                                classname );
192             throw new Exception JavaDoc( message );
193         }
194     }
195
196     /**
197      * Utility to get XMLServiceCreator if XML files are on
198      * ClassPath.
199      *
200      * @return the XML {@link ServiceCreator}
201      */

202     private static ServiceCreator createXMLServiceCreator()
203     {
204         ServiceCreator xmlServiceCreator = null;
205         try
206         {
207             xmlServiceCreator = new XMLServiceCreator();
208         }
209         catch( final Exception JavaDoc e )
210         {
211             //Ignore it if ClassNot found due to no
212
//XML Classes on classpath
213
}
214         return xmlServiceCreator;
215     }
216 }
217
Popular Tags