KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > axis > util > AxisTypeMappingUtil


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

22 package org.apache.beehive.wsm.axis.util;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26
27 import java.io.File JavaDoc;
28 import java.io.Serializable JavaDoc;
29
30 import java.rmi.Remote JavaDoc;
31
32 import java.util.Collection JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import javax.jws.WebParam;
38 import javax.xml.namespace.QName JavaDoc;
39 import javax.xml.soap.SOAPHeader JavaDoc;
40 import javax.wsdl.OperationType;
41
42 import org.apache.axis.Message;
43 import org.apache.axis.client.Call;
44 import org.apache.axis.client.Service;
45 import org.apache.axis.client.Stub;
46 import org.apache.axis.description.ElementDesc;
47 import org.apache.axis.description.FaultDesc;
48 import org.apache.axis.description.FieldDesc;
49 import org.apache.axis.description.OperationDesc;
50 import org.apache.axis.description.ParameterDesc;
51 import org.apache.axis.description.TypeDesc;
52 import org.apache.axis.encoding.DeserializerFactory;
53 import org.apache.axis.encoding.SerializerFactory;
54 import org.apache.axis.encoding.TypeMapping;
55 import org.apache.axis.encoding.ser.ArraySerializerFactory;
56 import org.apache.axis.encoding.ser.BeanSerializerFactory;
57 import org.apache.axis.encoding.ser.ArrayDeserializerFactory;
58 import org.apache.axis.encoding.ser.BeanDeserializerFactory;
59 import org.apache.axis.encoding.ser.VectorDeserializerFactory;
60 import org.apache.axis.message.SOAPHeaderElement;
61 import org.apache.axis.utils.BeanPropertyDescriptor;
62 import org.apache.axis.wsdl.fromJava.Namespaces;
63 import org.apache.axis.wsdl.fromJava.Types;
64
65 import org.apache.beehive.wsm.axis.util.encoding.XmlBeanDeserializerFactory;
66 import org.apache.beehive.wsm.axis.util.encoding.XmlBeanSerializerFactory;
67 import org.apache.beehive.wsm.util.InvalidTypeMappingException;
68 import org.apache.beehive.wsm.util.XmlBeanTypeMappingUtil;
69
70 import org.apache.xmlbeans.SchemaType;
71 import org.apache.xmlbeans.SchemaTypeLoader;
72 import org.apache.xmlbeans.XmlBeans;
73 import org.apache.xmlbeans.XmlObject;
74
75 import org.w3c.dom.Element JavaDoc;
76
77 /*******************************************************************************
78  *
79  * @author Jonathan Colwell
80  */

81 public class AxisTypeMappingUtil extends XmlBeanTypeMappingUtil {
82
83     private TypeMapping mTypeMapping;
84
85     public AxisTypeMappingUtil(TypeMapping tm) {
86         super();
87
88         mTypeMapping = tm;
89     }
90
91     public QName JavaDoc registerType(Class JavaDoc cls) throws InvalidTypeMappingException {
92         return registerType(cls, null);
93     }
94
95     public QName JavaDoc registerType(Class JavaDoc cls, QName JavaDoc expectedType)
96         throws InvalidTypeMappingException {
97
98         QName JavaDoc q;
99         
100         if (Void.TYPE.equals(cls)) {
101             q = null;
102         }
103         else {
104             
105             q = mTypeMapping.getTypeQName(cls);
106
107             if (q == null ||
108                 (expectedType != null && !expectedType.equals(q))) {
109       
110                 if (expectedType == null) {
111                     q = generateQName(cls, "http://no.namespace.specified");
112                 }
113                 else {
114                     q = expectedType;
115                 }
116             
117                 if (cls.isArray()) {
118                     if (!mTypeMapping.isRegistered(cls, q)) {
119                         mTypeMapping.register(cls, q,
120                                     new ArraySerializerFactory(cls, q),
121                                     new ArrayDeserializerFactory());
122                     }
123                     q = registerType(cls.getComponentType());
124                     // TODO: fix the expected type thing for arrays.
125
if (expectedType != null) {
126                         q = expectedType;
127                     }
128                 }
129                 else if (!mTypeMapping.isRegistered(cls, q)) {
130                     if (XmlObject.class.isAssignableFrom(cls)) {
131                         mTypeMapping.register(cls, q,
132                                     new XmlBeanSerializerFactory(cls, q),
133                                     new XmlBeanDeserializerFactory(cls, q));
134                     }
135                     /*
136                      * NOTE jcolwell@bea.com 2004-Oct-11 -- these datahandler
137                      * using classes are generally already registered but
138                      * just in case...
139                      */

140                     else if (isActivationEnabled() &&
141                              (java.awt.Image JavaDoc.class.isAssignableFrom(cls)
142                               || getMultipartClass().isAssignableFrom(cls)
143                               || getDataHandlerClass().isAssignableFrom(cls))) {
144                         try {
145                             /*
146                              * NOTE jcolwell@bea.com 2004-Oct-08 -- doing
147                              * reflection here in case AXIS was built without
148                              * attachment support.
149                              */

150                             ClassLoader JavaDoc cl = getClass().getClassLoader();
151                             Class JavaDoc<SerializerFactory> sfClass =
152                                 (Class JavaDoc<SerializerFactory>)
153                                 cl.loadClass("org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory");
154                             Class JavaDoc<DeserializerFactory> dsfClass =
155                                 (Class JavaDoc<DeserializerFactory>)
156                                 cl.loadClass("org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory");
157                             Constructor JavaDoc<SerializerFactory> sfCon =
158                                 sfClass.getConstructor(Class JavaDoc.class,
159                                                        QName JavaDoc.class);
160                             Constructor JavaDoc<DeserializerFactory> dsfCon =
161                                 dsfClass.getConstructor(Class JavaDoc.class,
162                                                         QName JavaDoc.class);
163                             SerializerFactory sf = sfCon.newInstance(cls, q);
164                             DeserializerFactory dsf = dsfCon.newInstance(cls, q);
165                             mTypeMapping.register(cls, q, sf, dsf);
166                         } catch (Exception JavaDoc e) {
167                             /*
168                              * FIXME jcolwell@bea.com 2004-Oct-08 --
169                              * log this properly
170                              */

171                             e.printStackTrace();
172                         }
173                     } else if (! Remote JavaDoc.class.isAssignableFrom(cls)
174                                /*
175                                 * NOTE jcolwell@bea.com 2004-Oct-11 --
176                                 * java.rmi.Remote is prohibited
177                                 * by the jax-rpc spec.
178                                 */

179                     
180                                /*
181                                 * NOTE jcolwell@bea.com 2004-Oct-11 --
182                                 * restricting against File since, it doesn't make
183                                 * sense to serialize as a bean.
184                                 * That and it causes an infinite
185                                 * loop as it keeps returning itself from the
186                                 * getAbsoluteFile and getCanonicalFile calls
187                                 */

188                                && !File JavaDoc.class.isAssignableFrom(cls)) {
189                         TypeDesc td = TypeDesc.getTypeDescForClass(cls);
190                         TypeDesc superTd = null;
191                         BeanPropertyDescriptor[] superPd = null;
192                         if (null == td) {
193                             td = new TypeDesc(cls);
194                             Class JavaDoc supa = cls.getSuperclass();
195                             if ((supa != null)
196                                 && (supa != java.lang.Object JavaDoc.class)
197                                 && (supa != java.lang.Exception JavaDoc.class)
198                                 && (supa != java.lang.Throwable JavaDoc.class)
199                                 && (supa != java.rmi.RemoteException JavaDoc.class)
200                                 && (supa != org.apache.axis.AxisFault.class)) {
201                                 registerType(supa);
202                             }
203                             superTd = TypeDesc
204                                 .getTypeDescForClass(supa);
205                             if (superTd != null) {
206                                 superPd = superTd.getPropertyDescriptors();
207                             }
208                             td.setXmlType(q);
209                             TypeDesc.registerTypeDescForClass(cls, td);
210                         } else {
211                             td = null;
212                         }
213                         mTypeMapping.register(cls, q,
214                                     new BeanSerializerFactory(cls, q),
215                                     /*
216                                      * NOTE jcolwell@bea.com 2004-Oct-11 --
217                                      * should check that the type to deserialize
218                                      * has a default contructor but with this
219                                      * setup there is no way to know if it is
220                                      * used only in serialization.
221                                      */

222                                     new BeanDeserializerFactory(cls, q));
223                         Map JavaDoc serProps = BeanDeserializerFactory
224                             .getProperties(cls, null);
225                         for (BeanPropertyDescriptor beanProps :
226                                  (Collection JavaDoc<BeanPropertyDescriptor>) serProps
227                                  .values()) {
228                             Class JavaDoc subType = beanProps.getType();
229                             if (!(subType.isPrimitive()
230                                   || subType.getName().startsWith("java.")
231                                   || subType.getName().startsWith("javax."))) {
232                                 registerType(subType);
233                             }
234                             if (td != null) {
235                                 String JavaDoc ns = q.getNamespaceURI();
236                                 if (superTd != null && superPd != null) {
237                                     for (int j = 0; j < superPd.length; j++) {
238                                         if (beanProps.getName()
239                                             .equals(superPd[j]
240                                                     .getName())) {
241                                             ns = superTd.getXmlType()
242                                                 .getNamespaceURI();
243                                             break;
244                                         }
245                                     }
246                                 }
247                                 FieldDesc fd = new ElementDesc();
248                                 fd.setJavaType(subType);
249                                 fd.setFieldName(beanProps.getName());
250                                 fd.setXmlName(new QName JavaDoc(ns,
251                                                         beanProps.getName()));
252                                 // NOTE jcolwell@bea.com 2004-Oct-28 -- might need
253
// to do more to ensure a useful type QName.
254
fd.setXmlType(mTypeMapping.getTypeQName(subType));
255                                 ((ElementDesc) fd).setNillable(true);
256                                     // mmerz@apache.com 2005-Mar-09: required since Axis 1.2RC3 to allow for null values in complex objects; note that there is no (JSR-181) annotation that allows configuring this value.
257

258                                 td.addFieldDesc(fd);
259                             }
260                         }
261                     } else {
262                         throw new InvalidTypeMappingException
263                             ("failed to register " + cls.getName()
264                              + " as a valid web service datatype,"
265                              + " consider using a custom type mapping");
266                     }
267                 }
268             }
269         }
270         return q;
271     }
272
273     public Class JavaDoc q2Class(QName JavaDoc qType) {
274
275         Class JavaDoc cls = super.q2Class(qType);
276         if (Object JavaDoc.class.equals(cls) && mTypeMapping != null) {
277             cls = mTypeMapping.getClassForQName(qType);
278             return cls;
279         }
280         else {
281             return null;
282         }
283     }
284
285     private boolean isActivationEnabled() {
286         return null != getDataHandlerClass() && null != getMultipartClass();
287     }
288
289     private Class JavaDoc getDataHandlerClass() {
290         try {
291             return getClass().getClassLoader()
292                     .loadClass("javax.activation.DataHandler");
293         } catch (Exception JavaDoc e) {
294         }
295         return null;
296     }
297
298     private Class JavaDoc getMultipartClass() {
299         try {
300             return getClass().getClassLoader()
301                     .loadClass("javax.mail.internet.MimeMultipart");
302         } catch (Exception JavaDoc e) {
303         }
304         return null;
305     }
306
307     public QName JavaDoc generateQName(Class JavaDoc type, String JavaDoc defaultNS) {
308
309         QName JavaDoc generated = super.generateQName(type, defaultNS);
310         if (generated == null) {
311             String JavaDoc namespace = Namespaces.makeNamespace(type.getName());
312             if (namespace == null || namespace
313                 .endsWith("DefaultNamespace")) {
314                 namespace = defaultNS;
315             }
316             generated = new QName JavaDoc(namespace,
317                                   Types.getLocalNameFromFullName(type
318                                                                  .getName()));
319         }
320         return generated;
321     }
322 }
323
Popular Tags