FileDocCategorySizeDatePackage
ServiceDescriptorImpl.javaAPI DocExample13558Tue May 29 16:56:34 BST 2007com.sun.xml.ws.mex.client

ServiceDescriptorImpl

public class ServiceDescriptorImpl extends com.sun.xml.ws.api.wsdl.parser.ServiceDescriptor
This class is used by the JAX-WS code when it needs to retrieve metadata from an endpoint using mex. An address is passed into the MetadataResolverImpl class, which creates a service descriptor and returns it.

Because wsdl and schema import@location attributes are removed from the data when empty, this class will add them back in for wsdl imports. The value that is used for the attribute matches the systemId of the Source that contains the imported wsdl (which may be different from the target namespace of the wsdl).

Fields Summary
private final List
wsdls
private final List
schemas
private final List
importsToPatch
private final Map
nsToSysIdMap
private static final String
LOCATION
private static final String
NAMESPACE
private static final Logger
logger
Constructors Summary
public ServiceDescriptorImpl(com.sun.xml.ws.mex.client.schema.Metadata mData)
The ServiceDescriptorImpl constructor does the work of parsing the data in the Metadata object.

    
                       
       
        wsdls = new ArrayList<Source>();
        schemas = new ArrayList<Source>();
        importsToPatch = new ArrayList<Node>();
        nsToSysIdMap = new HashMap<String, String>();
        populateLists(mData);
        if (!importsToPatch.isEmpty()) {
            patchImports();
        }
    
Methods Summary
private voidcheckWsdlImports(org.w3c.dom.Node wsdl)

        final NodeList kids = wsdl.getChildNodes();
        for (int i=0; i<kids.getLength(); i++) {
            final Node importNode = kids.item(i);
            if (importNode.getLocalName() != null &&
                importNode.getLocalName().equals("import")) {
                
                final Node location =
                    importNode.getAttributes().getNamedItem(LOCATION);
                if (location == null) {
                    importsToPatch.add(importNode);
                }
            }
        }
    
private javax.xml.transform.SourcecreateSource(com.sun.xml.ws.mex.client.schema.MetadataSection section, java.lang.String identifier)

        
        final Node node = (Node) section.getAny();
        String sysId = identifier;
        if (section.getDialect().equals(WSDL_DIALECT)) {
            final String targetNamespace = getNamespaceFromNode(node);
            if (sysId == null) {
                sysId = targetNamespace;
            }
            nsToSysIdMap.put(targetNamespace, sysId);
            checkWsdlImports(node);
        } else {
            if (sysId == null) {
                sysId = getNamespaceFromNode(node);
            }
        }
        final Source source = new DOMSource(node);
        source.setSystemId(sysId);
        return source;
    
private java.lang.StringgetNamespaceFromNode(org.w3c.dom.Node node)

        final Node namespace = node.getAttributes().getNamedItem("targetNamespace");
        if (namespace == null) {
            // bug in the server? want to avoid NPE if so
            logger.warning(
                MessagesMessages.MEX_0003_UNKNOWN_WSDL_NAMESPACE(
                node.getNodeName()));
            return null;
        }
        return namespace.getNodeValue();
    
public java.util.ListgetSchemas()

        return schemas;
    
private javax.xml.transform.SourcegetSourceFromLocation(java.lang.String address, java.lang.String identifier)

        
        try {
            final HttpPoster poster = new HttpPoster();
            final InputStream response = poster.makeGetCall(address);
            if (identifier != null) {
                final StreamSource source = new StreamSource(response);
                source.setSystemId(identifier);
                return source;
            }
            return parseAndConvertStream(address, response);
        } catch (IOException ioe) {
            final String exceptionMessage =
                MessagesMessages.MEX_0014_RETRIEVAL_FROM_ADDRESS_FAILURE(
                address);
            logger.log(Level.SEVERE, exceptionMessage, ioe);
            throw new WebServiceException(exceptionMessage, ioe);
        }
    
public java.util.ListgetWSDLs()

        return wsdls;
    
private voidhandleLocation(com.sun.xml.ws.mex.client.schema.MetadataSection section)

        final String location = section.getLocation();
        final String dialect = section.getDialect();
        final String identifier = section.getIdentifier();
        if (dialect.equals(WSDL_DIALECT)) {
            wsdls.add(getSourceFromLocation(location, identifier));
        } else if (dialect.equals(SCHEMA_DIALECT)) {
            schemas.add(getSourceFromLocation(location, identifier));
        } else {
            logger.warning(
                MessagesMessages.MEX_0002_UNKNOWN_DIALECT_WITH_ID(
                dialect, identifier));
        }
    
private voidhandleReference(com.sun.xml.ws.mex.client.schema.MetadataSection section)

        final MetadataReference ref = section.getMetadataReference();
        populateLists(new MetadataClient().retrieveMetadata(ref));
    
private voidhandleXml(com.sun.xml.ws.mex.client.schema.MetadataSection section)

        final String dialect = section.getDialect();
        final String identifier = section.getIdentifier();
        if (dialect.equals(WSDL_DIALECT)) {
            wsdls.add(createSource(section, identifier));
        } else if (dialect.equals(SCHEMA_DIALECT)) {
            schemas.add(createSource(section, identifier));
        } else {
            logger.warning(
                MessagesMessages.MEX_0002_UNKNOWN_DIALECT_WITH_ID(
                dialect, identifier));
        }
    
private javax.xml.transform.SourceparseAndConvertStream(java.lang.String address, java.io.InputStream stream)

        
        try {
            final Transformer xFormer =
                TransformerFactory.newInstance().newTransformer();
            Source source = new StreamSource(stream);
            final DOMResult result = new DOMResult();
            xFormer.transform(source, result);
            final Node wsdlDoc = result.getNode();
            source = new DOMSource(wsdlDoc);
            source.setSystemId(getNamespaceFromNode(wsdlDoc.getFirstChild()));
            return source;
        } catch (TransformerException te) {
            final String exceptionMessage =
                MessagesMessages.MEX_0004_TRANSFORMING_FAILURE(address);
            logger.log(Level.SEVERE, exceptionMessage, te);
            throw new WebServiceException(exceptionMessage, te);
        }
    
private voidpatchImports()

        for (Node importNode : importsToPatch) {
            final NamedNodeMap atts = importNode.getAttributes();
            final String targetNamespace =
                atts.getNamedItem(NAMESPACE).getNodeValue();
            final String sysId = nsToSysIdMap.get(targetNamespace);
            if (sysId == null) {
                logger.warning(
                    MessagesMessages.MEX_0005_WSDL_NOT_FOUND_WITH_NAMESPACE(
                    targetNamespace));
                continue;
            }
            final Attr locationAtt =
                importNode.getOwnerDocument().createAttribute(LOCATION);
            locationAtt.setValue(sysId);
            atts.setNamedItem(locationAtt);
        }
    
private voidpopulateLists(com.sun.xml.ws.mex.client.schema.Metadata mData)

        for (MetadataSection section : mData.getMetadataSection()) {
            if (section.getMetadataReference() != null) {
                handleReference(section);
            } else if (section.getLocation() != null) {
                handleLocation(section);
            } else {
                handleXml(section);
            }
        }