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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
equal deleted inserted replaced
-1:000000000000 0:373ffda63c9a
1 /*
2 * Copyright (c) 1997, 2012, 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;
27
28 import com.sun.istack.internal.Nullable;
29 import com.sun.xml.internal.ws.api.Cancelable;
30 import com.sun.xml.internal.ws.util.CompletedFuture;
31
32 import javax.xml.ws.AsyncHandler;
33 import javax.xml.ws.Response;
34 import javax.xml.ws.WebServiceException;
35 import java.util.Map;
36 import java.util.concurrent.FutureTask;
37
38 /**
39 * {@link Response} implementation. When Runnbale is executed, it just hands the
40 * request to Fiber and returns. When the Fiber finishes the execution, it sets
41 * response in the {@link FutureTask}
42 *
43 * @author Jitendra Kotamraju
44 */
45 public final class AsyncResponseImpl<T> extends FutureTask<T> implements Response<T>, ResponseContextReceiver {
46
47 /**
48 * Optional {@link AsyncHandler} that gets invoked
49 * at the completion of the task.
50 */
51 private final AsyncHandler<T> handler;
52 private ResponseContext responseContext;
53 private final Runnable callable;
54 private Cancelable cancelable;
55
56 /**
57 *
58 * @param runnable
59 * This {@link Runnable} is executed asynchronously.
60 * @param handler
61 * Optional {@link AsyncHandler} to invoke at the end
62 * of the processing. Can be null.
63 */
64 public AsyncResponseImpl(Runnable runnable, @Nullable AsyncHandler<T> handler) {
65 super(runnable, null);
66 this.callable = runnable;
67 this.handler = handler;
68 }
69
70 @Override
71 public void run() {
72 // override so that AsyncInvoker calls set()
73 // when Fiber calls the callback
74 try {
75 callable.run();
76 } catch (WebServiceException e) {
77 //it could be a WebServiceException or a ProtocolException or any RuntimeException
78 // resulting due to some internal bug.
79 set(null, e);
80 } catch (Throwable e) {
81 //its some other exception resulting from user error, wrap it in
82 // WebServiceException
83 set(null, new WebServiceException(e));
84 }
85 }
86
87
88 public ResponseContext getContext() {
89 return responseContext;
90 }
91
92 public void setResponseContext(ResponseContext rc) {
93 responseContext = rc;
94 }
95
96 public void set(final T v, final Throwable t) {
97 // call the handler before we mark the future as 'done'
98 if (handler!=null) {
99 try {
100 /**
101 * {@link Response} object passed into the callback.
102 * We need a separate {@link java.util.concurrent.Future} because we don't want {@link ResponseImpl}
103 * to be marked as 'done' before the callback finishes execution.
104 * (That would provide implicit synchronization between the application code
105 * in the main thread and the callback code, and is compatible with the JAX-RI 2.0 FCS.
106 */
107 class CallbackFuture<T> extends CompletedFuture<T> implements Response<T> {
108 public CallbackFuture(T v, Throwable t) {
109 super(v, t);
110 }
111
112 public Map<String, Object> getContext() {
113 return AsyncResponseImpl.this.getContext();
114 }
115 }
116 handler.handleResponse(new CallbackFuture<T>(v, t));
117 } catch (Throwable e) {
118 super.setException(e);
119 return;
120 }
121 }
122 if (t != null) {
123 super.setException(t);
124 } else {
125 super.set(v);
126 }
127 }
128
129 public void setCancelable(Cancelable cancelable) {
130 this.cancelable = cancelable;
131 }
132
133 public boolean cancel(boolean mayInterruptIfRunning) {
134 if (cancelable != null)
135 cancelable.cancel(mayInterruptIfRunning);
136 return super.cancel(mayInterruptIfRunning);
137 }
138 }

mercurial