Search in sources :

Example 1 with Port

use of com.sun.tools.ws.processor.model.Port in project metro-jax-ws by eclipse-ee4j.

the class JwsImplGenerator method visit.

@Override
public void visit(Service service) {
    QName serviceName = service.getName();
    // process the ordered service only if it is defined
    if (options.implServiceName != null && !equalsNSOptional(options.implServiceName, serviceName))
        return;
    for (Port port : service.getPorts()) {
        if (port.isProvider()) {
            // Not generating for Provider based endpoint
            continue;
        }
        // Generate the impl class name according to
        // Xpath(/definitions/service/port[@name]);
        QName portName = port.getName();
        // process the ordered port only if it is defined
        if (options.implPortName != null && !equalsNSOptional(options.implPortName, portName))
            continue;
        String simpleClassName = serviceName.getLocalPart() + "_" + portName.getLocalPart() + "Impl";
        String className = makePackageQualified(simpleClassName);
        implFiles.add(className);
        if (donotOverride && GeneratorUtil.classExists(options, className)) {
            log("Class " + className + " exists. Not overriding.");
            return;
        }
        JDefinedClass cls = null;
        try {
            cls = getClass(className, ClassType.CLASS);
        } catch (JClassAlreadyExistsException e) {
            log("Class " + className + " generates failed. JClassAlreadyExistsException[" + className + "].");
            return;
        }
        // Each serviceImpl will implements one port interface
        JavaInterface portIntf = port.getJavaInterface();
        String portClassName = Names.customJavaTypeClassName(portIntf);
        JDefinedClass portCls = null;
        try {
            portCls = getClass(portClassName, ClassType.INTERFACE);
        } catch (JClassAlreadyExistsException e) {
            log("Class " + className + " generates failed. JClassAlreadyExistsException[" + portClassName + "].");
            return;
        }
        cls._implements(portCls);
        // create a default constructor
        cls.constructor(JMod.PUBLIC);
        // write class comment - JAXWS warning
        JDocComment comment = cls.javadoc();
        if (service.getJavaDoc() != null) {
            comment.add(service.getJavaDoc());
            comment.add("\n\n");
        }
        comment.addAll(getJAXWSClassComment());
        // @WebService
        JAnnotationUse webServiceAnn = cls.annotate(cm.ref(WebService.class));
        writeWebServiceAnnotation(service, port, webServiceAnn);
        // @BindingType
        JAnnotationUse bindingTypeAnn = cls.annotate(cm.ref(BindingType.class));
        writeBindingTypeAnnotation(port, bindingTypeAnn);
        // extra annotation
        for (GeneratorExtension f : findService(GeneratorExtension.class)) {
            f.writeWebServiceAnnotation(model, cm, cls, port);
        }
        // WebMethods
        for (Operation operation : port.getOperations()) {
            JavaMethod method = operation.getJavaMethod();
            // @WebMethod
            JMethod m;
            JDocComment methodDoc;
            String methodJavaDoc = operation.getJavaDoc();
            if (method.getReturnType().getName().equals("void")) {
                m = cls.method(JMod.PUBLIC, void.class, method.getName());
                methodDoc = m.javadoc();
            } else {
                JAXBTypeAndAnnotation retType = method.getReturnType().getType();
                m = cls.method(JMod.PUBLIC, retType.getType(), method.getName());
                retType.annotate(m);
                methodDoc = m.javadoc();
                JCommentPart ret = methodDoc.addReturn();
                ret.add("returns " + retType.getName());
            }
            if (methodJavaDoc != null)
                methodDoc.add(methodJavaDoc);
            JClass holder = cm.ref(Holder.class);
            for (JavaParameter parameter : method.getParametersList()) {
                JVar var;
                JAXBTypeAndAnnotation paramType = parameter.getType().getType();
                if (parameter.isHolder()) {
                    var = m.param(holder.narrow(paramType.getType().boxify()), parameter.getName());
                } else {
                    var = m.param(paramType.getType(), parameter.getName());
                }
                methodDoc.addParam(var);
            }
            com.sun.tools.ws.wsdl.document.Operation wsdlOp = operation.getWSDLPortTypeOperation();
            for (Fault fault : operation.getFaultsSet()) {
                m._throws(fault.getExceptionClass());
                methodDoc.addThrows(fault.getExceptionClass());
                wsdlOp.putFault(fault.getWsdlFaultName(), fault.getExceptionClass());
            }
            m.body().block().directStatement("//replace with your impl here");
            m.body().block().directStatement(getReturnString(method.getReturnType().getName()));
        }
    }
}
Also used : JavaInterface(com.sun.tools.ws.processor.model.java.JavaInterface) JDefinedClass(com.sun.codemodel.JDefinedClass) WebService(jakarta.jws.WebService) QName(javax.xml.namespace.QName) Port(com.sun.tools.ws.processor.model.Port) JClass(com.sun.codemodel.JClass) JCommentPart(com.sun.codemodel.JCommentPart) Fault(com.sun.tools.ws.processor.model.Fault) Operation(com.sun.tools.ws.processor.model.Operation) JDocComment(com.sun.codemodel.JDocComment) JClassAlreadyExistsException(com.sun.codemodel.JClassAlreadyExistsException) JAXBTypeAndAnnotation(com.sun.tools.ws.processor.model.jaxb.JAXBTypeAndAnnotation) BindingType(jakarta.xml.ws.BindingType) JAnnotationUse(com.sun.codemodel.JAnnotationUse) JavaMethod(com.sun.tools.ws.processor.model.java.JavaMethod) JavaParameter(com.sun.tools.ws.processor.model.java.JavaParameter) JMethod(com.sun.codemodel.JMethod) JVar(com.sun.codemodel.JVar)

Example 2 with Port

use of com.sun.tools.ws.processor.model.Port in project metro-jax-ws by eclipse-ee4j.

the class ServiceGenerator method visit.

@Override
public void visit(Service service) {
    JavaInterface intf = service.getJavaInterface();
    String className = Names.customJavaTypeClassName(intf);
    if (donotOverride && GeneratorUtil.classExists(options, className)) {
        log("Class " + className + " exists. Not overriding.");
        return;
    }
    JDefinedClass cls;
    try {
        cls = getClass(className, ClassType.CLASS);
    } catch (JClassAlreadyExistsException e) {
        receiver.error(service.getLocator(), GeneratorMessages.GENERATOR_SERVICE_CLASS_ALREADY_EXIST(className, service.getName()));
        return;
    }
    cls._extends(jakarta.xml.ws.Service.class);
    String serviceFieldName = BindingHelper.mangleNameToClassName(service.getName().getLocalPart()).toUpperCase(Locale.ENGLISH);
    String wsdlLocationName = serviceFieldName + "_WSDL_LOCATION";
    JFieldVar urlField = cls.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, URL.class, wsdlLocationName);
    JFieldVar exField = cls.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, WebServiceException.class, serviceFieldName + "_EXCEPTION");
    String serviceName = serviceFieldName + "_QNAME";
    cls.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, QName.class, serviceName, JExpr._new(cm.ref(QName.class)).arg(service.getName().getNamespaceURI()).arg(service.getName().getLocalPart()));
    JClass qNameCls = cm.ref(QName.class);
    JInvocation inv;
    inv = JExpr._new(qNameCls);
    inv.arg("namespace");
    inv.arg("localpart");
    if (options.useBaseResourceAndURLToLoadWSDL) {
        writeClassLoaderBaseResourceWSDLLocation(className, cls, urlField, exField);
    } else if (wsdlLocation.startsWith("http://") || wsdlLocation.startsWith("https://") || wsdlLocation.startsWith("file:/")) {
        writeAbsWSDLLocation(cls, urlField, exField);
    } else if (wsdlLocation.startsWith("META-INF/")) {
        writeClassLoaderResourceWSDLLocation(className, cls, urlField, exField);
    } else {
        writeResourceWSDLLocation(className, cls, urlField, exField);
    }
    // write class comment - JAXWS warning
    JDocComment comment = cls.javadoc();
    if (service.getJavaDoc() != null) {
        comment.add(service.getJavaDoc());
        comment.add("\n\n");
    }
    comment.addAll(getJAXWSClassComment());
    // Generating constructor
    // for e.g:  public ExampleService()
    JMethod constructor1 = cls.constructor(JMod.PUBLIC);
    String constructor1Str = String.format("super(__getWsdlLocation(), %s);", serviceName);
    constructor1.body().directStatement(constructor1Str);
    // for e.g:  public ExampleService(WebServiceFeature ... features)
    if (options.target.isLaterThan(Options.Target.V2_2)) {
        JMethod constructor2 = cls.constructor(JMod.PUBLIC);
        constructor2.varParam(WebServiceFeature.class, "features");
        String constructor2Str = String.format("super(__getWsdlLocation(), %s, features);", serviceName);
        constructor2.body().directStatement(constructor2Str);
    }
    // for e.g:  public ExampleService(URL wsdlLocation)
    if (options.target.isLaterThan(Options.Target.V2_2)) {
        JMethod constructor3 = cls.constructor(JMod.PUBLIC);
        constructor3.param(URL.class, "wsdlLocation");
        String constructor3Str = String.format("super(wsdlLocation, %s);", serviceName);
        constructor3.body().directStatement(constructor3Str);
    }
    // for e.g:  public ExampleService(URL wsdlLocation, WebServiceFeature ... features)
    if (options.target.isLaterThan(Options.Target.V2_2)) {
        JMethod constructor4 = cls.constructor(JMod.PUBLIC);
        constructor4.param(URL.class, "wsdlLocation");
        constructor4.varParam(WebServiceFeature.class, "features");
        String constructor4Str = String.format("super(wsdlLocation, %s, features);", serviceName);
        constructor4.body().directStatement(constructor4Str);
    }
    // Generating constructor
    // for e.g:  public ExampleService(URL wsdlLocation, QName serviceName)
    JMethod constructor5 = cls.constructor(JMod.PUBLIC);
    constructor5.param(URL.class, "wsdlLocation");
    constructor5.param(QName.class, "serviceName");
    constructor5.body().directStatement("super(wsdlLocation, serviceName);");
    // for e.g:  public ExampleService(URL, QName, WebServiceFeature ...)
    if (options.target.isLaterThan(Options.Target.V2_2)) {
        JMethod constructor6 = cls.constructor(JMod.PUBLIC);
        constructor6.param(URL.class, "wsdlLocation");
        constructor6.param(QName.class, "serviceName");
        constructor6.varParam(WebServiceFeature.class, "features");
        constructor6.body().directStatement("super(wsdlLocation, serviceName, features);");
    }
    // @WebService
    JAnnotationUse webServiceClientAnn = cls.annotate(cm.ref(WebServiceClient.class));
    writeWebServiceClientAnnotation(service, webServiceClientAnn);
    // additional annotations
    for (GeneratorExtension f : ServiceFinder.find(GeneratorExtension.class, ServiceLoader.load(GeneratorExtension.class))) {
        f.writeWebServiceClientAnnotation(options, cm, cls);
    }
    // @HandlerChain
    writeHandlerConfig(Names.customJavaTypeClassName(service.getJavaInterface()), cls, options);
    for (Port port : service.getPorts()) {
        if (port.isProvider()) {
            // No getXYZPort() for porvider based endpoint
            continue;
        }
        // Get the SEI class
        JType retType;
        try {
            retType = getClass(port.getJavaInterface().getName(), ClassType.INTERFACE);
        } catch (JClassAlreadyExistsException e) {
            QName portTypeName = (QName) port.getProperty(ModelProperties.PROPERTY_WSDL_PORT_TYPE_NAME);
            Locator loc = null;
            if (portTypeName != null) {
                PortType pt = port.portTypes.get(portTypeName);
                if (pt != null) {
                    loc = pt.getLocator();
                }
            }
            receiver.error(loc, GeneratorMessages.GENERATOR_SEI_CLASS_ALREADY_EXIST(port.getJavaInterface().getName(), portTypeName));
            return;
        }
        // write getXyzPort()
        writeDefaultGetPort(port, retType, cls);
        // write getXyzPort(WebServicesFeature...)
        if (options.target.isLaterThan(Options.Target.V2_1)) {
            writeGetPort(port, retType, cls);
        }
    }
    writeGetWsdlLocation(cm.ref(URL.class), cls, urlField, exField);
}
Also used : JavaInterface(com.sun.tools.ws.processor.model.java.JavaInterface) JDefinedClass(com.sun.codemodel.JDefinedClass) QName(javax.xml.namespace.QName) JClass(com.sun.codemodel.JClass) Port(com.sun.tools.ws.processor.model.Port) JInvocation(com.sun.codemodel.JInvocation) WebServiceClient(jakarta.xml.ws.WebServiceClient) JDocComment(com.sun.codemodel.JDocComment) URL(java.net.URL) JClassAlreadyExistsException(com.sun.codemodel.JClassAlreadyExistsException) Locator(org.xml.sax.Locator) JFieldVar(com.sun.codemodel.JFieldVar) JAnnotationUse(com.sun.codemodel.JAnnotationUse) JMethod(com.sun.codemodel.JMethod) JType(com.sun.codemodel.JType) PortType(com.sun.tools.ws.wsdl.document.PortType)

Example 3 with Port

use of com.sun.tools.ws.processor.model.Port in project metro-jax-ws by eclipse-ee4j.

the class WSDLModeler method processPort.

/* (non-Javadoc)
     * @see WSDLModelerBase#processPort(WSDLPort, Service, WSDLDocument)
     */
protected boolean processPort(com.sun.tools.ws.wsdl.document.Port wsdlPort, Service service, WSDLDocument document) {
    try {
        // clear the  unique block map
        uniqueBodyBlocks.clear();
        QName portQName = getQNameOf(wsdlPort);
        Port port = new Port(portQName, wsdlPort);
        setDocumentationIfPresent(port, wsdlPort.getDocumentation());
        SOAPAddress soapAddress = (SOAPAddress) getExtensionOfType(wsdlPort, SOAPAddress.class);
        if (soapAddress == null) {
            if (options.isExtensionMode()) {
                warning(wsdlPort, ModelerMessages.WSDLMODELER_WARNING_NO_SOAP_ADDRESS(wsdlPort.getName()));
            } else {
                // not a SOAP port, ignore it
                warning(wsdlPort, ModelerMessages.WSDLMODELER_WARNING_IGNORING_NON_SOAP_PORT_NO_ADDRESS(wsdlPort.getName()));
                return false;
            }
        }
        if (soapAddress != null) {
            port.setAddress(soapAddress.getLocation());
        }
        Binding binding = wsdlPort.resolveBinding(document);
        QName bindingName = getQNameOf(binding);
        PortType portType = binding.resolvePortType(document);
        port.setProperty(ModelProperties.PROPERTY_WSDL_PORT_NAME, getQNameOf(wsdlPort));
        port.setProperty(ModelProperties.PROPERTY_WSDL_PORT_TYPE_NAME, getQNameOf(portType));
        port.setProperty(ModelProperties.PROPERTY_WSDL_BINDING_NAME, bindingName);
        boolean isProvider = isProvider(wsdlPort);
        if (_bindingNameToPortMap.containsKey(bindingName) && !isProvider) {
            // this binding has been processed before
            Port existingPort = _bindingNameToPortMap.get(bindingName);
            port.setOperations(existingPort.getOperations());
            port.setJavaInterface(existingPort.getJavaInterface());
            port.setStyle(existingPort.getStyle());
            port.setWrapped(existingPort.isWrapped());
        } else {
            // find out the SOAP binding extension, if any
            SOAPBinding soapBinding = (SOAPBinding) getExtensionOfType(binding, SOAPBinding.class);
            if (soapBinding == null) {
                soapBinding = (SOAPBinding) getExtensionOfType(binding, SOAP12Binding.class);
                if (soapBinding == null) {
                    if (!options.isExtensionMode()) {
                        // cannot deal with non-SOAP ports
                        warning(wsdlPort, ModelerMessages.WSDLMODELER_WARNING_IGNORING_NON_SOAP_PORT(wsdlPort.getName()));
                        return false;
                    } else {
                        warning(wsdlPort, ModelerMessages.WSDLMODELER_WARNING_NON_SOAP_PORT(wsdlPort.getName()));
                    }
                } else {
                    // we can only do soap1.2 if extensions are on
                    if (options.isExtensionMode()) {
                        warning(wsdlPort, ModelerMessages.WSDLMODELER_WARNING_PORT_SOAP_BINDING_12(wsdlPort.getName()));
                    } else {
                        warning(wsdlPort, ModelerMessages.WSDLMODELER_WARNING_IGNORING_SOAP_BINDING_12(wsdlPort.getName()));
                        return false;
                    }
                }
            }
            if (soapBinding != null && (soapBinding.getTransport() == null || (!soapBinding.getTransport().equals(SOAPConstants.URI_SOAP_TRANSPORT_HTTP) && !soapBinding.getTransport().equals(SOAP12Constants.URI_SOAP_TRANSPORT_HTTP)))) {
                if (!options.isExtensionMode()) {
                    // cannot deal with non-HTTP ports
                    warning(wsdlPort, ModelerMessages.WSDLMODELER_WARNING_IGNORING_SOAP_BINDING_NON_HTTP_TRANSPORT(wsdlPort.getName()));
                    return false;
                }
            }
            /*
                  validate wsdl:binding uniqueness in style, e.g. rpclit or doclit
                  ref: WSI BP 1.1 R 2705
                 */
            if (soapBinding != null && !validateWSDLBindingStyle(binding)) {
                if (options.isExtensionMode()) {
                    warning(wsdlPort, ModelerMessages.WSDLMODELER_WARNING_PORT_SOAP_BINDING_MIXED_STYLE(wsdlPort.getName()));
                } else {
                    error(wsdlPort, ModelerMessages.WSDLMODELER_WARNING_IGNORING_SOAP_BINDING_MIXED_STYLE(wsdlPort.getName()));
                }
            }
            if (soapBinding != null) {
                port.setStyle(soapBinding.getStyle());
            }
            boolean hasOverloadedOperations = false;
            Set<String> operationNames = new HashSet<>();
            for (Iterator iter = portType.operations(); iter.hasNext(); ) {
                com.sun.tools.ws.wsdl.document.Operation operation = (com.sun.tools.ws.wsdl.document.Operation) iter.next();
                if (operationNames.contains(operation.getName())) {
                    hasOverloadedOperations = true;
                    break;
                }
                operationNames.add(operation.getName());
                for (Iterator itr = binding.operations(); iter.hasNext(); ) {
                    BindingOperation bindingOperation = (BindingOperation) itr.next();
                    if (operation.getName().equals(bindingOperation.getName())) {
                        break;
                    } else if (!itr.hasNext()) {
                        error(bindingOperation, ModelerMessages.WSDLMODELER_INVALID_BINDING_OPERATION_NOT_FOUND(operation.getName(), bindingOperation.getName()));
                    }
                }
            }
            Map headers = new HashMap();
            boolean hasOperations = false;
            for (Iterator iter = binding.operations(); iter.hasNext(); ) {
                BindingOperation bindingOperation = (BindingOperation) iter.next();
                com.sun.tools.ws.wsdl.document.Operation portTypeOperation = null;
                Set operations = portType.getOperationsNamed(bindingOperation.getName());
                if (operations.isEmpty()) {
                    // the WSDL document is invalid
                    error(bindingOperation, ModelerMessages.WSDLMODELER_INVALID_BINDING_OPERATION_NOT_IN_PORT_TYPE(bindingOperation.getName(), binding.getName()));
                } else if (operations.size() == 1) {
                    portTypeOperation = (com.sun.tools.ws.wsdl.document.Operation) operations.iterator().next();
                } else {
                    boolean found = false;
                    String expectedInputName = bindingOperation.getInput().getName();
                    String expectedOutputName = bindingOperation.getOutput().getName();
                    for (Iterator iter2 = operations.iterator(); iter2.hasNext(); ) {
                        com.sun.tools.ws.wsdl.document.Operation candidateOperation = (com.sun.tools.ws.wsdl.document.Operation) iter2.next();
                        if (expectedInputName == null) {
                            // the WSDL document is invalid
                            error(bindingOperation, ModelerMessages.WSDLMODELER_INVALID_BINDING_OPERATION_MISSING_INPUT_NAME(bindingOperation.getName()));
                        }
                        if (expectedOutputName == null) {
                            // the WSDL document is invalid
                            error(bindingOperation, ModelerMessages.WSDLMODELER_INVALID_BINDING_OPERATION_MISSING_OUTPUT_NAME(bindingOperation.getName()));
                        }
                        if (expectedInputName.equals(candidateOperation.getInput().getName()) && expectedOutputName.equals(candidateOperation.getOutput().getName())) {
                            if (found) {
                                // the WSDL document is invalid
                                error(bindingOperation, ModelerMessages.WSDLMODELER_INVALID_BINDING_OPERATION_MULTIPLE_MATCHING_OPERATIONS(bindingOperation.getName(), bindingOperation.getName()));
                            }
                            // got it!
                            found = true;
                            portTypeOperation = candidateOperation;
                        }
                    }
                    if (!found) {
                        // the WSDL document is invalid
                        error(bindingOperation, ModelerMessages.WSDLMODELER_INVALID_BINDING_OPERATION_NOT_FOUND(bindingOperation.getName(), binding.getName()));
                    }
                }
                if (!isProvider) {
                    this.info = new ProcessSOAPOperationInfo(port, wsdlPort, portTypeOperation, bindingOperation, soapBinding, document, hasOverloadedOperations, headers);
                    Operation operation;
                    if (soapBinding != null) {
                        operation = processSOAPOperation();
                    } else {
                        operation = processNonSOAPOperation();
                    }
                    if (operation != null) {
                        port.addOperation(operation);
                        hasOperations = true;
                    }
                }
            }
            if (!isProvider && !hasOperations) {
                // emit a warning if there are no operations, except when its a provider port
                warning(wsdlPort, ModelerMessages.WSDLMODELER_WARNING_NO_OPERATIONS_IN_PORT(wsdlPort.getName()));
                return false;
            }
            createJavaInterfaceForPort(port, isProvider);
            PortType pt = binding.resolvePortType(document);
            String jd = (pt.getDocumentation() != null) ? pt.getDocumentation().getContent() : null;
            port.getJavaInterface().setJavaDoc(jd);
            _bindingNameToPortMap.put(bindingName, port);
        }
        service.addPort(port);
        applyPortMethodCustomization(port, wsdlPort);
        applyWrapperStyleCustomization(port, binding.resolvePortType(document));
        return true;
    } catch (NoSuchEntityException e) {
        warning(document.getDefinitions(), e.getMessage());
        // should not happen
        return false;
    }
}
Also used : Port(com.sun.tools.ws.processor.model.Port) com.sun.tools.ws.wsdl.document(com.sun.tools.ws.wsdl.document) Operation(com.sun.tools.ws.processor.model.Operation) JAXWSBinding(com.sun.tools.ws.wsdl.document.jaxws.JAXWSBinding) QName(javax.xml.namespace.QName)

Aggregations

Port (com.sun.tools.ws.processor.model.Port)3 QName (javax.xml.namespace.QName)3 JAnnotationUse (com.sun.codemodel.JAnnotationUse)2 JClass (com.sun.codemodel.JClass)2 JClassAlreadyExistsException (com.sun.codemodel.JClassAlreadyExistsException)2 JDefinedClass (com.sun.codemodel.JDefinedClass)2 JDocComment (com.sun.codemodel.JDocComment)2 JMethod (com.sun.codemodel.JMethod)2 Operation (com.sun.tools.ws.processor.model.Operation)2 JavaInterface (com.sun.tools.ws.processor.model.java.JavaInterface)2 JCommentPart (com.sun.codemodel.JCommentPart)1 JFieldVar (com.sun.codemodel.JFieldVar)1 JInvocation (com.sun.codemodel.JInvocation)1 JType (com.sun.codemodel.JType)1 JVar (com.sun.codemodel.JVar)1 Fault (com.sun.tools.ws.processor.model.Fault)1 JavaMethod (com.sun.tools.ws.processor.model.java.JavaMethod)1 JavaParameter (com.sun.tools.ws.processor.model.java.JavaParameter)1 JAXBTypeAndAnnotation (com.sun.tools.ws.processor.model.jaxb.JAXBTypeAndAnnotation)1 com.sun.tools.ws.wsdl.document (com.sun.tools.ws.wsdl.document)1