1/* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.xml.internal.ws.server.provider; 27 28import com.sun.istack.internal.NotNull; 29import com.sun.istack.internal.Nullable; 30import com.sun.xml.internal.ws.api.message.Packet; 31import com.sun.xml.internal.ws.api.pipe.Fiber; 32import com.sun.xml.internal.ws.api.pipe.NextAction; 33import com.sun.xml.internal.ws.api.pipe.ThrowableContainerPropertySet; 34import com.sun.xml.internal.ws.api.pipe.Tube; 35import com.sun.xml.internal.ws.api.server.AsyncProvider; 36import com.sun.xml.internal.ws.api.server.AsyncProviderCallback; 37import com.sun.xml.internal.ws.api.server.Invoker; 38import com.sun.xml.internal.ws.api.server.WSEndpoint; 39import com.sun.xml.internal.ws.server.AbstractWebServiceContext; 40 41import java.util.logging.Level; 42import java.util.logging.Logger; 43 44/** 45 * This {@link Tube} is used to invoke the {@link AsyncProvider} endpoints. 46 * 47 * @author Jitendra Kotamraju 48 */ 49public // TODO needed by factory 50class AsyncProviderInvokerTube<T> extends ProviderInvokerTube<T> { 51 52 private static final Logger LOGGER = Logger.getLogger( 53 com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.AsyncProviderInvokerTube"); 54 55 public AsyncProviderInvokerTube(Invoker invoker, ProviderArgumentsBuilder<T> argsBuilder) { 56 super(invoker, argsBuilder); 57 } 58 59 /* 60 * This binds the parameter for Provider endpoints and invokes the 61 * invoke() method of {@linke Provider} endpoint. The return value from 62 * invoke() is used to create a new {@link Message} that traverses 63 * through the Pipeline to transport. 64 */ 65 public @NotNull NextAction processRequest(@NotNull Packet request) { 66 T param = argsBuilder.getParameter(request); 67 NoSuspendResumer resumer = new NoSuspendResumer(); 68 @SuppressWarnings({ "rawtypes", "unchecked" }) 69 AsyncProviderCallbackImpl callback = new AsyncProviderInvokerTube.AsyncProviderCallbackImpl(request, resumer); 70 AsyncWebServiceContext ctxt = new AsyncWebServiceContext(getEndpoint(),request); 71 72 AsyncProviderInvokerTube.LOGGER.fine("Invoking AsyncProvider Endpoint"); 73 try { 74 getInvoker(request).invokeAsyncProvider(request, param, callback, ctxt); 75 } catch(Throwable e) { 76 LOGGER.log(Level.SEVERE, e.getMessage(), e); 77 return doThrow(e); 78 } 79 80 synchronized(callback) { 81 if (resumer.response != null) { 82 // Only used by AsyncProvider<Packet> 83 // Implementation may pass Packet containing throwable; use both 84 ThrowableContainerPropertySet tc = resumer.response.getSatellite(ThrowableContainerPropertySet.class); 85 Throwable t = (tc != null) ? tc.getThrowable() : null; 86 87 return t != null ? doThrow(resumer.response, t) : doReturnWith(resumer.response); 88 } 89 90 // Suspend the Fiber. AsyncProviderCallback will resume the Fiber after 91 // it receives response. 92 callback.resumer = new FiberResumer(); 93 return doSuspend(); 94 } 95 } 96 97 private interface Resumer { 98 public void onResume(Packet response); 99 } 100 101 /*private*/ public class FiberResumer implements Resumer { // TODO public for DISI 102 private final Fiber fiber; 103 104 public FiberResumer() { 105 this.fiber = Fiber.current(); 106 } 107 108 public void onResume(Packet response) { 109 // Only used by AsyncProvider<Packet> 110 // Implementation may pass Packet containing throwable; use both 111 ThrowableContainerPropertySet tc = response.getSatellite(ThrowableContainerPropertySet.class); 112 Throwable t = (tc != null) ? tc.getThrowable() : null; 113 fiber.resume(t, response); 114 } 115 } 116 117 private class NoSuspendResumer implements Resumer { 118 protected Packet response = null; 119 120 public void onResume(Packet response) { 121 this.response = response; 122 } 123 } 124 125 /*private*/ public class AsyncProviderCallbackImpl implements AsyncProviderCallback<T> { // TODO public for DISI 126 private final Packet request; 127 private Resumer resumer; 128 129 public AsyncProviderCallbackImpl(Packet request, Resumer resumer) { 130 this.request = request; 131 this.resumer = resumer; 132 } 133 134 public void send(@Nullable T param) { 135 if (param == null) { 136 if (request.transportBackChannel != null) { 137 request.transportBackChannel.close(); 138 } 139 } 140 Packet packet = argsBuilder.getResponse(request, param, getEndpoint().getPort(), getEndpoint().getBinding()); 141 synchronized(this) { 142 resumer.onResume(packet); 143 } 144 } 145 146 public void sendError(@NotNull Throwable t) { 147 Exception e; 148 if (t instanceof Exception) { 149 e = (Exception) t; 150 } else { 151 e = new RuntimeException(t); 152 } 153 Packet packet = argsBuilder.getResponse(request, e, getEndpoint().getPort(), getEndpoint().getBinding()); 154 synchronized(this) { 155 resumer.onResume(packet); 156 } 157 } 158 } 159 160 /** 161 * The single {@link javax.xml.ws.WebServiceContext} instance injected into application. 162 */ 163 /*private static final*/ public class AsyncWebServiceContext extends AbstractWebServiceContext { // TODO public for DISI 164 final Packet packet; 165 166 public AsyncWebServiceContext(WSEndpoint endpoint, Packet packet) { // TODO public for DISI 167 super(endpoint); 168 this.packet = packet; 169 } 170 171 public @NotNull Packet getRequestPacket() { 172 return packet; 173 } 174 } 175 176 public @NotNull NextAction processResponse(@NotNull Packet response) { 177 return doReturnWith(response); 178 } 179 180 public @NotNull NextAction processException(@NotNull Throwable t) { 181 return doThrow(t); 182 } 183 184} 185