Skip to content

Commit

Permalink
Add support for XML routes #70
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli authored and nicolaferraro committed Sep 14, 2018
1 parent 99f8129 commit ea382b1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private Routes() {
}

public static boolean isScripting(String resource) {
return resource.endsWith(".java") || resource.endsWith(".js") || resource.endsWith(".groovy");
return resource.endsWith(".java") || resource.endsWith(".js") || resource.endsWith(".groovy") || resource.endsWith(".xml");
}

public static RoutesLoader loaderForLanguage(String language) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.camel.Component;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.RoutesDefinition;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.groovy.control.CompilerConfiguration;
Expand Down Expand Up @@ -168,6 +169,35 @@ public void configure() throws Exception {
}
};
}
},
Xml {
@Override
public List<String> getSupportedLanguages() {
return Arrays.asList("xml");
}

@Override
public boolean test(String resource) {
String ext = StringUtils.substringAfterLast(resource, ".");
List<String> langs = getSupportedLanguages();

return langs.contains(ext);
}

@Override
public RouteBuilder load(String resource) throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
try (InputStream is = Routes.loadResourceAsInputStream(resource)) {
final CamelContext context = getContext();
final RoutesDefinition definitions = context.loadRoutesDefinition(is);

setRouteCollection(definitions);
}
}
};
}
};

// ********************************
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ public void testLoadGroovy() throws Exception {
assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
}

@Test
public void testLoadXml() throws Exception {
String resource = "classpath:routes.xml";
RoutesLoader loader = Routes.loaderForResource(resource);
RouteBuilder builder = loader.load(resource);

assertThat(loader).isSameAs(RoutesLoaders.Xml);
assertThat(builder).isNotNull();

builder.configure();

List<RouteDefinition> routes = builder.getRouteCollection().getRoutes();
assertThat(routes).hasSize(1);
assertThat(routes.get(0).getInputs().get(0).getEndpointUri()).isEqualTo("timer:tick");
assertThat(routes.get(0).getOutputs().get(0)).isInstanceOf(ToDefinition.class);
}

@Test(expected = IllegalArgumentException.class)
public void testResourceWithoutScheme() {
Routes.loaderForResource("routes.js");
Expand Down
7 changes: 7 additions & 0 deletions runtime/jvm/src/test/resources/routes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<routes xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="timer:tick"/>
<to uri="log:info"/>
</route>
</routes>

0 comments on commit ea382b1

Please sign in to comment.