325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.client.sei;
325N/A
325N/A//import com.sun.tools.internal.ws.wsdl.document.soap.SOAPBinding;
325N/A
325N/Aimport com.sun.istack.internal.NotNull;
325N/Aimport com.sun.istack.internal.Nullable;
325N/Aimport com.sun.xml.internal.ws.api.message.Message;
325N/Aimport com.sun.xml.internal.ws.api.message.Packet;
325N/Aimport com.sun.xml.internal.ws.api.pipe.Fiber;
325N/Aimport com.sun.xml.internal.ws.client.AsyncInvoker;
325N/Aimport com.sun.xml.internal.ws.client.AsyncResponseImpl;
325N/Aimport com.sun.xml.internal.ws.client.RequestContext;
325N/Aimport com.sun.xml.internal.ws.client.ResponseContext;
325N/Aimport com.sun.xml.internal.ws.fault.SOAPFaultBuilder;
325N/Aimport com.sun.xml.internal.ws.model.JavaMethodImpl;
325N/Aimport com.sun.xml.internal.ws.model.ParameterImpl;
325N/Aimport com.sun.xml.internal.ws.model.WrapperParameter;
325N/A
325N/Aimport javax.jws.soap.SOAPBinding.Style;
325N/Aimport javax.xml.ws.AsyncHandler;
325N/Aimport javax.xml.ws.Response;
325N/Aimport javax.xml.ws.WebServiceException;
325N/Aimport java.util.List;
325N/A
325N/A/**
325N/A * Common part between {@link CallbackMethodHandler} and {@link PollingMethodHandler}.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A * @author Jitendra Kotamraju
325N/A */
325N/Aabstract class AsyncMethodHandler extends SEIMethodHandler {
325N/A
325N/A private final ResponseBuilder responseBuilder;
325N/A /**
325N/A * Async bean class that has setters for all out parameters
325N/A */
325N/A private final @Nullable Class asyncBeanClass;
325N/A
325N/A AsyncMethodHandler(SEIStub owner, JavaMethodImpl jm, JavaMethodImpl sync) {
325N/A super(owner, sync);
325N/A
325N/A List<ParameterImpl> rp = sync.getResponseParameters();
325N/A int size = 0;
325N/A for( ParameterImpl param : rp ) {
325N/A if (param.isWrapperStyle()) {
325N/A WrapperParameter wrapParam = (WrapperParameter)param;
325N/A size += wrapParam.getWrapperChildren().size();
325N/A if (sync.getBinding().getStyle() == Style.DOCUMENT) {
325N/A // doc/asyncBeanClass - asyncBeanClass bean is in async signature
325N/A // Add 2 or more so that it is considered as async bean case
325N/A size += 2;
325N/A }
325N/A } else {
325N/A ++size;
325N/A }
325N/A }
325N/A
325N/A Class tempWrap = null;
325N/A if (size > 1) {
325N/A rp = jm.getResponseParameters();
325N/A for(ParameterImpl param : rp) {
325N/A if (param.isWrapperStyle()) {
325N/A WrapperParameter wrapParam = (WrapperParameter)param;
325N/A if (sync.getBinding().getStyle() == Style.DOCUMENT) {
325N/A // doc/asyncBeanClass style
325N/A tempWrap = (Class)wrapParam.getTypeReference().type;
325N/A break;
325N/A }
325N/A for(ParameterImpl p : wrapParam.getWrapperChildren()) {
325N/A if (p.getIndex() == -1) {
325N/A tempWrap = (Class)p.getTypeReference().type;
325N/A break;
325N/A }
325N/A }
325N/A if (tempWrap != null) {
325N/A break;
325N/A }
325N/A } else {
325N/A if (param.getIndex() == -1) {
325N/A tempWrap = (Class)param.getTypeReference().type;
325N/A break;
325N/A }
325N/A }
325N/A }
325N/A }
325N/A asyncBeanClass = tempWrap;
325N/A
325N/A switch(size) {
325N/A case 0 :
325N/A responseBuilder = buildResponseBuilder(sync, ValueSetterFactory.NONE);
325N/A break;
325N/A case 1 :
325N/A responseBuilder = buildResponseBuilder(sync, ValueSetterFactory.SINGLE);
325N/A break;
325N/A default :
325N/A responseBuilder = buildResponseBuilder(sync, new ValueSetterFactory.AsyncBeanValueSetterFactory(asyncBeanClass));
325N/A }
325N/A
325N/A }
325N/A
325N/A protected final Response<Object> doInvoke(Object proxy, Object[] args, AsyncHandler handler) {
325N/A
325N/A AsyncInvoker invoker = new SEIAsyncInvoker(proxy, args);
325N/A AsyncResponseImpl<Object> ft = new AsyncResponseImpl<Object>(invoker,handler);
325N/A invoker.setReceiver(ft);
325N/A ft.run();
325N/A return ft;
325N/A }
325N/A
325N/A private class SEIAsyncInvoker extends AsyncInvoker {
325N/A // snapshot the context now. this is necessary to avoid concurrency issue,
325N/A // and is required by the spec
325N/A private final RequestContext rc = owner.requestContext.copy();
325N/A private final Object[] args;
325N/A
325N/A SEIAsyncInvoker(Object proxy, Object[] args) {
325N/A this.args = args;
325N/A }
325N/A
325N/A public void do_run () {
325N/A Packet req = new Packet(createRequestMessage(args));
325N/A req.soapAction = soapAction;
325N/A req.expectReply = !isOneWay;
325N/A req.getMessage().assertOneWay(isOneWay);
325N/A
325N/A Fiber.CompletionCallback callback = new Fiber.CompletionCallback() {
325N/A
325N/A public void onCompletion(@NotNull Packet response) {
325N/A responseImpl.setResponseContext(new ResponseContext(response));
325N/A Message msg = response.getMessage();
325N/A if (msg == null) {
325N/A return;
325N/A }
325N/A try {
325N/A if(msg.isFault()) {
325N/A SOAPFaultBuilder faultBuilder = SOAPFaultBuilder.create(msg);
325N/A throw faultBuilder.createException(checkedExceptions);
325N/A } else {
325N/A Object[] rargs = new Object[1];
325N/A if (asyncBeanClass != null) {
325N/A rargs[0] = asyncBeanClass.newInstance();
325N/A }
325N/A responseBuilder.readResponse(msg, rargs);
325N/A responseImpl.set(rargs[0], null);
325N/A }
325N/A } catch (Throwable t) {
325N/A if (t instanceof RuntimeException) {
325N/A if (t instanceof WebServiceException) {
325N/A responseImpl.set(null, t);
325N/A return;
325N/A }
325N/A } else if (t instanceof Exception) {
325N/A responseImpl.set(null, t);
325N/A return;
325N/A }
325N/A //its RuntimeException or some other exception resulting from user error, wrap it in
325N/A // WebServiceException
325N/A responseImpl.set(null, new WebServiceException(t));
325N/A }
325N/A }
325N/A
325N/A
325N/A public void onCompletion(@NotNull Throwable error) {
325N/A if (error instanceof WebServiceException) {
325N/A responseImpl.set(null, error);
325N/A } else {
325N/A //its RuntimeException or some other exception resulting from user error, wrap it in
325N/A // WebServiceException
325N/A responseImpl.set(null, new WebServiceException(error));
325N/A }
325N/A }
325N/A };
325N/A owner.doProcessAsync(req, rc, callback);
325N/A }
325N/A }
325N/A
325N/A ValueGetterFactory getValueGetterFactory() {
325N/A return ValueGetterFactory.ASYNC;
325N/A }
325N/A
325N/A}