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