package com.sun.xml.internal.ws.server.provider;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import com.sun.xml.internal.ws.api.message.Packet;
import com.sun.xml.internal.ws.api.pipe.Fiber;
import com.sun.xml.internal.ws.api.pipe.NextAction;
import com.sun.xml.internal.ws.api.pipe.ThrowableContainerPropertySet;
import com.sun.xml.internal.ws.api.pipe.Tube;
import com.sun.xml.internal.ws.api.server.AsyncProvider;
import com.sun.xml.internal.ws.api.server.AsyncProviderCallback;
import com.sun.xml.internal.ws.api.server.Invoker;
import com.sun.xml.internal.ws.api.server.WSEndpoint;
import com.sun.xml.internal.ws.server.AbstractWebServiceContext;
import java.util.logging.Level;
import java.util.logging.Logger;
public
class AsyncProviderInvokerTube<T> extends ProviderInvokerTube<T> {
private static final Logger LOGGER = Logger.getLogger(
com.sun.xml.internal.ws.util.Constants.LoggingDomain + ".server.AsyncProviderInvokerTube");
public AsyncProviderInvokerTube(Invoker invoker, ProviderArgumentsBuilder<T> argsBuilder) {
super(invoker, argsBuilder);
}
public @NotNull NextAction processRequest(@NotNull Packet request) {
T param = argsBuilder.getParameter(request);
NoSuspendResumer resumer = new NoSuspendResumer();
@SuppressWarnings({ "rawtypes", "unchecked" })
AsyncProviderCallbackImpl callback = new AsyncProviderInvokerTube.AsyncProviderCallbackImpl(request, resumer);
AsyncWebServiceContext ctxt = new AsyncWebServiceContext(getEndpoint(),request);
AsyncProviderInvokerTube.LOGGER.fine("Invoking AsyncProvider Endpoint");
try {
getInvoker(request).invokeAsyncProvider(request, param, callback, ctxt);
} catch(Throwable e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
return doThrow(e);
}
synchronized(callback) {
if (resumer.response != null) {
ThrowableContainerPropertySet tc = resumer.response.getSatellite(ThrowableContainerPropertySet.class);
Throwable t = (tc != null) ? tc.getThrowable() : null;
return t != null ? doThrow(resumer.response, t) : doReturnWith(resumer.response);
}
callback.resumer = new FiberResumer();
return doSuspend();
}
}
private interface Resumer {
public void onResume(Packet response);
}
public class FiberResumer implements Resumer {
private final Fiber fiber;
public FiberResumer() {
this.fiber = Fiber.current();
}
public void onResume(Packet response) {
ThrowableContainerPropertySet tc = response.getSatellite(ThrowableContainerPropertySet.class);
Throwable t = (tc != null) ? tc.getThrowable() : null;
fiber.resume(t, response);
}
}
private class NoSuspendResumer implements Resumer {
protected Packet response = null;
public void onResume(Packet response) {
this.response = response;
}
}
public class AsyncProviderCallbackImpl implements AsyncProviderCallback<T> {
private final Packet request;
private Resumer resumer;
public AsyncProviderCallbackImpl(Packet request, Resumer resumer) {
this.request = request;
this.resumer = resumer;
}
public void send(@Nullable T param) {
if (param == null) {
if (request.transportBackChannel != null) {
request.transportBackChannel.close();
}
}
Packet packet = argsBuilder.getResponse(request, param, getEndpoint().getPort(), getEndpoint().getBinding());
synchronized(this) {
resumer.onResume(packet);
}
}
public void sendError(@NotNull Throwable t) {
Exception e;
if (t instanceof Exception) {
e = (Exception) t;
} else {
e = new RuntimeException(t);
}
Packet packet = argsBuilder.getResponse(request, e, getEndpoint().getPort(), getEndpoint().getBinding());
synchronized(this) {
resumer.onResume(packet);
}
}
}
public class AsyncWebServiceContext extends AbstractWebServiceContext {
final Packet packet;
public AsyncWebServiceContext(WSEndpoint endpoint, Packet packet) {
super(endpoint);
this.packet = packet;
}
public @NotNull Packet getRequestPacket() {
return packet;
}
}
public @NotNull NextAction processResponse(@NotNull Packet response) {
return doReturnWith(response);
}
public @NotNull NextAction processException(@NotNull Throwable t) {
return doThrow(t);
}
}