Search in sources :

Example 56 with RouteDefinition

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

the class CreateModelFromXmlTest method assertNamespacesPresent.

private void assertNamespacesPresent(RoutesDefinition routesDefinition, Map<String, String> expectedNamespaces) {
    for (RouteDefinition route : routesDefinition.getRoutes()) {
        Iterator<ExpressionNode> it = filterTypeInOutputs(route.getOutputs(), ExpressionNode.class);
        if (it.hasNext()) {
            ExpressionNode en = it.next();
            ExpressionDefinition ed = en.getExpression();
            NamespaceAware na = null;
            Expression exp = ed.getExpressionValue();
            if (exp != null && exp instanceof NamespaceAware) {
                na = (NamespaceAware) exp;
            } else if (ed instanceof NamespaceAware) {
                na = (NamespaceAware) ed;
            }
            assertNotNull(na);
            assertEquals(expectedNamespaces, na.getNamespaces());
        } else {
            fail("Expected to find at least one ExpressionNode in route");
        }
    }
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) Expression(org.apache.camel.Expression) ExpressionNode(org.apache.camel.model.ExpressionNode) NamespaceAware(org.apache.camel.spi.NamespaceAware) ExpressionDefinition(org.apache.camel.model.language.ExpressionDefinition)

Example 57 with RouteDefinition

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

the class OnExceptionRouteIdTest method testOnExceptionRouteId.

public void testOnExceptionRouteId() throws Exception {
    getMockEndpoint("mock:error").expectedMessageCount(0);
    getMockEndpoint("mock:foo").expectedMessageCount(1);
    template.sendBody("direct:foo", "Hello World");
    assertMockEndpointsSatisfied();
    RouteDefinition route = context.getRouteDefinition("foo");
    assertNotNull(route);
    assertEquals("foo", route.getId());
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition)

Example 58 with RouteDefinition

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

the class AdviceWithBuilder method before.

/**
     * Insert the following node(s) <b>before</b> the matched node(s)
     *
     * @return the builder to build the nodes.
     */
public ProcessorDefinition<?> before() {
    RouteDefinition route = builder.getOriginalRoute();
    PipelineDefinition answer = new PipelineDefinition();
    if (id != null) {
        builder.getAdviceWithTasks().add(AdviceWithTasks.beforeById(route, id, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
    } else if (toString != null) {
        builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByToString(route, toString, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
    } else if (toUri != null) {
        builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByToUri(route, toUri, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
    } else if (type != null) {
        builder.getAdviceWithTasks().add(AdviceWithTasks.beforeByType(route, type, answer, selectFirst, selectLast, selectFrom, selectTo, maxDeep));
    }
    return answer;
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) PipelineDefinition(org.apache.camel.model.PipelineDefinition)

Example 59 with RouteDefinition

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

the class ManagedRoute method updateRouteFromXml.

public void updateRouteFromXml(String xml) throws Exception {
    // convert to model from xml
    RouteDefinition def = ModelHelper.createModelFromXml(context, xml, RouteDefinition.class);
    if (def == null) {
        return;
    }
    // and not add a new route, adding a new route, use the MBean operation on ManagedCamelContext instead.
    if (ObjectHelper.isEmpty(def.getId())) {
        def.setId(getRouteId());
    } else if (!def.getId().equals(getRouteId())) {
        throw new IllegalArgumentException("Cannot update route from XML as routeIds does not match. routeId: " + getRouteId() + ", routeId from XML: " + def.getId());
    }
    LOG.debug("Updating route: {} from xml: {}", def.getId(), xml);
    try {
        // add will remove existing route first
        context.addRouteDefinition(def);
    } 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 route: " + def.getId() + " from xml: " + xml + " due: " + e.getMessage();
        LOG.warn(msg, e);
        throw e;
    }
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition)

Example 60 with RouteDefinition

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

the class ManagedRoute method dumpRouteAsXml.

@Override
public String dumpRouteAsXml(boolean resolvePlaceholders) throws Exception {
    String id = route.getId();
    RouteDefinition def = context.getRouteDefinition(id);
    if (def != null) {
        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);
                RouteDefinition copy = ModelHelper.createModelFromXml(context, xml, RouteDefinition.class);
                xml = ModelHelper.dumpModelAsXml(context, copy);
            }
        }
        return xml;
    }
    return null;
}
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) Document(org.w3c.dom.Document) XmlLineNumberParser(org.apache.camel.util.XmlLineNumberParser)

Aggregations

RouteDefinition (org.apache.camel.model.RouteDefinition)102 AdviceWithRouteBuilder (org.apache.camel.builder.AdviceWithRouteBuilder)17 RouteBuilder (org.apache.camel.builder.RouteBuilder)17 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)9 FromDefinition (org.apache.camel.model.FromDefinition)9 HashMap (java.util.HashMap)6 Processor (org.apache.camel.Processor)6 ConnectException (java.net.ConnectException)5 Exchange (org.apache.camel.Exchange)5 ProcessorDefinition (org.apache.camel.model.ProcessorDefinition)5 IOException (java.io.IOException)4 Map (java.util.Map)4 MBeanServer (javax.management.MBeanServer)4 ObjectName (javax.management.ObjectName)4 ChoiceDefinition (org.apache.camel.model.ChoiceDefinition)4 HystrixConfigurationDefinition (org.apache.camel.model.HystrixConfigurationDefinition)4 HystrixDefinition (org.apache.camel.model.HystrixDefinition)4 LogDefinition (org.apache.camel.model.LogDefinition)4 ModelCamelContext (org.apache.camel.model.ModelCamelContext)4