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/SEIStub.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,201 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2013, 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.istack.internal.NotNull; 1.32 +import com.sun.istack.internal.Nullable; 1.33 +import com.sun.xml.internal.ws.api.SOAPVersion; 1.34 +import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; 1.35 +import com.sun.xml.internal.ws.api.client.WSPortInfo; 1.36 +import com.sun.xml.internal.ws.api.databinding.Databinding; 1.37 +import com.sun.xml.internal.ws.api.addressing.WSEndpointReference; 1.38 +import com.sun.xml.internal.ws.api.message.Header; 1.39 +import com.sun.xml.internal.ws.api.message.Headers; 1.40 +import com.sun.xml.internal.ws.api.message.Packet; 1.41 +import com.sun.xml.internal.ws.api.model.MEP; 1.42 +import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation; 1.43 +import com.sun.xml.internal.ws.api.pipe.Fiber; 1.44 +import com.sun.xml.internal.ws.api.pipe.Tube; 1.45 +import com.sun.xml.internal.ws.api.server.Container; 1.46 +import com.sun.xml.internal.ws.api.server.ContainerResolver; 1.47 +import com.sun.xml.internal.ws.binding.BindingImpl; 1.48 +import com.sun.xml.internal.ws.client.AsyncResponseImpl; 1.49 +import com.sun.xml.internal.ws.client.RequestContext; 1.50 +import com.sun.xml.internal.ws.client.ResponseContextReceiver; 1.51 +import com.sun.xml.internal.ws.client.Stub; 1.52 +import com.sun.xml.internal.ws.client.WSServiceDelegate; 1.53 +import com.sun.xml.internal.ws.model.JavaMethodImpl; 1.54 +import com.sun.xml.internal.ws.model.SOAPSEIModel; 1.55 +import com.sun.xml.internal.ws.wsdl.OperationDispatcher; 1.56 + 1.57 +import javax.xml.namespace.QName; 1.58 +import java.lang.reflect.InvocationHandler; 1.59 +import java.lang.reflect.InvocationTargetException; 1.60 +import java.lang.reflect.Method; 1.61 +import java.lang.reflect.Modifier; 1.62 +import java.lang.reflect.Proxy; 1.63 +import java.util.HashMap; 1.64 +import java.util.Map; 1.65 + 1.66 +/** 1.67 + * {@link Stub} that handles method invocations 1.68 + * through a strongly-typed endpoint interface. 1.69 + * 1.70 + * @author Kohsuke Kawaguchi 1.71 + */ 1.72 +public final class SEIStub extends Stub implements InvocationHandler { 1.73 + 1.74 + Databinding databinding; 1.75 + 1.76 + @Deprecated 1.77 + public SEIStub(WSServiceDelegate owner, BindingImpl binding, SOAPSEIModel seiModel, Tube master, WSEndpointReference epr) { 1.78 + super(owner, master, binding, seiModel.getPort(), seiModel.getPort().getAddress(), epr); 1.79 + this.seiModel = seiModel; 1.80 + this.soapVersion = binding.getSOAPVersion(); 1.81 + databinding = seiModel.getDatabinding(); 1.82 + initMethodHandlers(); 1.83 + } 1.84 + 1.85 + // added portInterface to the constructor, otherwise AsyncHandler won't work 1.86 + public SEIStub(WSPortInfo portInfo, BindingImpl binding, SOAPSEIModel seiModel, WSEndpointReference epr) { 1.87 + super(portInfo, binding, seiModel.getPort().getAddress(),epr); 1.88 + this.seiModel = seiModel; 1.89 + this.soapVersion = binding.getSOAPVersion(); 1.90 + databinding = seiModel.getDatabinding(); 1.91 + initMethodHandlers(); 1.92 + } 1.93 + 1.94 + private void initMethodHandlers() { 1.95 + Map<WSDLBoundOperation, JavaMethodImpl> syncs = new HashMap<WSDLBoundOperation, JavaMethodImpl>(); 1.96 + 1.97 + // fill in methodHandlers. 1.98 + // first fill in sychronized versions 1.99 + for (JavaMethodImpl m : seiModel.getJavaMethods()) { 1.100 + if (!m.getMEP().isAsync) { 1.101 + SyncMethodHandler handler = new SyncMethodHandler(this, m); 1.102 + syncs.put(m.getOperation(), m); 1.103 + methodHandlers.put(m.getMethod(), handler); 1.104 + } 1.105 + } 1.106 + 1.107 + for (JavaMethodImpl jm : seiModel.getJavaMethods()) { 1.108 + JavaMethodImpl sync = syncs.get(jm.getOperation()); 1.109 + if (jm.getMEP() == MEP.ASYNC_CALLBACK) { 1.110 + Method m = jm.getMethod(); 1.111 + CallbackMethodHandler handler = new CallbackMethodHandler( 1.112 + this, m, m.getParameterTypes().length - 1); 1.113 + methodHandlers.put(m, handler); 1.114 + } 1.115 + if (jm.getMEP() == MEP.ASYNC_POLL) { 1.116 + Method m = jm.getMethod(); 1.117 + PollingMethodHandler handler = new PollingMethodHandler(this, m); 1.118 + methodHandlers.put(m, handler); 1.119 + } 1.120 + } 1.121 + } 1.122 + 1.123 + public final SOAPSEIModel seiModel; 1.124 + 1.125 + public final SOAPVersion soapVersion; 1.126 + 1.127 + /** 1.128 + * Nullable when there is no associated WSDL Model 1.129 + * @return 1.130 + */ 1.131 + public @Nullable 1.132 + OperationDispatcher getOperationDispatcher() { 1.133 + if(operationDispatcher == null && wsdlPort != null) 1.134 + operationDispatcher = new OperationDispatcher(wsdlPort,binding,seiModel); 1.135 + return operationDispatcher; 1.136 + } 1.137 + 1.138 + /** 1.139 + * For each method on the port interface we have 1.140 + * a {@link MethodHandler} that processes it. 1.141 + */ 1.142 + private final Map<Method, MethodHandler> methodHandlers = new HashMap<Method, MethodHandler>(); 1.143 + 1.144 + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 1.145 + validateInputs(proxy, method); 1.146 + Container old = ContainerResolver.getDefault().enterContainer(owner.getContainer()); 1.147 + try { 1.148 + MethodHandler handler = methodHandlers.get(method); 1.149 + if (handler != null) { 1.150 + return handler.invoke(proxy, args); 1.151 + } else { 1.152 + // we handle the other method invocations by ourselves 1.153 + try { 1.154 + return method.invoke(this, args); 1.155 + } catch (IllegalAccessException e) { 1.156 + // impossible 1.157 + throw new AssertionError(e); 1.158 + } catch (IllegalArgumentException e) { 1.159 + throw new AssertionError(e); 1.160 + } catch (InvocationTargetException e) { 1.161 + throw e.getCause(); 1.162 + } 1.163 + } 1.164 + } finally { 1.165 + ContainerResolver.getDefault().exitContainer(old); 1.166 + } 1.167 + } 1.168 + 1.169 + private void validateInputs(Object proxy, Method method) { 1.170 + if (proxy == null || !Proxy.isProxyClass(proxy.getClass())) { 1.171 + throw new IllegalStateException("Passed object is not proxy!"); 1.172 + } 1.173 + Class<?> declaringClass = method.getDeclaringClass(); 1.174 + if (method == null || declaringClass == null 1.175 + || Modifier.isStatic(method.getModifiers())) { 1.176 + throw new IllegalStateException("Invoking static method is not allowed!"); 1.177 + } 1.178 + } 1.179 + 1.180 + public final Packet doProcess(Packet request, RequestContext rc, ResponseContextReceiver receiver) { 1.181 + return super.process(request, rc, receiver); 1.182 + } 1.183 + 1.184 + public final void doProcessAsync(AsyncResponseImpl<?> receiver, Packet request, RequestContext rc, Fiber.CompletionCallback callback) { 1.185 + super.processAsync(receiver, request, rc, callback); 1.186 + } 1.187 + 1.188 + protected final @NotNull QName getPortName() { 1.189 + return wsdlPort.getName(); 1.190 + } 1.191 + 1.192 + 1.193 + public void setOutboundHeaders(Object... headers) { 1.194 + if(headers==null) 1.195 + throw new IllegalArgumentException(); 1.196 + Header[] hl = new Header[headers.length]; 1.197 + for( int i=0; i<hl.length; i++ ) { 1.198 + if(headers[i]==null) 1.199 + throw new IllegalArgumentException(); 1.200 + hl[i] = Headers.create(seiModel.getBindingContext(),headers[i]); 1.201 + } 1.202 + super.setOutboundHeaders(hl); 1.203 + } 1.204 +}