Search in sources :

Example 16 with RoutesDefinition

use of org.apache.camel.model.RoutesDefinition in project camel by apache.

the class XmlCdiBeanFactory method beansFrom.

Set<SyntheticBean<?>> beansFrom(URL url) throws JAXBException, IOException {
    try (InputStream xml = url.openStream()) {
        Object node = XmlCdiJaxbContexts.CAMEL_CDI.instance().createUnmarshaller().unmarshal(xml);
        if (node instanceof RoutesDefinition) {
            RoutesDefinition routes = (RoutesDefinition) node;
            return singleton(routesDefinitionBean(routes, url));
        } else if (node instanceof ApplicationContextFactoryBean) {
            ApplicationContextFactoryBean app = (ApplicationContextFactoryBean) node;
            Set<SyntheticBean<?>> beans = new HashSet<>();
            for (CamelContextFactoryBean factory : app.getContexts()) {
                SyntheticBean<?> bean = camelContextBean(factory, url);
                beans.add(bean);
                beans.addAll(camelContextBeans(factory, bean, url));
            }
            for (ErrorHandlerDefinition definition : app.getErrorHandlers()) {
                beans.add(errorHandlerBean(definition, url));
            }
            for (ImportDefinition definition : app.getImports()) {
                // Get the base URL as imports are relative to this
                String path = url.getFile().substring(0, url.getFile().lastIndexOf('/'));
                String base = url.getProtocol() + "://" + url.getHost() + path;
                beans.addAll(beansFrom(base + "/" + definition.getResource()));
            }
            for (RestContextDefinition factory : app.getRestContexts()) {
                beans.add(restContextBean(factory, url));
            }
            for (RouteContextDefinition factory : app.getRouteContexts()) {
                beans.add(routeContextBean(factory, url));
            }
            for (AbstractCamelFactoryBean<?> factory : app.getBeans()) {
                if (hasId(factory)) {
                    beans.add(camelContextBean(null, factory, url));
                }
            }
            return beans;
        } else if (node instanceof CamelContextFactoryBean) {
            CamelContextFactoryBean factory = (CamelContextFactoryBean) node;
            Set<SyntheticBean<?>> beans = new HashSet<>();
            SyntheticBean<?> bean = camelContextBean(factory, url);
            beans.add(bean);
            beans.addAll(camelContextBeans(factory, bean, url));
            return beans;
        } else if (node instanceof RestContextDefinition) {
            RestContextDefinition factory = (RestContextDefinition) node;
            return singleton(restContextBean(factory, url));
        } else if (node instanceof RouteContextDefinition) {
            RouteContextDefinition factory = (RouteContextDefinition) node;
            return singleton(routeContextBean(factory, url));
        }
    }
    return emptySet();
}
Also used : HashSet(java.util.HashSet) Collectors.toSet(java.util.stream.Collectors.toSet) Collections.emptySet(java.util.Collections.emptySet) Set(java.util.Set) ErrorHandlerDefinition(org.apache.camel.cdi.xml.ErrorHandlerDefinition) RestContextDefinition(org.apache.camel.cdi.xml.RestContextDefinition) InputStream(java.io.InputStream) RouteContextDefinition(org.apache.camel.cdi.xml.RouteContextDefinition) ImportDefinition(org.apache.camel.cdi.xml.ImportDefinition) AbstractCamelFactoryBean(org.apache.camel.core.xml.AbstractCamelFactoryBean) ApplicationContextFactoryBean(org.apache.camel.cdi.xml.ApplicationContextFactoryBean) RoutesDefinition(org.apache.camel.model.RoutesDefinition) CamelContextFactoryBean(org.apache.camel.cdi.xml.CamelContextFactoryBean) HashSet(java.util.HashSet)

Example 17 with RoutesDefinition

use of org.apache.camel.model.RoutesDefinition 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)

Example 18 with RoutesDefinition

use of org.apache.camel.model.RoutesDefinition in project syncope by apache.

the class SyncopeCamelContext method loadRouteDefinitions.

private void loadRouteDefinitions(final List<String> routes) {
    try {
        RoutesDefinition routeDefs = camelContext.loadRoutesDefinition(IOUtils.toInputStream("<routes xmlns=\"http://camel.apache.org/schema/spring\">" + StringUtils.join(routes) + "</routes>", StandardCharsets.UTF_8));
        camelContext.addRouteDefinitions(routeDefs.getRoutes());
    } catch (Exception e) {
        LOG.error("While adding route definitions into Camel Context {}", camelContext, e);
        throw new CamelException(e);
    }
}
Also used : RoutesDefinition(org.apache.camel.model.RoutesDefinition)

Example 19 with RoutesDefinition

use of org.apache.camel.model.RoutesDefinition in project ddf by codice.

the class MetacardStorageRoute method stop.

public void stop(int code) {
    try {
        List<RouteDefinition> routesToRemove = new ArrayList<>();
        ModelCamelContext context = getContext().adapt(ModelCamelContext.class);
        for (RouteDefinition routeDefinition : context.getRouteDefinitions()) {
            if (getRouteIds().contains(routeDefinition.getId())) {
                context.getRouteController().stopRoute(routeDefinition.getId());
                routesToRemove.add(routeDefinition);
                setRouteCollection(new RoutesDefinition());
            }
        }
        for (RouteDefinition routeDefinition : routesToRemove) {
            context.removeRouteDefinition(routeDefinition);
        }
    } catch (Exception e) {
        LOGGER.error("Could not stop route: {}", e);
    }
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) ArrayList(java.util.ArrayList) RoutesDefinition(org.apache.camel.model.RoutesDefinition) ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Example 20 with RoutesDefinition

use of org.apache.camel.model.RoutesDefinition in project syndesis by syndesisio.

the class IntegrationRouteBuilder method mandatoryConvertToRoutesDefinition.

@SuppressWarnings("PMD")
private RoutesDefinition mandatoryConvertToRoutesDefinition(String resource, Object instance) {
    final RoutesDefinition definitions;
    if (instance instanceof RoutesDefinition) {
        definitions = (RoutesDefinition) instance;
    } else if (instance instanceof RouteDefinition) {
        definitions = new RoutesDefinition();
        definitions.route((RouteDefinition) instance);
    } else if (instance instanceof RouteBuilder) {
        RouteBuilder builder = (RouteBuilder) instance;
        try {
            builder.configure();
            definitions = builder.getRouteCollection();
        } catch (Exception e) {
            LOGGER.warn("Unable to configure resource: " + resource, e);
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
    } else {
        throw new IllegalArgumentException("Unable to convert instance: " + instance);
    }
    return definitions;
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) RouteBuilder(org.apache.camel.builder.RouteBuilder) RoutesDefinition(org.apache.camel.model.RoutesDefinition) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Aggregations

RoutesDefinition (org.apache.camel.model.RoutesDefinition)20 Test (org.junit.Test)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 RouteDefinition (org.apache.camel.model.RouteDefinition)5 IOException (java.io.IOException)4 LinkedHashMap (java.util.LinkedHashMap)4 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 CamelContext (org.apache.camel.CamelContext)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 Element (de.pdark.decentxml.Element)1 Node (de.pdark.decentxml.Node)1 StepAction (io.syndesis.common.model.action.StepAction)1 FileNotFoundException (java.io.FileNotFoundException)1 StringReader (java.io.StringReader)1 Collections.emptySet (java.util.Collections.emptySet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1