summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060522/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/WSDLDefinitionRegistryImpl.java
blob: 1063dec3fc6acb343187a5de1f8fd6a71d4069f4 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
 *
 * 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.impl;

import java.io.IOException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import javax.wsdl.Definition;
import javax.wsdl.PortType;
import javax.wsdl.Service;
import javax.wsdl.WSDLException;
import javax.wsdl.extensions.ExtensionRegistry;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.xml.namespace.QName;

import org.apache.tuscany.common.resource.ResourceLoader;
import org.apache.tuscany.core.loader.WSDLDefinitionRegistry;
import org.osoa.sca.annotations.Scope;

/**
 * @version $Rev$ $Date$
 */
@org.osoa.sca.annotations.Service(interfaces = {WSDLDefinitionRegistry.class})
@Scope("MODULE")
public class WSDLDefinitionRegistryImpl implements WSDLDefinitionRegistry {
    private final WSDLFactory wsdlFactory;
    private final ExtensionRegistry registry;

    private final Map<ResourceLoader,Map<URL, Definition>> definitionsByLocationByLoader = new WeakHashMap<ResourceLoader,Map<URL, Definition>>();
    private final Map<ResourceLoader,Map<String, List<Definition>>> definitionsByNamespaceByLoader = new WeakHashMap<ResourceLoader,Map<String, List<Definition>>>();

    private Monitor monitor;

    public WSDLDefinitionRegistryImpl() throws WSDLException {
        wsdlFactory = WSDLFactory.newInstance();
        registry = wsdlFactory.newPopulatedExtensionRegistry();
    }

    @org.apache.tuscany.core.system.annotation.Monitor
    public void setMonitor(Monitor monitor) {
        this.monitor = monitor;
    }

    public ExtensionRegistry getExtensionRegistry() {
        return registry;
    }

    public Definition loadDefinition(String wsdlLocation, ResourceLoader resourceLoader) throws IOException, WSDLException {
        int index = wsdlLocation.indexOf(' ');
        if (index == -1) {
            throw new WSDLException(WSDLException.CONFIGURATION_ERROR, "Invalid wsdlLocation: " + wsdlLocation);
        }
        String namespace = wsdlLocation.substring(0, index).trim();
        URL url;
        URI uri;
        try {
            uri = new URI(wsdlLocation.substring(index + 1).trim());
        } catch (URISyntaxException e) {
            throw new WSDLException(WSDLException.CONFIGURATION_ERROR, "Invalid wsdlLocation: " + wsdlLocation);
        }
        if (uri.isAbsolute()) {
            url = uri.toURL();
        } else {
            url = resourceLoader.getResource(uri.toString());
            if (url == null) {
                throw new WSDLException(WSDLException.CONFIGURATION_ERROR, "Resource not found: " + uri);
            }
        }
        return loadDefinition(namespace, url, resourceLoader);
    }

    public Definition loadDefinition(String namespace, URL location, ResourceLoader resourceLoader) throws IOException, WSDLException {
        Map<URL, Definition> definitionsByLocation = getDefinitionsByLocation(resourceLoader);
        Map<String, List<Definition>> definitionsByNamespace = getDefinitionsByNamespace(resourceLoader);
        
        Definition definition = definitionsByLocation.get(location);
        if (definition != null) {
            // return cached copy
            return definition;
        }

        monitor.readingWSDL(namespace, location);
        WSDLReader reader = wsdlFactory.newWSDLReader();
        reader.setFeature("javax.wsdl.verbose", false);
        reader.setExtensionRegistry(registry);

        definition = reader.readWSDL(location.toString());
        String definitionNamespace = definition.getTargetNamespace();
        if (namespace != null && !namespace.equals(definitionNamespace)) {
            throw new WSDLException(WSDLException.CONFIGURATION_ERROR, namespace + " != " + definition.getTargetNamespace());
        }

        monitor.cachingDefinition(definitionNamespace, location);
        definitionsByLocation.put(location, definition);
        List<Definition> definitions = definitionsByNamespace.get(definitionNamespace);
        if (definitions == null) {
            definitions = new ArrayList<Definition>();
            definitionsByNamespace.put(definitionNamespace, definitions);
        }
        definitions.add(definition);

        return definition;
    }

    public List<Definition> getDefinitionsForNamespace(String namespace, ResourceLoader resourceLoader) {
        Map<String, List<Definition>> definitionsByNamespace = getDefinitionsByNamespace(resourceLoader);
        return definitionsByNamespace.get(namespace);
    }

    public PortType getPortType(QName name, ResourceLoader resourceLoader) {
        Map<String, List<Definition>> definitionsByNamespace = getDefinitionsByNamespace(resourceLoader);
        String namespace = name.getNamespaceURI();
        List<Definition> definitions = definitionsByNamespace.get(namespace);
        if (definitions == null) {
            return null;
        }
        for (Definition definition : definitions) {
            PortType portType = definition.getPortType(name);
            if (portType != null) {
                return portType;
            }
        }
        return null;
    }

    public Service getService(QName name, ResourceLoader resourceLoader) {
        Map<String, List<Definition>> definitionsByNamespace = getDefinitionsByNamespace(resourceLoader);
        String namespace = name.getNamespaceURI();
        List<Definition> definitions = definitionsByNamespace.get(namespace);
        if (definitions == null) {
            return null;
        }
        for (Definition definition : definitions) {
            Service service = definition.getService(name);
            if (service != null) {
                return service;
            }
        }
        return null;
    }
    
    private Map<String, List<Definition>> getDefinitionsByNamespace(ResourceLoader resourceLoader) {
        Map<String, List<Definition>> map = definitionsByNamespaceByLoader.get(resourceLoader);
        if (map == null) {
            map = new HashMap<String, List<Definition>>();
            definitionsByNamespaceByLoader.put(resourceLoader, map);
        }
        return map;
    }

    private Map<URL, Definition> getDefinitionsByLocation(ResourceLoader resourceLoader) {
        Map<URL, Definition> map = definitionsByLocationByLoader.get(resourceLoader);
        if (map == null) {
            map = new HashMap<URL, Definition>();
            definitionsByLocationByLoader.put(resourceLoader, map);
        }
        return map;
    }
    
    public static interface Monitor {
        /**
         * Monitor event emitted immediately before an attempt is made to
         * read WSDL for the supplied namespace from the supplied location.
         *
         * @param namespace the target namespace expected in the WSDL; may be null
         * @param location the location where we will attempt to read the WSDL definition from
         */
        void readingWSDL(String namespace, URL location);

        /**
         * Monitor event emitted immediately before registering a WSDL definition
         * in the cache.
         *
         * @param namespace the target namespace for the WSDL
         * @param location the location where the WSDL definition was read from
         */
        void cachingDefinition(String namespace, URL location);
    }

}