KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > routing > StreamSourceMessageProvider


1 package org.objectweb.celtix.routing;
2
3 import java.net.MalformedURLException JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.logging.Level JavaDoc;
9 import java.util.logging.Logger JavaDoc;
10
11 import javax.annotation.Resource;
12 import javax.wsdl.Definition;
13 import javax.wsdl.Port;
14 import javax.xml.namespace.QName JavaDoc;
15 import javax.xml.transform.stream.StreamSource JavaDoc;
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 JavaDoc> {
34     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(StreamSourceMessageProvider.class);
35     protected List JavaDoc<Dispatch<StreamSource JavaDoc>> dList;
36     private final Definition wsdlModel;
37     private final RouteType route;
38     private final URL JavaDoc wsdlLocation;
39     private boolean doInit;
40
41     /**
42      * Injectable context.
43      */

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 JavaDoc(wsdlModel.getDocumentBaseURI());
54         } catch (MalformedURLException JavaDoc 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 JavaDoc invoke(StreamSource JavaDoc request) {
69         if (doInit) {
70             init();
71         }
72         
73         Dispatch<StreamSource JavaDoc> dispatch = dList.get(0);
74
75         //TODO Set Request/Response Context like Transport Attributes.
76
updateRequestContext(dispatch.getRequestContext());
77
78         //TODO Use Async API
79
StreamSource JavaDoc resp = dispatch.invoke(request);
80         
81         updateWebServiceContext(dispatch.getResponseContext());
82         return resp;
83     }
84     
85     protected synchronized void init() {
86         if (doInit) {
87             List JavaDoc<DestinationType> dtList = route.getDestination();
88             if (null == dList) {
89                 dList = new ArrayList JavaDoc<Dispatch<StreamSource JavaDoc>>(dtList.size());
90             }
91
92             for (DestinationType dt : dtList) {
93                 Service dtService = createService(wsdlLocation, dt.getService());
94                 String JavaDoc 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 JavaDoc> streamDispatch = createDispatch(dtService, dt.getPort());
105                 if (null == streamDispatch) {
106                     LOG.log(Level.SEVERE,
107                             "CREATEDISPATCH_FAILURE",
108                             new Object JavaDoc[] {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 JavaDoc wsdlUrl, QName JavaDoc serviceName) {
121         //TODO Set Executor used by the Source Endpoint onto Service
122
//Currently destination service uses bus workqueue.
123
return Service.create(wsdlUrl, serviceName);
124     }
125     
126     protected Dispatch<StreamSource JavaDoc> createDispatch(Service destService, String JavaDoc portName) {
127         QName JavaDoc port = new QName JavaDoc(destService.getServiceName().getNamespaceURI(), portName);
128
129         return destService.createDispatch(port,
130                                       StreamSource JavaDoc.class,
131                                       Service.Mode.MESSAGE);
132     }
133     
134     private void updateRequestContext(Map JavaDoc<String JavaDoc, Object JavaDoc> 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 JavaDoc<String JavaDoc, Object JavaDoc> respCtx) {
143         //TODO
144
}
145 }
146
Popular Tags