src/share/jaxws_classes/com/sun/xml/internal/ws/api/WSService.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial