src/share/jaxws_classes/com/sun/xml/internal/ws/server/provider/AsyncProviderInvokerTube.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/server/provider/AsyncProviderInvokerTube.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,184 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.server.provider;
    1.30 +
    1.31 +import com.sun.istack.internal.NotNull;
    1.32 +import com.sun.istack.internal.Nullable;
    1.33 +import com.sun.xml.internal.ws.api.message.Packet;
    1.34 +import com.sun.xml.internal.ws.api.pipe.Fiber;
    1.35 +import com.sun.xml.internal.ws.api.pipe.NextAction;
    1.36 +import com.sun.xml.internal.ws.api.pipe.ThrowableContainerPropertySet;
    1.37 +import com.sun.xml.internal.ws.api.pipe.Tube;
    1.38 +import com.sun.xml.internal.ws.api.server.AsyncProvider;
    1.39 +import com.sun.xml.internal.ws.api.server.AsyncProviderCallback;
    1.40 +import com.sun.xml.internal.ws.api.server.Invoker;
    1.41 +import com.sun.xml.internal.ws.api.server.WSEndpoint;
    1.42 +import com.sun.xml.internal.ws.server.AbstractWebServiceContext;
    1.43 +
    1.44 +import java.util.logging.Level;
    1.45 +import java.util.logging.Logger;
    1.46 +
    1.47 +/**
    1.48 + * This {@link Tube} is used to invoke the {@link AsyncProvider} endpoints.
    1.49 + *
    1.50 + * @author Jitendra Kotamraju
    1.51 + */
    1.52 +public // TODO needed by factory
    1.53 +class AsyncProviderInvokerTube<T> extends ProviderInvokerTube<T> {
    1.54 +
    1.55 +    private static final Logger LOGGER = Logger.getLogger(
    1.56 +        com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.AsyncProviderInvokerTube");
    1.57 +
    1.58 +    public AsyncProviderInvokerTube(Invoker invoker, ProviderArgumentsBuilder<T> argsBuilder) {
    1.59 +        super(invoker, argsBuilder);
    1.60 +    }
    1.61 +
    1.62 +   /*
    1.63 +    * This binds the parameter for Provider endpoints and invokes the
    1.64 +    * invoke() method of {@linke Provider} endpoint. The return value from
    1.65 +    * invoke() is used to create a new {@link Message} that traverses
    1.66 +    * through the Pipeline to transport.
    1.67 +    */
    1.68 +    public @NotNull NextAction processRequest(@NotNull Packet request) {
    1.69 +        T param = argsBuilder.getParameter(request);
    1.70 +        NoSuspendResumer resumer = new NoSuspendResumer();
    1.71 +        @SuppressWarnings({ "rawtypes", "unchecked" })
    1.72 +                AsyncProviderCallbackImpl callback = new AsyncProviderInvokerTube.AsyncProviderCallbackImpl(request, resumer);
    1.73 +        AsyncWebServiceContext ctxt = new AsyncWebServiceContext(getEndpoint(),request);
    1.74 +
    1.75 +        AsyncProviderInvokerTube.LOGGER.fine("Invoking AsyncProvider Endpoint");
    1.76 +        try {
    1.77 +            getInvoker(request).invokeAsyncProvider(request, param, callback, ctxt);
    1.78 +        } catch(Throwable e) {
    1.79 +            LOGGER.log(Level.SEVERE, e.getMessage(), e);
    1.80 +            return doThrow(e);
    1.81 +        }
    1.82 +
    1.83 +        synchronized(callback) {
    1.84 +                if (resumer.response != null) {
    1.85 +                // Only used by AsyncProvider<Packet>
    1.86 +                // Implementation may pass Packet containing throwable; use both
    1.87 +                    ThrowableContainerPropertySet tc = resumer.response.getSatellite(ThrowableContainerPropertySet.class);
    1.88 +                    Throwable t = (tc != null) ? tc.getThrowable() : null;
    1.89 +
    1.90 +                        return t != null ? doThrow(resumer.response, t) : doReturnWith(resumer.response);
    1.91 +                }
    1.92 +
    1.93 +                // Suspend the Fiber. AsyncProviderCallback will resume the Fiber after
    1.94 +                // it receives response.
    1.95 +                callback.resumer = new FiberResumer();
    1.96 +                return doSuspend();
    1.97 +        }
    1.98 +    }
    1.99 +
   1.100 +    private interface Resumer {
   1.101 +        public void onResume(Packet response);
   1.102 +    }
   1.103 +
   1.104 +    /*private*/ public class FiberResumer implements Resumer { // TODO public for DISI
   1.105 +        private final Fiber fiber;
   1.106 +
   1.107 +        public FiberResumer() {
   1.108 +            this.fiber = Fiber.current();
   1.109 +        }
   1.110 +
   1.111 +        public void onResume(Packet response) {
   1.112 +            // Only used by AsyncProvider<Packet>
   1.113 +            // Implementation may pass Packet containing throwable; use both
   1.114 +            ThrowableContainerPropertySet tc = response.getSatellite(ThrowableContainerPropertySet.class);
   1.115 +            Throwable t = (tc != null) ? tc.getThrowable() : null;
   1.116 +                fiber.resume(t, response);
   1.117 +        }
   1.118 +    }
   1.119 +
   1.120 +    private class NoSuspendResumer implements Resumer {
   1.121 +        protected Packet response = null;
   1.122 +
   1.123 +                public void onResume(Packet response) {
   1.124 +                        this.response = response;
   1.125 +                }
   1.126 +    }
   1.127 +
   1.128 +    /*private*/ public class AsyncProviderCallbackImpl implements AsyncProviderCallback<T> { // TODO public for DISI
   1.129 +        private final Packet request;
   1.130 +        private Resumer resumer;
   1.131 +
   1.132 +        public AsyncProviderCallbackImpl(Packet request, Resumer resumer) {
   1.133 +            this.request = request;
   1.134 +            this.resumer = resumer;
   1.135 +        }
   1.136 +
   1.137 +        public void send(@Nullable T param) {
   1.138 +            if (param == null) {
   1.139 +                if (request.transportBackChannel != null) {
   1.140 +                    request.transportBackChannel.close();
   1.141 +                }
   1.142 +            }
   1.143 +            Packet packet = argsBuilder.getResponse(request, param, getEndpoint().getPort(), getEndpoint().getBinding());
   1.144 +            synchronized(this) {
   1.145 +                resumer.onResume(packet);
   1.146 +            }
   1.147 +        }
   1.148 +
   1.149 +        public void sendError(@NotNull Throwable t) {
   1.150 +            Exception e;
   1.151 +            if (t instanceof Exception) {
   1.152 +                e = (Exception) t;
   1.153 +            } else {
   1.154 +                e = new RuntimeException(t);
   1.155 +            }
   1.156 +            Packet packet = argsBuilder.getResponse(request, e, getEndpoint().getPort(), getEndpoint().getBinding());
   1.157 +            synchronized(this) {
   1.158 +                resumer.onResume(packet);
   1.159 +            }
   1.160 +        }
   1.161 +    }
   1.162 +
   1.163 +    /**
   1.164 +     * The single {@link javax.xml.ws.WebServiceContext} instance injected into application.
   1.165 +     */
   1.166 +    /*private static final*/ public class AsyncWebServiceContext extends AbstractWebServiceContext { // TODO public for DISI
   1.167 +        final Packet packet;
   1.168 +
   1.169 +        public AsyncWebServiceContext(WSEndpoint endpoint, Packet packet) { // TODO public for DISI
   1.170 +            super(endpoint);
   1.171 +            this.packet = packet;
   1.172 +        }
   1.173 +
   1.174 +        public @NotNull Packet getRequestPacket() {
   1.175 +            return packet;
   1.176 +        }
   1.177 +    }
   1.178 +
   1.179 +    public @NotNull NextAction processResponse(@NotNull Packet response) {
   1.180 +        return doReturnWith(response);
   1.181 +    }
   1.182 +
   1.183 +    public @NotNull NextAction processException(@NotNull Throwable t) {
   1.184 +        return doThrow(t);
   1.185 +    }
   1.186 +
   1.187 +}

mercurial