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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     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	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,172 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2010, 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.Tube;
    1.37 +import com.sun.xml.internal.ws.api.server.AsyncProvider;
    1.38 +import com.sun.xml.internal.ws.api.server.AsyncProviderCallback;
    1.39 +import com.sun.xml.internal.ws.api.server.Invoker;
    1.40 +import com.sun.xml.internal.ws.api.server.WSEndpoint;
    1.41 +import com.sun.xml.internal.ws.server.AbstractWebServiceContext;
    1.42 +
    1.43 +import java.util.logging.Level;
    1.44 +import java.util.logging.Logger;
    1.45 +
    1.46 +/**
    1.47 + * This {@link Tube} is used to invoke the {@link AsyncProvider} endpoints.
    1.48 + *
    1.49 + * @author Jitendra Kotamraju
    1.50 + */
    1.51 +class AsyncProviderInvokerTube<T> extends ProviderInvokerTube<T> {
    1.52 +
    1.53 +    private static final Logger LOGGER = Logger.getLogger(
    1.54 +        com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.AsyncProviderInvokerTube");
    1.55 +
    1.56 +    public AsyncProviderInvokerTube(Invoker invoker, ProviderArgumentsBuilder<T> argsBuilder) {
    1.57 +        super(invoker, argsBuilder);
    1.58 +    }
    1.59 +
    1.60 +   /*
    1.61 +    * This binds the parameter for Provider endpoints and invokes the
    1.62 +    * invoke() method of {@linke Provider} endpoint. The return value from
    1.63 +    * invoke() is used to create a new {@link Message} that traverses
    1.64 +    * through the Pipeline to transport.
    1.65 +    */
    1.66 +    public @NotNull NextAction processRequest(@NotNull Packet request) {
    1.67 +        T param = argsBuilder.getParameter(request);
    1.68 +        NoSuspendResumer resumer = new NoSuspendResumer();
    1.69 +        @SuppressWarnings({ "rawtypes", "unchecked" })
    1.70 +                AsyncProviderCallbackImpl callback = new AsyncProviderInvokerTube.AsyncProviderCallbackImpl(request, resumer);
    1.71 +        AsyncWebServiceContext ctxt = new AsyncWebServiceContext(getEndpoint(),request);
    1.72 +
    1.73 +        AsyncProviderInvokerTube.LOGGER.fine("Invoking AsyncProvider Endpoint");
    1.74 +        try {
    1.75 +            getInvoker(request).invokeAsyncProvider(request, param, callback, ctxt);
    1.76 +        } catch(Throwable e) {
    1.77 +            LOGGER.log(Level.SEVERE, e.getMessage(), e);
    1.78 +            return doThrow(e);
    1.79 +        }
    1.80 +
    1.81 +        synchronized(callback) {
    1.82 +                if (resumer.response != null)
    1.83 +                        return doReturnWith(resumer.response);
    1.84 +
    1.85 +                // Suspend the Fiber. AsyncProviderCallback will resume the Fiber after
    1.86 +                // it receives response.
    1.87 +                callback.resumer = new FiberResumer();
    1.88 +                return doSuspend();
    1.89 +        }
    1.90 +    }
    1.91 +
    1.92 +    private interface Resumer {
    1.93 +        public void onResume(Packet response);
    1.94 +    }
    1.95 +
    1.96 +    private class FiberResumer implements Resumer {
    1.97 +        private final Fiber fiber;
    1.98 +
    1.99 +        public FiberResumer() {
   1.100 +            this.fiber = Fiber.current();
   1.101 +        }
   1.102 +
   1.103 +        public void onResume(Packet response) {
   1.104 +                fiber.resume(response);
   1.105 +        }
   1.106 +    }
   1.107 +
   1.108 +    private class NoSuspendResumer implements Resumer {
   1.109 +        protected Packet response = null;
   1.110 +
   1.111 +                public void onResume(Packet response) {
   1.112 +                        this.response = response;
   1.113 +                }
   1.114 +    }
   1.115 +
   1.116 +    private class AsyncProviderCallbackImpl implements AsyncProviderCallback<T> {
   1.117 +        private final Packet request;
   1.118 +        private Resumer resumer;
   1.119 +
   1.120 +        public AsyncProviderCallbackImpl(Packet request, Resumer resumer) {
   1.121 +            this.request = request;
   1.122 +            this.resumer = resumer;
   1.123 +        }
   1.124 +
   1.125 +        public void send(@Nullable T param) {
   1.126 +            if (param == null) {
   1.127 +                if (request.transportBackChannel != null) {
   1.128 +                    request.transportBackChannel.close();
   1.129 +                }
   1.130 +            }
   1.131 +            Packet packet = argsBuilder.getResponse(request, param, getEndpoint().getPort(), getEndpoint().getBinding());
   1.132 +            synchronized(this) {
   1.133 +                resumer.onResume(packet);
   1.134 +            }
   1.135 +        }
   1.136 +
   1.137 +        public void sendError(@NotNull Throwable t) {
   1.138 +            Exception e;
   1.139 +            if (t instanceof RuntimeException) {
   1.140 +                e = (RuntimeException)t;
   1.141 +            } else {
   1.142 +                e = new RuntimeException(t);
   1.143 +            }
   1.144 +            Packet packet = argsBuilder.getResponse(request, e, getEndpoint().getPort(), getEndpoint().getBinding());
   1.145 +            synchronized(this) {
   1.146 +                resumer.onResume(packet);
   1.147 +            }
   1.148 +        }
   1.149 +    }
   1.150 +
   1.151 +    /**
   1.152 +     * The single {@link javax.xml.ws.WebServiceContext} instance injected into application.
   1.153 +     */
   1.154 +    private static final class AsyncWebServiceContext extends AbstractWebServiceContext {
   1.155 +        final Packet packet;
   1.156 +
   1.157 +        AsyncWebServiceContext(WSEndpoint endpoint, Packet packet) {
   1.158 +            super(endpoint);
   1.159 +            this.packet = packet;
   1.160 +        }
   1.161 +
   1.162 +        public @NotNull Packet getRequestPacket() {
   1.163 +            return packet;
   1.164 +        }
   1.165 +    }
   1.166 +
   1.167 +    public @NotNull NextAction processResponse(@NotNull Packet response) {
   1.168 +        return doReturnWith(response);
   1.169 +    }
   1.170 +
   1.171 +    public @NotNull NextAction processException(@NotNull Throwable t) {
   1.172 +        throw new IllegalStateException("AsyncProviderInvokerTube's processException shouldn't be called.");
   1.173 +    }
   1.174 +
   1.175 +}

mercurial