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");
}
}
}
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());
}
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;
}
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;
}
}
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;
}
Aggregations