001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.ws.wsdl.parser;
027:
028: import com.sun.xml.internal.ws.model.ParameterBinding;
029: import com.sun.xml.internal.ws.model.Mode;
030:
031: import javax.xml.namespace.QName;
032: import javax.xml.ws.soap.SOAPBinding;
033:
034: import java.util.ArrayList;
035: import java.util.Collection;
036: import java.util.HashMap;
037: import java.util.Iterator;
038: import java.util.LinkedHashMap;
039: import java.util.List;
040: import java.util.Map;
041:
042: public class WSDLDocument {
043: protected Map<QName, Message> messages;
044: protected Map<QName, PortType> portTypes;
045: protected Map<QName, Binding> bindings;
046: protected Map<QName, Service> services;
047:
048: public WSDLDocument() {
049: messages = new HashMap<QName, Message>();
050: portTypes = new HashMap<QName, PortType>();
051: bindings = new HashMap<QName, Binding>();
052: services = new LinkedHashMap<QName, Service>();
053: }
054:
055: public void addMessage(Message msg) {
056: messages.put(msg.getName(), msg);
057: }
058:
059: public Message getMessage(QName name) {
060: return messages.get(name);
061: }
062:
063: public void addPortType(PortType pt) {
064: portTypes.put(pt.getName(), pt);
065: }
066:
067: public PortType getPortType(QName name) {
068: return portTypes.get(name);
069: }
070:
071: public void addBinding(Binding binding) {
072: bindings.put(binding.getName(), binding);
073: }
074:
075: public Binding getBinding(QName name) {
076: return bindings.get(name);
077: }
078:
079: public void addService(Service svc) {
080: services.put(svc.getName(), svc);
081: }
082:
083: public Service getService(QName name) {
084: return services.get(name);
085: }
086:
087: public Map<QName, Service> getServices() {
088: return services;
089: }
090:
091: /**
092: * Returns the first service QName from insertion order
093: * @return
094: */
095: public QName getFirstServiceName() {
096: if (services.isEmpty())
097: return null;
098: return services.values().iterator().next().getName();
099: }
100:
101: /**
102: * Returns first port QName from first service as per the insertion order
103: * @return
104: */
105: public QName getFirstPortName() {
106: if (services.isEmpty())
107: return null;
108: Service service = services.values().iterator().next();
109: Iterator<QName> iter = service.keySet().iterator();
110: QName port = (iter.hasNext()) ? iter.next() : null;
111: return port;
112: }
113:
114: private Port getFirstPort() {
115: if (services.isEmpty())
116: return null;
117: Service service = services.values().iterator().next();
118: Collection<Port> coll = service.values();
119: Port port = (coll != null) ? ((coll.iterator().hasNext()) ? coll
120: .iterator().next()
121: : null)
122: : null;
123: return port;
124: }
125:
126: /**
127: * Returns biningId of the first port
128: * @return
129: */
130: public String getBindingId() {
131: Port port = getFirstPort();
132: if (port == null)
133: return null;
134: Binding binding = bindings.get(port.getBindingName());
135: if (binding == null)
136: return null;
137: return binding.getBindingId();
138: }
139:
140: /**
141: * Gives the binding Id of the given service and port
142: * @param service
143: * @param port
144: * @return
145: */
146: public String getBindingId(QName service, QName port) {
147: Service s = services.get(service);
148: if (s != null) {
149: Port p = s.get(port);
150: if (p != null) {
151: Binding b = bindings.get(p.getBindingName());
152: if (b != null)
153: return b.getBindingId();
154: }
155:
156: }
157: return null;
158: }
159:
160: /**
161: *
162: * @param serviceName non-null service QName
163: * @param portName non-null port QName
164: * @return
165: * BindingOperation on success otherwise null. throws NPE if any of the parameters null
166: */
167: public Binding getBinding(QName serviceName, QName portName) {
168: Service service = services.get(serviceName);
169: if (service != null) {
170: Port port = service.get(portName);
171: if (port != null) {
172: QName bindingName = port.getBindingName();
173: return bindings.get(bindingName);
174: }
175: }
176: return null;
177: }
178:
179: /**
180: * Returns the bindings for the given bindingId
181: * @param service non-null service
182: * @param bindingId non-null binding id
183: * @return
184: */
185: public List<Binding> getBindings(Service service, String bindingId) {
186: List<Binding> bs = new ArrayList<Binding>();
187: Collection<Port> ports = service.values();
188: if (ports.isEmpty())
189: return bs;
190: for (Port port : ports) {
191: Binding b = bindings.get(port.getName());
192: if (b == null)
193: return bs;
194: if (b.equals(bindingId))
195: bs.add(b);
196: }
197: return bs;
198: }
199:
200: public QName getPortName(QName serviceName, QName portType) {
201: Service service = services.get(serviceName);
202: for (Port port : service.values()) {
203: QName bindingName = port.getBindingName();
204: assert (bindingName != null);
205: Binding binding = bindings.get(bindingName);
206: QName ptName = binding.getPortTypeName();
207: assert (ptName != null);
208: if (ptName.equals(portType))
209: return port.getName();
210: }
211: return null;
212: }
213:
214: public void finalizeBinding(Binding binding) {
215: assert (binding != null);
216: QName portTypeName = binding.getPortTypeName();
217: if (portTypeName == null)
218: return;
219: PortType pt = portTypes.get(portTypeName);
220: if (pt == null)
221: return;
222: for (String op : binding.keySet()) {
223: PortTypeOperation pto = pt.get(op);
224: if (pto == null)
225: return;
226: QName inMsgName = pto.getInputMessage();
227: if (inMsgName == null)
228: continue;
229: Message inMsg = messages.get(inMsgName);
230: BindingOperation bo = binding.get(op);
231: int bodyindex = 0;
232: if (inMsg != null) {
233: for (String name : inMsg) {
234: ParameterBinding pb = bo.getInputBinding(name);
235: if (pb.isBody()) {
236: bo.addPart(new Part(name, pb, bodyindex++),
237: Mode.IN);
238: }
239: }
240: }
241: bodyindex = 0;
242: QName outMsgName = pto.getOutputMessage();
243: if (outMsgName == null)
244: continue;
245: Message outMsg = messages.get(outMsgName);
246: if (outMsg != null) {
247: for (String name : outMsg) {
248: ParameterBinding pb = bo.getOutputBinding(name);
249: if (pb.isBody()) {
250: bo.addPart(new Part(name, pb, bodyindex++),
251: Mode.OUT);
252: }
253: }
254: }
255: }
256: }
257: }
|