| 1 package org.objectweb.celtix.bus.jaxws; 2 3 import java.lang.reflect.Proxy ; 4 import java.net.URI ; 5 import java.net.URL ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Vector ; 9 import java.util.concurrent.Executor ; 10 import java.util.logging.Level ; 11 import java.util.logging.Logger ; 12 13 import javax.jws.WebService; 14 import javax.wsdl.Port; 15 import javax.wsdl.WSDLException; 16 import javax.xml.bind.JAXBContext; 17 import javax.xml.namespace.QName ; 18 import javax.xml.ws.Binding; 19 import javax.xml.ws.BindingProvider; 20 import javax.xml.ws.Dispatch; 21 import javax.xml.ws.Service; 22 import javax.xml.ws.WebServiceException; 23 import javax.xml.ws.handler.Handler; 24 import javax.xml.ws.handler.HandlerResolver; 25 import javax.xml.ws.spi.ServiceDelegate; 26 27 import org.objectweb.celtix.Bus; 28 import org.objectweb.celtix.bus.configuration.wsdl.WsdlPortProvider; 29 import org.objectweb.celtix.bus.handlers.AnnotationHandlerChainBuilder; 30 import org.objectweb.celtix.bus.handlers.HandlerResolverImpl; 31 import org.objectweb.celtix.bus.handlers.PortInfoImpl; 32 import org.objectweb.celtix.configuration.Configuration; 33 import org.objectweb.celtix.configuration.ConfigurationBuilder; 34 import org.objectweb.celtix.configuration.ConfigurationBuilderFactory; 35 import org.objectweb.celtix.ws.addressing.EndpointReferenceType; 36 import org.objectweb.celtix.wsdl.EndpointReferenceUtils; 37 38 public class ServiceImpl extends ServiceDelegate { 39 40 public static final String SERVICE_CONFIGURATION_URI = 41 "http://celtix.objectweb.org/bus/jaxws/service-config"; 42 public static final String PORT_CONFIGURATION_URI = 43 "http://celtix.objectweb.org/bus/jaxws/port-config"; 44 45 private static final Logger LOG = Logger.getLogger(ServiceImpl.class.getName()); 46 47 private URL wsdlLocation; 48 private QName serviceName; 49 private final List <QName > endpointList; 50 private final Bus bus; 51 private HandlerResolver handlerResolver; 52 private Executor executor; 53 54 58 public ServiceImpl(Bus b, URL location, QName name, Class <?> si) { 59 bus = b; 60 wsdlLocation = location; 61 serviceName = name; 62 endpointList = new Vector <QName >(); 63 handlerResolver = new HandlerResolverImpl(bus.getConfiguration(), serviceName); 64 executor = bus.getWorkQueueManager().getAutomaticWorkQueue(); 65 } 66 67 public void createPort(QName portName, URI bindingId, String endpointAddress) { 68 throw new UnsupportedOperationException ("addPort not yet supported"); 69 } 70 71 public <T> T getPort(QName portName, Class <T> serviceEndpointInterface) { 72 if (portName == null) { 73 throw new WebServiceException("No endpoint specified."); 74 } 75 76 return createPort(portName, serviceEndpointInterface); 77 } 78 79 public <T> T getPort(Class <T> serviceEndpointInterface) { 80 return createPort(null, serviceEndpointInterface); 81 } 82 83 public <T> Dispatch<T> createDispatch(QName portName, Class <T> serviceEndpointInterface, 84 Service.Mode mode) { 85 EndpointReferenceType ref = 86 EndpointReferenceUtils.getEndpointReference(wsdlLocation, 87 serviceName, 88 portName.getLocalPart()); 89 createPortConfiguration(portName, ref); 90 return new DispatchImpl<T>(bus, ref, mode, serviceEndpointInterface, executor); 91 } 92 93 public Dispatch<Object > createDispatch(QName portName, JAXBContext context, Service.Mode mode) { 94 95 EndpointReferenceType ref = 96 EndpointReferenceUtils.getEndpointReference(wsdlLocation, 97 serviceName, 98 portName.getLocalPart()); 99 createPortConfiguration(portName, ref); 100 101 return new DispatchImpl<Object >(bus, ref, mode, context, Object .class, executor); 102 } 103 104 public QName getServiceName() { 105 return serviceName; 106 } 107 108 public Iterator <QName > getPorts() { 109 return endpointList.iterator(); 110 } 111 112 public URL getWSDLDocumentLocation() { 113 return wsdlLocation; 114 } 115 116 protected <T> T createPort(QName portName, Class <T> serviceEndpointInterface) { 117 118 LOG.log(Level.FINE, "creating port for portName", portName); 119 LOG.log(Level.FINE, "endpoint interface:", serviceEndpointInterface); 120 121 javax.jws.WebService wsAnnotation = serviceEndpointInterface.getAnnotation(WebService.class); 123 124 if (wsdlLocation == null) { 125 wsdlLocation = getWsdlLocation(wsAnnotation); 126 } 127 128 if (wsdlLocation == null) { 129 throw new WebServiceException("No wsdl url specified"); 130 } 131 132 if (serviceName == null) { 133 serviceName = getServiceName(wsAnnotation); 134 } 135 136 EndpointReferenceType ref = EndpointReferenceUtils.getEndpointReference(wsdlLocation, 137 serviceName, portName.getLocalPart()); 138 139 Configuration portConfiguration = createPortConfiguration(portName, ref); 140 141 EndpointInvocationHandler endpointHandler = 142 new EndpointInvocationHandler(bus, ref, this, portConfiguration, serviceEndpointInterface); 143 144 createHandlerChainForBinding(serviceEndpointInterface, portName, endpointHandler.getBinding()); 145 146 Object obj = Proxy.newProxyInstance(serviceEndpointInterface.getClassLoader(), 147 new Class [] {serviceEndpointInterface, BindingProvider.class}, 148 endpointHandler); 149 150 LOG.log(Level.FINE, "created proxy", obj); 151 152 endpointList.add(portName); 153 154 return serviceEndpointInterface.cast(obj); 155 } 156 157 158 private <T> void createHandlerChainForBinding(Class <T> serviceEndpointInterface, 159 QName portName, Binding binding) { 160 LOG.fine("loading handler chain for service"); 161 assert handlerResolver != null; 162 PortInfoImpl portInfo = new PortInfoImpl(serviceName, portName, null); 163 List <Handler> handlers = handlerResolver.getHandlerChain(portInfo); 164 AnnotationHandlerChainBuilder handlerChainBuilder = new AnnotationHandlerChainBuilder(); 165 handlers = handlerChainBuilder.buildHandlerChainFor(serviceEndpointInterface, handlers); 166 binding.setHandlerChain(handlers); 167 } 168 169 private URL getWsdlLocation(WebService wsAnnotation) { 170 171 URL url = null; 172 if (wsAnnotation != null) { 173 try { 174 url = new URL (wsAnnotation.wsdlLocation()); 175 } catch (java.net.MalformedURLException mue) { 176 mue.printStackTrace(); 177 } 178 } 179 return url; 180 } 181 182 private QName getServiceName(WebService wsAnnotation) { 183 184 QName serviceQName = null; 185 if (wsAnnotation != null) { 186 serviceQName = new QName (wsAnnotation.targetNamespace(), wsAnnotation.serviceName()); 187 } 188 189 return serviceQName; 190 } 191 192 @Override  193 public void addPort(QName arg0, String arg1, String arg2) { 194 196 } 197 198 @Override  199 public HandlerResolver getHandlerResolver() { 200 return handlerResolver; 201 } 202 203 @Override  204 public void setHandlerResolver(HandlerResolver hr) { 205 handlerResolver = hr; 206 } 207 208 public Executor getExecutor() { 209 return executor; 210 } 211 212 public void setExecutor(Executor e) { 213 executor = e; 214 } 215 216 private Configuration createPortConfiguration(QName portName, EndpointReferenceType ref) { 219 220 Configuration portCfg = null; 221 String id = serviceName.toString() + "/" + portName.getLocalPart(); 222 ConfigurationBuilder cb = ConfigurationBuilderFactory.getBuilder(null); 223 portCfg = cb.getConfiguration(PORT_CONFIGURATION_URI, id, 224 bus.getConfiguration()); 225 if (null == portCfg) { 226 portCfg = cb.buildConfiguration(PORT_CONFIGURATION_URI, id, bus.getConfiguration()); 227 } 228 229 231 Port port = null; 232 try { 233 port = EndpointReferenceUtils.getPort(bus.getWSDLManager(), ref); 234 } catch (WSDLException ex) { 235 throw new WebServiceException("Could not get port from wsdl", ex); 236 } 237 portCfg.getProviders().add(new WsdlPortProvider(port)); 238 return portCfg; 239 } 240 241 } 242 243 | Popular Tags |