001: package org.objectweb.celtix.routing;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005: import java.util.logging.Level;
006: import java.util.logging.Logger;
007:
008: import javax.wsdl.Definition;
009: import javax.wsdl.Port;
010: import javax.wsdl.Service;
011: import javax.wsdl.extensions.ExtensibilityElement;
012: import javax.wsdl.extensions.ExtensionRegistry;
013: import javax.xml.bind.JAXBException;
014: import javax.xml.ws.WebServiceException;
015:
016: import org.objectweb.celtix.Bus; //import org.objectweb.celtix.common.i18n.Message;
017: import org.objectweb.celtix.common.logging.LogUtils;
018: import org.objectweb.celtix.routing.configuration.DestinationType;
019: import org.objectweb.celtix.routing.configuration.RouteType;
020: import org.objectweb.celtix.routing.configuration.SourceType;
021: import org.objectweb.celtix.wsdl.JAXBExtensionHelper;
022:
023: public class RouterFactory {
024: private static final Logger LOG = LogUtils
025: .getL7dLogger(RouterFactory.class);
026: private RouterManager mgr;
027: private Bus bus;
028:
029: public RouterFactory(RouterManager rm) {
030: mgr = rm;
031: }
032:
033: public void init(Bus b) {
034: bus = b;
035: registerRouterExtension(bus.getWSDLManager()
036: .getExtenstionRegistry());
037: }
038:
039: private void registerRouterExtension(ExtensionRegistry registry) {
040: try {
041: JAXBExtensionHelper.addExtensions(registry,
042: Definition.class, RouteType.class);
043: } catch (JAXBException e) {
044: throw new WebServiceException(
045: "Adding of routeType extension failed.");
046: }
047: }
048:
049: @SuppressWarnings("unchecked")
050: public List<Router> addRoutes(Definition def) {
051: List<Router> routerList = new ArrayList<Router>();
052: List<ExtensibilityElement> extList = def
053: .getExtensibilityElements();
054: for (ExtensibilityElement extEl : extList) {
055: if (extEl instanceof RouteType) {
056: RouteType rt = (RouteType) extEl;
057: if (isValidRoute(def, rt)) {
058: Router router = createRouter(def, rt);
059: router.init();
060: routerList.add(router);
061: } else {
062: //throw new WebServiceException(
063: // new Message("UNSUPPORTED_ROUTE", LOG, rt.getName()).toString());
064: if (LOG.isLoggable(Level.SEVERE)) {
065: LOG.log(Level.SEVERE, "UNSUPPORTED_ROUTE", rt
066: .getName());
067: }
068: }
069: }
070: }
071:
072: return routerList;
073: }
074:
075: @SuppressWarnings("unchecked")
076: private boolean isValidRoute(Definition model, RouteType route) {
077: List<SourceType> source = route.getSource();
078: List<DestinationType> dest = route.getDestination();
079: if (source.size() != 1 || dest.size() != 1
080: || route.isSetMultiRoute() || route.isSetOperation()) {
081: return false;
082: }
083:
084: //Check For Different Bindings.
085: SourceType st = source.get(0);
086: DestinationType dt = dest.get(0);
087:
088: //Get Service Name
089: Service sourceService = model.getService(st.getService());
090: Service destService = model.getService(dt.getService());
091: if (null == sourceService || null == destService) {
092: return false;
093: }
094:
095: Port sourcePort = sourceService.getPort(st.getPort());
096: Port destPort = destService.getPort(dt.getPort());
097:
098: if (null == sourcePort || null == destPort) {
099: return false;
100: }
101:
102: return true;
103: }
104:
105: public Router createRouter(Definition model, RouteType route) {
106: return new RouterImpl(mgr.getSEIClassLoader(), model, route);
107: }
108: }
|