Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.server; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer; |
aoqi@0 | 30 | import com.sun.xml.internal.stream.buffer.XMLStreamBufferResult; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.api.server.SDDocument; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.api.server.SDDocumentSource; |
aoqi@0 | 33 | |
aoqi@0 | 34 | import javax.xml.namespace.QName; |
aoqi@0 | 35 | import javax.xml.transform.Result; |
aoqi@0 | 36 | import javax.xml.ws.Holder; |
aoqi@0 | 37 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 38 | import java.net.URL; |
aoqi@0 | 39 | import java.net.MalformedURLException; |
aoqi@0 | 40 | import java.util.ArrayList; |
aoqi@0 | 41 | import java.util.HashMap; |
aoqi@0 | 42 | import java.util.List; |
aoqi@0 | 43 | import java.util.Map; |
aoqi@0 | 44 | |
aoqi@0 | 45 | /** |
aoqi@0 | 46 | * WSDLGenerator uses WSDLResolver while creating WSDL artifacts. WSDLResolver |
aoqi@0 | 47 | * is used to control the file names and which artifact to be generated or not. |
aoqi@0 | 48 | * |
aoqi@0 | 49 | * @author Jitendra Kotamraju |
aoqi@0 | 50 | */ |
aoqi@0 | 51 | final class WSDLGenResolver implements com.oracle.webservices.internal.api.databinding.WSDLResolver { |
aoqi@0 | 52 | |
aoqi@0 | 53 | private final List<SDDocumentImpl> docs; |
aoqi@0 | 54 | private final List<SDDocumentSource> newDocs = new ArrayList<SDDocumentSource>(); |
aoqi@0 | 55 | private SDDocumentSource concreteWsdlSource; |
aoqi@0 | 56 | |
aoqi@0 | 57 | private SDDocumentImpl abstractWsdl; |
aoqi@0 | 58 | private SDDocumentImpl concreteWsdl; |
aoqi@0 | 59 | |
aoqi@0 | 60 | /** |
aoqi@0 | 61 | * targetNS -> schema documents. |
aoqi@0 | 62 | */ |
aoqi@0 | 63 | private final Map<String, List<SDDocumentImpl>> nsMapping = new HashMap<String,List<SDDocumentImpl>>(); |
aoqi@0 | 64 | |
aoqi@0 | 65 | private final QName serviceName; |
aoqi@0 | 66 | private final QName portTypeName; |
aoqi@0 | 67 | |
aoqi@0 | 68 | public WSDLGenResolver(@NotNull List<SDDocumentImpl> docs,QName serviceName,QName portTypeName) { |
aoqi@0 | 69 | this.docs = docs; |
aoqi@0 | 70 | this.serviceName = serviceName; |
aoqi@0 | 71 | this.portTypeName = portTypeName; |
aoqi@0 | 72 | |
aoqi@0 | 73 | for (SDDocumentImpl doc : docs) { |
aoqi@0 | 74 | if(doc.isWSDL()) { |
aoqi@0 | 75 | SDDocument.WSDL wsdl = (SDDocument.WSDL) doc; |
aoqi@0 | 76 | if(wsdl.hasPortType()) |
aoqi@0 | 77 | abstractWsdl = doc; |
aoqi@0 | 78 | } |
aoqi@0 | 79 | if(doc.isSchema()) { |
aoqi@0 | 80 | SDDocument.Schema schema = (SDDocument.Schema) doc; |
aoqi@0 | 81 | List<SDDocumentImpl> sysIds = nsMapping.get(schema.getTargetNamespace()); |
aoqi@0 | 82 | if (sysIds == null) { |
aoqi@0 | 83 | sysIds = new ArrayList<SDDocumentImpl>(); |
aoqi@0 | 84 | nsMapping.put(schema.getTargetNamespace(), sysIds); |
aoqi@0 | 85 | } |
aoqi@0 | 86 | sysIds.add(doc); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | } |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | /** |
aoqi@0 | 92 | * Generates the concrete WSDL that contains service element. |
aoqi@0 | 93 | * |
aoqi@0 | 94 | * @return Result the generated concrete WSDL |
aoqi@0 | 95 | */ |
aoqi@0 | 96 | public Result getWSDL(String filename) { |
aoqi@0 | 97 | URL url = createURL(filename); |
aoqi@0 | 98 | MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer(); |
aoqi@0 | 99 | xsb.setSystemId(url.toExternalForm()); |
aoqi@0 | 100 | concreteWsdlSource = SDDocumentSource.create(url,xsb); |
aoqi@0 | 101 | newDocs.add(concreteWsdlSource); |
aoqi@0 | 102 | XMLStreamBufferResult r = new XMLStreamBufferResult(xsb); |
aoqi@0 | 103 | r.setSystemId(filename); |
aoqi@0 | 104 | return r; |
aoqi@0 | 105 | } |
aoqi@0 | 106 | |
aoqi@0 | 107 | /** |
aoqi@0 | 108 | * At present, it returns file URL scheme eventhough there is no resource |
aoqi@0 | 109 | * in the filesystem. |
aoqi@0 | 110 | * |
aoqi@0 | 111 | * @return URL of the generated document |
aoqi@0 | 112 | * |
aoqi@0 | 113 | */ |
aoqi@0 | 114 | private URL createURL(String filename) { |
aoqi@0 | 115 | try { |
aoqi@0 | 116 | return new URL("file:///"+filename); |
aoqi@0 | 117 | } catch (MalformedURLException e) { |
aoqi@0 | 118 | // TODO: I really don't think this is the right way to handle this error, |
aoqi@0 | 119 | // WSDLResolver needs to be documented carefully. |
aoqi@0 | 120 | throw new WebServiceException(e); |
aoqi@0 | 121 | } |
aoqi@0 | 122 | } |
aoqi@0 | 123 | |
aoqi@0 | 124 | /** |
aoqi@0 | 125 | * Updates filename if the suggested filename need to be changed in |
aoqi@0 | 126 | * wsdl:import. If the metadata already contains abstract wsdl(i.e. a WSDL |
aoqi@0 | 127 | * which has the porttype), then the abstract wsdl shouldn't be generated |
aoqi@0 | 128 | * |
aoqi@0 | 129 | * return null if abstract WSDL need not be generated |
aoqi@0 | 130 | * Result the abstract WSDL |
aoqi@0 | 131 | */ |
aoqi@0 | 132 | public Result getAbstractWSDL(Holder<String> filename) { |
aoqi@0 | 133 | if (abstractWsdl != null) { |
aoqi@0 | 134 | filename.value = abstractWsdl.getURL().toString(); |
aoqi@0 | 135 | return null; // Don't generate abstract WSDL |
aoqi@0 | 136 | } |
aoqi@0 | 137 | URL url = createURL(filename.value); |
aoqi@0 | 138 | MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer(); |
aoqi@0 | 139 | xsb.setSystemId(url.toExternalForm()); |
aoqi@0 | 140 | SDDocumentSource abstractWsdlSource = SDDocumentSource.create(url,xsb); |
aoqi@0 | 141 | newDocs.add(abstractWsdlSource); |
aoqi@0 | 142 | XMLStreamBufferResult r = new XMLStreamBufferResult(xsb); |
aoqi@0 | 143 | r.setSystemId(filename.value); |
aoqi@0 | 144 | return r; |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | /** |
aoqi@0 | 148 | * Updates filename if the suggested filename need to be changed in |
aoqi@0 | 149 | * xsd:import. If there is already a schema document for the namespace |
aoqi@0 | 150 | * in the metadata, then it is not generated. |
aoqi@0 | 151 | * |
aoqi@0 | 152 | * return null if schema need not be generated |
aoqi@0 | 153 | * Result the generated schema document |
aoqi@0 | 154 | */ |
aoqi@0 | 155 | public Result getSchemaOutput(String namespace, Holder<String> filename) { |
aoqi@0 | 156 | List<SDDocumentImpl> schemas = nsMapping.get(namespace); |
aoqi@0 | 157 | if (schemas != null) { |
aoqi@0 | 158 | if (schemas.size() > 1) { |
aoqi@0 | 159 | throw new ServerRtException("server.rt.err", |
aoqi@0 | 160 | "More than one schema for the target namespace "+namespace); |
aoqi@0 | 161 | } |
aoqi@0 | 162 | filename.value = schemas.get(0).getURL().toExternalForm(); |
aoqi@0 | 163 | return null; // Don't generate schema |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | URL url = createURL(filename.value); |
aoqi@0 | 167 | MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer(); |
aoqi@0 | 168 | xsb.setSystemId(url.toExternalForm()); |
aoqi@0 | 169 | SDDocumentSource sd = SDDocumentSource.create(url,xsb); |
aoqi@0 | 170 | newDocs.add(sd); |
aoqi@0 | 171 | |
aoqi@0 | 172 | XMLStreamBufferResult r = new XMLStreamBufferResult(xsb); |
aoqi@0 | 173 | r.setSystemId(filename.value); |
aoqi@0 | 174 | return r; |
aoqi@0 | 175 | } |
aoqi@0 | 176 | |
aoqi@0 | 177 | /** |
aoqi@0 | 178 | * Converts SDDocumentSource to SDDocumentImpl and updates original docs. It |
aoqi@0 | 179 | * categories the generated documents into WSDL, Schema types. |
aoqi@0 | 180 | * |
aoqi@0 | 181 | * @return the primary WSDL |
aoqi@0 | 182 | * null if it is not there in the generated documents |
aoqi@0 | 183 | * |
aoqi@0 | 184 | */ |
aoqi@0 | 185 | public SDDocumentImpl updateDocs() { |
aoqi@0 | 186 | for (SDDocumentSource doc : newDocs) { |
aoqi@0 | 187 | SDDocumentImpl docImpl = SDDocumentImpl.create(doc,serviceName,portTypeName); |
aoqi@0 | 188 | if (doc == concreteWsdlSource) { |
aoqi@0 | 189 | concreteWsdl = docImpl; |
aoqi@0 | 190 | } |
aoqi@0 | 191 | docs.add(docImpl); |
aoqi@0 | 192 | } |
aoqi@0 | 193 | return concreteWsdl; |
aoqi@0 | 194 | } |
aoqi@0 | 195 | |
aoqi@0 | 196 | } |