Search in sources :

Example 1 with RoutesDefinition

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

the class RoutesCollector method loadXmlRoutes.

// Helpers
private void loadXmlRoutes(ApplicationContext applicationContext, CamelContext camelContext, String directory) throws Exception {
    LOG.info("Loading additional Camel XML routes from: {}", directory);
    try {
        Resource[] xmlRoutes = applicationContext.getResources(directory);
        for (Resource xmlRoute : xmlRoutes) {
            LOG.debug("Found XML route: {}", xmlRoute);
            RoutesDefinition xmlDefinition = camelContext.loadRoutesDefinition(xmlRoute.getInputStream());
            camelContext.addRouteDefinitions(xmlDefinition.getRoutes());
        }
    } catch (FileNotFoundException e) {
        LOG.debug("No XML routes found in {}. Skipping XML routes detection.", directory);
    }
}
Also used : Resource(org.springframework.core.io.Resource) RoutesDefinition(org.apache.camel.model.RoutesDefinition) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with RoutesDefinition

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

the class CreateModelFromXmlTest method testCreateModelFromXmlForInputStreamWithDefaultNamespace.

@Test
public void testCreateModelFromXmlForInputStreamWithDefaultNamespace() throws Exception {
    RoutesDefinition routesDefinition = createModelFromXml("simpleRoute.xml", false);
    assertNotNull(routesDefinition);
    Map<String, String> expectedNamespaces = new LinkedHashMap<>();
    expectedNamespaces.put("xmlns", NS_CAMEL);
    assertNamespacesPresent(routesDefinition, expectedNamespaces);
}
Also used : RoutesDefinition(org.apache.camel.model.RoutesDefinition) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 3 with RoutesDefinition

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

the class ManagedCamelContext method dumpRoutesAsXml.

@Override
public String dumpRoutesAsXml(boolean resolvePlaceholders) throws Exception {
    List<RouteDefinition> routes = context.getRouteDefinitions();
    if (routes.isEmpty()) {
        return null;
    }
    // use a routes definition to dump the routes
    RoutesDefinition def = new RoutesDefinition();
    def.setRoutes(routes);
    String xml = ModelHelper.dumpModelAsXml(context, def);
    // if resolving placeholders we parse the xml, and resolve the property placeholders during parsing
    if (resolvePlaceholders) {
        final AtomicBoolean changed = new AtomicBoolean();
        InputStream is = new ByteArrayInputStream(xml.getBytes());
        Document dom = XmlLineNumberParser.parseXml(is, new XmlLineNumberParser.XmlTextTransformer() {

            @Override
            public String transform(String text) {
                try {
                    String after = getContext().resolvePropertyPlaceholders(text);
                    if (!changed.get()) {
                        changed.set(!text.equals(after));
                    }
                    return after;
                } catch (Exception e) {
                    // ignore
                    return text;
                }
            }
        });
        // okay there were some property placeholder replaced so re-create the model
        if (changed.get()) {
            xml = context.getTypeConverter().mandatoryConvertTo(String.class, dom);
            RoutesDefinition copy = ModelHelper.createModelFromXml(context, xml, RoutesDefinition.class);
            xml = ModelHelper.dumpModelAsXml(context, copy);
        }
    }
    return xml;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RouteDefinition(org.apache.camel.model.RouteDefinition) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RoutesDefinition(org.apache.camel.model.RoutesDefinition) Document(org.w3c.dom.Document) XmlLineNumberParser(org.apache.camel.util.XmlLineNumberParser) IOException(java.io.IOException)

Example 4 with RoutesDefinition

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

the class ManagedCamelContext method addOrUpdateRoutesFromXml.

public void addOrUpdateRoutesFromXml(String xml, boolean urlDecode) throws Exception {
    // decode String as it may have been encoded, from its xml source
    if (urlDecode) {
        xml = URLDecoder.decode(xml, "UTF-8");
    }
    InputStream is = context.getTypeConverter().mandatoryConvertTo(InputStream.class, xml);
    RoutesDefinition def = context.loadRoutesDefinition(is);
    if (def == null) {
        return;
    }
    try {
        // add will remove existing route first
        context.addRouteDefinitions(def.getRoutes());
    } catch (Exception e) {
        // log the error as warn as the management api may be invoked remotely over JMX which does not propagate such exception
        String msg = "Error updating routes from xml: " + xml + " due: " + e.getMessage();
        LOG.warn(msg, e);
        throw e;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RoutesDefinition(org.apache.camel.model.RoutesDefinition) IOException(java.io.IOException)

Example 5 with RoutesDefinition

use of org.apache.camel.model.RoutesDefinition in project fabric8 by jboss-fuse.

the class XmlModel method marshalRootElement.

/**
 * Returns the root element to be marshalled as XML
 *
 * @return
 */
public Object marshalRootElement() {
    if (justRoutes) {
        RoutesDefinition routes = new RoutesDefinition();
        routes.setRoutes(contextElement.getRoutes());
        return routes;
    } else {
        return contextElement;
    }
}
Also used : RoutesDefinition(org.apache.camel.model.RoutesDefinition)

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