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

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 721
06807f9a6835
parent 637
9c07ef4934dd
permissions
-rw-r--r--

merge

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

mercurial