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: package com.sun.xml.internal.ws.wsdl;
026:
027: import com.sun.xml.internal.ws.wsdl.parser.*;
028: import org.xml.sax.EntityResolver;
029: import org.xml.sax.SAXException;
030:
031: import javax.xml.namespace.QName;
032: import javax.xml.stream.XMLStreamException;
033: import javax.xml.ws.WebServiceException;
034: import javax.xml.ws.soap.SOAPBinding;
035:
036: import java.io.IOException;
037: import java.net.URL;
038: import java.util.Iterator;
039: import java.util.Map;
040: import java.util.Set;
041:
042: /**
043: * $author: JAXWS Development Team
044: */
045: public class WSDLContext {
046: private final URL orgWsdlLocation;
047: private String targetNamespace;
048: private final WSDLDocument wsdlDoc;
049:
050: /**
051: * Creates a {@link WSDLContext} by parsing the given wsdl file.
052: */
053: public WSDLContext(URL wsdlDocumentLocation,
054: EntityResolver entityResolver) throws WebServiceException {
055: //must get binding information
056: assert entityResolver != null;
057:
058: if (wsdlDocumentLocation == null)
059: throw new WebServiceException(
060: "No WSDL location Information present, error");
061:
062: orgWsdlLocation = wsdlDocumentLocation;
063: try {
064: wsdlDoc = RuntimeWSDLParser.parse(wsdlDocumentLocation,
065: entityResolver);
066: } catch (IOException e) {
067: throw new WebServiceException(e);
068: } catch (XMLStreamException e) {
069: throw new WebServiceException(e);
070: } catch (SAXException e) {
071: throw new WebServiceException(e);
072: }
073: }
074:
075: public URL getWsdlLocation() {
076: return orgWsdlLocation;
077: }
078:
079: public String getOrigURLPath() {
080: return orgWsdlLocation.getPath();
081: }
082:
083: public QName getServiceQName() {
084: return wsdlDoc.getFirstServiceName();
085: }
086:
087: public boolean contains(QName serviceName) {
088: return (wsdlDoc.getServices().containsKey(serviceName));
089: }
090:
091: //just get the first one for now
092: public String getEndpoint(QName serviceName) {
093: if (serviceName == null)
094: throw new WebServiceException(
095: "Service unknown, can not identify ports for an unknown Service.");
096: Service service = wsdlDoc.getService(serviceName);
097: String endpoint = null;
098: if (service != null) {
099: Iterator<Map.Entry<QName, Port>> iter = service.entrySet()
100: .iterator();
101: if (iter.hasNext()) {
102: Port port = iter.next().getValue();
103: endpoint = port.getAddress();
104: }
105: }
106: if (endpoint == null)
107: throw new WebServiceException(
108: "Endpoint not found. Check WSDL file to verify endpoint was provided.");
109: return endpoint;
110: }
111:
112: //just get the first one for now
113: public QName getPortName() {
114: return wsdlDoc.getFirstPortName();
115: }
116:
117: public String getBindingID(QName serviceName, QName portName) {
118: return getWsdlDocument().getBindingId(serviceName, portName);
119: }
120:
121: public String getTargetNamespace() {
122: return targetNamespace;
123: }
124:
125: public void setTargetNamespace(String tns) {
126: targetNamespace = tns;
127: }
128:
129: public Set<QName> getPortsAsSet(QName serviceName) {
130: Service service = wsdlDoc.getService(serviceName);
131: if (service != null) {
132: return service.keySet();
133: }
134: return null;
135: }
136:
137: public boolean contains(QName serviceName, QName portName) {
138: Service service = wsdlDoc.getService(serviceName);
139: if (service != null) {
140:
141: Iterator<Map.Entry<QName, Port>> iter = service.entrySet()
142: .iterator();
143: while (iter.hasNext()) {
144: Port port = iter.next().getValue();
145: if (port.getName().equals(portName))
146: return true;
147: }
148: }
149: return false;
150: }
151:
152: public QName getFirstServiceName() {
153: return wsdlDoc.getFirstServiceName();
154: }
155:
156: public Set<QName> getAllServiceNames() {
157: return wsdlDoc.getServices().keySet();
158: }
159:
160: public WSDLDocument getWsdlDocument() {
161: return wsdlDoc;
162: }
163:
164: public Binding getWsdlBinding(QName service, QName port) {
165: if (wsdlDoc == null)
166: return null;
167: return wsdlDoc.getBinding(service, port);
168: }
169:
170: public String getEndpoint(QName serviceName, QName portQName) {
171: Service service = wsdlDoc.getService(serviceName);
172: if (service != null) {
173: Port p = service.get(portQName);
174: if (p != null)
175: return p.getAddress();
176: else
177: throw new WebServiceException(
178: "No ports found for service " + serviceName);
179: } else {
180: throw new WebServiceException(
181: "Service unknown, can not identify ports for an unknown Service.");
182: }
183: }
184:
185: }
|