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.api; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.api.server.Container; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.api.server.ContainerResolver; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.api.server.WSEndpoint; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.client.WSServiceDelegate; |
aoqi@0 | 35 | |
aoqi@0 | 36 | import javax.xml.bind.JAXBContext; |
aoqi@0 | 37 | import javax.xml.namespace.QName; |
aoqi@0 | 38 | import javax.xml.ws.Dispatch; |
aoqi@0 | 39 | import javax.xml.ws.EndpointReference; |
aoqi@0 | 40 | import javax.xml.ws.Service; |
aoqi@0 | 41 | import javax.xml.ws.Service.Mode; |
aoqi@0 | 42 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 43 | import javax.xml.ws.WebServiceFeature; |
aoqi@0 | 44 | import javax.xml.ws.spi.ServiceDelegate; |
aoqi@0 | 45 | import java.lang.reflect.Field; |
aoqi@0 | 46 | import java.net.URL; |
aoqi@0 | 47 | import java.security.AccessController; |
aoqi@0 | 48 | import java.security.PrivilegedAction; |
aoqi@0 | 49 | import java.util.HashSet; |
aoqi@0 | 50 | import java.util.Set; |
aoqi@0 | 51 | import java.util.concurrent.CopyOnWriteArraySet; |
aoqi@0 | 52 | |
aoqi@0 | 53 | /** |
aoqi@0 | 54 | * JAX-WS implementation of {@link ServiceDelegate}. |
aoqi@0 | 55 | * |
aoqi@0 | 56 | * <p> |
aoqi@0 | 57 | * This abstract class is used only to improve the static type safety |
aoqi@0 | 58 | * of the JAX-WS internal API. |
aoqi@0 | 59 | * |
aoqi@0 | 60 | * <p> |
aoqi@0 | 61 | * The class name intentionally doesn't include "Delegate", |
aoqi@0 | 62 | * because the fact that it's a delegate is a detail of |
aoqi@0 | 63 | * the JSR-224 API, and for the layers above us this object |
aoqi@0 | 64 | * nevertheless represents {@link Service}. We want them |
aoqi@0 | 65 | * to think of this as an internal representation of a service. |
aoqi@0 | 66 | * |
aoqi@0 | 67 | * <p> |
aoqi@0 | 68 | * Only JAX-WS internal code may downcast this to {@link WSServiceDelegate}. |
aoqi@0 | 69 | * |
aoqi@0 | 70 | * @author Kohsuke Kawaguchi |
aoqi@0 | 71 | */ |
aoqi@0 | 72 | public abstract class WSService extends ServiceDelegate implements ComponentRegistry { |
aoqi@0 | 73 | private final Set<Component> components = new CopyOnWriteArraySet<Component>(); |
aoqi@0 | 74 | |
aoqi@0 | 75 | protected WSService() { |
aoqi@0 | 76 | } |
aoqi@0 | 77 | |
aoqi@0 | 78 | /** |
aoqi@0 | 79 | * Works like {@link #getPort(EndpointReference, Class, WebServiceFeature...)} |
aoqi@0 | 80 | * but takes {@link WSEndpointReference}. |
aoqi@0 | 81 | */ |
aoqi@0 | 82 | public abstract <T> T getPort(WSEndpointReference epr, Class<T> portInterface, WebServiceFeature... features); |
aoqi@0 | 83 | |
aoqi@0 | 84 | /** |
aoqi@0 | 85 | * Works like {@link #createDispatch(javax.xml.ws.EndpointReference, java.lang.Class, javax.xml.ws.Service.Mode, javax.xml.ws.WebServiceFeature[])} |
aoqi@0 | 86 | * but it takes the port name separately, so that EPR without embedded metadata can be used. |
aoqi@0 | 87 | */ |
aoqi@0 | 88 | public abstract <T> Dispatch<T> createDispatch(QName portName, WSEndpointReference wsepr, Class<T> aClass, Service.Mode mode, WebServiceFeature... features); |
aoqi@0 | 89 | |
aoqi@0 | 90 | /** |
aoqi@0 | 91 | * Works like {@link #createDispatch(javax.xml.ws.EndpointReference, javax.xml.bind.JAXBContext, javax.xml.ws.Service.Mode, javax.xml.ws.WebServiceFeature[])} |
aoqi@0 | 92 | * but it takes the port name separately, so that EPR without embedded metadata can be used. |
aoqi@0 | 93 | */ |
aoqi@0 | 94 | public abstract Dispatch<Object> createDispatch(QName portName, WSEndpointReference wsepr, JAXBContext jaxbContext, Service.Mode mode, WebServiceFeature... features); |
aoqi@0 | 95 | |
aoqi@0 | 96 | /** |
aoqi@0 | 97 | * Gets the {@link Container} object. |
aoqi@0 | 98 | * |
aoqi@0 | 99 | * <p> |
aoqi@0 | 100 | * The components inside {@link WSEndpoint} uses this reference |
aoqi@0 | 101 | * to communicate with the hosting environment. |
aoqi@0 | 102 | * |
aoqi@0 | 103 | * @return |
aoqi@0 | 104 | * always same object. If no "real" {@link Container} instance |
aoqi@0 | 105 | * is given, {@link Container#NONE} will be returned. |
aoqi@0 | 106 | */ |
aoqi@0 | 107 | public abstract @NotNull Container getContainer(); |
aoqi@0 | 108 | |
aoqi@0 | 109 | public @Nullable <S> S getSPI(@NotNull Class<S> spiType) { |
aoqi@0 | 110 | for (Component c : components) { |
aoqi@0 | 111 | S s = c.getSPI(spiType); |
aoqi@0 | 112 | if (s != null) |
aoqi@0 | 113 | return s; |
aoqi@0 | 114 | } |
aoqi@0 | 115 | |
aoqi@0 | 116 | return getContainer().getSPI(spiType); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | public @NotNull Set<Component> getComponents() { |
aoqi@0 | 120 | return components; |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | /** |
aoqi@0 | 124 | * Create a <code>Service</code> instance. |
aoqi@0 | 125 | * |
aoqi@0 | 126 | * The specified WSDL document location and service qualified name MUST |
aoqi@0 | 127 | * uniquely identify a <code>wsdl:service</code> element. |
aoqi@0 | 128 | * |
aoqi@0 | 129 | * @param wsdlDocumentLocation URL for the WSDL document location |
aoqi@0 | 130 | * for the service |
aoqi@0 | 131 | * @param serviceName QName for the service |
aoqi@0 | 132 | * @throws WebServiceException If any error in creation of the |
aoqi@0 | 133 | * specified service. |
aoqi@0 | 134 | **/ |
aoqi@0 | 135 | public static WSService create( URL wsdlDocumentLocation, QName serviceName) { |
aoqi@0 | 136 | return new WSServiceDelegate(wsdlDocumentLocation,serviceName,Service.class); |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | /** |
aoqi@0 | 140 | * Create a <code>Service</code> instance. |
aoqi@0 | 141 | * |
aoqi@0 | 142 | * @param serviceName QName for the service |
aoqi@0 | 143 | * @throws WebServiceException If any error in creation of the |
aoqi@0 | 144 | * specified service |
aoqi@0 | 145 | */ |
aoqi@0 | 146 | public static WSService create(QName serviceName) { |
aoqi@0 | 147 | return create(null,serviceName); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | /** |
aoqi@0 | 151 | * Creates a service with a dummy service name. |
aoqi@0 | 152 | */ |
aoqi@0 | 153 | public static WSService create() { |
aoqi@0 | 154 | return create(null,new QName(WSService.class.getName(),"dummy")); |
aoqi@0 | 155 | } |
aoqi@0 | 156 | |
aoqi@0 | 157 | /** |
aoqi@0 | 158 | * Typed parameter bag used by {@link WSService#create(URL, QName, InitParams)} |
aoqi@0 | 159 | * |
aoqi@0 | 160 | * @since 2.1.3 |
aoqi@0 | 161 | */ |
aoqi@0 | 162 | public static final class InitParams { |
aoqi@0 | 163 | private Container container; |
aoqi@0 | 164 | /** |
aoqi@0 | 165 | * Sets the {@link Container} object used by the created service. |
aoqi@0 | 166 | * This allows the client to use a specific {@link Container} instance |
aoqi@0 | 167 | * as opposed to the one obtained by {@link ContainerResolver}. |
aoqi@0 | 168 | */ |
aoqi@0 | 169 | public void setContainer(Container c) { |
aoqi@0 | 170 | this.container = c; |
aoqi@0 | 171 | } |
aoqi@0 | 172 | public Container getContainer() { |
aoqi@0 | 173 | return container; |
aoqi@0 | 174 | } |
aoqi@0 | 175 | } |
aoqi@0 | 176 | |
aoqi@0 | 177 | /** |
aoqi@0 | 178 | * To create a {@link Service}, we need to go through the API that doesn't let us |
aoqi@0 | 179 | * pass parameters, so as a hack we use thread local. |
aoqi@0 | 180 | */ |
aoqi@0 | 181 | protected static final ThreadLocal<InitParams> INIT_PARAMS = new ThreadLocal<InitParams>(); |
aoqi@0 | 182 | |
aoqi@0 | 183 | /** |
aoqi@0 | 184 | * Used as a immutable constant so that we can avoid null check. |
aoqi@0 | 185 | */ |
aoqi@0 | 186 | protected static final InitParams EMPTY_PARAMS = new InitParams(); |
aoqi@0 | 187 | |
aoqi@0 | 188 | /** |
aoqi@0 | 189 | * Creates a {@link Service} instance. |
aoqi@0 | 190 | * |
aoqi@0 | 191 | * <p> |
aoqi@0 | 192 | * This method works really like {@link Service#create(URL, QName)} |
aoqi@0 | 193 | * except it takes one more RI specific parameter. |
aoqi@0 | 194 | * |
aoqi@0 | 195 | * @param wsdlDocumentLocation |
aoqi@0 | 196 | * {@code URL} for the WSDL document location for the service. |
aoqi@0 | 197 | * Can be null, in which case WSDL is not loaded. |
aoqi@0 | 198 | * @param serviceName |
aoqi@0 | 199 | * {@code QName} for the service. |
aoqi@0 | 200 | * @param properties |
aoqi@0 | 201 | * Additional RI specific initialization parameters. Can be null. |
aoqi@0 | 202 | * @throws WebServiceException |
aoqi@0 | 203 | * If any error in creation of the specified service. |
aoqi@0 | 204 | **/ |
aoqi@0 | 205 | public static Service create( URL wsdlDocumentLocation, QName serviceName, InitParams properties) { |
aoqi@0 | 206 | if(INIT_PARAMS.get()!=null) |
aoqi@0 | 207 | throw new IllegalStateException("someone left non-null InitParams"); |
aoqi@0 | 208 | INIT_PARAMS.set(properties); |
aoqi@0 | 209 | try { |
aoqi@0 | 210 | Service svc = Service.create(wsdlDocumentLocation, serviceName); |
aoqi@0 | 211 | if(INIT_PARAMS.get()!=null) |
aoqi@0 | 212 | throw new IllegalStateException("Service "+svc+" didn't recognize InitParams"); |
aoqi@0 | 213 | return svc; |
aoqi@0 | 214 | } finally { |
aoqi@0 | 215 | // even in case of an exception still reset INIT_PARAMS |
aoqi@0 | 216 | INIT_PARAMS.set(null); |
aoqi@0 | 217 | } |
aoqi@0 | 218 | } |
aoqi@0 | 219 | |
aoqi@0 | 220 | /** |
aoqi@0 | 221 | * Obtains the {@link WSService} that's encapsulated inside a {@link Service}. |
aoqi@0 | 222 | * |
aoqi@0 | 223 | * @throws IllegalArgumentException |
aoqi@0 | 224 | * if the given service object is not from the JAX-WS RI. |
aoqi@0 | 225 | */ |
aoqi@0 | 226 | public static WSService unwrap(final Service svc) { |
aoqi@0 | 227 | return AccessController.doPrivileged(new PrivilegedAction<WSService>() { |
aoqi@0 | 228 | public WSService run() { |
aoqi@0 | 229 | try { |
aoqi@0 | 230 | Field f = svc.getClass().getField("delegate"); |
aoqi@0 | 231 | f.setAccessible(true); |
aoqi@0 | 232 | Object delegate = f.get(svc); |
aoqi@0 | 233 | if(!(delegate instanceof WSService)) |
aoqi@0 | 234 | throw new IllegalArgumentException(); |
aoqi@0 | 235 | return (WSService) delegate; |
aoqi@0 | 236 | } catch (NoSuchFieldException e) { |
aoqi@0 | 237 | AssertionError x = new AssertionError("Unexpected service API implementation"); |
aoqi@0 | 238 | x.initCause(e); |
aoqi@0 | 239 | throw x; |
aoqi@0 | 240 | } catch (IllegalAccessException e) { |
aoqi@0 | 241 | IllegalAccessError x = new IllegalAccessError(e.getMessage()); |
aoqi@0 | 242 | x.initCause(e); |
aoqi@0 | 243 | throw x; |
aoqi@0 | 244 | } |
aoqi@0 | 245 | } |
aoqi@0 | 246 | }); |
aoqi@0 | 247 | } |
aoqi@0 | 248 | } |