1 package org.objectweb.celtix.routing; 2 3 import java.lang.reflect.InvocationHandler ; 4 import java.lang.reflect.Proxy ; 5 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.HashMap ; 8 import java.util.List ; 9 import java.util.Map ; 10 import java.util.logging.Logger ; 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 ; 17 import javax.xml.transform.Source ; 18 import javax.xml.transform.stream.StreamSource ; 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 LOG = LogUtils.getL7dLogger(Router.class); 33 34 protected final ClassLoader seiClassLoader; 35 protected final Definition wsdlModel; 36 protected final RouteType route; 37 protected Map <QName , Port> sourcePortMap; 38 protected Map <QName , Port> destPortMap; 39 protected List <Endpoint> epList; 40 41 public RouterImpl(ClassLoader loader, Definition model , RouteType rt) { 42 seiClassLoader = loader; 43 wsdlModel = model; 44 route = rt; 45 getSourceServicesAndPorts(); 46 getDestinationServicesAndPorts(); 47 epList = new ArrayList <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 <SourceType> stList = route.getSource(); 60 61 List <Source > metadata = createMetadata(); 62 for (SourceType st : stList) { 63 Port p = sourcePortMap.get(st.getService()); 65 WsdlPortProvider portProvider = new WsdlPortProvider(p); 66 String srcBindingId = (String ) portProvider.getObject("bindingId"); 67 Object implementor = null; 68 if (isSameBindingId(srcBindingId)) { 69 implementor = new StreamSourceMessageProvider(wsdlModel, route); 71 } else { 72 InvocationHandler implHandler = new SEIImplHandler(wsdlModel, route); 74 implementor = createSEIImplementor(p.getBinding().getPortType(), implHandler); 75 } 76 77 Endpoint sourceEP = Endpoint.create(srcBindingId, implementor); 78 79 Map <String , Object > properties = 80 createEndpointProperties(st.getService(), p.getName()); 81 sourceEP.setMetadata(metadata); 82 sourceEP.setProperties(properties); 83 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 ) portProvider.getObject("address")); 93 } 94 } 95 96 protected boolean isSameBindingId(String srcId) { 97 Collection <Port> destPorts = destPortMap.values(); 98 for (Port destPort : destPorts) { 99 WsdlPortProvider portProvider = new WsdlPortProvider(destPort); 100 String destId = (String ) 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 createSEIImplementor(PortType portType, InvocationHandler implHandler) { 114 Object impl = null; 115 StringBuffer seiName = new StringBuffer (); 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 <?> sei = null; 122 try { 123 sei = seiClassLoader.loadClass(seiName.toString()); 124 } catch (ClassNotFoundException cnfe) { 125 throw new WebServiceException("Could not load sei", cnfe); 126 } 127 128 impl = Proxy.newProxyInstance(sei.getClassLoader(), 129 new Class [] {sei}, 130 implHandler); 131 return impl; 132 } 133 134 private Map <String , Object > createEndpointProperties(QName serviceName, String portName) { 135 Map <String , Object > props = new HashMap <String , Object >(2); 136 props.put(Endpoint.WSDL_SERVICE, serviceName); 137 props.put(Endpoint.WSDL_PORT, new QName (serviceName.getNamespaceURI(), portName)); 138 return props; 139 } 140 141 private List <Source > createMetadata() { 142 List <Source > metadata = new ArrayList <Source >(); 143 metadata.add(new StreamSource (wsdlModel.getDocumentBaseURI())); 144 return metadata; 145 } 146 147 private void getSourceServicesAndPorts() { 148 List <SourceType> stList = route.getSource(); 149 150 if (null == sourcePortMap) { 151 sourcePortMap = new HashMap <QName , 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 <DestinationType> dtList = route.getDestination(); 172 173 if (null == destPortMap) { 174 destPortMap = new HashMap <QName , 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 |