KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > registration > TypeRegistrar


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.registration;
21
22 import java.io.File JavaDoc;
23 import java.lang.reflect.Array JavaDoc;
24 import java.lang.reflect.Field JavaDoc;
25 import java.lang.reflect.GenericArrayType JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.ParameterizedType JavaDoc;
28 import java.lang.reflect.Type JavaDoc;
29 import java.rmi.Remote JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.apache.log4j.*;
34
35 import javax.jws.soap.SOAPBinding;
36 import javax.xml.namespace.QName JavaDoc;
37 import javax.xml.rpc.encoding.TypeMapping JavaDoc;
38 import javax.xml.rpc.holders.Holder JavaDoc;
39
40 import org.apache.beehive.wsm.databinding.BindingLookupService;
41 import org.apache.beehive.wsm.databinding.GenericHolder;
42
43 public abstract class TypeRegistrar {
44     static Logger logger = Logger.getLogger(TypeRegistrar.class);
45
46     protected TypeMapping JavaDoc mTypeMapping;
47
48     protected BindingLookupService lookupService;
49
50     /**
51      * @param typeMapping
52      * @param lookupService
53      */

54     public TypeRegistrar(TypeMapping JavaDoc typeMapping,
55             BindingLookupService lookupService) {
56         super();
57         mTypeMapping = typeMapping;
58         this.lookupService = lookupService;
59     }
60
61     /**
62      * @param cls
63      * @return QName
64      */

65     protected abstract QName JavaDoc getRegisteredQName(Class JavaDoc cls);
66
67     /**
68      * @param cls
69      * @param q
70      */

71     abstract protected void registerClassAsWithDefaultSearialization(Class JavaDoc cls,
72             QName JavaDoc q, SOAPBinding.Style style,
73             SOAPBinding.Use use);
74
75     /**
76      * @param cls
77      * @param q
78      */

79     abstract protected void registerClassAsXMLBeans(Class JavaDoc cls, QName JavaDoc q);
80
81     /**
82      * @param cls
83      * @param q
84      * @throws ClassNotFoundException
85      * @throws NoSuchMethodException
86      * @throws InstantiationException
87      * @throws IllegalAccessException
88      * @throws InvocationTargetException
89      */

90     abstract protected void registerClassAsDataHandler(Class JavaDoc cls, QName JavaDoc q)
91             throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc,
92             InstantiationException JavaDoc, IllegalAccessException JavaDoc,
93             InvocationTargetException JavaDoc;
94
95     /**
96      * @param cls
97      * @param q
98      */

99     abstract protected void registerClassAsSoapEncodedArray(Class JavaDoc cls, QName JavaDoc q);
100
101     /**
102      * @param cls
103      * @param q
104      * @return boolean
105      */

106     abstract protected boolean classIsRegistered(Class JavaDoc cls, QName JavaDoc q);
107
108     /**
109      * @return QName
110      */

111     abstract public QName JavaDoc getVoidType();
112
113     abstract protected boolean isBuiltInType(Class JavaDoc cls);
114
115     abstract protected QName JavaDoc getBuiltInTypeQname(Class JavaDoc cls);
116
117     public QName JavaDoc registerType(Class JavaDoc cls, SOAPBinding.Style style,
118             SOAPBinding.Use use) {
119         QName JavaDoc q;
120         if (SOAPBinding.Use.ENCODED == use && cls.isArray()) {
121             q = lookupService.class2qname(cls.getComponentType());
122         }
123         else {
124             q = lookupService.class2qname(cls);
125         }
126         return registerType(cls, q, style, use);
127     }
128
129     public QName JavaDoc registerType(Class JavaDoc cls, QName JavaDoc q, SOAPBinding.Style style,
130             SOAPBinding.Use use) {
131         try {
132             logger.debug("Register class: " + cls.getCanonicalName()
133                     + " qName: " + q);
134             if (Void.TYPE.equals(cls))
135                 return null;
136
137             if (isBuiltInType(cls))
138                 return getBuiltInTypeQname(cls);
139             if (q == null)
140                 throw new RuntimeException JavaDoc(
141                         "Invalid registeration requestion qname is null");
142
143             if (cls.isArray()) {
144                 if (SOAPBinding.Use.ENCODED == use) {
145
146                     // The Qname modification was needed to run the GoogleThe qname of the array must be different from the qname of the element.
147
// I don't think the qname really matters, as the array qname wont be in the message
148
// The qname of the array is available in the WSDL, but at this point we don't
149
// have the information, I have added "Array" at the end of the qname, it can
150
// be anything else and the code seems to work (from the client side).
151
// if this needs to be the real name of the array, somehow WSDL information
152
// must be made available to this method.
153
QName JavaDoc arrayQname = new QName JavaDoc(q.getNamespaceURI(), q.getLocalPart()+"Array");
154
155                     if (!classIsRegistered(cls, arrayQname))
156                         registerClassAsSoapEncodedArray(cls, arrayQname);
157                 }
158                 // incase of literal, or encoded register the class also
159
q = registerType(cls.getComponentType(), q, style, use);
160
161             } else if (!classIsRegistered(cls, q)) {
162                 if (org.apache.xmlbeans.XmlObject.class.isAssignableFrom(cls)) {
163                     registerClassAsXMLBeans(cls, q);
164                 }
165                 /*
166                  * NOTE jcolwell@bea.com 2004-Oct-11 -- these datahandler using
167                  * classes are generally already registered but just in case...
168                  */

169                 else if (isActivationEnabled()
170                         && (java.awt.Image JavaDoc.class.isAssignableFrom(cls)
171                                 || getMultipartClass().isAssignableFrom(cls) || getDataHandlerClass()
172                                 .isAssignableFrom(cls))) {
173                     try {
174                         registerClassAsDataHandler(cls, q);
175                     } catch (Exception JavaDoc e) {
176                         /*
177                          * FIXME jcolwell@bea.com 2004-Oct-08 -- log this
178                          * properly
179                          */

180                         e.printStackTrace();
181                     }
182                 } else if (!Remote JavaDoc.class.isAssignableFrom(cls)
183                 /*
184                  * NOTE jcolwell@bea.com 2004-Oct-11 -- java.rmi.Remote is
185                  * prohibited by the jax-rpc spec.
186                  */

187
188                 /*
189                  * NOTE jcolwell@bea.com 2004-Oct-11 -- restricting against File
190                  * since, it doesn't make sense to serialize as a bean. That and
191                  * it causes an infinite loop as it keeps returning itself from
192                  * the getAbsoluteFile and getCanonicalFile calls
193                  */

194                 && !File JavaDoc.class.isAssignableFrom(cls)) {
195                     registerClassAsWithDefaultSearialization(cls, q, style, use);
196                 } else {
197                     throw new RuntimeException JavaDoc("failed to register "
198                             + cls.getName()
199                             + " as a valid web service datatype,"
200                             + " consider using a custom type mapping");
201                 }
202             }
203             // }
204
// }
205
logger.info("Registered class: " + cls.getCanonicalName()
206                     + " qName: " + q);
207             return q;
208         } catch (RuntimeException JavaDoc e) {
209             logger.error("Failed to register class: " + cls.getCanonicalName()
210                     + " type: " + q.getNamespaceURI() + ":" + q.getLocalPart());
211
212             e.printStackTrace();
213             throw e;
214         }
215     } // public Class q2Class(QName qType) { //
216

217     private boolean isActivationEnabled() {
218         return null != getDataHandlerClass() && null != getMultipartClass();
219     }
220
221     private Class JavaDoc getDataHandlerClass() {
222         try {
223             return getClass().getClassLoader().loadClass(
224                     "javax.activation.DataHandler");
225         } catch (Exception JavaDoc e) {
226         }
227         return null;
228     }
229
230     private Class JavaDoc getMultipartClass() {
231         try {
232             return getClass().getClassLoader().loadClass(
233                     "javax.mail.internet.MimeMultipart");
234         } catch (Exception JavaDoc e) {
235         }
236         return null;
237     }
238
239     /**
240      * @param t
241      * @throws Exception
242      */

243     static public Class JavaDoc getHoldersValueClass(Type JavaDoc t) {
244         Class JavaDoc res = null;
245
246         if (t instanceof ParameterizedType JavaDoc) {
247             ParameterizedType JavaDoc pt = (ParameterizedType JavaDoc) t;
248             Type JavaDoc raw = pt.getRawType();
249             if (GenericHolder.class.isAssignableFrom((Class JavaDoc) raw)) {
250                 Type JavaDoc[] typeArgs = pt.getActualTypeArguments();
251                 if (typeArgs[0] instanceof GenericArrayType JavaDoc) {
252                     Class JavaDoc arrayElementType = (Class JavaDoc) ((GenericArrayType JavaDoc) typeArgs[0])
253                             .getGenericComponentType();
254                     res = Array.newInstance(arrayElementType, 0).getClass();
255                 } else {
256                     res = (Class JavaDoc) typeArgs[0];
257                 }
258             } else {
259                 throw new RuntimeException JavaDoc(
260                         "Invalid parameterized type for holder: " + t);
261             }
262
263         } else if (t instanceof Class JavaDoc) {
264             if (Holder JavaDoc.class.isAssignableFrom((Class JavaDoc) t)) {
265                 // if type is not a holder type, there is no point to dig any
266
// deeper
267
Field JavaDoc[] publicFields = ((Class JavaDoc) t).getFields();
268                 for (int i = 0; i < publicFields.length; i++) {
269                     if (0 == "value".compareTo(publicFields[i].getName())) {
270                         res = publicFields[i].getType();
271                         break;
272                     }
273                 }
274             } else {
275                 throw new RuntimeException JavaDoc("Invalid class. Type: " + t
276                         + " is not a holder.");
277             }
278         }
279         // other wise just return the type
280
return res;
281     }
282 }
283
Popular Tags