src/share/jaxws_classes/com/sun/tools/internal/ws/processor/modeler/wsdl/PseudoSchemaBuilder.java

Mon, 31 Mar 2014 15:59:00 +0200

author
mkos
date
Mon, 31 Mar 2014 15:59:00 +0200
changeset 532
460225fb335a
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8038307: JAX-WS conformance tests fail when running JCK-devtools-8 suite against RI in EBCDIC emulation mode
Reviewed-by: chegar

ohair@286 1 /*
mkos@532 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.tools.internal.ws.processor.modeler.wsdl;
ohair@286 27
ohair@286 28 import com.sun.tools.internal.ws.processor.generator.Names;
ohair@286 29 import static com.sun.tools.internal.ws.processor.modeler.wsdl.WSDLModelerBase.getExtensionOfType;
ohair@286 30 import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
ohair@286 31 import com.sun.tools.internal.ws.wscompile.WsimportOptions;
ohair@286 32 import com.sun.tools.internal.ws.wscompile.Options;
ohair@286 33 import com.sun.tools.internal.ws.wsdl.document.*;
ohair@286 34 import com.sun.tools.internal.ws.wsdl.document.jaxws.JAXWSBinding;
ohair@286 35 import com.sun.tools.internal.ws.wsdl.document.schema.SchemaKinds;
ohair@286 36 import com.sun.tools.internal.ws.wsdl.document.soap.SOAP12Binding;
ohair@286 37 import com.sun.tools.internal.ws.wsdl.document.soap.SOAPBinding;
ohair@286 38 import org.xml.sax.InputSource;
ohair@286 39
ohair@286 40 import javax.xml.namespace.QName;
ohair@286 41 import java.io.ByteArrayInputStream;
ohair@286 42 import java.io.StringReader;
ohair@286 43 import java.io.StringWriter;
mkos@532 44 import java.nio.charset.StandardCharsets;
ohair@286 45 import java.text.MessageFormat;
ohair@286 46 import java.util.*;
ohair@286 47
ohair@286 48
ohair@286 49 /**
ohair@286 50 * Builds all possible pseudo schemas for async operation ResponseBean to feed to XJC.
ohair@286 51 *
ohair@286 52 * @author Vivek Pandey
ohair@286 53 */
ohair@286 54 public class PseudoSchemaBuilder {
ohair@286 55
ohair@286 56 private final StringWriter buf = new StringWriter();
ohair@286 57 private final WSDLDocument wsdlDocument;
ohair@286 58 private WSDLModeler wsdlModeler;
ohair@286 59 private final List<InputSource> schemas = new ArrayList<InputSource>();
ohair@286 60 private final HashMap<QName, Port> bindingNameToPortMap = new HashMap<QName, Port>();
ohair@286 61 private static final String w3ceprSchemaBinding = "<bindings\n" +
ohair@286 62 " xmlns=\"http://java.sun.com/xml/ns/jaxb\"\n" +
ohair@286 63 " xmlns:wsa=\"http://www.w3.org/2005/08/addressing\"\n" +
ohair@286 64 " xmlns:xjc=\"http://java.sun.com/xml/ns/jaxb/xjc\"\n" +
ohair@286 65 " version=\"2.1\">\n" +
ohair@286 66 " \n" +
ohair@286 67 " <bindings scd=\"x-schema::wsa\" if-exists=\"true\">\n" +
ohair@286 68 //comment the following, otw JAXB won't generate ObjectFactory, classes from wsa schema. See JAX-WS-804
ohair@286 69 // " <schemaBindings map=\"false\" />\n" +
ohair@286 70 " <bindings scd=\"wsa:EndpointReference\">\n" +
ohair@286 71 " <class ref=\"javax.xml.ws.wsaddressing.W3CEndpointReference\" xjc:recursive=\"true\"/>\n" +
ohair@286 72 " </bindings>\n" +
ohair@286 73 " <bindings scd=\"~wsa:EndpointReferenceType\">\n" +
ohair@286 74 " <class ref=\"javax.xml.ws.wsaddressing.W3CEndpointReference\" xjc:recursive=\"true\"/>\n" +
ohair@286 75 " </bindings>\n" +
ohair@286 76 " </bindings>\n" +
ohair@286 77 "</bindings>";
ohair@286 78
ohair@286 79 private static final String memberSubmissionEPR = "<bindings\n" +
ohair@286 80 " xmlns=\"http://java.sun.com/xml/ns/jaxb\"\n" +
ohair@286 81 " xmlns:wsa=\"http://schemas.xmlsoap.org/ws/2004/08/addressing\"\n" +
ohair@286 82 " version=\"2.1\">\n" +
ohair@286 83 " \n" +
ohair@286 84 " <bindings scd=\"x-schema::wsa\" if-exists=\"true\">\n" +
ohair@286 85 //comment the following, otw JAXB won't generate ObjectFactory, classes from wsa schema. See JAX-WS-804
ohair@286 86 // " <schemaBindings map=\"false\" />\n" +
ohair@286 87 " <bindings scd=\"wsa:EndpointReference\">\n" +
ohair@286 88 " <class ref=\"com.sun.xml.internal.ws.developer.MemberSubmissionEndpointReference\"/>\n" +
ohair@286 89 " </bindings>\n" +
ohair@286 90 " <bindings scd=\"~wsa:EndpointReferenceType\">\n" +
ohair@286 91 " <class ref=\"com.sun.xml.internal.ws.developer.MemberSubmissionEndpointReference\"/>\n" +
ohair@286 92 " </bindings>\n" +
ohair@286 93 " </bindings>\n" +
ohair@286 94 "</bindings>";
ohair@286 95
ohair@286 96 private final static String sysId = "http://dummy.pseudo-schema#schema";
ohair@286 97
ohair@286 98 private WsimportOptions options;
ohair@286 99 public static List<InputSource> build(WSDLModeler wsdlModeler, WsimportOptions options, ErrorReceiver errReceiver) {
ohair@286 100 PseudoSchemaBuilder b = new PseudoSchemaBuilder(wsdlModeler.document);
ohair@286 101 b.wsdlModeler = wsdlModeler;
ohair@286 102 b.options = options;
ohair@286 103 b.build();
ohair@286 104 int i;
ohair@286 105 for(i = 0; i < b.schemas.size(); i++){
ohair@286 106 InputSource is = b.schemas.get(i);
ohair@286 107 is.setSystemId(sysId+(i + 1));
ohair@286 108 }
ohair@286 109 //add w3c EPR binding
ohair@286 110 if(!(options.noAddressingBbinding) && options.target.isLaterThan(Options.Target.V2_1)){
mkos@532 111 InputSource is = new InputSource(new ByteArrayInputStream(w3ceprSchemaBinding.getBytes(StandardCharsets.UTF_8)));
ohair@286 112 is.setSystemId(sysId+(++i +1));
ohair@286 113 b.schemas.add(is);
ohair@286 114 }
ohair@286 115
ohair@286 116
ohair@286 117 //TODO: uncomment after JAXB fixes the issue related to passing multiples of such bindings
ohair@286 118 //add member submission EPR binding
ohair@286 119 // InputSource is1 = new InputSource(new ByteArrayInputStream(memberSubmissionEPR.getBytes()));
ohair@286 120 // is1.setSystemId(sysId+(++i + 1));
ohair@286 121 // b.schemas.add(is1);
ohair@286 122
ohair@286 123 return b.schemas;
ohair@286 124 }
ohair@286 125
ohair@286 126
ohair@286 127 private PseudoSchemaBuilder(WSDLDocument _wsdl) {
ohair@286 128 this.wsdlDocument = _wsdl;
ohair@286 129 }
ohair@286 130
ohair@286 131 private void build() {
alanb@368 132 for(Iterator<Service> itr=wsdlDocument.getDefinitions().services(); itr.hasNext(); ) {
ohair@286 133 build(itr.next());
alanb@368 134 }
ohair@286 135 }
ohair@286 136
ohair@286 137 private void build(Service service) {
alanb@368 138 for( Iterator<Port> itr=service.ports(); itr.hasNext(); ) {
ohair@286 139 build(itr.next() );
alanb@368 140 }
ohair@286 141 }
ohair@286 142
ohair@286 143 private void build(Port port) {
ohair@286 144 if(wsdlModeler.isProvider(port))
ohair@286 145 return;
ohair@286 146 Binding binding = port.resolveBinding(wsdlDocument);
ohair@286 147
ohair@286 148 SOAPBinding soapBinding =
ohair@286 149 (SOAPBinding)getExtensionOfType(binding, SOAPBinding.class);
ohair@286 150 //lets try and see if its SOAP 1.2. dont worry about extension flag, its
ohair@286 151 // handled much earlier
ohair@286 152 if (soapBinding == null) {
ohair@286 153 soapBinding =
ohair@286 154 (SOAPBinding)getExtensionOfType(binding, SOAP12Binding.class);
ohair@286 155 }
ohair@286 156 if(soapBinding == null)
ohair@286 157 return;
ohair@286 158 PortType portType = binding.resolvePortType(wsdlDocument);
ohair@286 159
ohair@286 160 QName bindingName = WSDLModelerBase.getQNameOf(binding);
ohair@286 161
ohair@286 162 //we dont want to process the port bound to the binding processed earlier
ohair@286 163 if(bindingNameToPortMap.containsKey(bindingName))
ohair@286 164 return;
ohair@286 165
ohair@286 166 bindingNameToPortMap.put(bindingName, port);
ohair@286 167
ohair@286 168
ohair@286 169 for(Iterator itr=binding.operations(); itr.hasNext();){
ohair@286 170 BindingOperation bindingOperation = (BindingOperation)itr.next();
ohair@286 171
ohair@286 172 // get only the bounded operations
ohair@286 173 Set boundedOps = portType.getOperationsNamed(bindingOperation.getName());
ohair@286 174 if(boundedOps.size() != 1)
ohair@286 175 continue;
ohair@286 176 Operation operation = (Operation)boundedOps.iterator().next();
ohair@286 177
ohair@286 178 // No pseudo schema required for doc/lit
ohair@286 179 if(wsdlModeler.isAsync(portType, operation)){
ohair@286 180 buildAsync(portType, operation, bindingOperation);
ohair@286 181 }
ohair@286 182 }
ohair@286 183 }
ohair@286 184
ohair@286 185 /**
ohair@286 186 * @param portType
ohair@286 187 * @param operation
ohair@286 188 * @param bindingOperation
ohair@286 189 */
ohair@286 190 private void buildAsync(PortType portType, Operation operation, BindingOperation bindingOperation) {
ohair@286 191 String operationName = getCustomizedOperationName(operation);//operation.getName();
ohair@286 192 if(operationName == null)
ohair@286 193 return;
ohair@286 194 Message outputMessage = null;
ohair@286 195 if(operation.getOutput() != null)
ohair@286 196 outputMessage = operation.getOutput().resolveMessage(wsdlDocument);
ohair@286 197 if(outputMessage != null){
ohair@286 198 List<MessagePart> allParts = new ArrayList<MessagePart>(outputMessage.getParts());
alanb@368 199 if(options != null && options.additionalHeaders) {
ohair@286 200 List<MessagePart> addtionalHeaderParts = wsdlModeler.getAdditionHeaderParts(bindingOperation, outputMessage, false);
ohair@286 201 allParts.addAll(addtionalHeaderParts);
ohair@286 202 }
ohair@286 203 if(allParts.size() > 1)
ohair@286 204 build(getOperationName(operationName), allParts);
ohair@286 205 }
ohair@286 206
ohair@286 207 }
ohair@286 208
ohair@286 209 private String getCustomizedOperationName(Operation operation) {
ohair@286 210 JAXWSBinding jaxwsCustomization = (JAXWSBinding)getExtensionOfType(operation, JAXWSBinding.class);
ohair@286 211 String operationName = (jaxwsCustomization != null)?((jaxwsCustomization.getMethodName() != null)?jaxwsCustomization.getMethodName().getName():null):null;
ohair@286 212 if(operationName != null){
ohair@286 213 if(Names.isJavaReservedWord(operationName)){
ohair@286 214 return null;
ohair@286 215 }
ohair@286 216
ohair@286 217 return operationName;
ohair@286 218 }
ohair@286 219 return operation.getName();
ohair@286 220 }
ohair@286 221
ohair@286 222 private void writeImports(QName elementName, List<MessagePart> parts){
ohair@286 223 Set<String> uris = new HashSet<String>();
ohair@286 224 for(MessagePart p:parts){
ohair@286 225 String ns = p.getDescriptor().getNamespaceURI();
ohair@286 226 if(!uris.contains(ns) && !ns.equals("http://www.w3.org/2001/XMLSchema") && !ns.equals(elementName.getNamespaceURI())){
ohair@286 227 print("<xs:import namespace=''{0}''/>", ns);
ohair@286 228 uris.add(ns);
ohair@286 229 }
ohair@286 230 }
ohair@286 231 }
ohair@286 232
ohair@286 233 boolean asyncRespBeanBinding = false;
ohair@286 234 private void build(QName elementName, List<MessagePart> allParts){
ohair@286 235
ohair@286 236 print(
ohair@286 237 "<xs:schema xmlns:xs=''http://www.w3.org/2001/XMLSchema''" +
ohair@286 238 " xmlns:jaxb=''http://java.sun.com/xml/ns/jaxb''" +
ohair@286 239 " xmlns:xjc=''http://java.sun.com/xml/ns/jaxb/xjc''" +
ohair@286 240 " jaxb:extensionBindingPrefixes=''xjc''" +
ohair@286 241 " jaxb:version=''1.0''" +
ohair@286 242 " targetNamespace=''{0}''>",
ohair@286 243 elementName.getNamespaceURI());
ohair@286 244
ohair@286 245 writeImports(elementName, allParts);
ohair@286 246
ohair@286 247 if(!asyncRespBeanBinding){
ohair@286 248 print(
ohair@286 249 "<xs:annotation><xs:appinfo>" +
ohair@286 250 " <jaxb:schemaBindings>" +
ohair@286 251 " <jaxb:package name=''{0}'' />" +
ohair@286 252 " </jaxb:schemaBindings>" +
ohair@286 253 "</xs:appinfo></xs:annotation>",
ohair@286 254 wsdlModeler.getJavaPackage() );
ohair@286 255 asyncRespBeanBinding = true;
ohair@286 256 }
ohair@286 257
ohair@286 258 print("<xs:element name=''{0}''>", elementName.getLocalPart());
ohair@286 259 print("<xs:complexType>");
ohair@286 260 print("<xs:sequence>");
ohair@286 261
ohair@286 262
ohair@286 263 for(MessagePart p:allParts) {
ohair@286 264 //rpclit wsdl:part must reference schema type not element, also it must exclude headers and mime parts
ohair@286 265 if(p.getDescriptorKind() == SchemaKinds.XSD_ELEMENT){
ohair@286 266 print("<xs:element ref=''types:{0}'' xmlns:types=''{1}''/>",p.getDescriptor().getLocalPart(), p.getDescriptor().getNamespaceURI());
ohair@286 267 }else{
ohair@286 268 print("<xs:element name=''{0}'' type=''{1}'' xmlns=''{2}'' />",
ohair@286 269 p.getName(),
ohair@286 270 p.getDescriptor().getLocalPart(),
ohair@286 271 p.getDescriptor().getNamespaceURI() );
ohair@286 272 }
ohair@286 273 }
ohair@286 274
ohair@286 275 print("</xs:sequence>");
ohair@286 276 print("</xs:complexType>");
ohair@286 277 print("</xs:element>");
ohair@286 278 print("</xs:schema>");
ohair@286 279
ohair@286 280 // reset the StringWriter, so that next operation element could be written
ohair@286 281 if(buf.toString().length() > 0){
ohair@286 282 //System.out.println("Response bean Schema for operation========> "+ elementName+"\n\n"+buf);
ohair@286 283 InputSource is = new InputSource(new StringReader(buf.toString()));
ohair@286 284 schemas.add(is);
ohair@286 285 buf.getBuffer().setLength(0);
ohair@286 286 }
ohair@286 287 }
ohair@286 288
ohair@286 289 private QName getOperationName(String operationName){
ohair@286 290 if(operationName == null)
ohair@286 291 return null;
ohair@286 292 // String namespaceURI = wsdlDocument.getDefinitions().getTargetNamespaceURI()+"?"+portType.getName()+"?" + operationName;
ohair@286 293 String namespaceURI = "";
ohair@286 294 return new QName(namespaceURI, operationName+"Response");
ohair@286 295 }
ohair@286 296
ohair@286 297 private void print( String msg ) {
ohair@286 298 print( msg, new Object[0] );
ohair@286 299 }
ohair@286 300 private void print( String msg, Object arg1 ) {
ohair@286 301 print( msg, new Object[]{arg1} );
ohair@286 302 }
ohair@286 303 private void print( String msg, Object arg1, Object arg2 ) {
ohair@286 304 print( msg, new Object[]{arg1, arg2} );
ohair@286 305 }
ohair@286 306 private void print( String msg, Object arg1, Object arg2, Object arg3 ) {
ohair@286 307 print( msg, new Object[]{arg1,arg2,arg3} );
ohair@286 308 }
ohair@286 309 private void print( String msg, Object[] args ) {
ohair@286 310 buf.write(MessageFormat.format(msg,args));
ohair@286 311 buf.write('\n');
ohair@286 312 }
ohair@286 313
ohair@286 314 }

mercurial