KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > jaxws > ServiceImpl


1 package org.objectweb.celtix.bus.jaxws;
2
3 import java.lang.reflect.Proxy JavaDoc;
4 import java.net.URI JavaDoc;
5 import java.net.URL JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Vector JavaDoc;
9 import java.util.concurrent.Executor JavaDoc;
10 import java.util.logging.Level JavaDoc;
11 import java.util.logging.Logger JavaDoc;
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 JavaDoc;
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 JavaDoc SERVICE_CONFIGURATION_URI =
41         "http://celtix.objectweb.org/bus/jaxws/service-config";
42     public static final String JavaDoc PORT_CONFIGURATION_URI =
43         "http://celtix.objectweb.org/bus/jaxws/port-config";
44
45     private static final Logger JavaDoc LOG = Logger.getLogger(ServiceImpl.class.getName());
46
47     private URL JavaDoc wsdlLocation;
48     private QName JavaDoc serviceName;
49     private final List JavaDoc<QName JavaDoc> endpointList;
50     private final Bus bus;
51     private HandlerResolver handlerResolver;
52     private Executor JavaDoc executor;
53
54     /**
55      * Create a new Service.
56      * @throws WebServiceException If there is an exception creating Service.
57      */

58     public ServiceImpl(Bus b, URL JavaDoc location, QName JavaDoc name, Class JavaDoc<?> si) {
59         bus = b;
60         wsdlLocation = location;
61         serviceName = name;
62         endpointList = new Vector JavaDoc<QName JavaDoc>();
63         handlerResolver = new HandlerResolverImpl(bus.getConfiguration(), serviceName);
64         executor = bus.getWorkQueueManager().getAutomaticWorkQueue();
65     }
66
67     public void createPort(QName JavaDoc portName, URI JavaDoc bindingId, String JavaDoc endpointAddress) {
68         throw new UnsupportedOperationException JavaDoc("addPort not yet supported");
69     }
70
71     public <T> T getPort(QName JavaDoc portName, Class JavaDoc<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 JavaDoc<T> serviceEndpointInterface) {
80         return createPort(null, serviceEndpointInterface);
81     }
82
83     public <T> Dispatch<T> createDispatch(QName JavaDoc portName, Class JavaDoc<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 JavaDoc> createDispatch(QName JavaDoc 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 JavaDoc>(bus, ref, mode, context, Object JavaDoc.class, executor);
102     }
103
104     public QName JavaDoc getServiceName() {
105         return serviceName;
106     }
107
108     public Iterator JavaDoc<QName JavaDoc> getPorts() {
109         return endpointList.iterator();
110     }
111
112     public URL JavaDoc getWSDLDocumentLocation() {
113         return wsdlLocation;
114     }
115
116     protected <T> T createPort(QName JavaDoc portName, Class JavaDoc<T> serviceEndpointInterface) {
117
118         LOG.log(Level.FINE, "creating port for portName", portName);
119         LOG.log(Level.FINE, "endpoint interface:", serviceEndpointInterface);
120
121         //Assuming Annotation is Present
122
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 JavaDoc obj = Proxy.newProxyInstance(serviceEndpointInterface.getClassLoader(),
147                                             new Class JavaDoc[] {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 JavaDoc<T> serviceEndpointInterface,
159                                                   QName JavaDoc 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 JavaDoc<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 JavaDoc getWsdlLocation(WebService wsAnnotation) {
170
171         URL JavaDoc url = null;
172         if (wsAnnotation != null) {
173             try {
174                 url = new URL JavaDoc(wsAnnotation.wsdlLocation());
175             } catch (java.net.MalformedURLException JavaDoc mue) {
176                 mue.printStackTrace();
177             }
178         }
179         return url;
180     }
181
182     private QName JavaDoc getServiceName(WebService wsAnnotation) {
183
184         QName JavaDoc serviceQName = null;
185         if (wsAnnotation != null) {
186             serviceQName = new QName JavaDoc(wsAnnotation.targetNamespace(), wsAnnotation.serviceName());
187         }
188
189         return serviceQName;
190     }
191
192     @Override JavaDoc
193     public void addPort(QName JavaDoc arg0, String JavaDoc arg1, String JavaDoc arg2) {
194         // TODO Auto-generated method stub
195

196     }
197
198     @Override JavaDoc
199     public HandlerResolver getHandlerResolver() {
200         return handlerResolver;
201     }
202
203     @Override JavaDoc
204     public void setHandlerResolver(HandlerResolver hr) {
205         handlerResolver = hr;
206     }
207
208     public Executor JavaDoc getExecutor() {
209         return executor;
210     }
211
212     public void setExecutor(Executor JavaDoc e) {
213         executor = e;
214     }
215
216     //find the configuration for the port as a child of the bus configuration, or have
217
//the builder create it if it does not exist yet
218
private Configuration createPortConfiguration(QName JavaDoc portName, EndpointReferenceType ref) {
219
220         Configuration portCfg = null;
221         String JavaDoc 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         // add the additional provider
230

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