KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > jaxws > AsyncResponse


1 package org.objectweb.celtix.bus.jaxws;
2
3 import java.util.Map JavaDoc;
4 import java.util.concurrent.ExecutionException JavaDoc;
5 import java.util.concurrent.Future JavaDoc;
6 import java.util.concurrent.TimeUnit JavaDoc;
7 import java.util.concurrent.TimeoutException JavaDoc;
8 import java.util.logging.Level JavaDoc;
9 import java.util.logging.Logger JavaDoc;
10
11
12
13 import javax.xml.ws.Response;
14 import javax.xml.ws.WebServiceException;
15
16 import org.objectweb.celtix.common.logging.LogUtils;
17 import org.objectweb.celtix.context.ObjectMessageContext;
18
19 public class AsyncResponse<T> implements Response<T> {
20     
21     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(AsyncResponse.class);
22     private final Future JavaDoc<ObjectMessageContext> fObjMsgContext;
23     private T result;
24     private Class JavaDoc<T> cls;
25     
26     public AsyncResponse(Future JavaDoc<ObjectMessageContext> futureObjMsgContext, Class JavaDoc<T> c) {
27         fObjMsgContext = futureObjMsgContext;
28         cls = c;
29     }
30     
31     public boolean cancel(boolean interrupt) {
32         return fObjMsgContext.cancel(interrupt);
33     }
34     
35     public boolean isCancelled() {
36         return fObjMsgContext.isCancelled();
37     }
38
39     public boolean isDone() {
40         return fObjMsgContext.isDone();
41     }
42
43     public synchronized T get() throws InterruptedException JavaDoc, ExecutionException JavaDoc {
44         if (result == null) {
45             ObjectMessageContext ctx = fObjMsgContext.get();
46             result = cls.cast(ctx.getReturn());
47             Throwable JavaDoc t = ctx.getException();
48             if (t != null) {
49                 if (WebServiceException.class.isAssignableFrom(t.getClass())) {
50                     throw new ExecutionException JavaDoc(t);
51                 } else {
52                     throw new ExecutionException JavaDoc(new WebServiceException(t));
53                 }
54             }
55         }
56         return result;
57     }
58
59     
60     public T get(long timeout, TimeUnit JavaDoc unit)
61         throws InterruptedException JavaDoc, ExecutionException JavaDoc, TimeoutException JavaDoc {
62         if (result == null) {
63             ObjectMessageContext ctx = fObjMsgContext.get(timeout, unit);
64             result = cls.cast(ctx.getReturn());
65             Throwable JavaDoc t = ctx.getException();
66             if (t != null) {
67                 if (WebServiceException.class.isAssignableFrom(t.getClass())) {
68                     throw new ExecutionException JavaDoc(t);
69                 } else {
70                     throw new ExecutionException JavaDoc(new WebServiceException(t));
71                 }
72             }
73         }
74         return result;
75     }
76     
77     public Map JavaDoc<String JavaDoc, Object JavaDoc> getContext() {
78         try {
79             return fObjMsgContext.get();
80         } catch (Exception JavaDoc ex) {
81             LOG.log(Level.SEVERE, "Exception occured getting context", ex);
82             return null;
83         }
84     }
85
86 }
87
Popular Tags