aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.model; aoqi@0: aoqi@0: import com.sun.istack.internal.NotNull; aoqi@0: import com.sun.xml.internal.bind.v2.model.annotation.AnnotationReader; aoqi@0: import com.sun.xml.internal.bind.v2.model.nav.Navigator; aoqi@0: import com.sun.xml.internal.ws.spi.db.BindingHelper; aoqi@0: import com.sun.xml.internal.ws.util.StringUtils; aoqi@0: aoqi@0: import javax.jws.WebParam; aoqi@0: import javax.jws.WebResult; aoqi@0: import javax.xml.bind.annotation.*; aoqi@0: import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; aoqi@0: import javax.xml.ws.WebServiceException; aoqi@0: import java.lang.annotation.Annotation; aoqi@0: import java.lang.reflect.InvocationHandler; aoqi@0: import java.lang.reflect.Method; aoqi@0: import java.lang.reflect.Proxy; aoqi@0: import java.util.*; aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: /** aoqi@0: * Finds request/response wrapper and exception bean memebers. aoqi@0: * aoqi@0: *

aoqi@0: * It uses JAXB's {@link AnnotationReader}, {@link Navigator} so that aoqi@0: * tools can use this with annotation processing, and the runtime can use this with aoqi@0: * reflection. aoqi@0: * aoqi@0: * @author Jitendra Kotamraju aoqi@0: */ aoqi@0: public abstract class AbstractWrapperBeanGenerator { aoqi@0: aoqi@0: private static final Logger LOGGER = Logger.getLogger(AbstractWrapperBeanGenerator.class.getName()); aoqi@0: aoqi@0: private static final String RETURN = "return"; aoqi@0: private static final String EMTPY_NAMESPACE_ID = ""; aoqi@0: aoqi@0: private static final Class[] jaxbAnns = new Class[] { aoqi@0: XmlAttachmentRef.class, XmlMimeType.class, XmlJavaTypeAdapter.class, aoqi@0: XmlList.class, XmlElement.class aoqi@0: }; aoqi@0: aoqi@0: private static final Set skipProperties = new HashSet(); aoqi@0: static{ aoqi@0: skipProperties.add("getCause"); aoqi@0: skipProperties.add("getLocalizedMessage"); aoqi@0: skipProperties.add("getClass"); aoqi@0: skipProperties.add("getStackTrace"); aoqi@0: skipProperties.add("getSuppressed"); // JDK 7 adds this aoqi@0: } aoqi@0: aoqi@0: private final AnnotationReader annReader; aoqi@0: private final Navigator nav; aoqi@0: private final BeanMemberFactory factory; aoqi@0: aoqi@0: protected AbstractWrapperBeanGenerator(AnnotationReader annReader, aoqi@0: Navigator nav, BeanMemberFactory factory) { aoqi@0: this.annReader = annReader; aoqi@0: this.nav = nav; aoqi@0: this.factory = factory; aoqi@0: } aoqi@0: aoqi@0: public static interface BeanMemberFactory { aoqi@0: A createWrapperBeanMember(T paramType, String paramName, List jaxbAnnotations); aoqi@0: } aoqi@0: aoqi@0: // Collects the JAXB annotations on a method aoqi@0: private List collectJAXBAnnotations(M method) { aoqi@0: List jaxbAnnotation = new ArrayList(); aoqi@0: for(Class jaxbClass : jaxbAnns) { aoqi@0: Annotation ann = annReader.getMethodAnnotation(jaxbClass, method, null); aoqi@0: if (ann != null) { aoqi@0: jaxbAnnotation.add(ann); aoqi@0: } aoqi@0: } aoqi@0: return jaxbAnnotation; aoqi@0: } aoqi@0: aoqi@0: // Collects the JAXB annotations on a parameter aoqi@0: private List collectJAXBAnnotations(M method, int paramIndex) { aoqi@0: List jaxbAnnotation = new ArrayList(); aoqi@0: for(Class jaxbClass : jaxbAnns) { aoqi@0: Annotation ann = annReader.getMethodParameterAnnotation(jaxbClass, method, paramIndex, null); aoqi@0: if (ann != null) { aoqi@0: jaxbAnnotation.add(ann); aoqi@0: } aoqi@0: } aoqi@0: return jaxbAnnotation; aoqi@0: } aoqi@0: aoqi@0: protected abstract T getSafeType(T type); aoqi@0: aoqi@0: /** aoqi@0: * Returns Holder's value type. aoqi@0: * aoqi@0: * @return null if it not a Holder, otherwise return Holder's value type aoqi@0: */ aoqi@0: protected abstract T getHolderValueType(T type); aoqi@0: aoqi@0: protected abstract boolean isVoidType(T type); aoqi@0: aoqi@0: /** aoqi@0: * Computes request bean members for a method. Collects all IN and INOUT aoqi@0: * parameters as request bean fields. In this process, if a parameter aoqi@0: * has any known JAXB annotations they are collected as well. aoqi@0: * Special processing for @XmlElement annotation is done. aoqi@0: * aoqi@0: * @param method SEI method for which request bean members are computed aoqi@0: * @return List of request bean members aoqi@0: */ aoqi@0: public List collectRequestBeanMembers(M method) { aoqi@0: aoqi@0: List requestMembers = new ArrayList(); aoqi@0: int paramIndex = -1; aoqi@0: aoqi@0: for (T param : nav.getMethodParameters(method)) { aoqi@0: paramIndex++; aoqi@0: WebParam webParam = annReader.getMethodParameterAnnotation(WebParam.class, method, paramIndex, null); aoqi@0: if (webParam != null && (webParam.header() || webParam.mode().equals(WebParam.Mode.OUT))) { aoqi@0: continue; aoqi@0: } aoqi@0: T holderType = getHolderValueType(param); aoqi@0: // if (holderType != null && webParam != null && webParam.mode().equals(WebParam.Mode.IN)) { aoqi@0: // // Should we flag an error - holder cannot be IN part ?? aoqi@0: // continue; aoqi@0: // } aoqi@0: aoqi@0: T paramType = (holderType != null) ? holderType : getSafeType(param); aoqi@0: String paramName = (webParam != null && webParam.name().length() > 0) aoqi@0: ? webParam.name() : "arg"+paramIndex; aoqi@0: String paramNamespace = (webParam != null && webParam.targetNamespace().length() > 0) aoqi@0: ? webParam.targetNamespace() : EMTPY_NAMESPACE_ID; aoqi@0: aoqi@0: // Collect JAXB annotations on a parameter aoqi@0: List jaxbAnnotation = collectJAXBAnnotations(method, paramIndex); aoqi@0: aoqi@0: // If a parameter contains @XmlElement, process it. aoqi@0: processXmlElement(jaxbAnnotation, paramName, paramNamespace, paramType); aoqi@0: A member = factory.createWrapperBeanMember(paramType, aoqi@0: getPropertyName(paramName), jaxbAnnotation); aoqi@0: requestMembers.add(member); aoqi@0: } aoqi@0: return requestMembers; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Computes response bean members for a method. Collects all OUT and INOUT aoqi@0: * parameters as response bean fields. In this process, if a parameter aoqi@0: * has any known JAXB annotations they are collected as well. aoqi@0: * Special processing for @XmlElement annotation is done. aoqi@0: * aoqi@0: * @param method SEI method for which response bean members are computed aoqi@0: * @return List of response bean members aoqi@0: */ aoqi@0: public List collectResponseBeanMembers(M method) { aoqi@0: aoqi@0: List responseMembers = new ArrayList(); aoqi@0: aoqi@0: // return that need to be part response wrapper bean aoqi@0: String responseElementName = RETURN; aoqi@0: String responseNamespace = EMTPY_NAMESPACE_ID; aoqi@0: boolean isResultHeader = false; aoqi@0: WebResult webResult = annReader.getMethodAnnotation(WebResult.class, method ,null); aoqi@0: if (webResult != null) { aoqi@0: if (webResult.name().length() > 0) { aoqi@0: responseElementName = webResult.name(); aoqi@0: } aoqi@0: if (webResult.targetNamespace().length() > 0) { aoqi@0: responseNamespace = webResult.targetNamespace(); aoqi@0: } aoqi@0: isResultHeader = webResult.header(); aoqi@0: } aoqi@0: T returnType = getSafeType(nav.getReturnType(method)); aoqi@0: if (!isVoidType(returnType) && !isResultHeader) { aoqi@0: List jaxbRespAnnotations = collectJAXBAnnotations(method); aoqi@0: processXmlElement(jaxbRespAnnotations, responseElementName, responseNamespace, returnType); aoqi@0: responseMembers.add(factory.createWrapperBeanMember(returnType, getPropertyName(responseElementName), jaxbRespAnnotations)); aoqi@0: } aoqi@0: aoqi@0: // Now parameters that need to be part response wrapper bean aoqi@0: int paramIndex = -1; aoqi@0: for (T param : nav.getMethodParameters(method)) { aoqi@0: paramIndex++; aoqi@0: aoqi@0: T paramType = getHolderValueType(param); aoqi@0: WebParam webParam = annReader.getMethodParameterAnnotation(WebParam.class, method, paramIndex, null); aoqi@0: if (paramType == null || (webParam != null && webParam.header())) { aoqi@0: continue; // not a holder or a header - so don't add it aoqi@0: } aoqi@0: aoqi@0: String paramName = (webParam != null && webParam.name().length() > 0) aoqi@0: ? webParam.name() : "arg"+paramIndex; aoqi@0: String paramNamespace = (webParam != null && webParam.targetNamespace().length() > 0) aoqi@0: ? webParam.targetNamespace() : EMTPY_NAMESPACE_ID; aoqi@0: List jaxbAnnotation = collectJAXBAnnotations(method, paramIndex); aoqi@0: processXmlElement(jaxbAnnotation, paramName, paramNamespace, paramType); aoqi@0: A member = factory.createWrapperBeanMember(paramType, aoqi@0: getPropertyName(paramName), jaxbAnnotation); aoqi@0: responseMembers.add(member); aoqi@0: } aoqi@0: aoqi@0: return responseMembers; aoqi@0: } aoqi@0: aoqi@0: private void processXmlElement(List jaxb, String elemName, String elemNS, T type) { aoqi@0: XmlElement elemAnn = null; aoqi@0: for (Annotation a : jaxb) { aoqi@0: if (a.annotationType() == XmlElement.class) { aoqi@0: elemAnn = (XmlElement) a; aoqi@0: jaxb.remove(a); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: String name = (elemAnn != null && !elemAnn.name().equals("##default")) aoqi@0: ? elemAnn.name() : elemName; aoqi@0: aoqi@0: String ns = (elemAnn != null && !elemAnn.namespace().equals("##default")) aoqi@0: ? elemAnn.namespace() : elemNS; aoqi@0: aoqi@0: boolean nillable = nav.isArray(type) aoqi@0: || (elemAnn != null && elemAnn.nillable()); aoqi@0: aoqi@0: boolean required = elemAnn != null && elemAnn.required(); aoqi@0: XmlElementHandler handler = new XmlElementHandler(name, ns, nillable, required); aoqi@0: XmlElement elem = (XmlElement) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{XmlElement.class}, handler); aoqi@0: jaxb.add(elem); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: private static class XmlElementHandler implements InvocationHandler { aoqi@0: private String name; aoqi@0: private String namespace; aoqi@0: private boolean nillable; aoqi@0: private boolean required; aoqi@0: aoqi@0: XmlElementHandler(String name, String namespace, boolean nillable, aoqi@0: boolean required) { aoqi@0: this.name = name; aoqi@0: this.namespace = namespace; aoqi@0: this.nillable = nillable; aoqi@0: this.required = required; aoqi@0: } aoqi@0: aoqi@0: public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { aoqi@0: String methodName = method.getName(); aoqi@0: if (methodName.equals("name")) { aoqi@0: return name; aoqi@0: } else if (methodName.equals("namespace")) { aoqi@0: return namespace; aoqi@0: } else if (methodName.equals("nillable")) { aoqi@0: return nillable; aoqi@0: } else if (methodName.equals("required")) { aoqi@0: return required; aoqi@0: } else { aoqi@0: throw new WebServiceException("Not handling "+methodName); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Computes and sorts exception bean members for a given exception as per aoqi@0: * the 3.7 section of the spec. It takes all getter properties in the aoqi@0: * exception and its superclasses(except getCause, getLocalizedMessage, aoqi@0: * getStackTrace, getClass). The returned collection is sorted based aoqi@0: * on the property names. aoqi@0: * aoqi@0: *

aoqi@0: * But if the exception has @XmlType its values are honored. Only the aoqi@0: * propOrder properties are considered. The returned collection is sorted aoqi@0: * as per the given propOrder. aoqi@0: * aoqi@0: * @param exception aoqi@0: * @return list of properties in the correct order for an exception bean aoqi@0: */ aoqi@0: public Collection collectExceptionBeanMembers(C exception) { aoqi@0: return collectExceptionBeanMembers(exception, true); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Computes and sorts exception bean members for a given exception as per aoqi@0: * the 3.7 section of the spec. It takes all getter properties in the aoqi@0: * exception and its superclasses(except getCause, getLocalizedMessage, aoqi@0: * getStackTrace, getClass). The returned collection is sorted based aoqi@0: * on the property names. aoqi@0: * aoqi@0: *

aoqi@0: * But if the exception has @XmlType its values are honored. Only the aoqi@0: * propOrder properties are considered. The returned collection is sorted aoqi@0: * as per the given propOrder. aoqi@0: * aoqi@0: * @param exception aoqi@0: * @param decapitalize if true, all the property names are decapitalized aoqi@0: * aoqi@0: * @return list of properties in the correct order for an exception bean aoqi@0: */ aoqi@0: public Collection collectExceptionBeanMembers(C exception, boolean decapitalize ) { aoqi@0: TreeMap fields = new TreeMap(); aoqi@0: getExceptionProperties(exception, fields, decapitalize); aoqi@0: aoqi@0: // Consider only the @XmlType(propOrder) properties aoqi@0: XmlType xmlType = annReader.getClassAnnotation(XmlType.class, exception, null); aoqi@0: if (xmlType != null) { aoqi@0: String[] propOrder = xmlType.propOrder(); aoqi@0: // If not the default order of properties, use that propOrder aoqi@0: if (propOrder.length > 0 && propOrder[0].length() != 0) { aoqi@0: List list = new ArrayList(); aoqi@0: for(String prop : propOrder) { aoqi@0: A a = fields.get(prop); aoqi@0: if (a != null) { aoqi@0: list.add(a); aoqi@0: } else { aoqi@0: throw new WebServiceException("Exception "+exception+ aoqi@0: " has @XmlType and its propOrder contains unknown property "+prop); aoqi@0: } aoqi@0: } aoqi@0: return list; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return fields.values(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: private void getExceptionProperties(C exception, TreeMap fields, boolean decapitalize) { aoqi@0: C sc = nav.getSuperClass(exception); aoqi@0: if (sc != null) { aoqi@0: getExceptionProperties(sc, fields, decapitalize); aoqi@0: } aoqi@0: Collection methods = nav.getDeclaredMethods(exception); aoqi@0: aoqi@0: for (M method : methods) { aoqi@0: aoqi@0: // 2.1.x is doing the following: no final static, transient, non-public aoqi@0: // transient cannot used as modifier for method, so not doing it now aoqi@0: if (!nav.isPublicMethod(method) aoqi@0: || (nav.isStaticMethod(method) && nav.isFinalMethod(method))) { aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: if (!nav.isPublicMethod(method)) { aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: String name = nav.getMethodName(method); aoqi@0: aoqi@0: if (!(name.startsWith("get") || name.startsWith("is")) || skipProperties.contains(name) || aoqi@0: name.equals("get") || name.equals("is")) { aoqi@0: // Don't bother with invalid propertyNames. aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: T returnType = getSafeType(nav.getReturnType(method)); aoqi@0: if (nav.getMethodParameters(method).length == 0) { aoqi@0: String fieldName = name.startsWith("get") ? name.substring(3) : name.substring(2); aoqi@0: if (decapitalize) fieldName = StringUtils.decapitalize(fieldName); aoqi@0: fields.put(fieldName, factory.createWrapperBeanMember(returnType, fieldName, Collections.emptyList())); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Gets the property name by mangling using JAX-WS rules aoqi@0: * @param name to be mangled aoqi@0: * @return property name aoqi@0: */ aoqi@0: private static String getPropertyName(String name) { aoqi@0: String propertyName = BindingHelper.mangleNameToVariableName(name); aoqi@0: //We wont have to do this if JAXBRIContext.mangleNameToVariableName() takes aoqi@0: //care of mangling java identifiers aoqi@0: return getJavaReservedVarialbeName(propertyName); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: //TODO MOVE Names.java to runtime (instead of doing the following) aoqi@0: /* aoqi@0: * See if its a java keyword name, if so then mangle the name aoqi@0: */ aoqi@0: private static @NotNull String getJavaReservedVarialbeName(@NotNull String name) { aoqi@0: String reservedName = reservedWords.get(name); aoqi@0: return reservedName == null ? name : reservedName; aoqi@0: } aoqi@0: aoqi@0: private static final Map reservedWords; aoqi@0: aoqi@0: static { aoqi@0: reservedWords = new HashMap(); aoqi@0: reservedWords.put("abstract", "_abstract"); aoqi@0: reservedWords.put("assert", "_assert"); aoqi@0: reservedWords.put("boolean", "_boolean"); aoqi@0: reservedWords.put("break", "_break"); aoqi@0: reservedWords.put("byte", "_byte"); aoqi@0: reservedWords.put("case", "_case"); aoqi@0: reservedWords.put("catch", "_catch"); aoqi@0: reservedWords.put("char", "_char"); aoqi@0: reservedWords.put("class", "_class"); aoqi@0: reservedWords.put("const", "_const"); aoqi@0: reservedWords.put("continue", "_continue"); aoqi@0: reservedWords.put("default", "_default"); aoqi@0: reservedWords.put("do", "_do"); aoqi@0: reservedWords.put("double", "_double"); aoqi@0: reservedWords.put("else", "_else"); aoqi@0: reservedWords.put("extends", "_extends"); aoqi@0: reservedWords.put("false", "_false"); aoqi@0: reservedWords.put("final", "_final"); aoqi@0: reservedWords.put("finally", "_finally"); aoqi@0: reservedWords.put("float", "_float"); aoqi@0: reservedWords.put("for", "_for"); aoqi@0: reservedWords.put("goto", "_goto"); aoqi@0: reservedWords.put("if", "_if"); aoqi@0: reservedWords.put("implements", "_implements"); aoqi@0: reservedWords.put("import", "_import"); aoqi@0: reservedWords.put("instanceof", "_instanceof"); aoqi@0: reservedWords.put("int", "_int"); aoqi@0: reservedWords.put("interface", "_interface"); aoqi@0: reservedWords.put("long", "_long"); aoqi@0: reservedWords.put("native", "_native"); aoqi@0: reservedWords.put("new", "_new"); aoqi@0: reservedWords.put("null", "_null"); aoqi@0: reservedWords.put("package", "_package"); aoqi@0: reservedWords.put("private", "_private"); aoqi@0: reservedWords.put("protected", "_protected"); aoqi@0: reservedWords.put("public", "_public"); aoqi@0: reservedWords.put("return", "_return"); aoqi@0: reservedWords.put("short", "_short"); aoqi@0: reservedWords.put("static", "_static"); aoqi@0: reservedWords.put("strictfp", "_strictfp"); aoqi@0: reservedWords.put("super", "_super"); aoqi@0: reservedWords.put("switch", "_switch"); aoqi@0: reservedWords.put("synchronized", "_synchronized"); aoqi@0: reservedWords.put("this", "_this"); aoqi@0: reservedWords.put("throw", "_throw"); aoqi@0: reservedWords.put("throws", "_throws"); aoqi@0: reservedWords.put("transient", "_transient"); aoqi@0: reservedWords.put("true", "_true"); aoqi@0: reservedWords.put("try", "_try"); aoqi@0: reservedWords.put("void", "_void"); aoqi@0: reservedWords.put("volatile", "_volatile"); aoqi@0: reservedWords.put("while", "_while"); aoqi@0: reservedWords.put("enum", "_enum"); aoqi@0: } aoqi@0: aoqi@0: }