Search in sources :

Example 1 with CamelRouteContextFactoryBean

use of org.apache.camel.spring.CamelRouteContextFactoryBean in project fabric8 by jboss-fuse.

the class RouteXml method unmarshal.

public XmlModel unmarshal(Document doc, String message) throws Exception {
    Unmarshaller unmarshaller = jaxbContext().createUnmarshaller();
    // ("bean", springNamespace)
    Map<String, String> beans = new HashMap<String, String>();
    // lets pull out the spring beans...
    // TODO: shouldn't we use http://www.springframework.org/schema/beans namespace instead??
    List<Node> beanElems = nodesByNamespace(doc, springNS, "bean");
    for (Node n : beanElems) {
        if (n instanceof Element) {
            String id = ((Element) n).getAttributeValue("id");
            String cn = ((Element) n).getAttributeValue("class");
            if (id != null && cn != null) {
                beans.put(id, cn);
            }
        }
    }
    // now lets pull out the jaxb routes...
    List<String[]> search = Arrays.asList(new String[] { springNS, "routeContext" }, new String[] { springNS, "camelContext" }, new String[] { springNS, "routes" }, new String[] { blueprintNS, "routeContext" }, new String[] { blueprintNS, "camelContext" }, new String[] { blueprintNS, "routes" });
    List<Node> found = new LinkedList<Node>();
    for (String[] pair : search) {
        List<Node> nodes = nodesByNamespace(doc, pair[0], pair[1]);
        int n = nodes.size();
        if (n != 0) {
            if (n > 1) {
                LOG.warn(message + " contains " + n + " <" + pair[1] + "> elements. Only the first one will be used");
            }
            Node node = nodes.get(0);
            found.add(node);
        }
    }
    if (found.size() > 0) {
        Node n = found.get(0);
        if (n != null) {
            String ns = getNamespaceURI(n);
            Node parseNode;
            if (!ns.equals(springNS)) {
                parseNode = cloneAndReplaceNamespace(n, ns, springNS);
            } else {
                parseNode = n;
            }
            boolean justRoutes = false;
            boolean routesContext = false;
            String xmlText = nodeWithNamespacesToText(parseNode, (Element) n);
            Object object = unmarshaller.unmarshal(new StringReader(xmlText));
            CamelContextFactoryBean sc;
            if (object instanceof CamelContextFactoryBean) {
                LOG.debug("Found a valid CamelContextFactoryBean! {}", object);
                sc = (CamelContextFactoryBean) object;
            } else if (object instanceof RoutesDefinition) {
                justRoutes = true;
                sc = new CamelContextFactoryBean();
                sc.setRoutes(((RoutesDefinition) object).getRoutes());
            } else if (object instanceof CamelRouteContextFactoryBean) {
                routesContext = true;
                sc = new CamelContextFactoryBean();
                sc.setRoutes(((CamelRouteContextFactoryBean) object).getRoutes());
            } else if (object instanceof org.apache.camel.blueprint.CamelRouteContextFactoryBean) {
                routesContext = true;
                sc = new CamelContextFactoryBean();
                sc.setRoutes(((org.apache.camel.blueprint.CamelRouteContextFactoryBean) object).getRoutes());
            } else {
                LOG.warn("Unmarshalled not a CamelContext: {}", object);
                sc = new CamelContextFactoryBean();
            }
            return new XmlModel(sc, doc, beans, n, ns, justRoutes, routesContext);
        } else {
            LOG.info(message + " does not contain a CamelContext. Maybe the XML namespace is not spring: '{}' or blueprint: '{}'?", springNS, blueprintNS);
            // lets create a new collection
            return new XmlModel(new CamelContextFactoryBean(), doc, beans, null, CamelNamespaces.springNS, false, false);
        }
    }
    // ?
    return null;
}
Also used : CamelRouteContextFactoryBean(org.apache.camel.spring.CamelRouteContextFactoryBean) HashMap(java.util.HashMap) Node(de.pdark.decentxml.Node) Element(de.pdark.decentxml.Element) LinkedList(java.util.LinkedList) StringReader(java.io.StringReader) RoutesDefinition(org.apache.camel.model.RoutesDefinition) Unmarshaller(javax.xml.bind.Unmarshaller) CamelContextFactoryBean(org.apache.camel.spring.CamelContextFactoryBean)

Aggregations

Element (de.pdark.decentxml.Element)1 Node (de.pdark.decentxml.Node)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 RoutesDefinition (org.apache.camel.model.RoutesDefinition)1 CamelContextFactoryBean (org.apache.camel.spring.CamelContextFactoryBean)1 CamelRouteContextFactoryBean (org.apache.camel.spring.CamelRouteContextFactoryBean)1