1 package org.objectweb.celtix.routing; 2 3 import java.net.MalformedURLException ; 4 import java.net.URL ; 5 import java.util.ArrayList ; 6 import java.util.List ; 7 import java.util.Map ; 8 import java.util.logging.Level ; 9 import java.util.logging.Logger ; 10 11 import javax.annotation.Resource; 12 import javax.wsdl.Definition; 13 import javax.wsdl.Port; 14 import javax.xml.namespace.QName ; 15 import javax.xml.transform.stream.StreamSource ; 16 import javax.xml.ws.BindingProvider; 17 import javax.xml.ws.Dispatch; 18 import javax.xml.ws.Provider; 19 import javax.xml.ws.Service; 20 import javax.xml.ws.ServiceMode; 21 import javax.xml.ws.WebServiceContext; 22 import javax.xml.ws.WebServiceException; 23 import javax.xml.ws.WebServiceProvider; 24 import javax.xml.ws.handler.MessageContext; 25 26 import org.objectweb.celtix.common.i18n.Message; 27 import org.objectweb.celtix.common.logging.LogUtils; 28 import org.objectweb.celtix.routing.configuration.DestinationType; 29 import org.objectweb.celtix.routing.configuration.RouteType; 30 31 @WebServiceProvider 32 @ServiceMode(value = Service.Mode.MESSAGE) 33 public class StreamSourceMessageProvider implements Provider<StreamSource > { 34 private static final Logger LOG = LogUtils.getL7dLogger(StreamSourceMessageProvider.class); 35 protected List <Dispatch<StreamSource >> dList; 36 private final Definition wsdlModel; 37 private final RouteType route; 38 private final URL wsdlLocation; 39 private boolean doInit; 40 41 44 @Resource 45 private WebServiceContext wsCtx; 46 47 public StreamSourceMessageProvider(Definition model, RouteType rt) { 48 wsdlModel = model; 49 route = rt; 50 doInit = true; 51 52 try { 53 wsdlLocation = new URL (wsdlModel.getDocumentBaseURI()); 54 } catch (MalformedURLException mue) { 55 throw new WebServiceException("Invalid wsdl url", mue); 56 } 57 } 58 59 @Resource 60 public void setContext(WebServiceContext ctx) { 61 wsCtx = ctx; 62 } 63 64 public WebServiceContext getContext() { 65 return wsCtx; 66 } 67 68 public StreamSource invoke(StreamSource request) { 69 if (doInit) { 70 init(); 71 } 72 73 Dispatch<StreamSource > dispatch = dList.get(0); 74 75 updateRequestContext(dispatch.getRequestContext()); 77 78 StreamSource resp = dispatch.invoke(request); 80 81 updateWebServiceContext(dispatch.getResponseContext()); 82 return resp; 83 } 84 85 protected synchronized void init() { 86 if (doInit) { 87 List <DestinationType> dtList = route.getDestination(); 88 if (null == dList) { 89 dList = new ArrayList <Dispatch<StreamSource >>(dtList.size()); 90 } 91 92 for (DestinationType dt : dtList) { 93 Service dtService = createService(wsdlLocation, dt.getService()); 94 String portName; 95 96 if (dt.isSetPort()) { 97 portName = dt.getPort(); 98 } else { 99 javax.wsdl.Service destService = 100 wsdlModel.getService(dt.getService()); 101 portName = ((Port)destService.getPorts().values().iterator()).getName(); 102 } 103 104 Dispatch<StreamSource > streamDispatch = createDispatch(dtService, dt.getPort()); 105 if (null == streamDispatch) { 106 LOG.log(Level.SEVERE, 107 "CREATEDISPATCH_FAILURE", 108 new Object [] {dt.getService(), portName }); 109 throw new WebServiceException(new Message("CREATEDISPATCH_FAILURE", 110 LOG, 111 dt.getService(), 112 portName).toString()); 113 } 114 dList.add(streamDispatch); 115 } 116 doInit = false; 117 } 118 } 119 120 protected Service createService(URL wsdlUrl, QName serviceName) { 121 return Service.create(wsdlUrl, serviceName); 124 } 125 126 protected Dispatch<StreamSource > createDispatch(Service destService, String portName) { 127 QName port = new QName (destService.getServiceName().getNamespaceURI(), portName); 128 129 return destService.createDispatch(port, 130 StreamSource .class, 131 Service.Mode.MESSAGE); 132 } 133 134 private void updateRequestContext(Map <String , Object > reqCtx) { 135 MessageContext sourceMsgCtx = getContext().getMessageContext(); 136 reqCtx.put(BindingProvider.USERNAME_PROPERTY, 137 sourceMsgCtx.get(BindingProvider.USERNAME_PROPERTY)); 138 reqCtx.put(BindingProvider.PASSWORD_PROPERTY, 139 sourceMsgCtx.get(BindingProvider.PASSWORD_PROPERTY)); 140 } 141 142 private void updateWebServiceContext(Map <String , Object > respCtx) { 143 } 145 } 146 | Popular Tags |