KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
20
21 import org.apache.axis.description.TypeDesc;
22 import org.apache.axis.encoding.Serializer;
23 import org.apache.axis.utils.BeanPropertyDescriptor;
24 import org.apache.axis.utils.BeanUtils;
25 import org.apache.axis.utils.JavaUtils;
26
27 import javax.xml.namespace.QName JavaDoc;
28 import javax.xml.rpc.JAXRPCException JavaDoc;
29
30 /**
31  * SerializerFactory for Bean
32  *
33  * @author Rich Scheuerle <scheu@us.ibm.com>
34  */

35 public class BeanSerializerFactory extends BaseSerializerFactory {
36
37     protected transient TypeDesc typeDesc = null;
38     protected transient BeanPropertyDescriptor[] propertyDescriptor = null;
39
40     public BeanSerializerFactory(Class JavaDoc javaType, QName JavaDoc xmlType) {
41         super(BeanSerializer.class, xmlType, javaType);
42         init(javaType);
43
44     }
45
46     private void init(Class JavaDoc javaType) {
47         // Sometimes an Enumeration class is registered as a Bean.
48
// If this is the case, silently switch to the EnumSerializer
49
if (JavaUtils.isEnumClass(javaType)) {
50             serClass = EnumSerializer.class;
51         }
52
53         typeDesc = TypeDesc.getTypeDescForClass(javaType);
54
55         if (typeDesc != null) {
56             propertyDescriptor = typeDesc.getPropertyDescriptors();
57         } else {
58             propertyDescriptor = BeanUtils.getPd(javaType, null);
59         }
60     }
61
62     public javax.xml.rpc.encoding.Serializer JavaDoc getSerializerAs(String JavaDoc mechanismType)
63         throws JAXRPCException JavaDoc {
64         return (Serializer) super.getSerializerAs(mechanismType);
65     }
66
67     /**
68      * Optimize construction of a BeanSerializer by caching the
69      * type and property descriptors.
70      */

71     protected Serializer getGeneralPurpose(String JavaDoc mechanismType) {
72         if (javaType == null || xmlType == null) {
73            return super.getGeneralPurpose(mechanismType);
74         }
75
76         if (serClass == EnumSerializer.class) {
77            return super.getGeneralPurpose(mechanismType);
78         }
79
80         return new BeanSerializer(javaType, xmlType, typeDesc,
81                                   propertyDescriptor);
82     }
83
84     private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
85         in.defaultReadObject();
86         init(javaType);
87     }
88
89 }
90
Popular Tags