001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.jbi.framework.support;
018    
019    import javax.wsdl.Definition;
020    import javax.wsdl.Port;
021    import javax.wsdl.PortType;
022    import javax.wsdl.Service;
023    import javax.wsdl.factory.WSDLFactory;
024    import javax.wsdl.xml.WSDLReader;
025    import javax.xml.namespace.QName;
026    
027    import org.w3c.dom.Document;
028    
029    import com.ibm.wsdl.Constants;
030    
031    import org.apache.commons.logging.Log;
032    import org.apache.commons.logging.LogFactory;
033    import org.apache.servicemix.jbi.framework.Registry;
034    import org.apache.servicemix.jbi.servicedesc.InternalEndpoint;
035    
036    /**
037     * Retrieve interface implemented by the given endpoint using the WSDL 1 description.
038     * 
039     * @author gnodet
040     */
041    public class WSDL1Processor implements EndpointProcessor {
042    
043        public static final String WSDL1_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
044        
045        private static final Log LOG = LogFactory.getLog(WSDL1Processor.class);
046        
047        private Registry registry;
048        
049        public void init(Registry reg) {
050            this.registry = reg;
051        }
052    
053        /**
054         * Retrieve interface implemented by the given endpoint using the WSDL 1 description.
055         * 
056         * @param serviceEndpoint the endpoint being checked
057         */
058        public void process(InternalEndpoint serviceEndpoint) {
059            try {
060                Document document = registry.getEndpointDescriptor(serviceEndpoint);
061                if (document == null || document.getDocumentElement() == null) {
062                    if (LOG.isDebugEnabled()) {
063                        LOG.debug("Endpoint " + serviceEndpoint + " has no service description");
064                    }
065                    return;
066                }
067                if (!WSDL1_NAMESPACE.equals(document.getDocumentElement().getNamespaceURI())) {
068                    if (LOG.isDebugEnabled()) {
069                        LOG.debug("Endpoint " + serviceEndpoint + " has a non WSDL1 service description");
070                    }
071                    return;
072                }
073                WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
074                reader.setFeature(Constants.FEATURE_VERBOSE, false);
075                Definition definition = reader.readWSDL(null, document);
076                // Check if the wsdl is only a port type
077                // In these cases, only the port type is used, as the service name and endpoint name
078                // are provided on the jbi endpoint
079                if (definition.getPortTypes().keySet().size() == 1
080                        && definition.getServices().keySet().size() == 0) {
081                    PortType portType = (PortType) definition.getPortTypes().values().iterator().next();
082                    QName interfaceName = portType.getQName();
083                    if (LOG.isDebugEnabled()) {
084                        LOG.debug("Endpoint " + serviceEndpoint + " implements interface " + interfaceName);
085                    }
086                    serviceEndpoint.addInterface(interfaceName);
087                } else {
088                    Service service = definition.getService(serviceEndpoint.getServiceName());
089                    if (service == null) {
090                        LOG.info("Endpoint " + serviceEndpoint + " has a service description, but no matching service found in "
091                                        + definition.getServices().keySet());
092                        return;
093                    }
094                    Port port = service.getPort(serviceEndpoint.getEndpointName());
095                    if (port == null) {
096                        LOG.info("Endpoint " + serviceEndpoint + " has a service description, but no matching endpoint found in "
097                                        + service.getPorts().keySet());
098                        return;
099                    }
100                    if (port.getBinding() == null) {
101                        LOG.info("Endpoint " + serviceEndpoint + " has a service description, but no binding found");
102                        return;
103                    }
104                    if (port.getBinding().getPortType() == null) {
105                        LOG.info("Endpoint " + serviceEndpoint + " has a service description, but no port type found");
106                        return;
107                    }
108                    QName interfaceName = port.getBinding().getPortType().getQName();
109                    if (LOG.isDebugEnabled()) {
110                        LOG.debug("Endpoint " + serviceEndpoint + " implements interface " + interfaceName);
111                    }
112                    serviceEndpoint.addInterface(interfaceName);
113                }
114            } catch (Exception e) {
115                LOG.warn("Error retrieving interfaces from service description: " + e.getMessage());
116                if (LOG.isDebugEnabled()) {
117                    LOG.debug("Error retrieving interfaces from service description", e);
118                }
119            }
120        }
121    
122    }