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