001: package org.objectweb.celtix.routing;
002:
003: import java.lang.reflect.InvocationHandler;
004: import java.lang.reflect.Proxy;
005: import java.util.ArrayList;
006: import java.util.Collection;
007: import java.util.HashMap;
008: import java.util.List;
009: import java.util.Map;
010: import java.util.logging.Logger;
011:
012: import javax.wsdl.Definition;
013: import javax.wsdl.Port;
014: import javax.wsdl.PortType;
015: import javax.wsdl.Service;
016: import javax.xml.namespace.QName;
017: import javax.xml.transform.Source;
018: import javax.xml.transform.stream.StreamSource;
019: import javax.xml.ws.Endpoint;
020: import javax.xml.ws.WebServiceException;
021:
022: import org.objectweb.celtix.bus.configuration.wsdl.WsdlPortProvider;
023: import org.objectweb.celtix.common.i18n.Message;
024: import org.objectweb.celtix.common.logging.LogUtils;
025: import org.objectweb.celtix.jaxb.JAXBUtils;
026: import org.objectweb.celtix.routing.configuration.DestinationType;
027: import org.objectweb.celtix.routing.configuration.RouteType;
028: import org.objectweb.celtix.routing.configuration.SourceType;
029:
030: public class RouterImpl implements Router {
031: private static final Logger LOG = LogUtils
032: .getL7dLogger(Router.class);
033:
034: protected final ClassLoader seiClassLoader;
035: protected final Definition wsdlModel;
036: protected final RouteType route;
037: protected Map<QName, Port> sourcePortMap;
038: protected Map<QName, Port> destPortMap;
039: protected List<Endpoint> epList;
040:
041: public RouterImpl(ClassLoader loader, Definition model, RouteType rt) {
042: seiClassLoader = loader;
043: wsdlModel = model;
044: route = rt;
045: getSourceServicesAndPorts();
046: getDestinationServicesAndPorts();
047: epList = new ArrayList<Endpoint>(route.getSource().size());
048: }
049:
050: public Definition getWSDLModel() {
051: return wsdlModel;
052: }
053:
054: public RouteType getRoute() {
055: return route;
056: }
057:
058: public void init() {
059: List<SourceType> stList = route.getSource();
060:
061: List<Source> metadata = createMetadata();
062: for (SourceType st : stList) {
063: //TODO Config For Pass Through
064: Port p = sourcePortMap.get(st.getService());
065: WsdlPortProvider portProvider = new WsdlPortProvider(p);
066: String srcBindingId = (String) portProvider
067: .getObject("bindingId");
068: Object implementor = null;
069: if (isSameBindingId(srcBindingId)) {
070: //Pass Through Mode
071: implementor = new StreamSourceMessageProvider(
072: wsdlModel, route);
073: } else {
074: //CodeGenerated Servant
075: InvocationHandler implHandler = new SEIImplHandler(
076: wsdlModel, route);
077: implementor = createSEIImplementor(p.getBinding()
078: .getPortType(), implHandler);
079: }
080:
081: Endpoint sourceEP = Endpoint.create(srcBindingId,
082: implementor);
083:
084: Map<String, Object> properties = createEndpointProperties(
085: st.getService(), p.getName());
086: sourceEP.setMetadata(metadata);
087: sourceEP.setProperties(properties);
088: //TODO Set Executor on endpoint.
089: epList.add(sourceEP);
090: }
091: }
092:
093: public void publish() {
094: for (Endpoint ep : epList) {
095: Port port = (Port) sourcePortMap.get(ep.getProperties()
096: .get(Endpoint.WSDL_SERVICE));
097: WsdlPortProvider portProvider = new WsdlPortProvider(port);
098: ep.publish((String) portProvider.getObject("address"));
099: }
100: }
101:
102: protected boolean isSameBindingId(String srcId) {
103: Collection<Port> destPorts = destPortMap.values();
104: for (Port destPort : destPorts) {
105: WsdlPortProvider portProvider = new WsdlPortProvider(
106: destPort);
107: String destId = (String) portProvider
108: .getObject("bindingId");
109:
110: if (null == srcId && null == destId || srcId.equals(destId)) {
111: continue;
112: } else {
113: return false;
114: }
115: }
116: return true;
117: }
118:
119: protected Object createSEIImplementor(PortType portType,
120: InvocationHandler implHandler) {
121: Object impl = null;
122: StringBuffer seiName = new StringBuffer();
123: seiName.append(JAXBUtils.namespaceURIToPackage(portType
124: .getQName().getNamespaceURI()));
125: seiName.append(".");
126: seiName.append(JAXBUtils.nameToIdentifier(portType.getQName()
127: .getLocalPart(), JAXBUtils.IdentifierType.INTERFACE));
128:
129: Class<?> sei = null;
130: try {
131: sei = seiClassLoader.loadClass(seiName.toString());
132: } catch (ClassNotFoundException cnfe) {
133: throw new WebServiceException("Could not load sei", cnfe);
134: }
135:
136: impl = Proxy.newProxyInstance(sei.getClassLoader(),
137: new Class[] { sei }, implHandler);
138: return impl;
139: }
140:
141: private Map<String, Object> createEndpointProperties(
142: QName serviceName, String portName) {
143: Map<String, Object> props = new HashMap<String, Object>(2);
144: props.put(Endpoint.WSDL_SERVICE, serviceName);
145: props.put(Endpoint.WSDL_PORT, new QName(serviceName
146: .getNamespaceURI(), portName));
147: return props;
148: }
149:
150: private List<Source> createMetadata() {
151: List<Source> metadata = new ArrayList<Source>();
152: metadata.add(new StreamSource(wsdlModel.getDocumentBaseURI()));
153: return metadata;
154: }
155:
156: private void getSourceServicesAndPorts() {
157: List<SourceType> stList = route.getSource();
158:
159: if (null == sourcePortMap) {
160: sourcePortMap = new HashMap<QName, Port>(stList.size());
161: }
162:
163: for (SourceType st : stList) {
164: Service sourceService = wsdlModel.getService(st
165: .getService());
166: if (null == sourceService) {
167: throw new WebServiceException(new Message(
168: "UNDEFINED_SERVICE", LOG, st.getService())
169: .toString());
170: }
171: Port sourcePort = sourceService.getPort(st.getPort());
172:
173: if (null == sourcePort) {
174: throw new WebServiceException(new Message(
175: "UNDEFINED_PORT", LOG, st.getPort()).toString());
176: }
177: sourcePortMap.put(sourceService.getQName(), sourcePort);
178: }
179: }
180:
181: private void getDestinationServicesAndPorts() {
182: List<DestinationType> dtList = route.getDestination();
183:
184: if (null == destPortMap) {
185: destPortMap = new HashMap<QName, Port>(dtList.size());
186: }
187:
188: for (DestinationType dt : dtList) {
189: Service destService = wsdlModel.getService(dt.getService());
190: if (null == destService) {
191: throw new WebServiceException(new Message(
192: "UNDEFINED_SERVICE", LOG, dt.getService())
193: .toString());
194: }
195: Port destPort = destService.getPort(dt.getPort());
196:
197: if (null == destPort) {
198: throw new WebServiceException(new Message(
199: "UNDEFINED_PORT", LOG, dt.getPort()).toString());
200: }
201: destPortMap.put(destService.getQName(), destPort);
202: }
203: }
204: }
|