1 package org.objectweb.celtix.bus.jaxws; 2 3 import java.util.concurrent.Executor ; 4 import java.util.concurrent.Future ; 5 import java.util.logging.Level ; 6 import java.util.logging.Logger ; 7 8 import javax.xml.bind.JAXBContext; 9 import javax.xml.ws.AsyncHandler; 10 import javax.xml.ws.Dispatch; 11 import javax.xml.ws.ProtocolException; 12 import javax.xml.ws.Response; 13 import javax.xml.ws.Service; 14 import javax.xml.ws.WebServiceException; 15 16 import org.objectweb.celtix.Bus; 17 18 import org.objectweb.celtix.bindings.BindingFactory; 19 import org.objectweb.celtix.bindings.ClientBinding; 20 import org.objectweb.celtix.bindings.DataBindingCallback.Mode; 21 import org.objectweb.celtix.common.logging.LogUtils; 22 import org.objectweb.celtix.context.ObjectMessageContext; 23 import org.objectweb.celtix.ws.addressing.EndpointReferenceType; 24 25 public class DispatchImpl<T> extends BindingProviderImpl implements Dispatch<T> { 26 private static final Logger LOG = LogUtils.getL7dLogger(EndpointInvocationHandler.class); 27 28 protected ClientBinding cb; 29 protected DynamicDataBindingCallback callback; 30 31 private Bus bus; 32 private EndpointReferenceType ref; 33 private Mode mode; 34 private Class <T> cl; 35 private Executor executor; 36 private JAXBContext context; 37 private boolean initialised; 38 39 40 41 42 DispatchImpl(Bus b, EndpointReferenceType r, Service.Mode m, Class <T> clazz, Executor e) { 43 bus = b; 44 ref = r; 45 mode = Mode.fromServiceMode(m); 46 cl = clazz; 47 cb = null; 48 callback = null; 49 executor = e; 50 context = null; 51 initialised = false; 52 } 53 54 DispatchImpl(Bus b, EndpointReferenceType r, Service.Mode m, 55 JAXBContext ctx, Class <T> clazz, Executor e) { 56 bus = b; 57 ref = r; 58 mode = Mode.fromServiceMode(m); 59 cb = null; 60 callback = null; 61 executor = e; 62 context = ctx; 63 cl = clazz; 64 initialised = false; 65 } 66 67 protected void init() { 68 cb = createClientBinding(); 69 setBinding(cb.getBinding()); 70 if (context == null) { 71 callback = new DynamicDataBindingCallback(cl, mode); 72 } else { 73 callback = new DynamicDataBindingCallback(context, mode); 74 } 75 initialised = true; 76 } 77 78 public T invoke(T obj) { 79 80 if (LOG.isLoggable(Level.INFO)) { 81 LOG.info("Dispatch: invoke called"); 82 } 83 84 if (!initialised) { 85 init(); 86 } 87 88 ObjectMessageContext objMsgContext = cb.createObjectContext(); 89 objMsgContext.putAll(getRequestContext()); 95 objMsgContext.setMessageObjects(obj); 96 97 try { 98 objMsgContext = cb.invoke(objMsgContext, callback); 99 } catch (Exception ex) { 100 throwWebServiceException(ex); 101 } finally { 102 throwProtocolException(objMsgContext.getException()); 104 } 105 106 populateResponseContext(objMsgContext); 107 108 return cl.cast(objMsgContext.getReturn()); 109 } 110 111 public Future <?> invokeAsync(T obj, AsyncHandler<T> asyncHandler) { 112 113 if (LOG.isLoggable(Level.INFO)) { 114 LOG.info("Dispatch: callback invokeAsync called"); 115 } 116 117 if (!initialised) { 118 init(); 119 } 120 121 ObjectMessageContext objMsgContext = cb.createObjectContext(); 122 objMsgContext.putAll(getRequestContext()); 123 objMsgContext.setMessageObjects(obj); 124 125 AsyncCallbackFuture future = null; 126 127 try { 128 Future <ObjectMessageContext> objMsgContextAsynch = 129 cb.invokeAsync(objMsgContext, callback, executor); 130 Response<T> r = new AsyncResponse<T>(objMsgContextAsynch, cl); 131 future = new AsyncCallbackFuture(r, asyncHandler); 132 executor.execute(future); 133 } catch (Exception ex) { 134 throwWebServiceException(ex); 135 } 136 137 return future; 138 139 } 140 141 public Response<T> invokeAsync(T obj) { 142 143 if (LOG.isLoggable(Level.INFO)) { 144 LOG.info("Dispatch: polling invokeAsync called"); 145 } 146 147 if (!initialised) { 148 init(); 149 } 150 151 ObjectMessageContext objMsgContext = cb.createObjectContext(); 152 objMsgContext.putAll(getRequestContext()); 153 objMsgContext.setMessageObjects(obj); 154 155 Response<T> response = null; 156 157 try { 158 Future <ObjectMessageContext> objMsgContextAsynch = 159 cb.invokeAsync(objMsgContext, callback, executor); 160 response = new AsyncResponse<T>(objMsgContextAsynch, cl); 161 } catch (Exception ex) { 162 throwWebServiceException(ex); 163 } 164 165 return response; 166 } 167 168 public void invokeOneWay(T obj) { 169 170 if (LOG.isLoggable(Level.INFO)) { 171 LOG.info("Dispatch: invokeOneWay called"); 172 } 173 174 if (!initialised) { 175 init(); 176 } 177 178 ObjectMessageContext objMsgContext = cb.createObjectContext(); 179 objMsgContext.putAll(getRequestContext()); 180 objMsgContext.setMessageObjects(obj); 181 182 try { 183 cb.invokeOneWay(objMsgContext, callback); 184 } catch (Exception ex) { 185 throwWebServiceException(ex); 186 } 187 188 } 189 190 private ClientBinding createClientBinding() { 191 String bindingId = "http://schemas.xmlsoap.org/wsdl/soap/"; 193 ClientBinding binding = null; 194 try { 195 BindingFactory factory = bus.getBindingManager().getBindingFactory(bindingId); 196 assert factory != null : "unable to find binding factory for " + bindingId; 197 binding = factory.createClientBinding(ref); 198 } catch (Exception ex) { 199 throw new WebServiceException(ex); 200 } 201 return binding; 202 } 203 204 private void throwWebServiceException(Throwable t) { 205 if (null != t) { 206 LOG.log(Level.SEVERE, "DISPATCH_INVOKE_EXC", cl.getSimpleName()); 207 throw isJAXWSException(t) 208 ? (WebServiceException)t 209 : new WebServiceException(t); 210 } 211 } 212 213 private void throwProtocolException(Throwable t) { 214 if (null != t) { 215 LOG.log(Level.INFO, "DISPATCH_INVOKE_EXC", cl.getSimpleName()); 216 throw isJAXWSException(t) 217 ? (WebServiceException)t 218 : new ProtocolException(t); 219 } 220 } 221 222 private boolean isJAXWSException(Throwable t) { 223 return ProtocolException.class.isAssignableFrom(t.getClass()) 224 || WebServiceException.class.isAssignableFrom(t.getClass()); 225 } 226 227 } 228 | Popular Tags |