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: package com.sun.tools.ws.processor.modeler.wsdl;
037:
038: import com.sun.tools.ws.processor.generator.Names;
039: import static com.sun.tools.ws.processor.modeler.wsdl.WSDLModelerBase.getExtensionOfType;
040: import com.sun.tools.ws.wscompile.ErrorReceiver;
041: import com.sun.tools.ws.wscompile.WsimportOptions;
042: import com.sun.tools.ws.wsdl.document.*;
043: import com.sun.tools.ws.wsdl.document.jaxws.JAXWSBinding;
044: import com.sun.tools.ws.wsdl.document.schema.SchemaKinds;
045: import com.sun.tools.ws.wsdl.document.soap.SOAP12Binding;
046: import com.sun.tools.ws.wsdl.document.soap.SOAPBinding;
047: import org.xml.sax.InputSource;
048:
049: import javax.xml.namespace.QName;
050: import java.io.ByteArrayInputStream;
051: import java.io.StringReader;
052: import java.io.StringWriter;
053: import java.text.MessageFormat;
054: import java.util.*;
055:
056: /**
057: * Builds all possible pseudo schemas for async operation ResponseBean to feed to XJC.
058: *
059: * @author Vivek Pandey
060: */
061: public class PseudoSchemaBuilder {
062:
063: private final StringWriter buf = new StringWriter();
064: private final WSDLDocument wsdlDocument;
065: private WSDLModeler wsdlModeler;
066: private final List<InputSource> schemas = new ArrayList<InputSource>();
067: private final HashMap<QName, Port> bindingNameToPortMap = new HashMap<QName, Port>();
068: private static final String w3ceprSchemaBinding = "<bindings\n"
069: + " xmlns=\"http://java.sun.com/xml/ns/jaxb\"\n"
070: + " xmlns:wsa=\"http://www.w3.org/2005/08/addressing\"\n"
071: + " version=\"2.1\">\n"
072: + " \n"
073: + " <bindings scd=\"x-schema::wsa\" if-exists=\"true\">\n"
074: + " <schemaBindings map=\"false\" />\n"
075: + " <bindings scd=\"wsa:EndpointReference\">\n"
076: + " <class ref=\"javax.xml.ws.wsaddressing.W3CEndpointReference\"/>\n"
077: + " </bindings>\n"
078: + " <bindings scd=\"~wsa:EndpointReferenceType\">\n"
079: + " <class ref=\"javax.xml.ws.wsaddressing.W3CEndpointReference\"/>\n"
080: + " </bindings>\n" + " </bindings>\n" + "</bindings>";
081:
082: private static final String memberSubmissionEPR = "<bindings\n"
083: + " xmlns=\"http://java.sun.com/xml/ns/jaxb\"\n"
084: + " xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\"\n"
085: + " version=\"2.1\">\n"
086: + " \n"
087: + " <bindings scd=\"x-schema::wsa\" if-exists=\"true\">\n"
088: + " <schemaBindings map=\"false\" />\n"
089: + " <bindings scd=\"wsa:EndpointReference\">\n"
090: + " <class ref=\"com.sun.xml.ws.developer.MemberSubmissionEndpointReference\"/>\n"
091: + " </bindings>\n"
092: + " <bindings scd=\"~wsa:EndpointReferenceType\">\n"
093: + " <class ref=\"com.sun.xml.ws.developer.MemberSubmissionEndpointReference\"/>\n"
094: + " </bindings>\n" + " </bindings>\n" + "</bindings>";
095:
096: private final static String sysId = "http://dummy.pseudo-schema#schema";
097:
098: private WsimportOptions options;
099:
100: public static List<InputSource> build(WSDLModeler wsdlModeler,
101: WsimportOptions options, ErrorReceiver errReceiver) {
102: PseudoSchemaBuilder b = new PseudoSchemaBuilder(
103: wsdlModeler.document);
104: b.wsdlModeler = wsdlModeler;
105: b.options = options;
106: b.build();
107: int i;
108: for (i = 0; i < b.schemas.size(); i++) {
109: InputSource is = b.schemas.get(i);
110: is.setSystemId(sysId + (i + 1));
111: }
112: //add w3c EPR binding
113: if (!(options.noAddressingBbinding && options.isExtensionMode())) {
114: InputSource is = new InputSource(new ByteArrayInputStream(
115: w3ceprSchemaBinding.getBytes()));
116: is.setSystemId(sysId + (++i + 1));
117: b.schemas.add(is);
118: }
119:
120: //TODO: uncomment after JAXB fixes the issue related to passing multiples of such bindings
121: //add member submission EPR binding
122: // InputSource is1 = new InputSource(new ByteArrayInputStream(memberSubmissionEPR.getBytes()));
123: // is1.setSystemId(sysId+(++i + 1));
124: // b.schemas.add(is1);
125:
126: return b.schemas;
127: }
128:
129: private PseudoSchemaBuilder(WSDLDocument _wsdl) {
130: this .wsdlDocument = _wsdl;
131: }
132:
133: private void build() {
134: for (Iterator<Service> itr = wsdlDocument.getDefinitions()
135: .services(); itr.hasNext();)
136: build(itr.next());
137: }
138:
139: private void build(Service service) {
140: for (Iterator<Port> itr = service.ports(); itr.hasNext();)
141: build(itr.next());
142: }
143:
144: private void build(Port port) {
145: if (wsdlModeler.isProvider(port))
146: return;
147: Binding binding = port.resolveBinding(wsdlDocument);
148:
149: SOAPBinding soapBinding = (SOAPBinding) getExtensionOfType(
150: binding, SOAPBinding.class);
151: //lets try and see if its SOAP 1.2. dont worry about extension flag, its
152: // handled much earlier
153: if (soapBinding == null) {
154: soapBinding = (SOAPBinding) getExtensionOfType(binding,
155: SOAP12Binding.class);
156: }
157: if (soapBinding == null)
158: return;
159: PortType portType = binding.resolvePortType(wsdlDocument);
160:
161: QName bindingName = WSDLModelerBase.getQNameOf(binding);
162:
163: //we dont want to process the port bound to the binding processed earlier
164: if (bindingNameToPortMap.containsKey(bindingName))
165: return;
166:
167: bindingNameToPortMap.put(bindingName, port);
168:
169: for (Iterator itr = binding.operations(); itr.hasNext();) {
170: BindingOperation bindingOperation = (BindingOperation) itr
171: .next();
172:
173: // get only the bounded operations
174: Set boundedOps = portType
175: .getOperationsNamed(bindingOperation.getName());
176: if (boundedOps.size() != 1)
177: continue;
178: Operation operation = (Operation) boundedOps.iterator()
179: .next();
180:
181: // No pseudo schema required for doc/lit
182: if (wsdlModeler.isAsync(portType, operation)) {
183: buildAsync(portType, operation, bindingOperation);
184: }
185: }
186: }
187:
188: /**
189: * @param portType
190: * @param operation
191: * @param bindingOperation
192: */
193: private void buildAsync(PortType portType, Operation operation,
194: BindingOperation bindingOperation) {
195: String operationName = getCustomizedOperationName(operation);//operation.getName();
196: if (operationName == null)
197: return;
198: Message outputMessage = null;
199: if (operation.getOutput() != null)
200: outputMessage = operation.getOutput().resolveMessage(
201: wsdlDocument);
202: if (outputMessage != null) {
203: List<MessagePart> allParts = new ArrayList<MessagePart>(
204: outputMessage.getParts());
205: if (allParts.size() > 1)
206: build(getOperationName(operationName), allParts);
207: }
208:
209: }
210:
211: private String getCustomizedOperationName(Operation operation) {
212: JAXWSBinding jaxwsCustomization = (JAXWSBinding) getExtensionOfType(
213: operation, JAXWSBinding.class);
214: String operationName = (jaxwsCustomization != null) ? ((jaxwsCustomization
215: .getMethodName() != null) ? jaxwsCustomization
216: .getMethodName().getName() : null)
217: : null;
218: if (operationName != null) {
219: if (Names.isJavaReservedWord(operationName)) {
220: return null;
221: }
222:
223: return operationName;
224: }
225: return operation.getName();
226: }
227:
228: private void writeImports(QName elementName, List<MessagePart> parts) {
229: Set<String> uris = new HashSet<String>();
230: for (MessagePart p : parts) {
231: String ns = p.getDescriptor().getNamespaceURI();
232: if (!uris.contains(ns)
233: && !ns.equals("http://www.w3.org/2001/XMLSchema")
234: && !ns.equals(elementName.getNamespaceURI())) {
235: print("<xs:import namespace=''{0}''/>", ns);
236: uris.add(ns);
237: }
238: }
239: }
240:
241: boolean asyncRespBeanBinding = false;
242:
243: private void build(QName elementName, List<MessagePart> allParts) {
244:
245: print(
246: "<xs:schema xmlns:xs=''http://www.w3.org/2001/XMLSchema''"
247: + " xmlns:jaxb=''http://java.sun.com/xml/ns/jaxb''"
248: + " xmlns:xjc=''http://java.sun.com/xml/ns/jaxb/xjc''"
249: + " jaxb:extensionBindingPrefixes=''xjc''"
250: + " jaxb:version=''1.0''"
251: + " targetNamespace=''{0}''>",
252: elementName.getNamespaceURI());
253:
254: writeImports(elementName, allParts);
255:
256: if (!asyncRespBeanBinding) {
257: print("<xs:annotation><xs:appinfo>"
258: + " <jaxb:schemaBindings>"
259: + " <jaxb:package name=''{0}'' />"
260: + " </jaxb:schemaBindings>"
261: + "</xs:appinfo></xs:annotation>", wsdlModeler
262: .getJavaPackage());
263: asyncRespBeanBinding = true;
264: }
265:
266: print("<xs:element name=''{0}''>", elementName.getLocalPart());
267: print("<xs:complexType>");
268: print("<xs:sequence>");
269:
270: for (MessagePart p : allParts) {
271: //rpclit wsdl:part must reference schema type not element, also it must exclude headers and mime parts
272: if (p.getDescriptorKind() == SchemaKinds.XSD_ELEMENT) {
273: print(
274: "<xs:element ref=''types:{0}'' xmlns:types=''{1}''/>",
275: p.getDescriptor().getLocalPart(), p
276: .getDescriptor().getNamespaceURI());
277: } else {
278: print(
279: "<xs:element name=''{0}'' type=''{1}'' xmlns=''{2}'' />",
280: p.getName(), p.getDescriptor().getLocalPart(),
281: p.getDescriptor().getNamespaceURI());
282: }
283: }
284:
285: print("</xs:sequence>");
286: print("</xs:complexType>");
287: print("</xs:element>");
288: print("</xs:schema>");
289:
290: // reset the StringWriter, so that next operation element could be written
291: if (buf.toString().length() > 0) {
292: //System.out.println("Response bean Schema for operation========> "+ elementName+"\n\n"+buf);
293: InputSource is = new InputSource(new StringReader(buf
294: .toString()));
295: schemas.add(is);
296: buf.getBuffer().setLength(0);
297: }
298: }
299:
300: private QName getOperationName(String operationName) {
301: if (operationName == null)
302: return null;
303: // String namespaceURI = wsdlDocument.getDefinitions().getTargetNamespaceURI()+"?"+portType.getName()+"?" + operationName;
304: String namespaceURI = "";
305: return new QName(namespaceURI, operationName + "Response");
306: }
307:
308: private void print(String msg) {
309: print(msg, new Object[0]);
310: }
311:
312: private void print(String msg, Object arg1) {
313: print(msg, new Object[] { arg1 });
314: }
315:
316: private void print(String msg, Object arg1, Object arg2) {
317: print(msg, new Object[] { arg1, arg2 });
318: }
319:
320: private void print(String msg, Object arg1, Object arg2, Object arg3) {
321: print(msg, new Object[] { arg1, arg2, arg3 });
322: }
323:
324: private void print(String msg, Object[] args) {
325: buf.write(MessageFormat.format(msg, args));
326: buf.write('\n');
327: }
328:
329: }
|