public static WSDLInterface createWSDLInterface()

in modules/core/src/main/java/org/apache/tuscany/sca/core/assembly/impl/WSDLHelper.java [180:348]


    public static WSDLInterface createWSDLInterface(ExtensionPointRegistry registry, String wsdl) {
        try {
            // Read all the WSDL and XSD in from the wsdl string. The WSDL and XSD appear sequentially in
            // the following format:
            //
            // filename
            // _X_
            // wsdl xml
            // _X_
            // xsd xml
            // _X_
            // xsd xml
            //
            // So we need to read each WSDL and XSD separately and then fix up the includes/imports as appropriate
            String xmlArray[] = wsdl.split("_X_");
            
            String topWSDLLocation = null;
            Map<String, XMLString> xmlMap = new HashMap<String, XMLString>();
            
            for (int i = 0; i < xmlArray.length; i = i + 2){
                String location = xmlArray[i];
                String xml = xmlArray[i+1];
                // strip the file name out of the location
                location = location.substring(location.lastIndexOf("/") + 1);
                
                if (location.endsWith(".wsdl")){
                    xmlMap.put(location,
                               new WSDLInfo(xmlArray[i],
                                            xml));
                    
                    if (topWSDLLocation == null){
                        topWSDLLocation = location;
                    }
                } else {
                    xmlMap.put(location,
                            new XSDInfo(xmlArray[i],
                                        xml));
                }
            }
            
            FactoryExtensionPoint modelFactories = registry.getExtensionPoint(FactoryExtensionPoint.class);
            org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory wsdlFactory = modelFactories.getFactory(org.apache.tuscany.sca.interfacedef.wsdl.WSDLFactory.class);
            XSDFactory xsdFactory = modelFactories.getFactory(XSDFactory.class);
            XmlSchemaCollection schemaCollection = new XmlSchemaCollection();
            schemaCollection.setSchemaResolver(new XSDURIResolverImpl(xmlMap));
            ContributionFactory contributionFactory = modelFactories.getFactory(ContributionFactory.class);
            final org.apache.tuscany.sca.contribution.Contribution contribution = contributionFactory.createContribution();
            ProcessorContext processorContext = new ProcessorContext();
            
            ExtensibleModelResolver extensibleResolver = new ExtensibleModelResolver(contribution, registry.getExtensionPoint(ModelResolverExtensionPoint.class), modelFactories);
            ModelResolver wsdlResolver = (ModelResolver)extensibleResolver.getModelResolverInstance(WSDLDefinition.class);
            XSDModelResolver xsdResolver = (XSDModelResolver)extensibleResolver.getModelResolverInstance(XSDefinition.class);
            contribution.setURI("temp");
            contribution.setLocation(topWSDLLocation);
            contribution.setModelResolver(extensibleResolver);
    
            // read
            for (XMLString xmlString : xmlMap.values()){
                if (xmlString instanceof WSDLInfo){
                    WSDLReader reader;
                    try {
                        reader =  AccessController.doPrivileged(new PrivilegedExceptionAction<WSDLReader>() {
                            public WSDLReader run() throws WSDLException {
                                return javax.wsdl.factory.WSDLFactory.newInstance().newWSDLReader();                         
                            }
                        });
                    } catch (PrivilegedActionException e){
                        throw (WSDLException)e.getException();
                    }
                    reader.setFeature("javax.wsdl.verbose", false);
                    reader.setFeature("javax.wsdl.importDocuments", true);
                    final WSDLLocatorImpl locator = new WSDLLocatorImpl(xmlString.getBaseURI(), xmlMap);
                    final WSDLReader freader = reader;
                    Definition readDefinition;
                    try {
                        readDefinition = AccessController.doPrivileged(new PrivilegedExceptionAction<Definition>() {
                            public Definition run() throws WSDLException {
                                return freader.readWSDL(locator);                        
                            }
                        });
                    } catch (PrivilegedActionException e){
                        throw (WSDLException)e.getException();
                    }
                    
                    WSDLDefinition wsdlDefinition = wsdlFactory.createWSDLDefinition();
                    wsdlDefinition.setDefinition(readDefinition);
                    wsdlDefinition.setLocation(new URI(xmlString.getBaseURI()));
                    
                    ((WSDLInfo)xmlString).setWsdlDefintion(wsdlDefinition);
                    wsdlResolver.addModel(wsdlDefinition, processorContext);
                    
                } else {
                    InputStream inputStream = new ByteArrayInputStream(xmlString.getXmlString().getBytes());
                    InputSource inputSource = new InputSource(inputStream);
                    inputSource.setSystemId(xmlString.getBaseURI());
                    XmlSchema schema = schemaCollection.read(inputSource, null);
                    inputStream.close();
                    
                    XSDefinition xsdDefinition = xsdFactory.createXSDefinition();
                    xsdDefinition.setSchema(schema);
                    
                    ((XSDInfo)xmlString).setXsdDefinition(xsdDefinition);
                    xsdResolver.addModel(xsdDefinition, processorContext);
                }
            }
            
            // resolve
            for (XMLString xmlString : xmlMap.values()){
                if (xmlString instanceof WSDLInfo){
                   WSDLDefinition wsdlDefinition = ((WSDLInfo)xmlString).getWsdlDefintion();
                   
                   // link to imports
                   for (Map.Entry<String, List<javax.wsdl.Import>> entry :
                       ((Map<String, List<javax.wsdl.Import>>)wsdlDefinition.getDefinition().getImports()).entrySet()) {
                       for (javax.wsdl.Import imp : entry.getValue()) {
                           String wsdlName = imp.getDefinition().getDocumentBaseURI();
                           WSDLInfo wsdlInfo = (WSDLInfo)xmlMap.get(getFilenameWithoutPath(wsdlName));
                           wsdlDefinition.getImportedDefinitions().add(wsdlInfo.getWsdlDefintion());
                       }
                   }
                   
                   // extract any in-line types in the Tuscany model
                   Types types = wsdlDefinition.getDefinition().getTypes();
                   if ( types != null){                       
/*  read XSD from WSDL rather than from registry 
                       for (int i=0; i < types.getExtensibilityElements().size(); i++){
                       
                           String schemaName = xmlString.getBaseURI() + "#" + i++;
                           XSDInfo xsdInfo = (XSDInfo)xmlMap.get(getFilenameWithoutPath(schemaName));
                           if (xsdInfo != null){
                               wsdlDefinition.getXmlSchemas().add(xsdInfo.getXsdDefinition());
                           }
*/ 
                       int index = 0;
                       for (Object ext : types.getExtensibilityElements()) {
                           ExtensibilityElement extElement = (ExtensibilityElement)ext;
                           Element element = null;
                           if (extElement instanceof Schema) {
                               element = ((Schema)extElement).getElement();
                           }
                           if (element != null) {
                               XSDefinition xsDefinition = xsdFactory.createXSDefinition();
                               xsDefinition.setUnresolved(true);
                               xsDefinition.setNamespace(element.getAttribute("targetNamespace"));
                               xsDefinition.setDocument(element.getOwnerDocument());
                               XmlSchema schema = schemaCollection.read(element, null);
                               xsDefinition.setSchema(schema);
                               xsDefinition.setLocation(URI.create(xmlString.getBaseURI() + "#" + index));
                               wsdlDefinition.getXmlSchemas().add(xsDefinition);
                               index++;
                           }
                       }
                   }
                } else {
                    // TODO
                }
            }
            
            WSDLInfo topWSDL = (WSDLInfo)xmlMap.get(topWSDLLocation);
            WSDLDefinition topWSDLDefinition = topWSDL.getWsdlDefintion();
            
            PortType portType = (PortType)topWSDLDefinition.getDefinition().getAllPortTypes().values().iterator().next();
            WSDLInterface readWSDLInterface = wsdlFactory.createWSDLInterface(portType, topWSDLDefinition, extensibleResolver, null);        
            
            return readWSDLInterface;
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
    }