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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 418
43240b8b995b
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, 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.server;
aoqi@0 27
aoqi@0 28 import com.sun.istack.internal.NotNull;
aoqi@0 29 import com.sun.xml.internal.ws.api.message.Message;
aoqi@0 30 import com.sun.xml.internal.ws.api.message.Packet;
aoqi@0 31 import com.sun.xml.internal.ws.resources.ServerMessages;
aoqi@0 32 import com.sun.xml.internal.ws.resources.WsservletMessages;
aoqi@0 33 import com.sun.xml.internal.ws.server.ServerRtException;
aoqi@0 34 import com.sun.xml.internal.ws.server.SingletonResolver;
aoqi@0 35
aoqi@0 36 import javax.xml.ws.Provider;
aoqi@0 37 import javax.xml.ws.WebServiceContext;
aoqi@0 38 import javax.xml.ws.WebServiceException;
aoqi@0 39 import java.lang.annotation.Annotation;
aoqi@0 40 import java.lang.reflect.InvocationTargetException;
aoqi@0 41 import java.lang.reflect.Method;
aoqi@0 42 import java.util.logging.Level;
aoqi@0 43 import java.util.logging.Logger;
aoqi@0 44
aoqi@0 45 /**
aoqi@0 46 * Determines the instance that serves
aoqi@0 47 * the given request packet.
aoqi@0 48 *
aoqi@0 49 * <p>
aoqi@0 50 * The JAX-WS spec always use a singleton instance
aoqi@0 51 * to serve all the requests, but this hook provides
aoqi@0 52 * a convenient way to route messages to a proper receiver.
aoqi@0 53 *
aoqi@0 54 * <p>
aoqi@0 55 * Externally, an instance of {@link InstanceResolver} is
aoqi@0 56 * associated with {@link WSEndpoint}.
aoqi@0 57 *
aoqi@0 58 * <h2>Possible Uses</h2>
aoqi@0 59 * <p>
aoqi@0 60 * One can use WS-Addressing message properties to
aoqi@0 61 * decide which instance to deliver a message. This
aoqi@0 62 * would be an important building block for a stateful
aoqi@0 63 * web services.
aoqi@0 64 *
aoqi@0 65 * <p>
aoqi@0 66 * One can associate an instance of a service
aoqi@0 67 * with a specific WS-RM session.
aoqi@0 68 *
aoqi@0 69 * @author Kohsuke Kawaguchi
aoqi@0 70 */
aoqi@0 71 public abstract class InstanceResolver<T> {
aoqi@0 72 /**
aoqi@0 73 * Decides which instance of 'T' serves the given request message.
aoqi@0 74 *
aoqi@0 75 * <p>
aoqi@0 76 * This method is called concurrently by multiple threads.
aoqi@0 77 * It is also on a criticail path that affects the performance.
aoqi@0 78 * A good implementation should try to avoid any synchronization,
aoqi@0 79 * and should minimize the amount of work as much as possible.
aoqi@0 80 *
aoqi@0 81 * @param request
aoqi@0 82 * Always non-null. Represents the request message to be served.
aoqi@0 83 * The caller may not consume the {@link Message}.
aoqi@0 84 */
aoqi@0 85 public abstract @NotNull T resolve(@NotNull Packet request);
aoqi@0 86
aoqi@0 87 /**
aoqi@0 88 * Called by the default {@link Invoker} after the method call is done.
aoqi@0 89 * This gives {@link InstanceResolver} a chance to do clean up.
aoqi@0 90 *
aoqi@0 91 * <p>
aoqi@0 92 * Alternatively, one could override {@link #createInvoker()} to
aoqi@0 93 * create a custom invoker to do this in more flexible way.
aoqi@0 94 *
aoqi@0 95 * <p>
aoqi@0 96 * The default implementation is a no-op.
aoqi@0 97 *
aoqi@0 98 * @param request
aoqi@0 99 * The same request packet given to {@link #resolve(Packet)} method.
aoqi@0 100 * @param servant
aoqi@0 101 * The object returned from the {@link #resolve(Packet)} method.
aoqi@0 102 * @since 2.1.2
aoqi@0 103 */
aoqi@0 104 public void postInvoke(@NotNull Packet request, @NotNull T servant) {
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 /**
aoqi@0 108 * Called by {@link WSEndpoint} when it's set up.
aoqi@0 109 *
aoqi@0 110 * <p>
aoqi@0 111 * This is an opportunity for {@link InstanceResolver}
aoqi@0 112 * to do a endpoint-specific initialization process.
aoqi@0 113 *
aoqi@0 114 * @param wsc
aoqi@0 115 * The {@link WebServiceContext} instance to be injected
aoqi@0 116 * to the user instances (assuming {@link InstanceResolver}
aoqi@0 117 */
aoqi@0 118 public void start(@NotNull WSWebServiceContext wsc, @NotNull WSEndpoint endpoint) {
aoqi@0 119 // backward compatibility
aoqi@0 120 start(wsc);
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 /**
aoqi@0 124 * @deprecated
aoqi@0 125 * Use {@link #start(WSWebServiceContext,WSEndpoint)}.
aoqi@0 126 */
aoqi@0 127 public void start(@NotNull WebServiceContext wsc) {}
aoqi@0 128
aoqi@0 129 /**
aoqi@0 130 * Called by {@link WSEndpoint}
aoqi@0 131 * when {@link WSEndpoint#dispose()} is called.
aoqi@0 132 *
aoqi@0 133 * This allows {@link InstanceResolver} to do final clean up.
aoqi@0 134 *
aoqi@0 135 * <p>
aoqi@0 136 * This method is guaranteed to be only called once by {@link WSEndpoint}.
aoqi@0 137 */
aoqi@0 138 public void dispose() {}
aoqi@0 139
aoqi@0 140
aoqi@0 141 /**
aoqi@0 142 * Creates a {@link InstanceResolver} implementation that always
aoqi@0 143 * returns the specified singleton instance.
aoqi@0 144 */
aoqi@0 145 public static <T> InstanceResolver<T> createSingleton(T singleton) {
aoqi@0 146 assert singleton!=null;
aoqi@0 147 InstanceResolver ir = createFromInstanceResolverAnnotation(singleton.getClass());
aoqi@0 148 if(ir==null)
aoqi@0 149 ir = new SingletonResolver<T>(singleton);
aoqi@0 150 return ir;
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 /**
aoqi@0 154 * @deprecated
aoqi@0 155 * This is added here because a Glassfish integration happened
aoqi@0 156 * with this signature. Please do not use this. Will be removed
aoqi@0 157 * after the next GF integration.
aoqi@0 158 */
aoqi@0 159 public static <T> InstanceResolver<T> createDefault(@NotNull Class<T> clazz, boolean bool) {
aoqi@0 160 return createDefault(clazz);
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 /**
aoqi@0 164 * Creates a default {@link InstanceResolver} that serves the given class.
aoqi@0 165 */
aoqi@0 166 public static <T> InstanceResolver<T> createDefault(@NotNull Class<T> clazz) {
aoqi@0 167 InstanceResolver<T> ir = createFromInstanceResolverAnnotation(clazz);
aoqi@0 168 if(ir==null)
aoqi@0 169 ir = new SingletonResolver<T>(createNewInstance(clazz));
aoqi@0 170 return ir;
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 /**
aoqi@0 174 * Checks for {@link InstanceResolverAnnotation} and creates an instance resolver from it if any.
aoqi@0 175 * Otherwise null.
aoqi@0 176 */
aoqi@0 177 public static <T> InstanceResolver<T> createFromInstanceResolverAnnotation(@NotNull Class<T> clazz) {
aoqi@0 178 for( Annotation a : clazz.getAnnotations() ) {
aoqi@0 179 InstanceResolverAnnotation ira = a.annotationType().getAnnotation(InstanceResolverAnnotation.class);
aoqi@0 180 if(ira==null) continue;
aoqi@0 181 Class<? extends InstanceResolver> ir = ira.value();
aoqi@0 182 try {
aoqi@0 183 return ir.getConstructor(Class.class).newInstance(clazz);
aoqi@0 184 } catch (InstantiationException e) {
aoqi@0 185 throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
aoqi@0 186 ir.getName(),a.annotationType(),clazz.getName()));
aoqi@0 187 } catch (IllegalAccessException e) {
aoqi@0 188 throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
aoqi@0 189 ir.getName(),a.annotationType(),clazz.getName()));
aoqi@0 190 } catch (InvocationTargetException e) {
aoqi@0 191 throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
aoqi@0 192 ir.getName(),a.annotationType(),clazz.getName()));
aoqi@0 193 } catch (NoSuchMethodException e) {
aoqi@0 194 throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER(
aoqi@0 195 ir.getName(),a.annotationType(),clazz.getName()));
aoqi@0 196 }
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 return null;
aoqi@0 200 }
aoqi@0 201
aoqi@0 202 protected static <T> T createNewInstance(Class<T> cl) {
aoqi@0 203 try {
aoqi@0 204 return cl.newInstance();
aoqi@0 205 } catch (InstantiationException e) {
aoqi@0 206 logger.log(Level.SEVERE, e.getMessage(), e);
aoqi@0 207 throw new ServerRtException(
aoqi@0 208 WsservletMessages.ERROR_IMPLEMENTOR_FACTORY_NEW_INSTANCE_FAILED(cl));
aoqi@0 209 } catch (IllegalAccessException e) {
aoqi@0 210 logger.log(Level.SEVERE, e.getMessage(), e);
aoqi@0 211 throw new ServerRtException(
aoqi@0 212 WsservletMessages.ERROR_IMPLEMENTOR_FACTORY_NEW_INSTANCE_FAILED(cl));
aoqi@0 213 }
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 /**
aoqi@0 217 * Wraps this {@link InstanceResolver} into an {@link Invoker}.
aoqi@0 218 */
aoqi@0 219 public @NotNull Invoker createInvoker() {
aoqi@0 220 return new Invoker() {
aoqi@0 221 @Override
aoqi@0 222 public void start(@NotNull WSWebServiceContext wsc, @NotNull WSEndpoint endpoint) {
aoqi@0 223 InstanceResolver.this.start(wsc,endpoint);
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 @Override
aoqi@0 227 public void dispose() {
aoqi@0 228 InstanceResolver.this.dispose();
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 @Override
aoqi@0 232 public Object invoke(Packet p, Method m, Object... args) throws InvocationTargetException, IllegalAccessException {
aoqi@0 233 T t = resolve(p);
aoqi@0 234 try {
aoqi@0 235 return MethodUtil.invoke(t, m, args );
aoqi@0 236 } finally {
aoqi@0 237 postInvoke(p,t);
aoqi@0 238 }
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 @Override
aoqi@0 242 public <U> U invokeProvider(@NotNull Packet p, U arg) {
aoqi@0 243 T t = resolve(p);
aoqi@0 244 try {
aoqi@0 245 return ((Provider<U>) t).invoke(arg);
aoqi@0 246 } finally {
aoqi@0 247 postInvoke(p,t);
aoqi@0 248 }
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 public String toString() {
aoqi@0 252 return "Default Invoker over "+InstanceResolver.this.toString();
aoqi@0 253 }
aoqi@0 254 };
aoqi@0 255 }
aoqi@0 256
aoqi@0 257 private static final Logger logger =
aoqi@0 258 Logger.getLogger(
aoqi@0 259 com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server");
aoqi@0 260 }

mercurial