|
1 /* |
|
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.ws.client.sei; |
|
27 |
|
28 //import com.sun.tools.internal.ws.wsdl.document.soap.SOAPBinding; |
|
29 |
|
30 import com.sun.istack.internal.NotNull; |
|
31 import com.sun.istack.internal.Nullable; |
|
32 import com.sun.xml.internal.ws.api.databinding.ClientCallBridge; |
|
33 import com.sun.xml.internal.ws.api.message.Message; |
|
34 import com.sun.xml.internal.ws.api.message.Packet; |
|
35 import com.sun.xml.internal.ws.api.pipe.Fiber; |
|
36 import com.sun.xml.internal.ws.api.pipe.FiberContextSwitchInterceptor; |
|
37 import com.sun.xml.internal.ws.client.AsyncInvoker; |
|
38 import com.sun.xml.internal.ws.client.AsyncResponseImpl; |
|
39 import com.sun.xml.internal.ws.client.RequestContext; |
|
40 import com.sun.xml.internal.ws.client.ResponseContext; |
|
41 import com.sun.xml.internal.ws.fault.SOAPFaultBuilder; |
|
42 import com.sun.xml.internal.ws.model.JavaMethodImpl; |
|
43 import com.sun.xml.internal.ws.model.ParameterImpl; |
|
44 import com.sun.xml.internal.ws.model.WrapperParameter; |
|
45 import com.sun.xml.internal.org.jvnet.ws.databinding.JavaCallInfo; |
|
46 |
|
47 import javax.jws.soap.SOAPBinding.Style; |
|
48 import javax.xml.ws.AsyncHandler; |
|
49 import javax.xml.ws.Response; |
|
50 import javax.xml.ws.WebServiceException; |
|
51 |
|
52 import java.lang.reflect.Method; |
|
53 import java.util.List; |
|
54 |
|
55 /** |
|
56 * Common part between {@link CallbackMethodHandler} and {@link PollingMethodHandler}. |
|
57 * |
|
58 * @author Kohsuke Kawaguchi |
|
59 * @author Jitendra Kotamraju |
|
60 */ |
|
61 abstract class AsyncMethodHandler extends MethodHandler { |
|
62 |
|
63 AsyncMethodHandler(SEIStub owner, Method m) { |
|
64 super(owner, m); |
|
65 } |
|
66 |
|
67 // private final ResponseBuilder responseBuilder; |
|
68 // /** |
|
69 // * Async bean class that has setters for all out parameters |
|
70 // */ |
|
71 // private final @Nullable Class asyncBeanClass; |
|
72 // |
|
73 // AsyncMethodHandler(SEIStub owner, JavaMethodImpl jm, JavaMethodImpl sync) { |
|
74 // super(owner, sync); |
|
75 // |
|
76 // List<ParameterImpl> rp = sync.getResponseParameters(); |
|
77 // int size = 0; |
|
78 // for( ParameterImpl param : rp ) { |
|
79 // if (param.isWrapperStyle()) { |
|
80 // WrapperParameter wrapParam = (WrapperParameter)param; |
|
81 // size += wrapParam.getWrapperChildren().size(); |
|
82 // if (sync.getBinding().getStyle() == Style.DOCUMENT) { |
|
83 // // doc/asyncBeanClass - asyncBeanClass bean is in async signature |
|
84 // // Add 2 or more so that it is considered as async bean case |
|
85 // size += 2; |
|
86 // } |
|
87 // } else { |
|
88 // ++size; |
|
89 // } |
|
90 // } |
|
91 // |
|
92 // Class tempWrap = null; |
|
93 // if (size > 1) { |
|
94 // rp = jm.getResponseParameters(); |
|
95 // for(ParameterImpl param : rp) { |
|
96 // if (param.isWrapperStyle()) { |
|
97 // WrapperParameter wrapParam = (WrapperParameter)param; |
|
98 // if (sync.getBinding().getStyle() == Style.DOCUMENT) { |
|
99 // // doc/asyncBeanClass style |
|
100 // tempWrap = (Class)wrapParam.getTypeReference().type; |
|
101 // break; |
|
102 // } |
|
103 // for(ParameterImpl p : wrapParam.getWrapperChildren()) { |
|
104 // if (p.getIndex() == -1) { |
|
105 // tempWrap = (Class)p.getTypeReference().type; |
|
106 // break; |
|
107 // } |
|
108 // } |
|
109 // if (tempWrap != null) { |
|
110 // break; |
|
111 // } |
|
112 // } else { |
|
113 // if (param.getIndex() == -1) { |
|
114 // tempWrap = (Class)param.getTypeReference().type; |
|
115 // break; |
|
116 // } |
|
117 // } |
|
118 // } |
|
119 // } |
|
120 // asyncBeanClass = tempWrap; |
|
121 // |
|
122 // switch(size) { |
|
123 // case 0 : |
|
124 // responseBuilder = buildResponseBuilder(sync, ValueSetterFactory.NONE); |
|
125 // break; |
|
126 // case 1 : |
|
127 // responseBuilder = buildResponseBuilder(sync, ValueSetterFactory.SINGLE); |
|
128 // break; |
|
129 // default : |
|
130 // responseBuilder = buildResponseBuilder(sync, new ValueSetterFactory.AsyncBeanValueSetterFactory(asyncBeanClass)); |
|
131 // } |
|
132 // |
|
133 // } |
|
134 |
|
135 protected final Response<Object> doInvoke(Object proxy, Object[] args, AsyncHandler handler) { |
|
136 |
|
137 AsyncInvoker invoker = new SEIAsyncInvoker(proxy, args); |
|
138 invoker.setNonNullAsyncHandlerGiven(handler != null); |
|
139 AsyncResponseImpl<Object> ft = new AsyncResponseImpl<Object>(invoker,handler); |
|
140 invoker.setReceiver(ft); |
|
141 ft.run(); |
|
142 return ft; |
|
143 } |
|
144 |
|
145 private class SEIAsyncInvoker extends AsyncInvoker { |
|
146 // snapshot the context now. this is necessary to avoid concurrency issue, |
|
147 // and is required by the spec |
|
148 private final RequestContext rc = owner.requestContext.copy(); |
|
149 private final Object[] args; |
|
150 |
|
151 SEIAsyncInvoker(Object proxy, Object[] args) { |
|
152 this.args = args; |
|
153 } |
|
154 |
|
155 public void do_run () { |
|
156 JavaCallInfo call = owner.databinding.createJavaCallInfo(method, args); |
|
157 Packet req = (Packet)owner.databinding.serializeRequest(call); |
|
158 |
|
159 Fiber.CompletionCallback callback = new Fiber.CompletionCallback() { |
|
160 |
|
161 public void onCompletion(@NotNull Packet response) { |
|
162 responseImpl.setResponseContext(new ResponseContext(response)); |
|
163 Message msg = response.getMessage(); |
|
164 if (msg == null) { |
|
165 return; |
|
166 } |
|
167 try { |
|
168 Object[] rargs = new Object[1]; |
|
169 JavaCallInfo call = owner.databinding.createJavaCallInfo(method, rargs); |
|
170 call = owner.databinding.deserializeResponse(response, call); |
|
171 if (call.getException() != null) { |
|
172 throw call.getException(); |
|
173 } else { |
|
174 responseImpl.set(rargs[0], null); |
|
175 } |
|
176 // dbHandler.readResponse(response, call); |
|
177 // responseImpl.set(rargs[0], null); |
|
178 // if(msg.isFault()) { |
|
179 // SOAPFaultBuilder faultBuilder = SOAPFaultBuilder.create(msg); |
|
180 // throw faultBuilder.createException(checkedExceptions); |
|
181 // } else { |
|
182 // Object[] rargs = new Object[1]; |
|
183 // if (asyncBeanClass != null) { |
|
184 // rargs[0] = asyncBeanClass.newInstance(); |
|
185 // } |
|
186 // responseBuilder.readResponse(msg, rargs); |
|
187 // responseImpl.set(rargs[0], null); |
|
188 // } |
|
189 } catch (Throwable t) { |
|
190 if (t instanceof RuntimeException) { |
|
191 if (t instanceof WebServiceException) { |
|
192 responseImpl.set(null, t); |
|
193 return; |
|
194 } |
|
195 } else if (t instanceof Exception) { |
|
196 responseImpl.set(null, t); |
|
197 return; |
|
198 } |
|
199 //its RuntimeException or some other exception resulting from user error, wrap it in |
|
200 // WebServiceException |
|
201 responseImpl.set(null, new WebServiceException(t)); |
|
202 } |
|
203 } |
|
204 |
|
205 |
|
206 public void onCompletion(@NotNull Throwable error) { |
|
207 if (error instanceof WebServiceException) { |
|
208 responseImpl.set(null, error); |
|
209 } else { |
|
210 //its RuntimeException or some other exception resulting from user error, wrap it in |
|
211 // WebServiceException |
|
212 responseImpl.set(null, new WebServiceException(error)); |
|
213 } |
|
214 } |
|
215 }; |
|
216 owner.doProcessAsync(responseImpl, req, rc, callback); |
|
217 } |
|
218 } |
|
219 |
|
220 ValueGetterFactory getValueGetterFactory() { |
|
221 return ValueGetterFactory.ASYNC; |
|
222 } |
|
223 |
|
224 } |