KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > encoding > ser > BaseSerializerFactory


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

55
56 package org.jboss.axis.encoding.ser;
57
58 import org.jboss.axis.Constants;
59 import org.jboss.axis.encoding.Serializer;
60 import org.jboss.axis.encoding.SerializerFactory;
61 import org.jboss.axis.utils.Messages;
62
63 import javax.xml.namespace.QName JavaDoc;
64 import javax.xml.rpc.JAXRPCException JavaDoc;
65 import java.lang.reflect.Constructor JavaDoc;
66 import java.lang.reflect.InvocationTargetException JavaDoc;
67 import java.lang.reflect.Method JavaDoc;
68 import java.util.Iterator JavaDoc;
69 import java.util.Vector JavaDoc;
70
71 /**
72  * Base class for Axis Serialization Factory classes for code reuse
73  *
74  * @author Rich Scheuerle <scheu@us.ibm.com>
75  */

76 public abstract class BaseSerializerFactory extends BaseFactory
77         implements SerializerFactory
78 {
79
80    transient static Vector JavaDoc mechanisms = null;
81
82    protected Class JavaDoc serClass = null;
83    protected QName JavaDoc xmlType = null;
84    protected Class JavaDoc javaType = null;
85
86    transient protected Serializer ser = null;
87    transient protected Constructor JavaDoc serClassConstructor = null;
88    transient protected Method JavaDoc getSerializer = null;
89
90    /**
91     * Constructor
92     *
93     * @param serClass is the class of the Serializer
94     * Sharing is only valid for xml primitives.
95     */

96    public BaseSerializerFactory(Class JavaDoc serClass)
97    {
98       this.serClass = serClass;
99    }
100
101    public BaseSerializerFactory(Class JavaDoc serClass,
102                                 QName JavaDoc xmlType, Class JavaDoc javaType)
103    {
104       this(serClass);
105       this.xmlType = xmlType;
106       this.javaType = javaType;
107    }
108
109    public javax.xml.rpc.encoding.Serializer JavaDoc
110            getSerializerAs(String JavaDoc mechanismType)
111            throws JAXRPCException JavaDoc
112    {
113       synchronized (this)
114       {
115          if (ser == null)
116          {
117             ser = getSerializerAsInternal(mechanismType);
118          }
119          return ser;
120       }
121    }
122
123    protected Serializer getSerializerAsInternal(String JavaDoc mechanismType)
124            throws JAXRPCException JavaDoc
125    {
126       // Try getting a specialized Serializer
127
Serializer serializer = getSpecialized(mechanismType);
128
129       // Try getting a general purpose Serializer via constructor
130
// invocation
131
if (serializer == null)
132       {
133          serializer = getGeneralPurpose(mechanismType);
134       }
135
136       try
137       {
138          // If not successfull, try newInstance
139
if (serializer == null)
140          {
141             serializer = (Serializer)serClass.newInstance();
142          }
143       }
144       catch (Exception JavaDoc e)
145       {
146          throw new JAXRPCException JavaDoc(Messages.getMessage("CantGetSerializer",
147                  serClass.getName()),
148                  e);
149       }
150       return serializer;
151    }
152
153    /**
154     * Obtains a serializer by invoking <constructor>(javaType, xmlType)
155     * on the serClass.
156     */

157    protected Serializer getGeneralPurpose(String JavaDoc mechanismType)
158    {
159       if (javaType != null && xmlType != null)
160       {
161          Constructor JavaDoc serClassConstructor = getSerClassConstructor();
162          if (serClassConstructor != null)
163          {
164             try
165             {
166                return (Serializer)
167                        serClassConstructor.newInstance(new Object JavaDoc[]{javaType, xmlType});
168             }
169             catch (InstantiationException JavaDoc e)
170             {
171             }
172             catch (IllegalAccessException JavaDoc e)
173             {
174             }
175             catch (InvocationTargetException JavaDoc e)
176             {
177             }
178          }
179       }
180       return null;
181    }
182
183    /**
184     * return constructor for class if any
185     */

186    private Constructor JavaDoc getConstructor(Class JavaDoc clazz)
187    {
188       try
189       {
190          return clazz.getConstructor(new Class JavaDoc[]{Class JavaDoc.class, QName JavaDoc.class});
191       }
192       catch (NoSuchMethodException JavaDoc e)
193       {
194       }
195       return null;
196    }
197
198    /**
199     * Obtains a serializer by invoking getSerializer method in the
200     * javaType class or its Helper class.
201     */

202    protected Serializer getSpecialized(String JavaDoc mechanismType)
203    {
204       if (javaType != null && xmlType != null)
205       {
206          if (getSerializer != null)
207          {
208             try
209             {
210                return (Serializer)
211                        getSerializer.invoke(null,
212                                new Object JavaDoc[]{mechanismType,
213                                             javaType,
214                                             xmlType});
215             }
216             catch (IllegalAccessException JavaDoc e)
217             {
218             }
219             catch (InvocationTargetException JavaDoc e)
220             {
221             }
222          }
223       }
224       return null;
225    }
226
227    /**
228     * Returns a list of all XML processing mechanism types supported
229     * by this SerializerFactory.
230     *
231     * @return List of unique identifiers for the supported XML
232     * processing mechanism types
233     */

234    public Iterator JavaDoc getSupportedMechanismTypes()
235    {
236       if (mechanisms == null)
237       {
238          mechanisms = new Vector JavaDoc();
239          mechanisms.add(Constants.AXIS_SAX);
240       }
241       return mechanisms.iterator();
242    }
243
244    /**
245     * get xmlType
246     *
247     * @return xmlType QName for this factory
248     */

249    public QName JavaDoc getXMLType()
250    {
251       return xmlType;
252    }
253
254    /**
255     * get javaType
256     *
257     * @return javaType Class for this factory
258     */

259    public Class JavaDoc getJavaType()
260    {
261       return javaType;
262    }
263
264    /**
265     * Utility method that intospects on a factory class to decide how to
266     * create the factory. Tries in the following order:
267     * public static create(Class javaType, QName xmlType)
268     * public <constructor>(Class javaType, QName xmlType)
269     * public <constructor>()
270     *
271     * @param factory class
272     * @param xmlType
273     * @param javaType
274     */

275    public static SerializerFactory createFactory(Class JavaDoc factory,
276                                                  Class JavaDoc javaType,
277                                                  QName JavaDoc xmlType)
278    {
279
280       SerializerFactory sf = null;
281       try
282       {
283          Method JavaDoc method =
284                  factory.getMethod("create",
285                          new Class JavaDoc[]{Class JavaDoc.class, QName JavaDoc.class});
286          sf = (SerializerFactory)
287                  method.invoke(null,
288                          new Object JavaDoc[]{javaType, xmlType});
289       }
290       catch (NoSuchMethodException JavaDoc e)
291       {
292       }
293       catch (IllegalAccessException JavaDoc e)
294       {
295       }
296       catch (InvocationTargetException JavaDoc e)
297       {
298       }
299
300       if (sf == null)
301       {
302          try
303          {
304             Constructor JavaDoc constructor =
305                     factory.getConstructor(new Class JavaDoc[]{Class JavaDoc.class, QName JavaDoc.class});
306             sf = (SerializerFactory)
307                     constructor.newInstance(new Object JavaDoc[]{javaType, xmlType});
308          }
309          catch (NoSuchMethodException JavaDoc e)
310          {
311          }
312          catch (InstantiationException JavaDoc e)
313          {
314          }
315          catch (IllegalAccessException JavaDoc e)
316          {
317          }
318          catch (InvocationTargetException JavaDoc e)
319          {
320          }
321       }
322
323       if (sf == null)
324       {
325          try
326          {
327             sf = (SerializerFactory)factory.newInstance();
328          }
329          catch (InstantiationException JavaDoc e)
330          {
331          }
332          catch (IllegalAccessException JavaDoc e)
333          {
334          }
335       }
336       return sf;
337    }
338
339    /**
340     * Returns the getSerializer.
341     *
342     * @return Method
343     */

344    protected Method JavaDoc getGetSerializer()
345    {
346       if (getSerializer == null)
347       {
348          getSerializer = getMethod(javaType, "getSerializer");
349       }
350       return getSerializer;
351    }
352
353    /**
354     * Returns the serClassConstructor.
355     *
356     * @return Constructor
357     */

358    protected Constructor JavaDoc getSerClassConstructor()
359    {
360       if (serClassConstructor == null)
361       {
362          serClassConstructor = getConstructor(serClass);
363       }
364       return serClassConstructor;
365    }
366
367 }
368
Popular Tags