src/share/jaxws_classes/com/sun/xml/internal/ws/client/sei/AsyncMethodHandler.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/client/sei/AsyncMethodHandler.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,224 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.ws.client.sei;
    1.30 +
    1.31 +//import com.sun.tools.internal.ws.wsdl.document.soap.SOAPBinding;
    1.32 +
    1.33 +import com.sun.istack.internal.NotNull;
    1.34 +import com.sun.istack.internal.Nullable;
    1.35 +import com.sun.xml.internal.ws.api.databinding.ClientCallBridge;
    1.36 +import com.sun.xml.internal.ws.api.message.Message;
    1.37 +import com.sun.xml.internal.ws.api.message.Packet;
    1.38 +import com.sun.xml.internal.ws.api.pipe.Fiber;
    1.39 +import com.sun.xml.internal.ws.api.pipe.FiberContextSwitchInterceptor;
    1.40 +import com.sun.xml.internal.ws.client.AsyncInvoker;
    1.41 +import com.sun.xml.internal.ws.client.AsyncResponseImpl;
    1.42 +import com.sun.xml.internal.ws.client.RequestContext;
    1.43 +import com.sun.xml.internal.ws.client.ResponseContext;
    1.44 +import com.sun.xml.internal.ws.fault.SOAPFaultBuilder;
    1.45 +import com.sun.xml.internal.ws.model.JavaMethodImpl;
    1.46 +import com.sun.xml.internal.ws.model.ParameterImpl;
    1.47 +import com.sun.xml.internal.ws.model.WrapperParameter;
    1.48 +import com.sun.xml.internal.org.jvnet.ws.databinding.JavaCallInfo;
    1.49 +
    1.50 +import javax.jws.soap.SOAPBinding.Style;
    1.51 +import javax.xml.ws.AsyncHandler;
    1.52 +import javax.xml.ws.Response;
    1.53 +import javax.xml.ws.WebServiceException;
    1.54 +
    1.55 +import java.lang.reflect.Method;
    1.56 +import java.util.List;
    1.57 +
    1.58 +/**
    1.59 + * Common part between {@link CallbackMethodHandler} and {@link PollingMethodHandler}.
    1.60 + *
    1.61 + * @author Kohsuke Kawaguchi
    1.62 + * @author Jitendra Kotamraju
    1.63 + */
    1.64 +abstract class AsyncMethodHandler extends MethodHandler {
    1.65 +
    1.66 +    AsyncMethodHandler(SEIStub owner, Method m) {
    1.67 +        super(owner, m);
    1.68 +    }
    1.69 +
    1.70 +//    private final ResponseBuilder responseBuilder;
    1.71 +//    /**
    1.72 +//     * Async bean class that has setters for all out parameters
    1.73 +//     */
    1.74 +//    private final @Nullable Class asyncBeanClass;
    1.75 +//
    1.76 +//    AsyncMethodHandler(SEIStub owner, JavaMethodImpl jm, JavaMethodImpl sync) {
    1.77 +//        super(owner, sync);
    1.78 +//
    1.79 +//        List<ParameterImpl> rp = sync.getResponseParameters();
    1.80 +//        int size = 0;
    1.81 +//        for( ParameterImpl param : rp ) {
    1.82 +//            if (param.isWrapperStyle()) {
    1.83 +//                WrapperParameter wrapParam = (WrapperParameter)param;
    1.84 +//                size += wrapParam.getWrapperChildren().size();
    1.85 +//                if (sync.getBinding().getStyle() == Style.DOCUMENT) {
    1.86 +//                    // doc/asyncBeanClass - asyncBeanClass bean is in async signature
    1.87 +//                    // Add 2 or more so that it is considered as async bean case
    1.88 +//                    size += 2;
    1.89 +//                }
    1.90 +//            } else {
    1.91 +//                ++size;
    1.92 +//            }
    1.93 +//        }
    1.94 +//
    1.95 +//        Class tempWrap = null;
    1.96 +//        if (size > 1) {
    1.97 +//            rp = jm.getResponseParameters();
    1.98 +//            for(ParameterImpl param : rp) {
    1.99 +//                if (param.isWrapperStyle()) {
   1.100 +//                    WrapperParameter wrapParam = (WrapperParameter)param;
   1.101 +//                    if (sync.getBinding().getStyle() == Style.DOCUMENT) {
   1.102 +//                        // doc/asyncBeanClass style
   1.103 +//                        tempWrap = (Class)wrapParam.getTypeReference().type;
   1.104 +//                        break;
   1.105 +//                    }
   1.106 +//                    for(ParameterImpl p : wrapParam.getWrapperChildren()) {
   1.107 +//                        if (p.getIndex() == -1) {
   1.108 +//                            tempWrap = (Class)p.getTypeReference().type;
   1.109 +//                            break;
   1.110 +//                        }
   1.111 +//                    }
   1.112 +//                    if (tempWrap != null) {
   1.113 +//                        break;
   1.114 +//                    }
   1.115 +//                } else {
   1.116 +//                    if (param.getIndex() == -1) {
   1.117 +//                        tempWrap = (Class)param.getTypeReference().type;
   1.118 +//                        break;
   1.119 +//                    }
   1.120 +//                }
   1.121 +//            }
   1.122 +//        }
   1.123 +//        asyncBeanClass = tempWrap;
   1.124 +//
   1.125 +//        switch(size) {
   1.126 +//            case 0 :
   1.127 +//                responseBuilder = buildResponseBuilder(sync, ValueSetterFactory.NONE);
   1.128 +//                break;
   1.129 +//            case 1 :
   1.130 +//                responseBuilder = buildResponseBuilder(sync, ValueSetterFactory.SINGLE);
   1.131 +//                break;
   1.132 +//            default :
   1.133 +//                responseBuilder = buildResponseBuilder(sync, new ValueSetterFactory.AsyncBeanValueSetterFactory(asyncBeanClass));
   1.134 +//        }
   1.135 +//
   1.136 +//    }
   1.137 +
   1.138 +    protected final Response<Object> doInvoke(Object proxy, Object[] args, AsyncHandler handler) {
   1.139 +
   1.140 +        AsyncInvoker invoker = new SEIAsyncInvoker(proxy, args);
   1.141 +        invoker.setNonNullAsyncHandlerGiven(handler != null);
   1.142 +        AsyncResponseImpl<Object> ft = new AsyncResponseImpl<Object>(invoker,handler);
   1.143 +        invoker.setReceiver(ft);
   1.144 +        ft.run();
   1.145 +        return ft;
   1.146 +    }
   1.147 +
   1.148 +    private class SEIAsyncInvoker extends AsyncInvoker {
   1.149 +        // snapshot the context now. this is necessary to avoid concurrency issue,
   1.150 +        // and is required by the spec
   1.151 +        private final RequestContext rc = owner.requestContext.copy();
   1.152 +        private final Object[] args;
   1.153 +
   1.154 +        SEIAsyncInvoker(Object proxy, Object[] args) {
   1.155 +            this.args = args;
   1.156 +        }
   1.157 +
   1.158 +        public void do_run () {
   1.159 +                JavaCallInfo call = owner.databinding.createJavaCallInfo(method, args);
   1.160 +            Packet req = (Packet)owner.databinding.serializeRequest(call);
   1.161 +
   1.162 +            Fiber.CompletionCallback callback = new Fiber.CompletionCallback() {
   1.163 +
   1.164 +                public void onCompletion(@NotNull Packet response) {
   1.165 +                    responseImpl.setResponseContext(new ResponseContext(response));
   1.166 +                    Message msg = response.getMessage();
   1.167 +                    if (msg == null) {
   1.168 +                        return;
   1.169 +                    }
   1.170 +                    try {
   1.171 +                        Object[] rargs = new Object[1];
   1.172 +                        JavaCallInfo call = owner.databinding.createJavaCallInfo(method, rargs);
   1.173 +                        call = owner.databinding.deserializeResponse(response, call);
   1.174 +                        if (call.getException() != null) {
   1.175 +                            throw call.getException();
   1.176 +                        } else {
   1.177 +                            responseImpl.set(rargs[0], null);
   1.178 +                        }
   1.179 +//                      dbHandler.readResponse(response, call);
   1.180 +//                      responseImpl.set(rargs[0], null);
   1.181 +//                        if(msg.isFault()) {
   1.182 +//                            SOAPFaultBuilder faultBuilder = SOAPFaultBuilder.create(msg);
   1.183 +//                            throw faultBuilder.createException(checkedExceptions);
   1.184 +//                        } else {
   1.185 +//                            Object[] rargs = new Object[1];
   1.186 +//                            if (asyncBeanClass != null) {
   1.187 +//                                rargs[0] = asyncBeanClass.newInstance();
   1.188 +//                            }
   1.189 +//                            responseBuilder.readResponse(msg, rargs);
   1.190 +//                            responseImpl.set(rargs[0], null);
   1.191 +//                        }
   1.192 +                   } catch (Throwable t) {
   1.193 +                        if (t instanceof RuntimeException) {
   1.194 +                            if (t instanceof WebServiceException) {
   1.195 +                                responseImpl.set(null, t);
   1.196 +                                return;
   1.197 +                            }
   1.198 +                        }  else if (t instanceof Exception) {
   1.199 +                            responseImpl.set(null, t);
   1.200 +                            return;
   1.201 +                        }
   1.202 +                        //its RuntimeException or some other exception resulting from user error, wrap it in
   1.203 +                        // WebServiceException
   1.204 +                        responseImpl.set(null, new WebServiceException(t));
   1.205 +                    }
   1.206 +                }
   1.207 +
   1.208 +
   1.209 +                public void onCompletion(@NotNull Throwable error) {
   1.210 +                    if (error instanceof WebServiceException) {
   1.211 +                        responseImpl.set(null, error);
   1.212 +                    } else {
   1.213 +                        //its RuntimeException or some other exception resulting from user error, wrap it in
   1.214 +                        // WebServiceException
   1.215 +                        responseImpl.set(null, new WebServiceException(error));
   1.216 +                    }
   1.217 +                }
   1.218 +            };
   1.219 +            owner.doProcessAsync(responseImpl, req, rc, callback);
   1.220 +        }
   1.221 +    }
   1.222 +
   1.223 +    ValueGetterFactory getValueGetterFactory() {
   1.224 +        return ValueGetterFactory.ASYNC;
   1.225 +    }
   1.226 +
   1.227 +}

mercurial