1/*
2 * Copyright (c) 1997, 2013, 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.model.wsdl;
27
28import com.sun.istack.internal.NotNull;
29import com.sun.istack.internal.Nullable;
30import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
31import com.sun.xml.internal.ws.api.model.wsdl.WSDLService;
32import com.sun.xml.internal.ws.api.model.wsdl.editable.EditableWSDLModel;
33import com.sun.xml.internal.ws.api.model.wsdl.editable.EditableWSDLPort;
34import com.sun.xml.internal.ws.api.model.wsdl.editable.EditableWSDLService;
35
36import javax.xml.namespace.QName;
37import javax.xml.stream.XMLStreamReader;
38
39import java.util.LinkedHashMap;
40import java.util.Map;
41
42/**
43 * Implementation of {@link WSDLService}
44 *
45 * @author Vivek Pandey
46 */
47public final class WSDLServiceImpl extends AbstractExtensibleImpl implements EditableWSDLService {
48    private final QName name;
49    private final Map<QName, EditableWSDLPort> ports;
50    private final EditableWSDLModel parent;
51
52    public WSDLServiceImpl(XMLStreamReader xsr, EditableWSDLModel parent, QName name) {
53        super(xsr);
54        this.parent = parent;
55        this.name = name;
56        ports = new LinkedHashMap<QName, EditableWSDLPort>();
57    }
58
59    public @NotNull
60    EditableWSDLModel getParent() {
61        return parent;
62    }
63
64    public QName getName() {
65        return name;
66    }
67
68    public EditableWSDLPort get(QName portName) {
69        return ports.get(portName);
70    }
71
72    public EditableWSDLPort getFirstPort() {
73        if(ports.isEmpty())
74            return null;
75        else
76            return ports.values().iterator().next();
77    }
78
79    public Iterable<EditableWSDLPort> getPorts(){
80        return ports.values();
81    }
82
83    /**
84    * gets the first port in this service which matches the portType
85    */
86    public @Nullable
87    EditableWSDLPort getMatchingPort(QName portTypeName){
88        for(EditableWSDLPort port : getPorts()){
89            QName ptName = port.getBinding().getPortTypeName();
90            assert (ptName != null);
91            if(ptName.equals(portTypeName))
92                return port;
93        }
94        return null;
95    }
96
97    /**
98     * Populates the Map that holds port name as key and {@link WSDLPort} as the value.
99     *
100     * @param portName Must be non-null
101     * @param port     Must be non-null
102     * @throws NullPointerException if either opName or ptOp is null
103     */
104    public void put(QName portName, EditableWSDLPort port) {
105        if (portName == null || port == null)
106            throw new NullPointerException();
107        ports.put(portName, port);
108    }
109
110    public void freeze(EditableWSDLModel root) {
111        for (EditableWSDLPort port : ports.values()) {
112            port.freeze(root);
113        }
114    }
115}
116