Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, 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.server.provider; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.istack.internal.Nullable; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.api.message.Packet; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.api.pipe.Fiber; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.api.pipe.NextAction; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.api.pipe.ThrowableContainerPropertySet; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.api.pipe.Tube; |
aoqi@0 | 35 | import com.sun.xml.internal.ws.api.server.AsyncProvider; |
aoqi@0 | 36 | import com.sun.xml.internal.ws.api.server.AsyncProviderCallback; |
aoqi@0 | 37 | import com.sun.xml.internal.ws.api.server.Invoker; |
aoqi@0 | 38 | import com.sun.xml.internal.ws.api.server.WSEndpoint; |
aoqi@0 | 39 | import com.sun.xml.internal.ws.server.AbstractWebServiceContext; |
aoqi@0 | 40 | |
aoqi@0 | 41 | import java.util.logging.Level; |
aoqi@0 | 42 | import java.util.logging.Logger; |
aoqi@0 | 43 | |
aoqi@0 | 44 | /** |
aoqi@0 | 45 | * This {@link Tube} is used to invoke the {@link AsyncProvider} endpoints. |
aoqi@0 | 46 | * |
aoqi@0 | 47 | * @author Jitendra Kotamraju |
aoqi@0 | 48 | */ |
aoqi@0 | 49 | public // TODO needed by factory |
aoqi@0 | 50 | class AsyncProviderInvokerTube<T> extends ProviderInvokerTube<T> { |
aoqi@0 | 51 | |
aoqi@0 | 52 | private static final Logger LOGGER = Logger.getLogger( |
aoqi@0 | 53 | com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.AsyncProviderInvokerTube"); |
aoqi@0 | 54 | |
aoqi@0 | 55 | public AsyncProviderInvokerTube(Invoker invoker, ProviderArgumentsBuilder<T> argsBuilder) { |
aoqi@0 | 56 | super(invoker, argsBuilder); |
aoqi@0 | 57 | } |
aoqi@0 | 58 | |
aoqi@0 | 59 | /* |
aoqi@0 | 60 | * This binds the parameter for Provider endpoints and invokes the |
aoqi@0 | 61 | * invoke() method of {@linke Provider} endpoint. The return value from |
aoqi@0 | 62 | * invoke() is used to create a new {@link Message} that traverses |
aoqi@0 | 63 | * through the Pipeline to transport. |
aoqi@0 | 64 | */ |
aoqi@0 | 65 | public @NotNull NextAction processRequest(@NotNull Packet request) { |
aoqi@0 | 66 | T param = argsBuilder.getParameter(request); |
aoqi@0 | 67 | NoSuspendResumer resumer = new NoSuspendResumer(); |
aoqi@0 | 68 | @SuppressWarnings({ "rawtypes", "unchecked" }) |
aoqi@0 | 69 | AsyncProviderCallbackImpl callback = new AsyncProviderInvokerTube.AsyncProviderCallbackImpl(request, resumer); |
aoqi@0 | 70 | AsyncWebServiceContext ctxt = new AsyncWebServiceContext(getEndpoint(),request); |
aoqi@0 | 71 | |
aoqi@0 | 72 | AsyncProviderInvokerTube.LOGGER.fine("Invoking AsyncProvider Endpoint"); |
aoqi@0 | 73 | try { |
aoqi@0 | 74 | getInvoker(request).invokeAsyncProvider(request, param, callback, ctxt); |
aoqi@0 | 75 | } catch(Throwable e) { |
aoqi@0 | 76 | LOGGER.log(Level.SEVERE, e.getMessage(), e); |
aoqi@0 | 77 | return doThrow(e); |
aoqi@0 | 78 | } |
aoqi@0 | 79 | |
aoqi@0 | 80 | synchronized(callback) { |
aoqi@0 | 81 | if (resumer.response != null) { |
aoqi@0 | 82 | // Only used by AsyncProvider<Packet> |
aoqi@0 | 83 | // Implementation may pass Packet containing throwable; use both |
aoqi@0 | 84 | ThrowableContainerPropertySet tc = resumer.response.getSatellite(ThrowableContainerPropertySet.class); |
aoqi@0 | 85 | Throwable t = (tc != null) ? tc.getThrowable() : null; |
aoqi@0 | 86 | |
aoqi@0 | 87 | return t != null ? doThrow(resumer.response, t) : doReturnWith(resumer.response); |
aoqi@0 | 88 | } |
aoqi@0 | 89 | |
aoqi@0 | 90 | // Suspend the Fiber. AsyncProviderCallback will resume the Fiber after |
aoqi@0 | 91 | // it receives response. |
aoqi@0 | 92 | callback.resumer = new FiberResumer(); |
aoqi@0 | 93 | return doSuspend(); |
aoqi@0 | 94 | } |
aoqi@0 | 95 | } |
aoqi@0 | 96 | |
aoqi@0 | 97 | private interface Resumer { |
aoqi@0 | 98 | public void onResume(Packet response); |
aoqi@0 | 99 | } |
aoqi@0 | 100 | |
aoqi@0 | 101 | /*private*/ public class FiberResumer implements Resumer { // TODO public for DISI |
aoqi@0 | 102 | private final Fiber fiber; |
aoqi@0 | 103 | |
aoqi@0 | 104 | public FiberResumer() { |
aoqi@0 | 105 | this.fiber = Fiber.current(); |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | public void onResume(Packet response) { |
aoqi@0 | 109 | // Only used by AsyncProvider<Packet> |
aoqi@0 | 110 | // Implementation may pass Packet containing throwable; use both |
aoqi@0 | 111 | ThrowableContainerPropertySet tc = response.getSatellite(ThrowableContainerPropertySet.class); |
aoqi@0 | 112 | Throwable t = (tc != null) ? tc.getThrowable() : null; |
aoqi@0 | 113 | fiber.resume(t, response); |
aoqi@0 | 114 | } |
aoqi@0 | 115 | } |
aoqi@0 | 116 | |
aoqi@0 | 117 | private class NoSuspendResumer implements Resumer { |
aoqi@0 | 118 | protected Packet response = null; |
aoqi@0 | 119 | |
aoqi@0 | 120 | public void onResume(Packet response) { |
aoqi@0 | 121 | this.response = response; |
aoqi@0 | 122 | } |
aoqi@0 | 123 | } |
aoqi@0 | 124 | |
aoqi@0 | 125 | /*private*/ public class AsyncProviderCallbackImpl implements AsyncProviderCallback<T> { // TODO public for DISI |
aoqi@0 | 126 | private final Packet request; |
aoqi@0 | 127 | private Resumer resumer; |
aoqi@0 | 128 | |
aoqi@0 | 129 | public AsyncProviderCallbackImpl(Packet request, Resumer resumer) { |
aoqi@0 | 130 | this.request = request; |
aoqi@0 | 131 | this.resumer = resumer; |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | public void send(@Nullable T param) { |
aoqi@0 | 135 | if (param == null) { |
aoqi@0 | 136 | if (request.transportBackChannel != null) { |
aoqi@0 | 137 | request.transportBackChannel.close(); |
aoqi@0 | 138 | } |
aoqi@0 | 139 | } |
aoqi@0 | 140 | Packet packet = argsBuilder.getResponse(request, param, getEndpoint().getPort(), getEndpoint().getBinding()); |
aoqi@0 | 141 | synchronized(this) { |
aoqi@0 | 142 | resumer.onResume(packet); |
aoqi@0 | 143 | } |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | public void sendError(@NotNull Throwable t) { |
aoqi@0 | 147 | Exception e; |
aoqi@0 | 148 | if (t instanceof Exception) { |
aoqi@0 | 149 | e = (Exception) t; |
aoqi@0 | 150 | } else { |
aoqi@0 | 151 | e = new RuntimeException(t); |
aoqi@0 | 152 | } |
aoqi@0 | 153 | Packet packet = argsBuilder.getResponse(request, e, getEndpoint().getPort(), getEndpoint().getBinding()); |
aoqi@0 | 154 | synchronized(this) { |
aoqi@0 | 155 | resumer.onResume(packet); |
aoqi@0 | 156 | } |
aoqi@0 | 157 | } |
aoqi@0 | 158 | } |
aoqi@0 | 159 | |
aoqi@0 | 160 | /** |
aoqi@0 | 161 | * The single {@link javax.xml.ws.WebServiceContext} instance injected into application. |
aoqi@0 | 162 | */ |
aoqi@0 | 163 | /*private static final*/ public class AsyncWebServiceContext extends AbstractWebServiceContext { // TODO public for DISI |
aoqi@0 | 164 | final Packet packet; |
aoqi@0 | 165 | |
aoqi@0 | 166 | public AsyncWebServiceContext(WSEndpoint endpoint, Packet packet) { // TODO public for DISI |
aoqi@0 | 167 | super(endpoint); |
aoqi@0 | 168 | this.packet = packet; |
aoqi@0 | 169 | } |
aoqi@0 | 170 | |
aoqi@0 | 171 | public @NotNull Packet getRequestPacket() { |
aoqi@0 | 172 | return packet; |
aoqi@0 | 173 | } |
aoqi@0 | 174 | } |
aoqi@0 | 175 | |
aoqi@0 | 176 | public @NotNull NextAction processResponse(@NotNull Packet response) { |
aoqi@0 | 177 | return doReturnWith(response); |
aoqi@0 | 178 | } |
aoqi@0 | 179 | |
aoqi@0 | 180 | public @NotNull NextAction processException(@NotNull Throwable t) { |
aoqi@0 | 181 | return doThrow(t); |
aoqi@0 | 182 | } |
aoqi@0 | 183 | |
aoqi@0 | 184 | } |