KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.encoding.ser;
18
19 import org.apache.axis.Constants;
20 import org.apache.axis.components.logger.LogFactory;
21 import org.apache.axis.encoding.Deserializer;
22 import org.apache.axis.encoding.DeserializerFactory;
23 import org.apache.axis.i18n.Messages;
24 import org.apache.commons.logging.Log;
25
26 import javax.xml.namespace.QName JavaDoc;
27 import javax.xml.rpc.JAXRPCException JavaDoc;
28 import java.lang.reflect.Constructor JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 /**
35  * Base class for Axis Deserialization Factory classes for code reuse
36  *
37  * @author Rich Scheuerle <scheu@us.ibm.com>
38  */

39 public abstract class BaseDeserializerFactory extends BaseFactory
40     implements DeserializerFactory {
41
42     protected static Log log =
43             LogFactory.getLog(BaseDeserializerFactory.class.getName());
44
45     transient static Vector JavaDoc mechanisms = null;
46     
47     protected Class JavaDoc deserClass = null;
48     protected QName JavaDoc xmlType = null;
49     protected Class JavaDoc javaType = null;
50     
51     transient protected Constructor JavaDoc deserClassConstructor = null;
52     transient protected Method JavaDoc getDeserializer = null;
53
54     /**
55      * Constructor
56      * @param deserClass is the class of the Deserializer
57      */

58     public BaseDeserializerFactory(Class JavaDoc deserClass) {
59         if (!Deserializer.class.isAssignableFrom(deserClass)) {
60             throw new ClassCastException JavaDoc(
61                     Messages.getMessage("BadImplementation00",
62                             deserClass.getName(),
63                             Deserializer.class.getName()));
64         }
65         this.deserClass = deserClass;
66     }
67
68     public BaseDeserializerFactory(Class JavaDoc deserClass,
69                                    QName JavaDoc xmlType,
70                                    Class JavaDoc javaType) {
71         this(deserClass);
72         this.xmlType = xmlType;
73         this.javaType = javaType;
74     }
75
76     public javax.xml.rpc.encoding.Deserializer JavaDoc
77         getDeserializerAs(String JavaDoc mechanismType)
78         throws JAXRPCException JavaDoc {
79         Deserializer deser = null;
80
81         // Need to add code to check against mechanisms vector.
82

83         // Try getting a specialized Deserializer
84
deser = getSpecialized(mechanismType);
85         
86         // Try getting a general purpose Deserializer via constructor
87
// invocation
88
if (deser == null) {
89             deser = getGeneralPurpose(mechanismType);
90         }
91         
92         try {
93             // If not successfull, try newInstance
94
if (deser == null) {
95                 deser = (Deserializer) deserClass.newInstance();
96             }
97         } catch (Exception JavaDoc e) {
98             throw new JAXRPCException JavaDoc(e);
99         }
100         return deser;
101     }
102
103    /**
104      * Obtains a deserializer by invoking <constructor>(javaType, xmlType)
105      * on the deserClass.
106      */

107     protected Deserializer getGeneralPurpose(String JavaDoc mechanismType) {
108         if (javaType != null && xmlType != null) {
109             Constructor JavaDoc deserClassConstructor = getDeserClassConstructor();
110             if (deserClassConstructor != null) {
111                 try {
112                     return (Deserializer)
113                         deserClassConstructor.newInstance(
114                             new Object JavaDoc[] {javaType, xmlType});
115                 } catch (InstantiationException JavaDoc e) {
116                     if(log.isDebugEnabled()) {
117                         log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
118                     }
119                 } catch (IllegalAccessException JavaDoc e) {
120                     if(log.isDebugEnabled()) {
121                         log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
122                     }
123                 } catch (InvocationTargetException JavaDoc e) {
124                     if(log.isDebugEnabled()) {
125                         log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
126                     }
127                 }
128             }
129         }
130         return null;
131     }
132
133     private static final Class JavaDoc[] CLASS_QNAME_CLASS = new Class JavaDoc[] {Class JavaDoc.class, QName JavaDoc.class};
134     
135     /**
136      * return constructor for class if any
137      */

138      private Constructor JavaDoc getConstructor(Class JavaDoc clazz) {
139         try {
140             return clazz.getConstructor(CLASS_QNAME_CLASS);
141         } catch (NoSuchMethodException JavaDoc e) {}
142         return null;
143      }
144
145     /**
146      * Obtains a deserializer by invoking getDeserializer method in the
147      * javaType class or its Helper class.
148      */

149     protected Deserializer getSpecialized(String JavaDoc mechanismType) {
150         if (javaType != null && xmlType != null) {
151             Method JavaDoc getDeserializer = getGetDeserializer();
152             if (getDeserializer != null) {
153                 try {
154                     return (Deserializer)
155                         getDeserializer.invoke(
156                                              null,
157                                              new Object JavaDoc[] {mechanismType,
158                                                            javaType,
159                                                            xmlType});
160                 } catch (IllegalAccessException JavaDoc e) {
161                     if(log.isDebugEnabled()) {
162                         log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
163                     }
164                 } catch (InvocationTargetException JavaDoc e) {
165                     if(log.isDebugEnabled()) {
166                         log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
167                     }
168                 }
169             }
170         }
171         return null;
172     }
173
174     /**
175      * Returns a list of all XML processing mechanism types supported by this DeserializerFactory.
176      *
177      * @return List of unique identifiers for the supported XML processing mechanism types
178      */

179     public Iterator JavaDoc getSupportedMechanismTypes() {
180         if (mechanisms == null) {
181             mechanisms = new Vector JavaDoc(1);
182             mechanisms.add(Constants.AXIS_SAX);
183         }
184         return mechanisms.iterator();
185     }
186
187     /**
188      * Utility method that intospects on a factory class to decide how to
189      * create the factory. Tries in the following order:
190      * public static create(Class javaType, QName xmlType)
191      * public <constructor>(Class javaType, QName xmlType)
192      * public <constructor>()
193      * @param factory class
194      * @param javaType
195      * @param xmlType
196      */

197     public static DeserializerFactory createFactory(Class JavaDoc factory,
198                                                     Class JavaDoc javaType,
199                                                     QName JavaDoc xmlType) {
200         if (factory == null) {
201             return null;
202         }
203
204         try {
205             if (factory == BeanDeserializerFactory.class) {
206                 return new BeanDeserializerFactory(javaType, xmlType);
207             } else if (factory == SimpleDeserializerFactory.class) {
208                 return new SimpleDeserializerFactory(javaType, xmlType);
209             } else if (factory == EnumDeserializerFactory.class) {
210                 return new EnumDeserializerFactory(javaType, xmlType);
211             } else if (factory == ElementDeserializerFactory.class) {
212                 return new ElementDeserializerFactory();
213             } else if (factory == SimpleListDeserializerFactory.class) {
214                 return new SimpleListDeserializerFactory(javaType, xmlType);
215             }
216         } catch (Exception JavaDoc e) {
217             if (log.isDebugEnabled()) {
218                 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
219             }
220             return null;
221         }
222
223         DeserializerFactory df = null;
224         try {
225             Method JavaDoc method =
226                 factory.getMethod("create", CLASS_QNAME_CLASS);
227             df = (DeserializerFactory)
228                 method.invoke(null,
229                               new Object JavaDoc[] {javaType, xmlType});
230         } catch (NoSuchMethodException JavaDoc e) {
231             if(log.isDebugEnabled()) {
232                 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
233             }
234         } catch (IllegalAccessException JavaDoc e) {
235             if(log.isDebugEnabled()) {
236                 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
237             }
238         } catch (InvocationTargetException JavaDoc e) {
239             if(log.isDebugEnabled()) {
240                 log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
241             }
242         }
243
244         if (df == null) {
245             try {
246                 Constructor JavaDoc constructor =
247                     factory.getConstructor(CLASS_QNAME_CLASS);
248                 df = (DeserializerFactory)
249                     constructor.newInstance(
250                         new Object JavaDoc[] {javaType, xmlType});
251             } catch (NoSuchMethodException JavaDoc e) {
252                 if(log.isDebugEnabled()) {
253                     log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
254                 }
255             } catch (InstantiationException JavaDoc e) {
256                 if(log.isDebugEnabled()) {
257                     log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
258                 }
259             } catch (IllegalAccessException JavaDoc e) {
260                 if(log.isDebugEnabled()) {
261                     log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
262                 }
263             } catch (InvocationTargetException JavaDoc e) {
264                 if(log.isDebugEnabled()) {
265                     log.debug(org.apache.axis.utils.Messages.getMessage("exception00"), e);
266                 }
267             }
268         }
269         
270         if (df == null) {
271             try {
272                 df = (DeserializerFactory) factory.newInstance();
273             } catch (InstantiationException JavaDoc e) {
274             } catch (IllegalAccessException JavaDoc e) {}
275         }
276         return df;
277     }
278     /**
279      * Returns the deserClassConstructor.
280      * @return Constructor
281      */

282     protected Constructor JavaDoc getDeserClassConstructor() {
283         if (deserClassConstructor == null) {
284             deserClassConstructor = getConstructor(deserClass);
285         }
286         return deserClassConstructor;
287     }
288
289     /**
290      * Returns the getDeserializer.
291      * @return Method
292      */

293     protected Method JavaDoc getGetDeserializer() {
294         if (getDeserializer == null) {
295             getDeserializer = getMethod(javaType,"getDeserializer");
296         }
297         return getDeserializer;
298     }
299
300 }
301
Popular Tags