KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > axis > registration > AxisTypeRegistrar


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

20 package org.apache.beehive.wsm.axis.registration;
21
22 import java.io.File JavaDoc;
23 import java.lang.reflect.Constructor JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Type JavaDoc;
26 import java.rmi.Remote JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.jws.soap.SOAPBinding;
32 import javax.xml.namespace.QName JavaDoc;
33
34 import org.apache.axis.Constants;
35 import org.apache.axis.description.ElementDesc;
36 import org.apache.axis.description.FieldDesc;
37 import org.apache.axis.description.TypeDesc;
38 import org.apache.axis.encoding.DeserializerFactory;
39 import org.apache.axis.encoding.SerializerFactory;
40 import org.apache.axis.encoding.TypeMapping;
41 import org.apache.axis.encoding.XMLType;
42 import org.apache.axis.encoding.ser.ArrayDeserializerFactory;
43 import org.apache.axis.encoding.ser.ArraySerializerFactory;
44 import org.apache.axis.encoding.ser.BeanDeserializerFactory;
45 import org.apache.axis.encoding.ser.BeanSerializerFactory;
46 import org.apache.axis.types.HexBinary;
47 import org.apache.axis.utils.BeanPropertyDescriptor;
48 import org.apache.beehive.wsm.axis.ant.AxisTypeGeneratorTask;
49 import org.apache.beehive.wsm.axis.databinding.AxisTypeLookup;
50 import org.apache.beehive.wsm.axis.util.encoding.XmlBeanDeserializerFactory;
51 import org.apache.beehive.wsm.axis.util.encoding.XmlBeanSerializerFactory;
52 import org.apache.beehive.wsm.databinding.BindingLookupService;
53 import org.apache.beehive.wsm.util.InvalidTypeMappingException;
54 import org.apache.beehive.wsm.registration.TypeRegistrar;
55 import org.apache.log4j.Logger;
56 import org.apache.xmlbeans.XmlObject;
57
58 public class AxisTypeRegistrar extends TypeRegistrar {
59     static Logger logger = Logger.getLogger(AxisTypeRegistrar.class);
60
61     public AxisTypeRegistrar(TypeMapping tm, BindingLookupService lookupService) {
62         super(tm, lookupService);
63  
64     }
65     
66     
67     /**
68      * @param cls
69      * @return boolean
70      */

71     protected boolean isBuiltInType(Class JavaDoc cls) {
72         return (AxisTypeMappingMetaData.isBuiltInType(cls));
73     }
74
75
76     /**
77      * @param cls
78      * @param q
79      */

80     protected void registerClassAsWithDefaultSearialization(Class JavaDoc cls, QName JavaDoc q, SOAPBinding.Style style,
81             SOAPBinding.Use use) {
82         try {
83             logger.debug("Assigned Default Serialization to class: " + cls.getCanonicalName()
84                     + " qname:" + q);
85             TypeDesc td = TypeDesc.getTypeDescForClass(cls); // a class can
86
// provide its
87
// own
88
// descriptors
89
TypeDesc superTd = null;
90             BeanPropertyDescriptor[] superPd = null;
91             boolean shoudRegisterFields = false;
92             if (null == td) {
93                 shoudRegisterFields = true;
94                 td = new TypeDesc(cls);
95                 Class JavaDoc supa = cls.getSuperclass();
96                 if ((supa != null) && (supa != java.lang.Object JavaDoc.class)
97                         && (supa != java.lang.Exception JavaDoc.class)
98                         && (supa != java.lang.Throwable JavaDoc.class)
99                         && (supa != java.rmi.RemoteException JavaDoc.class)
100                         && (supa != org.apache.axis.AxisFault.class)) {
101                     registerType(supa, style, use);
102                     superTd = TypeDesc.getTypeDescForClass(supa);
103                     if (superTd != null) {
104                         superPd = superTd.getPropertyDescriptors();
105                     }
106                 }
107                 td.setXmlType(q);
108                 TypeDesc.registerTypeDescForClass(cls, td);
109             }
110             mTypeMapping.register(cls, q, new BeanSerializerFactory(cls, q),
111             /*
112              * NOTE jcolwell@bea.com 2004-Oct-11 -- should check that the type
113              * to deserialize has a default contructor but with this setup there
114              * is no way to know if it is used only in serialization.
115              */

116             new BeanDeserializerFactory(cls, q));
117             Map JavaDoc serProps = BeanDeserializerFactory.getProperties(cls, null);
118             for (BeanPropertyDescriptor beanProps : (Collection JavaDoc<BeanPropertyDescriptor>) serProps
119                     .values()) {
120                 Class JavaDoc subType = beanProps.getType();
121                 if (!(subType.isPrimitive()
122                         || subType.getName().startsWith("java.") || subType
123                         .getName().startsWith("javax."))) {
124                     registerType(subType, style, use);
125                 }
126                 if (shoudRegisterFields) {
127                     String JavaDoc ns = q.getNamespaceURI();
128                     if (superTd != null && superPd != null) {
129                         for (int j = 0; j < superPd.length; j++) {
130                             if (beanProps.getName()
131                                     .equals(superPd[j].getName())) {
132                                 ns = superTd.getXmlType().getNamespaceURI();
133                                 break;
134                             }
135                         }
136                     }
137                     FieldDesc fd = new ElementDesc();
138                     fd.setJavaType(subType);
139                     fd.setFieldName(beanProps.getName());
140                     fd.setXmlName(new QName JavaDoc(ns, beanProps.getName()));
141                     // NOTE jcolwell@bea.com 2004-Oct-28 -- might
142
// need
143
// to do more to ensure a useful type QName.
144
fd.setXmlType(getRegisteredQName(subType));
145                     ((ElementDesc) fd).setNillable(true);
146                     // mmerz@apache.com 2005-Mar-09: required since Axis 1.2RC3
147
// to allow for null values in complex objects; note that
148
// there is no (JSR-181) annotation that allows configuring
149
// this value.
150

151                     td.addFieldDesc(fd);
152                 }
153             }
154         } catch (RuntimeException JavaDoc e) {
155             logger.error("Error in registering class: "
156                     + cls.getCanonicalName() + " qname:" + q);
157             e.printStackTrace();
158             throw e;
159         }
160     }
161
162     /**
163      * @param cls
164      * @param q
165      */

166     protected void registerClassAsXMLBeans(Class JavaDoc cls, QName JavaDoc q) {
167         logger.debug("Assigned XMLBeans Serialization to class: " + cls.getCanonicalName()
168                 + " qname:" + q); mTypeMapping.register(cls, q, new XmlBeanSerializerFactory(cls, q),
169                 new XmlBeanDeserializerFactory(cls, q));
170     }
171
172     /**
173      * @param cls
174      * @param q
175      * @throws ClassNotFoundException
176      * @throws NoSuchMethodException
177      * @throws InstantiationException
178      * @throws IllegalAccessException
179      * @throws InvocationTargetException
180      */

181     protected void registerClassAsDataHandler(Class JavaDoc cls, QName JavaDoc q)
182             throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc,
183             InstantiationException JavaDoc, IllegalAccessException JavaDoc,
184             InvocationTargetException JavaDoc {
185         logger.debug("Assigned DataHandler to class: " + cls.getCanonicalName()
186                 + " qname:" + q); /*
187          * NOTE jcolwell@bea.com 2004-Oct-08 -- doing reflection here in case
188          * AXIS was built without attachment support.
189          */

190         ClassLoader JavaDoc cl = getClass().getClassLoader();
191         Class JavaDoc<SerializerFactory> sfClass = (Class JavaDoc<SerializerFactory>) cl
192                 .loadClass("org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory");
193         Class JavaDoc<DeserializerFactory> dsfClass = (Class JavaDoc<DeserializerFactory>) cl
194                 .loadClass("org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory");
195         Constructor JavaDoc<SerializerFactory> sfCon = sfClass.getConstructor(
196                 Class JavaDoc.class, QName JavaDoc.class);
197         Constructor JavaDoc<DeserializerFactory> dsfCon = dsfClass.getConstructor(
198                 Class JavaDoc.class, QName JavaDoc.class);
199         SerializerFactory sf = sfCon.newInstance(cls, q);
200         DeserializerFactory dsf = dsfCon.newInstance(cls, q);
201         mTypeMapping.register(cls, q, sf, dsf);
202     }
203
204     /**
205      * @param cls
206      * @param q
207      */

208     protected void registerClassAsSoapEncodedArray(Class JavaDoc cls, QName JavaDoc q) {
209         logger.debug("Assigned Array Serialization to class: " + cls.getCanonicalName()
210                 + " qname:" + q + " Array element qname: " + lookupService.class2qname(cls.getComponentType()));
211                 
212         
213         mTypeMapping.register(cls, q, new ArraySerializerFactory(lookupService.class2qname(cls.getComponentType())),
214                 new ArrayDeserializerFactory());
215     }
216
217     /**
218      * @param cls
219      * @param q
220      * @return boolean
221      */

222     protected boolean classIsRegistered(Class JavaDoc cls, QName JavaDoc q) {
223         return mTypeMapping.isRegistered(cls, q);
224     }
225
226     /**
227      * @param cls
228      * @return QName
229      */

230     protected QName JavaDoc getRegisteredQName(Class JavaDoc cls) {
231         QName JavaDoc currentQName = ((org.apache.axis.encoding.TypeMapping) mTypeMapping)
232                 .getTypeQName(cls);
233         return currentQName;
234     }
235
236     /**
237      * @return QName
238      */

239     public QName JavaDoc getVoidType() {
240         return XMLType.AXIS_VOID;
241
242     }
243     
244     
245  
246
247     /* (non-Javadoc)
248      * @see org.apache.beehive.wsm.registration.TypeRegistrar#getBuiltInTypeQname(java.lang.Class)
249      */

250     protected QName JavaDoc getBuiltInTypeQname(Class JavaDoc cls) {
251         return AxisTypeMappingMetaData.getBuiltInTypeQname(cls);
252     }
253 }
254
Popular Tags