|
1 /* |
|
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.ws.model; |
|
27 |
|
28 import com.sun.istack.internal.NotNull; |
|
29 import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader; |
|
30 import com.sun.xml.internal.bind.v2.model.nav.Navigator; |
|
31 import com.sun.xml.internal.ws.spi.db.BindingHelper; |
|
32 import com.sun.xml.internal.ws.util.StringUtils; |
|
33 |
|
34 import javax.jws.WebParam; |
|
35 import javax.jws.WebResult; |
|
36 import javax.xml.bind.annotation.*; |
|
37 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; |
|
38 import javax.xml.ws.WebServiceException; |
|
39 import java.lang.annotation.Annotation; |
|
40 import java.lang.reflect.InvocationHandler; |
|
41 import java.lang.reflect.Method; |
|
42 import java.lang.reflect.Proxy; |
|
43 import java.util.*; |
|
44 import java.util.logging.Logger; |
|
45 |
|
46 /** |
|
47 * Finds request/response wrapper and exception bean memebers. |
|
48 * |
|
49 * <p> |
|
50 * It uses JAXB's {@link AnnotationReader}, {@link Navigator} so that |
|
51 * tools can use this with annotation processing, and the runtime can use this with |
|
52 * reflection. |
|
53 * |
|
54 * @author Jitendra Kotamraju |
|
55 */ |
|
56 public abstract class AbstractWrapperBeanGenerator<T,C,M,A extends Comparable> { |
|
57 |
|
58 private static final Logger LOGGER = Logger.getLogger(AbstractWrapperBeanGenerator.class.getName()); |
|
59 |
|
60 private static final String RETURN = "return"; |
|
61 private static final String EMTPY_NAMESPACE_ID = ""; |
|
62 |
|
63 private static final Class[] jaxbAnns = new Class[] { |
|
64 XmlAttachmentRef.class, XmlMimeType.class, XmlJavaTypeAdapter.class, |
|
65 XmlList.class, XmlElement.class |
|
66 }; |
|
67 |
|
68 private static final Set<String> skipProperties = new HashSet<String>(); |
|
69 static{ |
|
70 skipProperties.add("getCause"); |
|
71 skipProperties.add("getLocalizedMessage"); |
|
72 skipProperties.add("getClass"); |
|
73 skipProperties.add("getStackTrace"); |
|
74 skipProperties.add("getSuppressed"); // JDK 7 adds this |
|
75 } |
|
76 |
|
77 private final AnnotationReader<T,C,?,M> annReader; |
|
78 private final Navigator<T,C,?,M> nav; |
|
79 private final BeanMemberFactory<T,A> factory; |
|
80 |
|
81 protected AbstractWrapperBeanGenerator(AnnotationReader<T,C,?,M> annReader, |
|
82 Navigator<T,C,?,M> nav, BeanMemberFactory<T,A> factory) { |
|
83 this.annReader = annReader; |
|
84 this.nav = nav; |
|
85 this.factory = factory; |
|
86 } |
|
87 |
|
88 public static interface BeanMemberFactory<T,A> { |
|
89 A createWrapperBeanMember(T paramType, String paramName, List<Annotation> jaxbAnnotations); |
|
90 } |
|
91 |
|
92 // Collects the JAXB annotations on a method |
|
93 private List<Annotation> collectJAXBAnnotations(M method) { |
|
94 List<Annotation> jaxbAnnotation = new ArrayList<Annotation>(); |
|
95 for(Class jaxbClass : jaxbAnns) { |
|
96 Annotation ann = annReader.getMethodAnnotation(jaxbClass, method, null); |
|
97 if (ann != null) { |
|
98 jaxbAnnotation.add(ann); |
|
99 } |
|
100 } |
|
101 return jaxbAnnotation; |
|
102 } |
|
103 |
|
104 // Collects the JAXB annotations on a parameter |
|
105 private List<Annotation> collectJAXBAnnotations(M method, int paramIndex) { |
|
106 List<Annotation> jaxbAnnotation = new ArrayList<Annotation>(); |
|
107 for(Class jaxbClass : jaxbAnns) { |
|
108 Annotation ann = annReader.getMethodParameterAnnotation(jaxbClass, method, paramIndex, null); |
|
109 if (ann != null) { |
|
110 jaxbAnnotation.add(ann); |
|
111 } |
|
112 } |
|
113 return jaxbAnnotation; |
|
114 } |
|
115 |
|
116 protected abstract T getSafeType(T type); |
|
117 |
|
118 /** |
|
119 * Returns Holder's value type. |
|
120 * |
|
121 * @return null if it not a Holder, otherwise return Holder's value type |
|
122 */ |
|
123 protected abstract T getHolderValueType(T type); |
|
124 |
|
125 protected abstract boolean isVoidType(T type); |
|
126 |
|
127 /** |
|
128 * Computes request bean members for a method. Collects all IN and INOUT |
|
129 * parameters as request bean fields. In this process, if a parameter |
|
130 * has any known JAXB annotations they are collected as well. |
|
131 * Special processing for @XmlElement annotation is done. |
|
132 * |
|
133 * @param method SEI method for which request bean members are computed |
|
134 * @return List of request bean members |
|
135 */ |
|
136 public List<A> collectRequestBeanMembers(M method) { |
|
137 |
|
138 List<A> requestMembers = new ArrayList<A>(); |
|
139 int paramIndex = -1; |
|
140 |
|
141 for (T param : nav.getMethodParameters(method)) { |
|
142 paramIndex++; |
|
143 WebParam webParam = annReader.getMethodParameterAnnotation(WebParam.class, method, paramIndex, null); |
|
144 if (webParam != null && (webParam.header() || webParam.mode().equals(WebParam.Mode.OUT))) { |
|
145 continue; |
|
146 } |
|
147 T holderType = getHolderValueType(param); |
|
148 // if (holderType != null && webParam != null && webParam.mode().equals(WebParam.Mode.IN)) { |
|
149 // // Should we flag an error - holder cannot be IN part ?? |
|
150 // continue; |
|
151 // } |
|
152 |
|
153 T paramType = (holderType != null) ? holderType : getSafeType(param); |
|
154 String paramName = (webParam != null && webParam.name().length() > 0) |
|
155 ? webParam.name() : "arg"+paramIndex; |
|
156 String paramNamespace = (webParam != null && webParam.targetNamespace().length() > 0) |
|
157 ? webParam.targetNamespace() : EMTPY_NAMESPACE_ID; |
|
158 |
|
159 // Collect JAXB annotations on a parameter |
|
160 List<Annotation> jaxbAnnotation = collectJAXBAnnotations(method, paramIndex); |
|
161 |
|
162 // If a parameter contains @XmlElement, process it. |
|
163 processXmlElement(jaxbAnnotation, paramName, paramNamespace, paramType); |
|
164 A member = factory.createWrapperBeanMember(paramType, |
|
165 getPropertyName(paramName), jaxbAnnotation); |
|
166 requestMembers.add(member); |
|
167 } |
|
168 return requestMembers; |
|
169 } |
|
170 |
|
171 /** |
|
172 * Computes response bean members for a method. Collects all OUT and INOUT |
|
173 * parameters as response bean fields. In this process, if a parameter |
|
174 * has any known JAXB annotations they are collected as well. |
|
175 * Special processing for @XmlElement annotation is done. |
|
176 * |
|
177 * @param method SEI method for which response bean members are computed |
|
178 * @return List of response bean members |
|
179 */ |
|
180 public List<A> collectResponseBeanMembers(M method) { |
|
181 |
|
182 List<A> responseMembers = new ArrayList<A>(); |
|
183 |
|
184 // return that need to be part response wrapper bean |
|
185 String responseElementName = RETURN; |
|
186 String responseNamespace = EMTPY_NAMESPACE_ID; |
|
187 boolean isResultHeader = false; |
|
188 WebResult webResult = annReader.getMethodAnnotation(WebResult.class, method ,null); |
|
189 if (webResult != null) { |
|
190 if (webResult.name().length() > 0) { |
|
191 responseElementName = webResult.name(); |
|
192 } |
|
193 if (webResult.targetNamespace().length() > 0) { |
|
194 responseNamespace = webResult.targetNamespace(); |
|
195 } |
|
196 isResultHeader = webResult.header(); |
|
197 } |
|
198 T returnType = getSafeType(nav.getReturnType(method)); |
|
199 if (!isVoidType(returnType) && !isResultHeader) { |
|
200 List<Annotation> jaxbRespAnnotations = collectJAXBAnnotations(method); |
|
201 processXmlElement(jaxbRespAnnotations, responseElementName, responseNamespace, returnType); |
|
202 responseMembers.add(factory.createWrapperBeanMember(returnType, getPropertyName(responseElementName), jaxbRespAnnotations)); |
|
203 } |
|
204 |
|
205 // Now parameters that need to be part response wrapper bean |
|
206 int paramIndex = -1; |
|
207 for (T param : nav.getMethodParameters(method)) { |
|
208 paramIndex++; |
|
209 |
|
210 T paramType = getHolderValueType(param); |
|
211 WebParam webParam = annReader.getMethodParameterAnnotation(WebParam.class, method, paramIndex, null); |
|
212 if (paramType == null || (webParam != null && webParam.header())) { |
|
213 continue; // not a holder or a header - so don't add it |
|
214 } |
|
215 |
|
216 String paramName = (webParam != null && webParam.name().length() > 0) |
|
217 ? webParam.name() : "arg"+paramIndex; |
|
218 String paramNamespace = (webParam != null && webParam.targetNamespace().length() > 0) |
|
219 ? webParam.targetNamespace() : EMTPY_NAMESPACE_ID; |
|
220 List<Annotation> jaxbAnnotation = collectJAXBAnnotations(method, paramIndex); |
|
221 processXmlElement(jaxbAnnotation, paramName, paramNamespace, paramType); |
|
222 A member = factory.createWrapperBeanMember(paramType, |
|
223 getPropertyName(paramName), jaxbAnnotation); |
|
224 responseMembers.add(member); |
|
225 } |
|
226 |
|
227 return responseMembers; |
|
228 } |
|
229 |
|
230 private void processXmlElement(List<Annotation> jaxb, String elemName, String elemNS, T type) { |
|
231 XmlElement elemAnn = null; |
|
232 for (Annotation a : jaxb) { |
|
233 if (a.annotationType() == XmlElement.class) { |
|
234 elemAnn = (XmlElement) a; |
|
235 jaxb.remove(a); |
|
236 break; |
|
237 } |
|
238 } |
|
239 String name = (elemAnn != null && !elemAnn.name().equals("##default")) |
|
240 ? elemAnn.name() : elemName; |
|
241 |
|
242 String ns = (elemAnn != null && !elemAnn.namespace().equals("##default")) |
|
243 ? elemAnn.namespace() : elemNS; |
|
244 |
|
245 boolean nillable = nav.isArray(type) |
|
246 || (elemAnn != null && elemAnn.nillable()); |
|
247 |
|
248 boolean required = elemAnn != null && elemAnn.required(); |
|
249 XmlElementHandler handler = new XmlElementHandler(name, ns, nillable, required); |
|
250 XmlElement elem = (XmlElement) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class<?>[]{XmlElement.class}, handler); |
|
251 jaxb.add(elem); |
|
252 } |
|
253 |
|
254 |
|
255 private static class XmlElementHandler implements InvocationHandler { |
|
256 private String name; |
|
257 private String namespace; |
|
258 private boolean nillable; |
|
259 private boolean required; |
|
260 |
|
261 XmlElementHandler(String name, String namespace, boolean nillable, |
|
262 boolean required) { |
|
263 this.name = name; |
|
264 this.namespace = namespace; |
|
265 this.nillable = nillable; |
|
266 this.required = required; |
|
267 } |
|
268 |
|
269 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
|
270 String methodName = method.getName(); |
|
271 if (methodName.equals("name")) { |
|
272 return name; |
|
273 } else if (methodName.equals("namespace")) { |
|
274 return namespace; |
|
275 } else if (methodName.equals("nillable")) { |
|
276 return nillable; |
|
277 } else if (methodName.equals("required")) { |
|
278 return required; |
|
279 } else { |
|
280 throw new WebServiceException("Not handling "+methodName); |
|
281 } |
|
282 } |
|
283 } |
|
284 |
|
285 /** |
|
286 * Computes and sorts exception bean members for a given exception as per |
|
287 * the 3.7 section of the spec. It takes all getter properties in the |
|
288 * exception and its superclasses(except getCause, getLocalizedMessage, |
|
289 * getStackTrace, getClass). The returned collection is sorted based |
|
290 * on the property names. |
|
291 * |
|
292 * <p> |
|
293 * But if the exception has @XmlType its values are honored. Only the |
|
294 * propOrder properties are considered. The returned collection is sorted |
|
295 * as per the given propOrder. |
|
296 * |
|
297 * @param exception |
|
298 * @return list of properties in the correct order for an exception bean |
|
299 */ |
|
300 public Collection<A> collectExceptionBeanMembers(C exception) { |
|
301 TreeMap<String, A> fields = new TreeMap<String, A>(); |
|
302 getExceptionProperties(exception, fields); |
|
303 |
|
304 // Consider only the @XmlType(propOrder) properties |
|
305 XmlType xmlType = annReader.getClassAnnotation(XmlType.class, exception, null); |
|
306 if (xmlType != null) { |
|
307 String[] propOrder = xmlType.propOrder(); |
|
308 // If not the default order of properties, use that propOrder |
|
309 if (propOrder.length > 0 && propOrder[0].length() != 0) { |
|
310 List<A> list = new ArrayList<A>(); |
|
311 for(String prop : propOrder) { |
|
312 A a = fields.get(prop); |
|
313 if (a != null) { |
|
314 list.add(a); |
|
315 } else { |
|
316 throw new WebServiceException("Exception "+exception+ |
|
317 " has @XmlType and its propOrder contains unknown property "+prop); |
|
318 } |
|
319 } |
|
320 return list; |
|
321 } |
|
322 } |
|
323 |
|
324 return fields.values(); |
|
325 } |
|
326 |
|
327 |
|
328 private void getExceptionProperties(C exception, TreeMap<String, A> fields) { |
|
329 C sc = nav.getSuperClass(exception); |
|
330 if (sc != null) { |
|
331 getExceptionProperties(sc, fields); |
|
332 } |
|
333 Collection<? extends M> methods = nav.getDeclaredMethods(exception); |
|
334 |
|
335 for (M method : methods) { |
|
336 |
|
337 // 2.1.x is doing the following: no final static, transient, non-public |
|
338 // transient cannot used as modifier for method, so not doing it now |
|
339 if (!nav.isPublicMethod(method) |
|
340 || (nav.isStaticMethod(method) && nav.isFinalMethod(method))) { |
|
341 continue; |
|
342 } |
|
343 |
|
344 if (!nav.isPublicMethod(method)) { |
|
345 continue; |
|
346 } |
|
347 |
|
348 String name = nav.getMethodName(method); |
|
349 |
|
350 if (!(name.startsWith("get") || name.startsWith("is")) || skipProperties.contains(name) || |
|
351 name.equals("get") || name.equals("is")) { |
|
352 // Don't bother with invalid propertyNames. |
|
353 continue; |
|
354 } |
|
355 |
|
356 T returnType = getSafeType(nav.getReturnType(method)); |
|
357 if (nav.getMethodParameters(method).length == 0) { |
|
358 String fieldName = name.startsWith("get") |
|
359 ? StringUtils.decapitalize(name.substring(3)) |
|
360 : StringUtils.decapitalize(name.substring(2)); |
|
361 fields.put(fieldName, factory.createWrapperBeanMember(returnType, fieldName, Collections.<Annotation>emptyList())); |
|
362 } |
|
363 } |
|
364 |
|
365 } |
|
366 |
|
367 /** |
|
368 * Gets the property name by mangling using JAX-WS rules |
|
369 * @param name to be mangled |
|
370 * @return property name |
|
371 */ |
|
372 private static String getPropertyName(String name) { |
|
373 String propertyName = BindingHelper.mangleNameToVariableName(name); |
|
374 //We wont have to do this if JAXBRIContext.mangleNameToVariableName() takes |
|
375 //care of mangling java identifiers |
|
376 return getJavaReservedVarialbeName(propertyName); |
|
377 } |
|
378 |
|
379 |
|
380 //TODO MOVE Names.java to runtime (instead of doing the following) |
|
381 /* |
|
382 * See if its a java keyword name, if so then mangle the name |
|
383 */ |
|
384 private static @NotNull String getJavaReservedVarialbeName(@NotNull String name) { |
|
385 String reservedName = reservedWords.get(name); |
|
386 return reservedName == null ? name : reservedName; |
|
387 } |
|
388 |
|
389 private static final Map<String, String> reservedWords; |
|
390 |
|
391 static { |
|
392 reservedWords = new HashMap<String, String>(); |
|
393 reservedWords.put("abstract", "_abstract"); |
|
394 reservedWords.put("assert", "_assert"); |
|
395 reservedWords.put("boolean", "_boolean"); |
|
396 reservedWords.put("break", "_break"); |
|
397 reservedWords.put("byte", "_byte"); |
|
398 reservedWords.put("case", "_case"); |
|
399 reservedWords.put("catch", "_catch"); |
|
400 reservedWords.put("char", "_char"); |
|
401 reservedWords.put("class", "_class"); |
|
402 reservedWords.put("const", "_const"); |
|
403 reservedWords.put("continue", "_continue"); |
|
404 reservedWords.put("default", "_default"); |
|
405 reservedWords.put("do", "_do"); |
|
406 reservedWords.put("double", "_double"); |
|
407 reservedWords.put("else", "_else"); |
|
408 reservedWords.put("extends", "_extends"); |
|
409 reservedWords.put("false", "_false"); |
|
410 reservedWords.put("final", "_final"); |
|
411 reservedWords.put("finally", "_finally"); |
|
412 reservedWords.put("float", "_float"); |
|
413 reservedWords.put("for", "_for"); |
|
414 reservedWords.put("goto", "_goto"); |
|
415 reservedWords.put("if", "_if"); |
|
416 reservedWords.put("implements", "_implements"); |
|
417 reservedWords.put("import", "_import"); |
|
418 reservedWords.put("instanceof", "_instanceof"); |
|
419 reservedWords.put("int", "_int"); |
|
420 reservedWords.put("interface", "_interface"); |
|
421 reservedWords.put("long", "_long"); |
|
422 reservedWords.put("native", "_native"); |
|
423 reservedWords.put("new", "_new"); |
|
424 reservedWords.put("null", "_null"); |
|
425 reservedWords.put("package", "_package"); |
|
426 reservedWords.put("private", "_private"); |
|
427 reservedWords.put("protected", "_protected"); |
|
428 reservedWords.put("public", "_public"); |
|
429 reservedWords.put("return", "_return"); |
|
430 reservedWords.put("short", "_short"); |
|
431 reservedWords.put("static", "_static"); |
|
432 reservedWords.put("strictfp", "_strictfp"); |
|
433 reservedWords.put("super", "_super"); |
|
434 reservedWords.put("switch", "_switch"); |
|
435 reservedWords.put("synchronized", "_synchronized"); |
|
436 reservedWords.put("this", "_this"); |
|
437 reservedWords.put("throw", "_throw"); |
|
438 reservedWords.put("throws", "_throws"); |
|
439 reservedWords.put("transient", "_transient"); |
|
440 reservedWords.put("true", "_true"); |
|
441 reservedWords.put("try", "_try"); |
|
442 reservedWords.put("void", "_void"); |
|
443 reservedWords.put("volatile", "_volatile"); |
|
444 reservedWords.put("while", "_while"); |
|
445 reservedWords.put("enum", "_enum"); |
|
446 } |
|
447 |
|
448 } |