summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/tags/java-M1-20060522/java/sca/core/src/main/java/org/apache/tuscany/core/loader/assembly/InterfaceWSDLLoader.java
blob: 9617cf805f1465036e5f3737c9d2dceca2c863f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
 *
 * Copyright 2006 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.core.loader.assembly;

import java.io.IOException;

import org.apache.tuscany.common.resource.ResourceLoader;
import org.apache.tuscany.core.config.ConfigurationLoadException;
import org.apache.tuscany.core.config.MissingInterfaceException;
import org.apache.tuscany.core.loader.WSDLDefinitionRegistry;
import org.apache.tuscany.core.loader.StAXUtil;
import org.apache.tuscany.core.loader.LoaderContext;
import org.apache.tuscany.core.system.annotation.Autowire;
import org.apache.tuscany.model.assembly.Scope;
import org.apache.tuscany.model.types.wsdl.WSDLServiceContract;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.wsdl.PortType;
import javax.wsdl.WSDLException;

/**
 * @version $Rev$ $Date$
 */
@org.osoa.sca.annotations.Scope("MODULE")
public class InterfaceWSDLLoader extends AbstractLoader {
    private static final String WSDLI = "http://www.w3.org/2006/01/wsdl-instance";
    private static final String WSDLI_LOCATION = "wsdlLocation";

    private WSDLDefinitionRegistry wsdlRegistry;

    @Autowire
    public void setWsdlRegistry(WSDLDefinitionRegistry wsdlRegistry) {
        this.wsdlRegistry = wsdlRegistry;
    }

    public QName getXMLType() {
        return AssemblyConstants.INTERFACE_WSDL;
    }

    public WSDLServiceContract load(XMLStreamReader reader, LoaderContext loaderContext) throws XMLStreamException, ConfigurationLoadException {
        assert AssemblyConstants.INTERFACE_WSDL.equals(reader.getName());
        WSDLServiceContract serviceContract = factory.createWSDLServiceContract();
        serviceContract.setScope(Scope.INSTANCE);
        
        ResourceLoader resourceLoader = loaderContext.getResourceLoader();

        String location = reader.getAttributeValue(WSDLI, WSDLI_LOCATION);
        if (location != null) {
            try {
                wsdlRegistry.loadDefinition(location, resourceLoader);
            } catch (IOException e) {
                throw new MissingInterfaceException(e);
            } catch (WSDLException e) {
                throw new MissingInterfaceException(e);
            }
        }

        String portTypeURI = reader.getAttributeValue(null, "interface");
        if (portTypeURI != null) {
            serviceContract.setPortType(getPortType(portTypeURI, resourceLoader));
        }

        portTypeURI = reader.getAttributeValue(null, "callbackInterface");
        if (portTypeURI != null) {
            serviceContract.setCallbackPortType(getPortType(portTypeURI, resourceLoader));
        }
        StAXUtil.skipToEndElement(reader);
        return serviceContract;
    }

    protected PortType getPortType(String uri, ResourceLoader resourceLoader) throws MissingInterfaceException {
        
        // We currently support two syntaxes for specifying a WSDL portType:
        // namespace#portTypeName, this is what we supported in the initial contribution, we will
        // deprecate this after M1
        // namespace#wsdl.interface(portTypeName), this is the WSDL 2.0 syntax
        
        int index = uri.indexOf('#');
        String namespace = uri.substring(0, index);
        String fragment = uri.substring(index + 1);
        String localName;
        if (fragment.startsWith("wsdl.interface(") && fragment.endsWith(")")) {
            localName = fragment.substring(15, fragment.length()-1);
        } else {
            localName = fragment;
        }
        QName qname = new QName(namespace, localName);
        PortType portType = wsdlRegistry.getPortType(qname, resourceLoader);
        if (portType == null) {
            throw new MissingInterfaceException(uri);
        }
        return portType;
    }
}