aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.ws.api.server; aoqi@0: aoqi@0: import com.sun.istack.internal.NotNull; aoqi@0: import com.sun.xml.internal.ws.api.message.Message; aoqi@0: import com.sun.xml.internal.ws.api.message.Packet; aoqi@0: import com.sun.xml.internal.ws.resources.ServerMessages; aoqi@0: import com.sun.xml.internal.ws.resources.WsservletMessages; aoqi@0: import com.sun.xml.internal.ws.server.ServerRtException; aoqi@0: import com.sun.xml.internal.ws.server.SingletonResolver; aoqi@0: aoqi@0: import javax.xml.ws.Provider; aoqi@0: import javax.xml.ws.WebServiceContext; aoqi@0: import javax.xml.ws.WebServiceException; aoqi@0: import java.lang.annotation.Annotation; aoqi@0: import java.lang.reflect.InvocationTargetException; aoqi@0: import java.lang.reflect.Method; aoqi@0: import java.util.logging.Level; aoqi@0: import java.util.logging.Logger; aoqi@0: aoqi@0: /** aoqi@0: * Determines the instance that serves aoqi@0: * the given request packet. aoqi@0: * aoqi@0: *

aoqi@0: * The JAX-WS spec always use a singleton instance aoqi@0: * to serve all the requests, but this hook provides aoqi@0: * a convenient way to route messages to a proper receiver. aoqi@0: * aoqi@0: *

aoqi@0: * Externally, an instance of {@link InstanceResolver} is aoqi@0: * associated with {@link WSEndpoint}. aoqi@0: * aoqi@0: *

Possible Uses

aoqi@0: *

aoqi@0: * One can use WS-Addressing message properties to aoqi@0: * decide which instance to deliver a message. This aoqi@0: * would be an important building block for a stateful aoqi@0: * web services. aoqi@0: * aoqi@0: *

aoqi@0: * One can associate an instance of a service aoqi@0: * with a specific WS-RM session. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: public abstract class InstanceResolver { aoqi@0: /** aoqi@0: * Decides which instance of 'T' serves the given request message. aoqi@0: * aoqi@0: *

aoqi@0: * This method is called concurrently by multiple threads. aoqi@0: * It is also on a criticail path that affects the performance. aoqi@0: * A good implementation should try to avoid any synchronization, aoqi@0: * and should minimize the amount of work as much as possible. aoqi@0: * aoqi@0: * @param request aoqi@0: * Always non-null. Represents the request message to be served. aoqi@0: * The caller may not consume the {@link Message}. aoqi@0: */ aoqi@0: public abstract @NotNull T resolve(@NotNull Packet request); aoqi@0: aoqi@0: /** aoqi@0: * Called by the default {@link Invoker} after the method call is done. aoqi@0: * This gives {@link InstanceResolver} a chance to do clean up. aoqi@0: * aoqi@0: *

aoqi@0: * Alternatively, one could override {@link #createInvoker()} to aoqi@0: * create a custom invoker to do this in more flexible way. aoqi@0: * aoqi@0: *

aoqi@0: * The default implementation is a no-op. aoqi@0: * aoqi@0: * @param request aoqi@0: * The same request packet given to {@link #resolve(Packet)} method. aoqi@0: * @param servant aoqi@0: * The object returned from the {@link #resolve(Packet)} method. aoqi@0: * @since 2.1.2 aoqi@0: */ aoqi@0: public void postInvoke(@NotNull Packet request, @NotNull T servant) { aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Called by {@link WSEndpoint} when it's set up. aoqi@0: * aoqi@0: *

aoqi@0: * This is an opportunity for {@link InstanceResolver} aoqi@0: * to do a endpoint-specific initialization process. aoqi@0: * aoqi@0: * @param wsc aoqi@0: * The {@link WebServiceContext} instance to be injected aoqi@0: * to the user instances (assuming {@link InstanceResolver} aoqi@0: */ aoqi@0: public void start(@NotNull WSWebServiceContext wsc, @NotNull WSEndpoint endpoint) { aoqi@0: // backward compatibility aoqi@0: start(wsc); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * @deprecated aoqi@0: * Use {@link #start(WSWebServiceContext,WSEndpoint)}. aoqi@0: */ aoqi@0: public void start(@NotNull WebServiceContext wsc) {} aoqi@0: aoqi@0: /** aoqi@0: * Called by {@link WSEndpoint} aoqi@0: * when {@link WSEndpoint#dispose()} is called. aoqi@0: * aoqi@0: * This allows {@link InstanceResolver} to do final clean up. aoqi@0: * aoqi@0: *

aoqi@0: * This method is guaranteed to be only called once by {@link WSEndpoint}. aoqi@0: */ aoqi@0: public void dispose() {} aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Creates a {@link InstanceResolver} implementation that always aoqi@0: * returns the specified singleton instance. aoqi@0: */ aoqi@0: public static InstanceResolver createSingleton(T singleton) { aoqi@0: assert singleton!=null; aoqi@0: InstanceResolver ir = createFromInstanceResolverAnnotation(singleton.getClass()); aoqi@0: if(ir==null) aoqi@0: ir = new SingletonResolver(singleton); aoqi@0: return ir; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * @deprecated aoqi@0: * This is added here because a Glassfish integration happened aoqi@0: * with this signature. Please do not use this. Will be removed aoqi@0: * after the next GF integration. aoqi@0: */ aoqi@0: public static InstanceResolver createDefault(@NotNull Class clazz, boolean bool) { aoqi@0: return createDefault(clazz); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates a default {@link InstanceResolver} that serves the given class. aoqi@0: */ aoqi@0: public static InstanceResolver createDefault(@NotNull Class clazz) { aoqi@0: InstanceResolver ir = createFromInstanceResolverAnnotation(clazz); aoqi@0: if(ir==null) aoqi@0: ir = new SingletonResolver(createNewInstance(clazz)); aoqi@0: return ir; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Checks for {@link InstanceResolverAnnotation} and creates an instance resolver from it if any. aoqi@0: * Otherwise null. aoqi@0: */ aoqi@0: public static InstanceResolver createFromInstanceResolverAnnotation(@NotNull Class clazz) { aoqi@0: for( Annotation a : clazz.getAnnotations() ) { aoqi@0: InstanceResolverAnnotation ira = a.annotationType().getAnnotation(InstanceResolverAnnotation.class); aoqi@0: if(ira==null) continue; aoqi@0: Class ir = ira.value(); aoqi@0: try { aoqi@0: return ir.getConstructor(Class.class).newInstance(clazz); aoqi@0: } catch (InstantiationException e) { aoqi@0: throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER( aoqi@0: ir.getName(),a.annotationType(),clazz.getName())); aoqi@0: } catch (IllegalAccessException e) { aoqi@0: throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER( aoqi@0: ir.getName(),a.annotationType(),clazz.getName())); aoqi@0: } catch (InvocationTargetException e) { aoqi@0: throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER( aoqi@0: ir.getName(),a.annotationType(),clazz.getName())); aoqi@0: } catch (NoSuchMethodException e) { aoqi@0: throw new WebServiceException(ServerMessages.FAILED_TO_INSTANTIATE_INSTANCE_RESOLVER( aoqi@0: ir.getName(),a.annotationType(),clazz.getName())); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return null; aoqi@0: } aoqi@0: aoqi@0: protected static T createNewInstance(Class cl) { aoqi@0: try { aoqi@0: return cl.newInstance(); aoqi@0: } catch (InstantiationException e) { aoqi@0: logger.log(Level.SEVERE, e.getMessage(), e); aoqi@0: throw new ServerRtException( aoqi@0: WsservletMessages.ERROR_IMPLEMENTOR_FACTORY_NEW_INSTANCE_FAILED(cl)); aoqi@0: } catch (IllegalAccessException e) { aoqi@0: logger.log(Level.SEVERE, e.getMessage(), e); aoqi@0: throw new ServerRtException( aoqi@0: WsservletMessages.ERROR_IMPLEMENTOR_FACTORY_NEW_INSTANCE_FAILED(cl)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Wraps this {@link InstanceResolver} into an {@link Invoker}. aoqi@0: */ aoqi@0: public @NotNull Invoker createInvoker() { aoqi@0: return new Invoker() { aoqi@0: @Override aoqi@0: public void start(@NotNull WSWebServiceContext wsc, @NotNull WSEndpoint endpoint) { aoqi@0: InstanceResolver.this.start(wsc,endpoint); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void dispose() { aoqi@0: InstanceResolver.this.dispose(); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Object invoke(Packet p, Method m, Object... args) throws InvocationTargetException, IllegalAccessException { aoqi@0: T t = resolve(p); aoqi@0: try { aoqi@0: return MethodUtil.invoke(t, m, args ); aoqi@0: } finally { aoqi@0: postInvoke(p,t); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public U invokeProvider(@NotNull Packet p, U arg) { aoqi@0: T t = resolve(p); aoqi@0: try { aoqi@0: return ((Provider) t).invoke(arg); aoqi@0: } finally { aoqi@0: postInvoke(p,t); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public String toString() { aoqi@0: return "Default Invoker over "+InstanceResolver.this.toString(); aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: aoqi@0: private static final Logger logger = aoqi@0: Logger.getLogger( aoqi@0: com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server"); aoqi@0: }