src/share/jaxws_classes/com/sun/xml/internal/ws/spi/ProviderImpl.java

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 408
b0610cd08440
child 637
9c07ef4934dd
child 721
06807f9a6835
permissions
-rw-r--r--

8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu

ohair@286 1 /*
mkos@515 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.xml.internal.ws.spi;
ohair@286 27
ohair@286 28
ohair@286 29 import com.sun.xml.internal.ws.api.BindingID;
ohair@286 30 import com.sun.xml.internal.ws.api.WSService;
ohair@286 31 import com.sun.xml.internal.ws.api.ServiceSharedFeatureMarker;
ohair@286 32 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
ohair@286 33 import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
mkos@408 34 import com.sun.xml.internal.ws.api.model.wsdl.WSDLModel;
ohair@286 35 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
ohair@286 36 import com.sun.xml.internal.ws.api.model.wsdl.WSDLService;
ohair@286 37 import com.sun.xml.internal.ws.api.server.BoundEndpoint;
ohair@286 38 import com.sun.xml.internal.ws.api.server.Container;
ohair@286 39 import com.sun.xml.internal.ws.api.server.ContainerResolver;
ohair@286 40 import com.sun.xml.internal.ws.api.server.Module;
ohair@286 41 import com.sun.xml.internal.ws.api.server.WSEndpoint;
ohair@286 42 import com.sun.xml.internal.ws.api.wsdl.parser.WSDLParserExtension;
ohair@286 43 import com.sun.xml.internal.ws.client.WSServiceDelegate;
ohair@286 44 import com.sun.xml.internal.ws.developer.MemberSubmissionEndpointReference;
ohair@286 45 import com.sun.xml.internal.ws.resources.ProviderApiMessages;
ohair@286 46 import com.sun.xml.internal.ws.transport.http.server.EndpointImpl;
ohair@286 47 import com.sun.xml.internal.ws.util.ServiceFinder;
ohair@286 48 import com.sun.xml.internal.ws.util.xml.XmlUtil;
ohair@286 49 import com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser;
mkos@408 50
ohair@286 51 import org.w3c.dom.Element;
ohair@286 52 import org.xml.sax.EntityResolver;
ohair@286 53
ohair@286 54 import javax.xml.bind.JAXBContext;
ohair@286 55 import javax.xml.bind.JAXBException;
ohair@286 56 import javax.xml.bind.Unmarshaller;
ohair@286 57 import javax.xml.namespace.QName;
ohair@286 58 import javax.xml.transform.Source;
ohair@286 59 import javax.xml.transform.stream.StreamSource;
ohair@286 60 import javax.xml.ws.Endpoint;
ohair@286 61 import javax.xml.ws.EndpointReference;
ohair@286 62 import javax.xml.ws.Service;
ohair@286 63 import javax.xml.ws.WebServiceException;
ohair@286 64 import javax.xml.ws.WebServiceFeature;
ohair@286 65 import javax.xml.ws.spi.Provider;
ohair@286 66 import javax.xml.ws.spi.ServiceDelegate;
ohair@286 67 import javax.xml.ws.spi.Invoker;
ohair@286 68 import javax.xml.ws.wsaddressing.W3CEndpointReference;
mkos@408 69
ohair@286 70 import java.net.URL;
ohair@286 71 import java.security.AccessController;
ohair@286 72 import java.security.PrivilegedAction;
ohair@286 73 import java.util.List;
ohair@286 74 import java.util.Map;
ohair@286 75
ohair@286 76 /**
ohair@286 77 * The entry point to the JAX-WS RI from the JAX-WS API.
ohair@286 78 *
ohair@286 79 * @author WS Development Team
ohair@286 80 */
ohair@286 81 public class ProviderImpl extends Provider {
ohair@286 82
mkos@515 83 private final static ContextClassloaderLocal<JAXBContext> eprjc = new ContextClassloaderLocal<JAXBContext>() {
mkos@515 84 @Override
mkos@515 85 protected JAXBContext initialValue() throws Exception {
mkos@515 86 return getEPRJaxbContext();
mkos@515 87 }
mkos@515 88 };
ohair@286 89
ohair@286 90 /**
ohair@286 91 * Convenient singleton instance.
ohair@286 92 */
ohair@286 93 public static final ProviderImpl INSTANCE = new ProviderImpl();
ohair@286 94
ohair@286 95 @Override
ohair@286 96 public Endpoint createEndpoint(String bindingId, Object implementor) {
ohair@286 97 return new EndpointImpl(
ohair@286 98 (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()),
ohair@286 99 implementor);
ohair@286 100 }
ohair@286 101
ohair@286 102 @Override
ohair@286 103 public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass) {
ohair@286 104 return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass);
ohair@286 105 }
ohair@286 106
ohair@286 107 public ServiceDelegate createServiceDelegate( URL wsdlDocumentLocation, QName serviceName, Class serviceClass,
ohair@286 108 WebServiceFeature ... features) {
ohair@286 109 for (WebServiceFeature feature : features) {
ohair@286 110 if (!(feature instanceof ServiceSharedFeatureMarker))
ohair@286 111 throw new WebServiceException("Doesn't support any Service specific features");
ohair@286 112 }
ohair@286 113 return new WSServiceDelegate(wsdlDocumentLocation, serviceName, serviceClass, features);
ohair@286 114 }
ohair@286 115
ohair@286 116 public ServiceDelegate createServiceDelegate( Source wsdlSource, QName serviceName, Class serviceClass) {
ohair@286 117 return new WSServiceDelegate(wsdlSource, serviceName, serviceClass);
ohair@286 118 }
ohair@286 119
ohair@286 120 @Override
ohair@286 121 public Endpoint createAndPublishEndpoint(String address,
ohair@286 122 Object implementor) {
ohair@286 123 Endpoint endpoint = new EndpointImpl(
ohair@286 124 BindingID.parse(implementor.getClass()),
ohair@286 125 implementor);
ohair@286 126 endpoint.publish(address);
ohair@286 127 return endpoint;
ohair@286 128 }
ohair@286 129
ohair@286 130 public Endpoint createEndpoint(String bindingId, Object implementor, WebServiceFeature... features) {
ohair@286 131 return new EndpointImpl(
ohair@286 132 (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementor.getClass()),
ohair@286 133 implementor, features);
ohair@286 134 }
ohair@286 135
ohair@286 136 public Endpoint createAndPublishEndpoint(String address, Object implementor, WebServiceFeature... features) {
ohair@286 137 Endpoint endpoint = new EndpointImpl(
ohair@286 138 BindingID.parse(implementor.getClass()), implementor, features);
ohair@286 139 endpoint.publish(address);
ohair@286 140 return endpoint;
ohair@286 141 }
ohair@286 142
ohair@286 143 public Endpoint createEndpoint(String bindingId, Class implementorClass, Invoker invoker, WebServiceFeature... features) {
ohair@286 144 return new EndpointImpl(
ohair@286 145 (bindingId != null) ? BindingID.parse(bindingId) : BindingID.parse(implementorClass),
ohair@286 146 implementorClass, invoker, features);
ohair@286 147 }
ohair@286 148
ohair@286 149 public EndpointReference readEndpointReference(final Source eprInfoset) {
ohair@286 150 // EPR constructors are private, so we need privilege escalation.
ohair@286 151 // this unmarshalling can only access instances of a fixed, known set of classes,
ohair@286 152 // so doing that shouldn't introduce security vulnerability.
ohair@286 153 return AccessController.doPrivileged(new PrivilegedAction<EndpointReference>() {
ohair@286 154 public EndpointReference run() {
ohair@286 155 try {
mkos@515 156 Unmarshaller unmarshaller = eprjc.get().createUnmarshaller();
ohair@286 157 return (EndpointReference) unmarshaller.unmarshal(eprInfoset);
ohair@286 158 } catch (JAXBException e) {
ohair@286 159 throw new WebServiceException("Error creating Marshaller or marshalling.", e);
ohair@286 160 }
ohair@286 161 }
ohair@286 162 });
ohair@286 163 }
ohair@286 164
ohair@286 165 public <T> T getPort(EndpointReference endpointReference, Class<T> clazz, WebServiceFeature... webServiceFeatures) {
ohair@286 166 /*
ohair@286 167 final @NotNull MemberSubmissionEndpointReference msepr =
ohair@286 168 EndpointReferenceUtil.transform(MemberSubmissionEndpointReference.class, endpointReference);
ohair@286 169 WSService service = new WSServiceDelegate(msepr.toWSDLSource(), msepr.serviceName.name, Service.class);
ohair@286 170 */
ohair@286 171 if(endpointReference == null)
ohair@286 172 throw new WebServiceException(ProviderApiMessages.NULL_EPR());
ohair@286 173 WSEndpointReference wsepr = new WSEndpointReference(endpointReference);
ohair@286 174 WSEndpointReference.Metadata metadata = wsepr.getMetaData();
ohair@286 175 WSService service;
ohair@286 176 if(metadata.getWsdlSource() != null)
ohair@286 177 service = (WSService) createServiceDelegate(metadata.getWsdlSource(), metadata.getServiceName(), Service.class);
ohair@286 178 else
ohair@286 179 throw new WebServiceException("WSDL metadata is missing in EPR");
ohair@286 180 return service.getPort(wsepr, clazz, webServiceFeatures);
ohair@286 181 }
ohair@286 182
ohair@286 183 public W3CEndpointReference createW3CEndpointReference(String address, QName serviceName, QName portName, List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters) {
ohair@286 184 return createW3CEndpointReference(address, null, serviceName, portName, metadata, wsdlDocumentLocation, referenceParameters, null, null);
ohair@286 185 }
ohair@286 186
ohair@286 187 public W3CEndpointReference createW3CEndpointReference(String address, QName interfaceName, QName serviceName, QName portName,
ohair@286 188 List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters,
ohair@286 189 List<Element> elements, Map<QName, String> attributes) {
ohair@286 190 Container container = ContainerResolver.getInstance().getContainer();
ohair@286 191 if (address == null) {
ohair@286 192 if (serviceName == null || portName == null) {
ohair@286 193 throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS_SERVICE_ENDPOINT());
ohair@286 194 } else {
ohair@286 195 //check if it is run in a Java EE Container and if so, get address using serviceName and portName
ohair@286 196 Module module = container.getSPI(Module.class);
ohair@286 197 if (module != null) {
ohair@286 198 List<BoundEndpoint> beList = module.getBoundEndpoints();
ohair@286 199 for (BoundEndpoint be : beList) {
ohair@286 200 WSEndpoint wse = be.getEndpoint();
ohair@286 201 if (wse.getServiceName().equals(serviceName) && wse.getPortName().equals(portName)) {
ohair@286 202 try {
ohair@286 203 address = be.getAddress().toString();
ohair@286 204 } catch (WebServiceException e) {
ohair@286 205 // May be the container does n't support this
ohair@286 206 //just ignore the exception
ohair@286 207 }
ohair@286 208 break;
ohair@286 209 }
ohair@286 210 }
ohair@286 211 }
ohair@286 212 //address is still null? may be its not run in a JavaEE Container
ohair@286 213 if (address == null)
ohair@286 214 throw new IllegalStateException(ProviderApiMessages.NULL_ADDRESS());
ohair@286 215 }
ohair@286 216 }
ohair@286 217 if((serviceName==null) && (portName != null)) {
ohair@286 218 throw new IllegalStateException(ProviderApiMessages.NULL_SERVICE());
ohair@286 219 }
ohair@286 220 //Validate Service and Port in WSDL
ohair@286 221 String wsdlTargetNamespace = null;
ohair@286 222 if (wsdlDocumentLocation != null) {
ohair@286 223 try {
ohair@286 224 EntityResolver er = XmlUtil.createDefaultCatalogResolver();
ohair@286 225
ohair@286 226 URL wsdlLoc = new URL(wsdlDocumentLocation);
mkos@408 227 WSDLModel wsdlDoc = RuntimeWSDLParser.parse(wsdlLoc, new StreamSource(wsdlLoc.toExternalForm()), er,
ohair@286 228 true, container, ServiceFinder.find(WSDLParserExtension.class).toArray());
ohair@286 229 if (serviceName != null) {
ohair@286 230 WSDLService wsdlService = wsdlDoc.getService(serviceName);
ohair@286 231 if (wsdlService == null)
ohair@286 232 throw new IllegalStateException(ProviderApiMessages.NOTFOUND_SERVICE_IN_WSDL(
ohair@286 233 serviceName,wsdlDocumentLocation));
ohair@286 234 if (portName != null) {
ohair@286 235 WSDLPort wsdlPort = wsdlService.get(portName);
ohair@286 236 if (wsdlPort == null)
ohair@286 237 throw new IllegalStateException(ProviderApiMessages.NOTFOUND_PORT_IN_WSDL(
ohair@286 238 portName,serviceName,wsdlDocumentLocation));
ohair@286 239 }
ohair@286 240 wsdlTargetNamespace = serviceName.getNamespaceURI();
ohair@286 241 } else {
ohair@286 242 QName firstService = wsdlDoc.getFirstServiceName();
ohair@286 243 wsdlTargetNamespace = firstService.getNamespaceURI();
ohair@286 244 }
ohair@286 245 } catch (Exception e) {
ohair@286 246 throw new IllegalStateException(ProviderApiMessages.ERROR_WSDL(wsdlDocumentLocation),e);
ohair@286 247 }
ohair@286 248 }
ohair@286 249 //wcf3.0/3.5 rejected empty metadata element.
ohair@286 250 if (metadata != null && metadata.size() == 0) {
ohair@286 251 metadata = null;
ohair@286 252 }
ohair@286 253 return new WSEndpointReference(
ohair@286 254 AddressingVersion.fromSpecClass(W3CEndpointReference.class),
ohair@286 255 address, serviceName, portName, interfaceName, metadata, wsdlDocumentLocation, wsdlTargetNamespace,referenceParameters, elements, attributes).toSpec(W3CEndpointReference.class);
ohair@286 256
ohair@286 257 }
ohair@286 258
ohair@286 259 private static JAXBContext getEPRJaxbContext() {
ohair@286 260 // EPRs have package and private fields, so we need privilege escalation.
ohair@286 261 // this access only fixed, known set of classes, so doing that
ohair@286 262 // shouldn't introduce security vulnerability.
ohair@286 263 return AccessController.doPrivileged(new PrivilegedAction<JAXBContext>() {
ohair@286 264 public JAXBContext run() {
ohair@286 265 try {
ohair@286 266 return JAXBContext.newInstance(MemberSubmissionEndpointReference.class, W3CEndpointReference.class);
ohair@286 267 } catch (JAXBException e) {
ohair@286 268 throw new WebServiceException("Error creating JAXBContext for W3CEndpointReference. ", e);
ohair@286 269 }
ohair@286 270 }
ohair@286 271 });
ohair@286 272 }
ohair@286 273 }

mercurial