KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.celtix.routing;
2
3 import java.lang.reflect.InvocationHandler JavaDoc;
4 import java.lang.reflect.Proxy JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Collection JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.logging.Logger JavaDoc;
11
12 import javax.wsdl.Definition;
13 import javax.wsdl.Port;
14 import javax.wsdl.PortType;
15 import javax.wsdl.Service;
16 import javax.xml.namespace.QName JavaDoc;
17 import javax.xml.transform.Source JavaDoc;
18 import javax.xml.transform.stream.StreamSource JavaDoc;
19 import javax.xml.ws.Endpoint;
20 import javax.xml.ws.WebServiceException;
21
22 import org.objectweb.celtix.bus.configuration.wsdl.WsdlPortProvider;
23 import org.objectweb.celtix.common.i18n.Message;
24 import org.objectweb.celtix.common.logging.LogUtils;
25 import org.objectweb.celtix.jaxb.JAXBUtils;
26 import org.objectweb.celtix.routing.configuration.DestinationType;
27 import org.objectweb.celtix.routing.configuration.RouteType;
28 import org.objectweb.celtix.routing.configuration.SourceType;
29
30
31 public class RouterImpl implements Router {
32     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(Router.class);
33     
34     protected final ClassLoader JavaDoc seiClassLoader;
35     protected final Definition wsdlModel;
36     protected final RouteType route;
37     protected Map JavaDoc<QName JavaDoc, Port> sourcePortMap;
38     protected Map JavaDoc<QName JavaDoc, Port> destPortMap;
39     protected List JavaDoc<Endpoint> epList;
40     
41     public RouterImpl(ClassLoader JavaDoc loader, Definition model , RouteType rt) {
42         seiClassLoader = loader;
43         wsdlModel = model;
44         route = rt;
45         getSourceServicesAndPorts();
46         getDestinationServicesAndPorts();
47         epList = new ArrayList JavaDoc<Endpoint>(route.getSource().size());
48     }
49     
50     public Definition getWSDLModel() {
51         return wsdlModel;
52     }
53     
54     public RouteType getRoute() {
55         return route;
56     }
57     
58     public void init() {
59         List JavaDoc<SourceType> stList = route.getSource();
60         
61         List JavaDoc<Source JavaDoc> metadata = createMetadata();
62         for (SourceType st : stList) {
63             //TODO Config For Pass Through
64
Port p = sourcePortMap.get(st.getService());
65             WsdlPortProvider portProvider = new WsdlPortProvider(p);
66             String JavaDoc srcBindingId = (String JavaDoc) portProvider.getObject("bindingId");
67             Object JavaDoc implementor = null;
68             if (isSameBindingId(srcBindingId)) {
69                 //Pass Through Mode
70
implementor = new StreamSourceMessageProvider(wsdlModel, route);
71             } else {
72                 //CodeGenerated Servant
73
InvocationHandler JavaDoc implHandler = new SEIImplHandler(wsdlModel, route);
74                 implementor = createSEIImplementor(p.getBinding().getPortType(), implHandler);
75             }
76
77             Endpoint sourceEP = Endpoint.create(srcBindingId, implementor);
78
79             Map JavaDoc<String JavaDoc, Object JavaDoc> properties =
80                 createEndpointProperties(st.getService(), p.getName());
81             sourceEP.setMetadata(metadata);
82             sourceEP.setProperties(properties);
83             //TODO Set Executor on endpoint.
84
epList.add(sourceEP);
85         }
86     }
87
88     public void publish() {
89         for (Endpoint ep : epList) {
90             Port port = (Port) sourcePortMap.get(ep.getProperties().get(Endpoint.WSDL_SERVICE));
91             WsdlPortProvider portProvider = new WsdlPortProvider(port);
92             ep.publish((String JavaDoc) portProvider.getObject("address"));
93         }
94     }
95     
96     protected boolean isSameBindingId(String JavaDoc srcId) {
97         Collection JavaDoc<Port> destPorts = destPortMap.values();
98         for (Port destPort : destPorts) {
99             WsdlPortProvider portProvider = new WsdlPortProvider(destPort);
100             String JavaDoc destId = (String JavaDoc) portProvider.getObject("bindingId");
101             
102             if (null == srcId
103                 && null == destId
104                 || srcId.equals(destId)) {
105                 continue;
106             } else {
107                 return false;
108             }
109         }
110         return true;
111     }
112
113     protected Object JavaDoc createSEIImplementor(PortType portType, InvocationHandler JavaDoc implHandler) {
114         Object JavaDoc impl = null;
115         StringBuffer JavaDoc seiName = new StringBuffer JavaDoc();
116         seiName.append(JAXBUtils.namespaceURIToPackage(portType.getQName().getNamespaceURI()));
117         seiName.append(".");
118         seiName.append(JAXBUtils.nameToIdentifier(portType.getQName().getLocalPart(),
119                                                   JAXBUtils.IdentifierType.INTERFACE));
120
121         Class JavaDoc<?> sei = null;
122         try {
123             sei = seiClassLoader.loadClass(seiName.toString());
124         } catch (ClassNotFoundException JavaDoc cnfe) {
125             throw new WebServiceException("Could not load sei", cnfe);
126         }
127         
128         impl = Proxy.newProxyInstance(sei.getClassLoader(),
129                                       new Class JavaDoc[] {sei},
130                                       implHandler);
131         return impl;
132     }
133
134     private Map JavaDoc<String JavaDoc, Object JavaDoc> createEndpointProperties(QName JavaDoc serviceName, String JavaDoc portName) {
135         Map JavaDoc<String JavaDoc, Object JavaDoc> props = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(2);
136         props.put(Endpoint.WSDL_SERVICE, serviceName);
137         props.put(Endpoint.WSDL_PORT, new QName JavaDoc(serviceName.getNamespaceURI(), portName));
138         return props;
139     }
140
141     private List JavaDoc<Source JavaDoc> createMetadata() {
142         List JavaDoc<Source JavaDoc> metadata = new ArrayList JavaDoc<Source JavaDoc>();
143         metadata.add(new StreamSource JavaDoc(wsdlModel.getDocumentBaseURI()));
144         return metadata;
145     }
146     
147     private void getSourceServicesAndPorts() {
148         List JavaDoc<SourceType> stList = route.getSource();
149         
150         if (null == sourcePortMap) {
151             sourcePortMap = new HashMap JavaDoc<QName JavaDoc, Port>(stList.size());
152         }
153
154         for (SourceType st : stList) {
155             Service sourceService = wsdlModel.getService(st.getService());
156             if (null == sourceService) {
157                 throw new WebServiceException(
158                             new Message("UNDEFINED_SERVICE", LOG, st.getService()).toString());
159             }
160             Port sourcePort = sourceService.getPort(st.getPort());
161             
162             if (null == sourcePort) {
163                 throw new WebServiceException(
164                             new Message("UNDEFINED_PORT", LOG, st.getPort()).toString());
165             }
166             sourcePortMap.put(sourceService.getQName(), sourcePort);
167         }
168     }
169     
170     private void getDestinationServicesAndPorts() {
171         List JavaDoc<DestinationType> dtList = route.getDestination();
172         
173         if (null == destPortMap) {
174             destPortMap = new HashMap JavaDoc<QName JavaDoc, Port>(dtList.size());
175         }
176
177         for (DestinationType dt : dtList) {
178             Service destService = wsdlModel.getService(dt.getService());
179             if (null == destService) {
180                 throw new WebServiceException(
181                             new Message("UNDEFINED_SERVICE", LOG, dt.getService()).toString());
182             }
183             Port destPort = destService.getPort(dt.getPort());
184             
185             if (null == destPort) {
186                 throw new WebServiceException(
187                             new Message("UNDEFINED_PORT", LOG, dt.getPort()).toString());
188             }
189             destPortMap.put(destService.getQName(), destPort);
190         }
191     }
192 }
193
Popular Tags