001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.model.wsdl;
038:
039: import com.sun.istack.NotNull;
040: import com.sun.xml.ws.api.model.ParameterBinding;
041: import com.sun.xml.ws.api.model.wsdl.WSDLBoundPortType;
042: import com.sun.xml.ws.api.model.wsdl.WSDLMessage;
043: import com.sun.xml.ws.api.model.wsdl.WSDLModel;
044: import com.sun.xml.ws.api.model.wsdl.WSDLOperation;
045: import com.sun.xml.ws.api.model.wsdl.WSDLPort;
046: import com.sun.xml.ws.api.model.wsdl.WSDLPortType;
047: import com.sun.xml.ws.api.model.wsdl.WSDLService;
048:
049: import javax.jws.WebParam.Mode;
050: import javax.xml.namespace.QName;
051: import java.net.URL;
052: import java.util.Collections;
053: import java.util.HashMap;
054: import java.util.Iterator;
055: import java.util.LinkedHashMap;
056: import java.util.Map;
057:
058: /**
059: * Implementation of {@link WSDLModel}
060: *
061: * @author Vivek Pandey
062: */
063: public final class WSDLModelImpl extends AbstractExtensibleImpl
064: implements WSDLModel {
065: private final Map<QName, WSDLMessageImpl> messages = new HashMap<QName, WSDLMessageImpl>();
066: private final Map<QName, WSDLPortTypeImpl> portTypes = new HashMap<QName, WSDLPortTypeImpl>();
067: private final Map<QName, WSDLBoundPortTypeImpl> bindings = new HashMap<QName, WSDLBoundPortTypeImpl>();
068: private final Map<QName, WSDLServiceImpl> services = new LinkedHashMap<QName, WSDLServiceImpl>();
069:
070: private final Map<QName, WSDLBoundPortType> unmBindings = Collections
071: .<QName, WSDLBoundPortType> unmodifiableMap(bindings);
072:
073: public WSDLModelImpl(@NotNull
074: String systemId) {
075: super (systemId, -1);
076: }
077:
078: /**
079: * To create {@link WSDLModelImpl} from WSDL that doesn't have a system ID.
080: */
081: public WSDLModelImpl() {
082: super (null, -1);
083: }
084:
085: public void addMessage(WSDLMessageImpl msg) {
086: messages.put(msg.getName(), msg);
087: }
088:
089: public WSDLMessageImpl getMessage(QName name) {
090: return messages.get(name);
091: }
092:
093: public void addPortType(WSDLPortTypeImpl pt) {
094: portTypes.put(pt.getName(), pt);
095: }
096:
097: public WSDLPortTypeImpl getPortType(QName name) {
098: return portTypes.get(name);
099: }
100:
101: public void addBinding(WSDLBoundPortTypeImpl boundPortType) {
102: assert !bindings.containsValue(boundPortType);
103: bindings.put(boundPortType.getName(), boundPortType);
104: }
105:
106: public WSDLBoundPortTypeImpl getBinding(QName name) {
107: return bindings.get(name);
108: }
109:
110: public void addService(WSDLServiceImpl svc) {
111: services.put(svc.getName(), svc);
112: }
113:
114: public WSDLServiceImpl getService(QName name) {
115: return services.get(name);
116: }
117:
118: public Map<QName, WSDLMessageImpl> getMessages() {
119: return messages;
120: }
121:
122: public @NotNull
123: Map<QName, WSDLPortTypeImpl> getPortTypes() {
124: return portTypes;
125: }
126:
127: public @NotNull
128: Map<QName, WSDLBoundPortType> getBindings() {
129: return unmBindings;
130: }
131:
132: public @NotNull
133: Map<QName, WSDLServiceImpl> getServices() {
134: return services;
135: }
136:
137: /**
138: * Returns the first service QName from insertion order
139: */
140: public QName getFirstServiceName() {
141: if (services.isEmpty())
142: return null;
143: return services.values().iterator().next().getName();
144: }
145:
146: /**
147: * Returns first port QName from first service as per the insertion order
148: */
149: public QName getFirstPortName() {
150: WSDLPort fp = getFirstPort();
151: if (fp == null)
152: return null;
153: else
154: return fp.getName();
155: }
156:
157: private WSDLPort getFirstPort() {
158: if (services.isEmpty())
159: return null;
160: WSDLService service = services.values().iterator().next();
161: Iterator<? extends WSDLPort> iter = service.getPorts()
162: .iterator();
163: WSDLPort port = iter.hasNext() ? iter.next() : null;
164: return port;
165: }
166:
167: /**
168: * gets the first port in the wsdl which matches the serviceName and portType
169: */
170: public WSDLPortImpl getMatchingPort(QName serviceName,
171: QName portType) {
172: return getService(serviceName).getMatchingPort(portType);
173: }
174:
175: /**
176: *
177: * @param serviceName non-null service QName
178: * @param portName non-null port QName
179: * @return
180: * WSDLBoundOperation on success otherwise null. throws NPE if any of the parameters null
181: */
182: public WSDLBoundPortTypeImpl getBinding(QName serviceName,
183: QName portName) {
184: WSDLServiceImpl service = services.get(serviceName);
185: if (service != null) {
186: WSDLPortImpl port = service.get(portName);
187: if (port != null)
188: return port.getBinding();
189: }
190: return null;
191: }
192:
193: void finalizeRpcLitBinding(WSDLBoundPortTypeImpl boundPortType) {
194: assert (boundPortType != null);
195: QName portTypeName = boundPortType.getPortTypeName();
196: if (portTypeName == null)
197: return;
198: WSDLPortType pt = portTypes.get(portTypeName);
199: if (pt == null)
200: return;
201: for (WSDLBoundOperationImpl bop : boundPortType
202: .getBindingOperations()) {
203: WSDLOperation pto = pt.get(bop.getName().getLocalPart());
204: WSDLMessage inMsgName = pto.getInput().getMessage();
205: if (inMsgName == null)
206: continue;
207: WSDLMessageImpl inMsg = messages.get(inMsgName.getName());
208: int bodyindex = 0;
209: if (inMsg != null) {
210: for (WSDLPartImpl part : inMsg.parts()) {
211: String name = part.getName();
212: ParameterBinding pb = bop.getInputBinding(name);
213: if (pb.isBody()) {
214: part.setIndex(bodyindex++);
215: part.setBinding(pb);
216: bop.addPart(part, Mode.IN);
217: }
218: }
219: }
220: bodyindex = 0;
221: if (pto.isOneWay())
222: continue;
223: WSDLMessage outMsgName = pto.getOutput().getMessage();
224: if (outMsgName == null)
225: continue;
226: WSDLMessageImpl outMsg = messages.get(outMsgName.getName());
227: if (outMsg != null) {
228: for (WSDLPartImpl part : outMsg.parts()) {
229: String name = part.getName();
230: ParameterBinding pb = bop.getOutputBinding(name);
231: if (pb.isBody()) {
232: part.setIndex(bodyindex++);
233: part.setBinding(pb);
234: bop.addPart(part, Mode.OUT);
235: }
236: }
237: }
238: }
239: }
240:
241: /**
242: * Invoked at the end of the model construction to fix up references, etc.
243: */
244: public void freeze() {
245: for (WSDLServiceImpl service : services.values()) {
246: service.freeze(this );
247: }
248: for (WSDLBoundPortTypeImpl bp : bindings.values()) {
249: bp.freeze();
250: }
251: }
252: }
|