01: package org.objectweb.celtix.bus.jaxws;
02:
03: import java.util.Map;
04: import java.util.concurrent.ExecutionException;
05: import java.util.concurrent.Future;
06: import java.util.concurrent.TimeUnit;
07: import java.util.concurrent.TimeoutException;
08: import java.util.logging.Level;
09: import java.util.logging.Logger;
10:
11: import javax.xml.ws.Response;
12: import javax.xml.ws.WebServiceException;
13:
14: import org.objectweb.celtix.common.logging.LogUtils;
15: import org.objectweb.celtix.context.ObjectMessageContext;
16:
17: public class AsyncResponse<T> implements Response<T> {
18:
19: private static final Logger LOG = LogUtils
20: .getL7dLogger(AsyncResponse.class);
21: private final Future<ObjectMessageContext> fObjMsgContext;
22: private T result;
23: private Class<T> cls;
24:
25: public AsyncResponse(
26: Future<ObjectMessageContext> futureObjMsgContext, Class<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,
44: ExecutionException {
45: if (result == null) {
46: ObjectMessageContext ctx = fObjMsgContext.get();
47: result = cls.cast(ctx.getReturn());
48: Throwable t = ctx.getException();
49: if (t != null) {
50: if (WebServiceException.class.isAssignableFrom(t
51: .getClass())) {
52: throw new ExecutionException(t);
53: } else {
54: throw new ExecutionException(
55: new WebServiceException(t));
56: }
57: }
58: }
59: return result;
60: }
61:
62: public T get(long timeout, TimeUnit unit)
63: throws InterruptedException, ExecutionException,
64: TimeoutException {
65: if (result == null) {
66: ObjectMessageContext ctx = fObjMsgContext
67: .get(timeout, unit);
68: result = cls.cast(ctx.getReturn());
69: Throwable t = ctx.getException();
70: if (t != null) {
71: if (WebServiceException.class.isAssignableFrom(t
72: .getClass())) {
73: throw new ExecutionException(t);
74: } else {
75: throw new ExecutionException(
76: new WebServiceException(t));
77: }
78: }
79: }
80: return result;
81: }
82:
83: public Map<String, Object> getContext() {
84: try {
85: return fObjMsgContext.get();
86: } catch (Exception ex) {
87: LOG.log(Level.SEVERE, "Exception occured getting context",
88: ex);
89: return null;
90: }
91: }
92:
93: }
|