KickJava   Java API By Example, From Geeks To Geeks.

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


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.Deserializer;
60 import org.jboss.axis.encoding.DeserializerFactory;
61
62 import javax.xml.namespace.QName JavaDoc;
63 import javax.xml.rpc.JAXRPCException JavaDoc;
64 import java.lang.reflect.Constructor JavaDoc;
65 import java.lang.reflect.InvocationTargetException JavaDoc;
66 import java.lang.reflect.Method JavaDoc;
67 import java.util.Iterator JavaDoc;
68 import java.util.Vector JavaDoc;
69
70 /**
71  * Base class for Axis Deserialization Factory classes for code reuse
72  *
73  * @author Rich Scheuerle <scheu@us.ibm.com>
74  */

75 public abstract class BaseDeserializerFactory extends BaseFactory
76         implements DeserializerFactory
77 {
78
79    transient static Vector JavaDoc mechanisms = null;
80
81    protected Class JavaDoc deserClass = null;
82    protected QName JavaDoc xmlType = null;
83    protected Class JavaDoc javaType = null;
84
85    transient protected Constructor JavaDoc deserClassConstructor = null;
86    transient protected Method JavaDoc getDeserializer = null;
87
88    /**
89     * Constructor
90     *
91     * @param deserClass is the class of the Deserializer
92     */

93    public BaseDeserializerFactory(Class JavaDoc deserClass)
94    {
95       this.deserClass = deserClass;
96       this.mechanisms = new Vector JavaDoc();
97       this.mechanisms.add(Constants.AXIS_SAX);
98    }
99
100    public BaseDeserializerFactory(Class JavaDoc deserClass,
101                                   QName JavaDoc xmlType,
102                                   Class JavaDoc javaType)
103    {
104       this(deserClass);
105       this.xmlType = xmlType;
106       this.javaType = javaType;
107    }
108
109    public javax.xml.rpc.encoding.Deserializer JavaDoc
110            getDeserializerAs(String JavaDoc mechanismType)
111            throws JAXRPCException JavaDoc
112    {
113       Deserializer deser = null;
114
115       // Need to add code to check against mechanisms vector.
116

117       // Try getting a specialized Deserializer
118
deser = getSpecialized(mechanismType);
119
120       // Try getting a general purpose Deserializer via constructor
121
// invocation
122
if (deser == null)
123       {
124          deser = getGeneralPurpose(mechanismType);
125       }
126
127       try
128       {
129          // If not successfull, try newInstance
130
if (deser == null)
131          {
132             deser = (Deserializer)deserClass.newInstance();
133          }
134       }
135       catch (Exception JavaDoc e)
136       {
137          throw new JAXRPCException JavaDoc(e);
138       }
139       return deser;
140    }
141
142    /**
143     * Obtains a deserializer by invoking <constructor>(javaType, xmlType)
144     * on the deserClass.
145     */

146    protected Deserializer getGeneralPurpose(String JavaDoc mechanismType)
147    {
148       if (javaType != null && xmlType != null)
149       {
150          Constructor JavaDoc deserClassConstructor = getDeserClassConstructor();
151          if (deserClassConstructor != null)
152          {
153             try
154             {
155                return (Deserializer)
156                        deserClassConstructor.newInstance(new Object JavaDoc[]{javaType, xmlType});
157             }
158             catch (InstantiationException JavaDoc e)
159             {
160             }
161             catch (IllegalAccessException JavaDoc e)
162             {
163             }
164             catch (InvocationTargetException JavaDoc e)
165             {
166             }
167          }
168       }
169       return null;
170    }
171
172    /**
173     * return constructor for class if any
174     */

175    private Constructor JavaDoc getConstructor(Class JavaDoc clazz)
176    {
177       try
178       {
179          return clazz.getConstructor(new Class JavaDoc[]{Class JavaDoc.class, QName JavaDoc.class});
180       }
181       catch (NoSuchMethodException JavaDoc e)
182       {
183       }
184       return null;
185    }
186
187    /**
188     * Obtains a deserializer by invoking getDeserializer method in the
189     * javaType class or its Helper class.
190     */

191    protected Deserializer getSpecialized(String JavaDoc mechanismType)
192    {
193       if (javaType != null && xmlType != null)
194       {
195          Method JavaDoc getDeserializer = getGetDeserializer();
196          if (getDeserializer != null)
197          {
198             try
199             {
200                return (Deserializer)
201                        getDeserializer.invoke(null,
202                                new Object JavaDoc[]{mechanismType,
203                                             javaType,
204                                             xmlType});
205             }
206             catch (IllegalAccessException JavaDoc e)
207             {
208             }
209             catch (InvocationTargetException JavaDoc e)
210             {
211             }
212          }
213       }
214       return null;
215    }
216
217    /**
218     * Returns a list of all XML processing mechanism types supported by this DeserializerFactory.
219     *
220     * @return List of unique identifiers for the supported XML processing mechanism types
221     */

222    public Iterator JavaDoc getSupportedMechanismTypes()
223    {
224       return mechanisms.iterator();
225    }
226
227    /**
228     * Utility method that intospects on a factory class to decide how to
229     * create the factory. Tries in the following order:
230     * public static create(Class javaType, QName xmlType)
231     * public <constructor>(Class javaType, QName xmlType)
232     * public <constructor>()
233     *
234     * @param factory class
235     * @param javaType
236     * @param xmlType
237     */

238    public static DeserializerFactory createFactory(Class JavaDoc factory,
239                                                    Class JavaDoc javaType,
240                                                    QName JavaDoc xmlType)
241    {
242
243       DeserializerFactory df = null;
244       try
245       {
246          Method JavaDoc method =
247                  factory.getMethod("create",
248                          new Class JavaDoc[]{Class JavaDoc.class, QName JavaDoc.class});
249          df = (DeserializerFactory)
250                  method.invoke(null,
251                          new Object JavaDoc[]{javaType, xmlType});
252       }
253       catch (NoSuchMethodException JavaDoc e)
254       {
255       }
256       catch (IllegalAccessException JavaDoc e)
257       {
258       }
259       catch (InvocationTargetException JavaDoc e)
260       {
261       }
262
263       if (df == null)
264       {
265          try
266          {
267             Constructor JavaDoc constructor =
268                     factory.getConstructor(new Class JavaDoc[]{Class JavaDoc.class, QName JavaDoc.class});
269             df = (DeserializerFactory)
270                     constructor.newInstance(new Object JavaDoc[]{javaType, xmlType});
271          }
272          catch (NoSuchMethodException JavaDoc e)
273          {
274          }
275          catch (InstantiationException JavaDoc e)
276          {
277          }
278          catch (IllegalAccessException JavaDoc e)
279          {
280          }
281          catch (InvocationTargetException JavaDoc e)
282          {
283          }
284       }
285
286       if (df == null)
287       {
288          try
289          {
290             df = (DeserializerFactory)factory.newInstance();
291          }
292          catch (InstantiationException JavaDoc e)
293          {
294          }
295          catch (IllegalAccessException JavaDoc e)
296          {
297          }
298       }
299       return df;
300    }
301
302    /**
303     * Returns the deserClassConstructor.
304     *
305     * @return Constructor
306     */

307    protected Constructor JavaDoc getDeserClassConstructor()
308    {
309       if (deserClassConstructor == null)
310       {
311          deserClassConstructor = getConstructor(deserClass);
312       }
313       return deserClassConstructor;
314    }
315
316    /**
317     * Returns the getDeserializer.
318     *
319     * @return Method
320     */

321    protected Method JavaDoc getGetDeserializer()
322    {
323       if (getDeserializer == null)
324       {
325          getDeserializer = getMethod(javaType, "getDeserializer");
326       }
327       return getDeserializer;
328    }
329
330 }
331
Popular Tags