1 package org.objectweb.celtix.bus.jaxws; 2 3 import java.util.Map ; 4 import java.util.concurrent.ExecutionException ; 5 import java.util.concurrent.Future ; 6 import java.util.concurrent.TimeUnit ; 7 import java.util.concurrent.TimeoutException ; 8 import java.util.logging.Level ; 9 import java.util.logging.Logger ; 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 LOG = LogUtils.getL7dLogger(AsyncResponse.class); 22 private final Future <ObjectMessageContext> fObjMsgContext; 23 private T result; 24 private Class <T> cls; 25 26 public AsyncResponse(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 , ExecutionException { 44 if (result == null) { 45 ObjectMessageContext ctx = fObjMsgContext.get(); 46 result = cls.cast(ctx.getReturn()); 47 Throwable t = ctx.getException(); 48 if (t != null) { 49 if (WebServiceException.class.isAssignableFrom(t.getClass())) { 50 throw new ExecutionException (t); 51 } else { 52 throw new ExecutionException (new WebServiceException(t)); 53 } 54 } 55 } 56 return result; 57 } 58 59 60 public T get(long timeout, TimeUnit unit) 61 throws InterruptedException , ExecutionException , TimeoutException { 62 if (result == null) { 63 ObjectMessageContext ctx = fObjMsgContext.get(timeout, unit); 64 result = cls.cast(ctx.getReturn()); 65 Throwable t = ctx.getException(); 66 if (t != null) { 67 if (WebServiceException.class.isAssignableFrom(t.getClass())) { 68 throw new ExecutionException (t); 69 } else { 70 throw new ExecutionException (new WebServiceException(t)); 71 } 72 } 73 } 74 return result; 75 } 76 77 public Map <String , Object > getContext() { 78 try { 79 return fObjMsgContext.get(); 80 } catch (Exception ex) { 81 LOG.log(Level.SEVERE, "Exception occured getting context", ex); 82 return null; 83 } 84 } 85 86 } 87 | Popular Tags |