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