wsdl11toaxisservicebuilder.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 1,531 行 · 第 1/5 页

JAVA
1,531
字号
            axisBinding.addChild(axisBindingOperation.getName(), axisBindingOperation);
        }
        axisBinding.setProperty(WSDL2Constants.HTTP_LOCATION_TABLE, httpLocationMap);
    }

    /**
     * contains all code which gathers non-service specific information from the
     * wsdl. <p/> After all the setup completes successfully, the setupComplete
     * field is set so that any subsequent calls to setup() will result in a
     * no-op. Note that subclass WSDL11ToAllAxisServicesBuilder will call
     * populateService for each port in the WSDL. Separating the non-service
     * specific information here allows WSDL11ToAllAxisServicesBuilder to only
     * do this work 1 time per WSDL, instead of for each port on each service.
     *
     * @throws WSDLException if readInTheWSDLFile fails
     */
    protected void setup() throws WSDLException {
        if (setupComplete) { // already setup, just do nothing and return
            return;
        }
        if (wsdl4jDefinition == null) {
            wsdl4jDefinition = readInTheWSDLFile(in);
        }
        if (wsdl4jDefinition == null) {
            return; // can't continue without wsdl
        }

        // process the imports
//        WSDL4JImportedWSDLHelper.processImports(wsdl4jDefinition, new ArrayList());

        // setup the schemaMap
        this.schemaMap = new HashMap();
        populateSchemaMap(wsdl4jDefinition, new Stack());

        setPolicyRegistryFromService(axisService);

        setupComplete = true; // if any part of setup fails, don't mark
        // setupComplete        
    }


    /**
     * Populate a map of targetNamespace vs DOM schema element This is used to
     * grab the correct schema element when adding a new element
     *
     * @param definition
     */

    private void populateSchemaMap(Definition definition, Stack stack) {
        stack.push(definition);
        Types types = definition.getTypes();
        Object extensibilityElement;
        if (types != null) {
            for (Iterator iterator = types.getExtensibilityElements().iterator(); iterator.hasNext();)
            {
                extensibilityElement = iterator.next();
                if (extensibilityElement instanceof Schema) {
                    Element schemaElement = ((Schema) extensibilityElement).getElement();
                    schemaMap.put(schemaElement.getAttribute(XSD_TARGETNAMESPACE), schemaElement);
                }
            }
        }

        // popualte the imports as well
        Iterator iter = definition.getImports().values().iterator();
        Vector values = null;
        Import wsdlImport = null;
        for (; iter.hasNext();) {
            values = (Vector) iter.next();
            for (Iterator valuesIter = values.iterator(); valuesIter.hasNext();) {
                wsdlImport = (Import) valuesIter.next();
                Definition innerDefinition = wsdlImport.getDefinition();
                if(!stack.contains(innerDefinition)) {
                    populateSchemaMap(innerDefinition, stack);
            }
        }
    }
        stack.pop();
    }


    /**
     * return the service to process
     * if user has specified we check whether it exists
     * else pick a random service and throws an exception if not found any thing
     *
     * @param definition
     * @return service to process
     * @throws AxisFault
     */

    private Service findService(Definition definition) throws AxisFault {
        Map services = definition.getServices();
        Service service = null;
        if (serviceName != null) {
            // i.e if a user has specified a pirticular port
            service = (Service) services.get(serviceName);
            if (service == null) {
                throw new AxisFault("Service " + serviceName
                                    + " was not found in the WSDL");
            }
        } else {
            if (services.size() > 0) {
                for (Iterator iter = services.values().iterator(); iter.hasNext();) {
                    service = (Service) iter.next();
                    if (service.getPorts().size() > 0) {
                        //i.e we have found a service with ports
                        break;
                    }
                }
                if ((service == null) || (service.getPorts().size() == 0)) {
                    throw new AxisFault("there is no service with ports to pick");
                }

            } else {
                throw new AxisFault("No service was not found in the WSDL at " +
                                    definition.getDocumentBaseURI()
                                    + " with targetnamespace "
                                    + definition.getTargetNamespace());
            }
        }
        return service;
    }

    /**
     * Look for the relevant binding!
     * if user has spcifed a port get it
     * otherwise find first soap port or pick random one if there is no soap port
     *
     * @param dif
     * @param service service can not be null
     * @throws AxisFault
     */
    private Binding findBinding(Definition dif, Service service) throws AxisFault {

        Binding binding = null;
        Port port = null;
        copyExtensibleElements(service.getExtensibilityElements(), dif, axisService, SERVICE);
        if (portName != null) {
            // i.e if user has specified a service
            port = service.getPort(portName);
            if (port == null) {
                throw new AxisFault("No port found for the given name :" + portName);
            }
        } else {
            Map ports = service.getPorts();
            if (ports != null && ports.size() > 0) {
                // pick the port with the SOAP address as the default port
                port = findPort(ports);
                if (port == null) {
                    // a SOAP port was not found - log a warning
                    // and use the first port in the list
                    log.info("A SOAP port was not found - "
                             + "picking a random port!");
                    port = (Port) ports.values().toArray()[0];
                }

                if (port != null) {
                    // i.e we have find a correct port
                    if (!this.isAllPorts) {
                        // if user has not set all option
                        // we have to generate code only for that option.
                        this.portName = port.getName();
                    }
                }
            }
        }

        axisService.setName(service.getQName().getLocalPart());

        if (port != null) {
            copyExtensibleElements(port.getExtensibilityElements(), dif,
                                   axisService, PORT);
//            binding = dif.getBinding(port.getBinding().getQName());
            binding = getBinding(port.getBinding().getQName(), dif);
            if (binding == null) {
                binding = port.getBinding();
            }
        }

        return binding;
    }

    /**
     * find the message from imported wsdls
     *
     * @param messageQName
     * @param definition
     * @return message
     */
    private Message getMessage(QName messageQName, Definition definition) {
        Message message = null;
        // this can be in a imported wsdl
        Iterator iter = definition.getImports().values().iterator();
        Vector values = null;
        Import wsdlImport = null;
        for (; iter.hasNext();) {
            values = (Vector) iter.next();
            for (Iterator valuesIter = values.iterator(); valuesIter.hasNext();) {
                wsdlImport = (Import) valuesIter.next();
                // find the binding recursively
                message = getMessage(messageQName, wsdlImport.getDefinition());
                if (message != null) {
                    break;
                }
            }
            if (message != null) {
                break;
            }
        }

        if (message == null) {
            message = definition.getMessage(messageQName);
        }
        return message;
    }

    /**
     * get the port type form all the imported documents
     *
     * @param portTypeQName
     * @param definition
     * @return portType
     */

    private PortType getPortType(QName portTypeQName, Definition definition) {
        return getPortType(portTypeQName, definition, new Stack());
    }

    /**
     * get the port type form all the imported documents
     *
     * @param portTypeQName
     * @param definition
     * @return portType
     */

    private PortType getPortType(QName portTypeQName, Definition definition, Stack stack) {
        stack.push(definition);
        PortType portType = null;
        Iterator iter = definition.getImports().values().iterator();
        Vector values = null;
        Import wsdlImport = null;
        for (; iter.hasNext();) {
            values = (Vector) iter.next();
            for (Iterator valuesIter = values.iterator(); valuesIter.hasNext();) {
                wsdlImport = (Import) valuesIter.next();
                Definition innerDefinition = wsdlImport.getDefinition();
                if(stack.contains(innerDefinition)){
                // find the binding recursively
                    portType = getPortType(portTypeQName, innerDefinition, stack);
                if (portType != null) {
                    break;
                }
            }
            }
            if (portType != null) {
                break;
            }
        }

        if (portType == null) {
            // this can be in a imported wsdl
            portType = definition.getPortType(portTypeQName);
        }
        stack.pop();
        return portType;
    }

    private Binding getBinding(QName bindingQName, Definition definition) {
        ArrayList list = new ArrayList();
        Binding binding = getBinding(bindingQName, definition, list);
        if (binding == null) {
            for(int i=0;i<list.size();i++){
                Binding binding2 = definition.getBinding(bindingQName);
                if(binding2 != null && binding2.getPortType() != null){
                    binding = binding2;
                    break;
                }
            }
        }
        return binding;
    }

    /**
     * first find the binding in the given definition
     * if not found serch in the imported doucuments
     *
     * @param bindingQName
     * @param definition
     * @return binding
     */

    private Binding getBinding(QName bindingQName, Definition definition, ArrayList list) {
        list.add(definition);
        Binding binding = null;
        //first try to find a binding in the upper inmport
        Iterator iter = definition.getImports().values().iterator();
        Vector values = null;
        Import wsdlImport = null;
        for (; iter.hasNext();) {
            values = (Vector) iter.next();
            for (Iterator valuesIter = values.iterator(); valuesIter.hasNext();) {
                wsdlImport = (Import) valuesIter.next();
                Definition innerDefinition = wsdlImport.getDefinition();
                if(!list.contains(innerDefinition)) {
                // find the binding recursively

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?