Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   * 
  *   http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.    
  */
 
 package org.apache.tuscany.sca.interfacedef.java.jaxws;
 
 import java.util.List;
 
 
Introspect the java class/interface with JSR-181 and JAXWS annotations

Version:
$Rev: 1063548 $ $Date: 2011-01-26 03:18:02 +0200 (Wed, 26 Jan 2011) $
 
 public class JAXWSJavaInterfaceProcessor implements JavaInterfaceVisitor {
     private static final String JAXB_DATABINDING = .;
     private static final String GET = "get";
     private WSDLFactory wsdlFactory;
 
 
         
         FactoryExtensionPoint factories = registry.getExtensionPoint(FactoryExtensionPoint.class);
         this. = factories.getFactory(JavaInterfaceFactory.class);
         this. = factories.getFactory(WSDLFactory.class);
     }
 
   
     public JAXWSJavaInterfaceProcessor() {
         super();
     }
 
     private ParameterMode getParameterMode(WebParam.Mode mode) {
     	if (mode == .) {
     		return .;
     	} else if (mode == .) {
     		return .;
    	} else {
    		return .;
    	}
    }
    
    private static String capitalize(String name) {
        if (name == null || name.length() == 0) {
            return name;
        } else {
            return Character.toUpperCase(name.charAt(0)) + name.substring(1);
        }
    }
    public void visitInterface(JavaInterface contractthrows InvalidInterfaceException {
        final Class<?> clazz = contract.getJavaClass();
        
        contract = JAXWSUtils.configureJavaInterface(contractclazz);
        String tns = contract.getQName().getNamespaceURI();      
        
        if (!contract.isRemotable()) {
            return;
        }
        // SOAP binding (doc/lit/wrapped|bare or rpc/lit)
        SOAPBinding soapBinding = clazz.getAnnotation(SOAPBinding.class);
        for (Iterator<Operationit = contract.getOperations().iterator(); it.hasNext();) {
            final JavaOperation operation = (JavaOperation)it.next();
            final Method method = operation.getJavaMethod();
            introspectFaultTypes(operation);
            // SOAP binding (doc/lit/wrapped|bare or rpc/lit)
            SOAPBinding methodSOAPBinding = method.getAnnotation(SOAPBinding.class);
            if (methodSOAPBinding == null) {
                methodSOAPBinding = soapBinding;
            }
            boolean documentStyle = true;
            boolean bare = false;
            if (methodSOAPBinding != null) {
                bare = methodSOAPBinding.parameterStyle() == ..;
                if(bare) {
                    // For BARE parameter style, the data won't be unwrapped
                    // The wrapper should be null
                    operation.setWrapperStyle(false);
                }
                documentStyle = methodSOAPBinding.style() == .;
            }
            String operationName = operation.getName();
            // WebMethod
            WebMethod webMethod = method.getAnnotation(WebMethod.class);
            if (webMethod != null) {
                if (webMethod.exclude()) {
                    // Exclude the method
                    it.remove();
                    continue;
                }
                operationName = getValue(webMethod.operationName(), operationName);
                operation.setName(operationName);
                operation.setAction(webMethod.action());
            }
            // Is one way?
            Oneway oneway = method.getAnnotation(Oneway.class);
            if (oneway != null) {
                // JSR 181
                assert method.getReturnType() == void.class;
                operation.setNonBlocking(true);
            }
            // Handle BARE mapping
            if (bare) {
                for (int i = 0; i < method.getParameterTypes().lengthi++) {
                    WebParam param = getAnnotation(methodiWebParam.class);
                    if (param != null) {
                        String ns = getValue(param.targetNamespace(), tns);
                        // Default to <operationName> for doc-bare
                        String name = getValue(param.name(), documentStyle ? operationName : "arg" + i);
                        QName element = new QName(nsname);
                        Object logical = operation.getInputType().getLogical().get(i).getLogical();
                        if (logical instanceof XMLType) {
                            ((XMLType)logical).setElementName(element);
                        }
                        operation.getParameterModes().set(igetParameterMode(param.mode()));
                    }
                    ParameterMode mode = operation.getParameterModes().get(i);
                }
        
                WebResult result = method.getAnnotation(WebResult.class);
                if (result != null) {
                    String ns = getValue(result.targetNamespace(), tns);
                    // Default to <operationName>Response for doc-bare
                    String name = getValue(result.name(), documentStyle ? operationName + "Response" : "return");
                    QName element = new QName(nsname);
                    if (!operation.hasReturnTypeVoid()) {
                        List<DataTypeoutputDataTypes = operation.getOutputType().getLogical();                    
                        DataType returnDataType = outputDataTypes.get(0);
                        if (returnDataType instanceof XMLType) {
                            ((XMLType)returnDataType).setElementName(element);
                        }
                    }
                }
                // FIXME: [rfeng] For the BARE mapping, do we need to create a Wrapper?
                // it's null at this point
            } else {
                RequestWrapper requestWrapper = method.getAnnotation(RequestWrapper.class);
                String ns = requestWrapper == null ? tns : getValue(requestWrapper.targetNamespace(), tns);
                String name =
                    requestWrapper == null ? operationName : getValue(requestWrapper.localName(), operationName);
                String wrapperBeanName = requestWrapper == null ? "" : requestWrapper.className();
                if ("".equals(wrapperBeanName)) {
                    wrapperBeanName = CodeGenerationHelper.getPackagePrefix(clazz) + capitalize(method.getName());
                }
                DataType<XMLTypeinputWrapperDT = null;
                final String inputWrapperClassName = wrapperBeanName;
                final String inputNS = ns;
                final String inputName = name;
                inputWrapperDT = AccessController.doPrivileged(new PrivilegedAction<DataType<XMLType>>() {
                    public DataType<XMLTyperun() {
                        try {
                            Class<?> wrapperClass = Class.forName(inputWrapperClassNamefalseclazz.getClassLoader());
                            QName qname = new QName(inputNSinputName);
                            DataType dt = new DataTypeImpl<XMLType>(wrapperClassnew XMLType(qnameqname));
                            .introspectType(dtoperation);
                            // TUSCANY-2505
                            if (dt.getLogical() instanceof XMLType) {
                                XMLType xmlType = (XMLType)dt.getLogical();
                                xmlType.setElementName(qname);
                            }
                            return dt;
                        } catch (ClassNotFoundException e) {
                            GeneratedClassLoader cl = new GeneratedClassLoader(clazz.getClassLoader());
                            return new GeneratedDataTypeImpl(methodinputWrapperClassNameinputNSinputNametrue,
                                                             cl);
                        }
                    }
                });
                QName inputWrapper = inputWrapperDT.getLogical().getElementName();
                ResponseWrapper responseWrapper = method.getAnnotation(ResponseWrapper.class);
                ns = responseWrapper == null ? tns : getValue(responseWrapper.targetNamespace(), tns);
                name =
                    responseWrapper == null ? operationName + "Response" : getValue(responseWrapper.localName(),
                                                                                    operationName + "Response");
                wrapperBeanName = responseWrapper == null ? "" : responseWrapper.className();
                if ("".equals(wrapperBeanName)) {
                    wrapperBeanName =
                        CodeGenerationHelper.getPackagePrefix(clazz) + capitalize(method.getName()) + "Response";
                }
                DataType<XMLTypeoutputWrapperDT = null;
                final String outputWrapperClassName = wrapperBeanName;
                final String outputNS = ns;
                final String outputName = name;
                outputWrapperDT = AccessController.doPrivileged(new PrivilegedAction<DataType<XMLType>>() {
                    public DataType<XMLTyperun() {
                        try {
                            Class<?> wrapperClass =
                                Class.forName(outputWrapperClassNamefalseclazz.getClassLoader());
                            QName qname = new QName(outputNSoutputName);
                            DataType dt = new DataTypeImpl<XMLType>(wrapperClassnew XMLType(qnameqname));
                            .introspectType(dtoperation);
                            // TUSCANY-2505
                            if (dt.getLogical() instanceof XMLType) {
                                XMLType xmlType = (XMLType)dt.getLogical();
                                xmlType.setElementName(qname);
                            }
                            return dt;
                        } catch (ClassNotFoundException e) {
                            GeneratedClassLoader cl = new GeneratedClassLoader(clazz.getClassLoader());
                            return new GeneratedDataTypeImpl(methodoutputWrapperClassNameoutputNSoutputName,
                                                             falsecl);
                        }
                    }
                });
                QName outputWrapper = outputWrapperDT.getLogical().getElementName();
                
                //
                // Since JAX-WS specifies that the output wrapper bean consists of the return type output first followed
                // by any other outputs carried in Holder(s), let's look at the output first.
                //
                List<ElementInfooutputElements = new ArrayList<ElementInfo>();
                WebResult result = method.getAnnotation(WebResult.class);
                // Default to "" for doc-lit-wrapped && non-header
                ns = result != null ? result.targetNamespace() : "";
                ns = getValue(nsdocumentStyle && (result == null || !result.header()) ? "" : tns);
                name = result != null ? result.name() : "";
                name = getValue(name"return");
                QName element = new QName(nsname);
                if (!operation.hasReturnTypeVoid()) {
                    Object logical = operation.getOutputType().getLogical().get(0).getLogical();
                    QName type = null;
                    if (logical instanceof XMLType) {
                        ((XMLType)logical).setElementName(element);
                        type = ((XMLType)logical).getTypeName();
                    }
                    outputElements.add(new ElementInfo(elementnew TypeInfo(typefalsenull)));
                }
                
                List<ElementInfoinputElements = new ArrayList<ElementInfo>();
                for (int i = 0; i < method.getParameterTypes().lengthi++) {
                    WebParam param = getAnnotation(methodiWebParam.class);
                    ns = param != null ? param.targetNamespace() : "";
                    // Default to "" for doc-lit-wrapped && non-header
                    ns = getValue(nsdocumentStyle && (param == null || !param.header()) ? "" : tns);
                    name = param != null ? param.name() : "";
                    name = getValue(name"arg" + i);
                    element = new QName(nsname);
                    Object logical = operation.getInputType().getLogical().get(i).getLogical();
                    QName type = null;
                    if (logical instanceof XMLType) {
                        ((XMLType)logical).setElementName(element);
                        type = ((XMLType)logical).getTypeName();
                    }
                                        
                    if (param != null) {
                        ParameterMode mode = getParameterMode(param.mode());
                        operation.getParameterModes().set(imode);
                    }
                    ParameterMode mode = operation.getParameterModes().get(i);
                    if (mode.equals(.)) {
                        inputElements.add(new ElementInfo(elementnew TypeInfo(typefalsenull)));
                        outputElements.add(new ElementInfo(elementnew TypeInfo(typefalsenull)));
                    } else if (mode.equals(.)) {
                        outputElements.add(new ElementInfo(elementnew TypeInfo(typefalsenull)));
                    } else {
                        inputElements.add(new ElementInfo(elementnew TypeInfo(typefalsenull)));
                    }
                }                                  
                    
                String db = inputWrapperDT != null ? inputWrapperDT.getDataBinding() : ;
                WrapperInfo wrapperInfo =
                    new WrapperInfo(dbnew ElementInfo(inputWrappernull), new ElementInfo(outputWrappernull),
                                    inputElementsoutputElements);
                wrapperInfo.setInputWrapperType(inputWrapperDT);
                wrapperInfo.setOutputWrapperType(outputWrapperDT);
                operation.setWrapper(wrapperInfo);
            }
            
            // In both bare and wrapped cases, remove OUT-only parameters from input DataType.
            // This is a key point then because it's the last time in which the number of parameters in 
            // Java matches the number of logical inputs.  After this, things will be out of synch, for
            // example the number of parameter modes won't match the number of inputs.
            List<ParameterModeparmModes = operation.getParameterModes();
            List<DataTypeinputDTs = operation.getInputType().getLogical();
            for (int i = parmModes.size() - 1; i>=0; i--) {
                if (parmModes.get(i).equals(.)) {
                    inputDTs.remove(i);
                }
            }
            
        }
    }
    @SuppressWarnings("unchecked")
    private void introspectFaultTypes(Operation operation) {
        if (operation != null && operation.getFaultTypes() != null) {
            for (DataType exceptionType : operation.getFaultTypes()) {
                .introspectFaultDataType(exceptionTypeoperationtrue);
                DataType faultType = (DataType)exceptionType.getLogical();
                if (..equals(faultType.getDataBinding())) {
                    // The exception class doesn't have an associated bean class, so
                    // synthesize a virtual bean by introspecting the exception class.
                    createSyntheticBean(operationexceptionType);
                }
            }
        }
    }
    private void createSyntheticBean(Operation operationDataType exceptionType) {
        DataType faultType = (DataType)exceptionType.getLogical();
        QName faultBeanName = ((XMLType)faultType.getLogical()).getElementName();
        List<DataType<XMLType>> beanDataTypes = new ArrayList<DataType<XMLType>>();
        for (Method aMethod : exceptionType.getPhysical().getMethods()) {
            if (Modifier.isPublic(aMethod.getModifiers()) && aMethod.getName().startsWith()
                && aMethod.getParameterTypes().length == 0
                && JAXWSFaultExceptionMapper.isMappedGetter(aMethod.getName())) {
                String propName = resolvePropertyFromMethod(aMethod.getName());
                QName propQName = new QName(faultBeanName.getNamespaceURI(), propName);
                Class<?> propType = aMethod.getReturnType();
                XMLType xmlPropType = new XMLType(propQNamenull);
                DataType<XMLTypepropDT = new DataTypeImpl<XMLType>(propTypexmlPropType);
                org.apache.tuscany.sca.databinding.annotation.DataType dt =
                    aMethod.getAnnotation(org.apache.tuscany.sca.databinding.annotation.DataType.class);
                if (dt != null) {
                    propDT.setDataBinding(dt.value());
                }
                .introspectType(propDToperation);
                // sort the list lexicographically as specified in JAX-WS spec section 3.7
                int i = 0;
                for (; i < beanDataTypes.size(); i++) {
                    if (beanDataTypes.get(i).getLogical().getElementName().getLocalPart().compareTo(propName) > 0) {
                        break;
                    }
                }
                beanDataTypes.add(ipropDT);
            }
        }
        operation.getFaultBeans().put(faultBeanNamebeanDataTypes);
    }
    private String resolvePropertyFromMethod(String methodName) {
        StringBuffer propName = new StringBuffer();
        propName.append(Character.toLowerCase(methodName.charAt(.length())));
        propName.append(methodName.substring(.length() + 1));
        return propName.toString();
    }
    private <T extends Annotation> T getAnnotation(Method methodint indexClass<T> annotationType) {
        Annotation[] annotations = method.getParameterAnnotations()[index];
        for (Annotation annotation : annotations) {
            if (annotation.annotationType() == annotationType) {
                return annotationType.cast(annotation);
            }
        }
        return null;
    }
    private static String getValue(String valueString defaultValue) {
        return "".equals(value) ? defaultValue : value;
    }
New to GrepCode? Check out our FAQ X