1/*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.server;
27
28import com.sun.istack.internal.NotNull;
29import com.sun.xml.internal.ws.api.server.SDDocument;
30import com.sun.xml.internal.ws.api.server.SDDocumentFilter;
31import com.sun.xml.internal.ws.api.server.ServiceDefinition;
32import com.sun.xml.internal.ws.wsdl.SDDocumentResolver;
33
34import java.util.ArrayList;
35import java.util.Collection;
36import java.util.HashMap;
37import java.util.Iterator;
38import java.util.List;
39import java.util.Map;
40
41/**
42 * {@link ServiceDefinition} implementation.
43 *
44 * <p>
45 * You construct a {@link ServiceDefinitionImpl} by first constructing
46 * a list of {@link SDDocumentImpl}s.
47 *
48 * @author Kohsuke Kawaguchi
49 */
50public final class ServiceDefinitionImpl implements ServiceDefinition, SDDocumentResolver {
51    private final Collection<SDDocumentImpl> docs;
52
53    private final Map<String,SDDocumentImpl> bySystemId;
54    private final @NotNull SDDocumentImpl primaryWsdl;
55
56    /**
57     * Set when {@link WSEndpointImpl} is created.
58     */
59    /*package*/ WSEndpointImpl<?> owner;
60
61    /*package*/ final List<SDDocumentFilter> filters = new ArrayList<SDDocumentFilter>();
62
63    /**
64     * @param docs
65     *      List of {@link SDDocumentImpl}s to form the description.
66     *      There must be at least one entry.
67     *      The first document is considered {@link #getPrimary() primary}.
68     */
69    public ServiceDefinitionImpl(Collection<SDDocumentImpl> docs, @NotNull SDDocumentImpl primaryWsdl) {
70        assert docs.contains(primaryWsdl);
71        this.docs = docs;
72        this.primaryWsdl = primaryWsdl;
73        this.bySystemId = new HashMap<String, SDDocumentImpl>();
74    }
75
76    private boolean isInitialized = false;
77
78    private synchronized void init() {
79        if (isInitialized)
80            return;
81        isInitialized = true;
82
83        for (SDDocumentImpl doc : docs) {
84            bySystemId.put(doc.getURL().toExternalForm(),doc);
85            doc.setFilters(filters);
86            doc.setResolver(this);
87        }
88    }
89
90    /**
91     * The owner is set when {@link WSEndpointImpl} is created.
92     */
93    /*package*/ void setOwner(WSEndpointImpl<?> owner) {
94        assert owner!=null && this.owner==null;
95        this.owner = owner;
96    }
97
98    public @NotNull SDDocument getPrimary() {
99        return primaryWsdl;
100    }
101
102    public void addFilter(SDDocumentFilter filter) {
103        filters.add(filter);
104    }
105
106    public Iterator<SDDocument> iterator() {
107        init();
108        return (Iterator)docs.iterator();
109    }
110
111    /**
112     * Gets the {@link SDDocumentImpl} whose {@link SDDocumentImpl#getURL()}
113     * returns the specified value.
114     *
115     * @return
116     *      null if none is found.
117     */
118    public SDDocument resolve(String systemId) {
119        init();
120        return bySystemId.get(systemId);
121    }
122}
123