Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/axis/encoding/ser/SimpleDeserializerFactory.java


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.utils.BeanUtils;
20  import org.apache.axis.utils.BeanPropertyDescriptor;
21  import org.apache.axis.utils.JavaUtils;
22  
23  import javax.xml.namespace.QName;
24  import javax.xml.rpc.JAXRPCException;
25  import java.lang.reflect.Constructor;
26  import java.io.IOException;
27  
28  /**
29   * A deserializer for any simple type with a (String) constructor.  Note:
30   * this class is designed so that subclasses need only override the makeValue 
31   * method in order to construct objects of their own type.
32   *
33   * @author Glen Daniels (gdaniels@apache.org)
34   * @author Sam Ruby (rubys@us.ibm.com)
35   * Modified for JAX-RPC @author Rich Scheuerle (scheu@us.ibm.com)
36   */
37  public class SimpleDeserializerFactory extends BaseDeserializerFactory {
38  
39      private static final Class[] STRING_STRING_CLASS = 
40          new Class [] {String.class, String.class};
41      
42      private static final Class[] STRING_CLASS = 
43          new Class [] {String.class};
44  
45      private transient Constructor constructor = null;
46      private boolean isBasicType = false;
47      /**
48       * Note that the factory is constructed with the QName and xmlType.  This is important
49       * to allow distinction between primitive values and java.lang wrappers.
50       **/
51      public SimpleDeserializerFactory(Class javaType, QName xmlType) {
52          super(SimpleDeserializer.class, xmlType, javaType);
53          this.isBasicType = JavaUtils.isBasic(javaType);
54          initConstructor(javaType);
55      }
56  
57      private void initConstructor(Class javaType) {
58          if (!this.isBasicType) {
59              // discover the constructor for non basic types
60              try {
61                  if (QName.class.isAssignableFrom(javaType)) {
62                      constructor =
63                          javaType.getDeclaredConstructor(STRING_STRING_CLASS);
64                  } else {
65                      constructor =
66                          javaType.getDeclaredConstructor(STRING_CLASS);
67                  }
68              } catch (NoSuchMethodException e) {
69                  try {
70                      constructor =
71                          javaType.getDeclaredConstructor(new Class[]{});
72                      BeanPropertyDescriptor[] pds = BeanUtils.getPd(javaType);
73                      if(pds != null) {
74                          if(BeanUtils.getSpecificPD(pds,"_value")!=null){
75                              return;
76                          }
77                      }
78                      throw new IllegalArgumentException(e.toString());
79                  } catch (NoSuchMethodException ex) {
80                      throw new IllegalArgumentException(ex.toString());
81                  }
82              }
83          }
84      }
85  
86      /**
87       * Get the Deserializer and the set the Constructor so the
88       * deserializer does not have to do introspection.
89       */
90      public javax.xml.rpc.encoding.Deserializer getDeserializerAs(String mechanismType)
91          throws JAXRPCException {
92          if (javaType == java.lang.Object.class) {
93              return null;
94          }
95          if (this.isBasicType) {
96              return new SimpleDeserializer(javaType, xmlType);
97          } else {
98              // XXX: don't think we can always expect to be SimpleDeserializer
99              // since getSpecialized() might return a different type
100             SimpleDeserializer deser = 
101                 (SimpleDeserializer) super.getDeserializerAs(mechanismType);
102             if (deser != null) {
103                 deser.setConstructor(constructor);
104             }
105             return deser;
106         }
107     }
108 
109     private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
110         in.defaultReadObject();
111         initConstructor(javaType);
112     }
113 
114 }