Skip to content

Commit

Permalink
kamelets: create a camel-kamelet component apache#375
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Aug 12, 2020
1 parent 068e20c commit 16a524b
Show file tree
Hide file tree
Showing 42 changed files with 1,121 additions and 467 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.apache.camel.builder.RouteBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.Runtime
import org.apache.camel.k.Sources
import org.apache.camel.k.listener.RoutesConfigurer
import org.apache.camel.k.listener.SourcesConfigurer
import org.apache.camel.model.FromDefinition
import org.apache.camel.model.ToDefinition
import spock.lang.Specification
Expand All @@ -35,7 +35,7 @@ class LoaderTest extends Specification {
def source = Sources.fromURI("classpath:routes.groovy")

when:
def loader = RoutesConfigurer.load(runtime, source)
def loader = SourcesConfigurer.load(runtime, source)

then:
loader instanceof GroovySourceLoader
Expand All @@ -60,7 +60,7 @@ class LoaderTest extends Specification {
def source = Sources.fromURI("classpath:routes-with-endpoint-dsl.groovy")

when:
def loader = RoutesConfigurer.load(runtime, source)
def loader = SourcesConfigurer.load(runtime, source)

then:
loader instanceof GroovySourceLoader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import spock.lang.Specification

import javax.sql.DataSource

import static org.apache.camel.k.listener.RoutesConfigurer.forRoutes
import static org.apache.camel.k.listener.SourcesConfigurer.forRoutes

class IntegrationTest extends Specification {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.camel.k.Source;
import org.apache.camel.k.SourceLoader;
import org.apache.camel.k.Sources;
import org.apache.camel.k.listener.RoutesConfigurer;
import org.apache.camel.k.listener.SourcesConfigurer;
import org.apache.camel.model.ProcessDefinition;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.SetBodyDefinition;
Expand All @@ -47,7 +47,7 @@ public class RoutesLoaderTest {
public void testLoadJavaWithNestedClass() throws Exception {
TestRuntime runtime = new TestRuntime();
Source source = Sources.fromURI("classpath:MyRoutesWithNestedClass.java");
SourceLoader loader = RoutesConfigurer.load(runtime, source);
SourceLoader loader = SourcesConfigurer.load(runtime, source);

assertThat(loader).isInstanceOf(JavaSourceLoader.class);
assertThat(runtime.builders).hasSize(1);
Expand All @@ -69,7 +69,7 @@ public void testLoadJavaWithNestedClass() throws Exception {
public void testLoadJavaWithRestConfiguration() throws Exception {
TestRuntime runtime = new TestRuntime();
Source source = Sources.fromURI("classpath:MyRoutesWithRestConfiguration.java");
SourceLoader loader = RoutesConfigurer.load(runtime, source);
SourceLoader loader = SourcesConfigurer.load(runtime, source);

assertThat(loader).isInstanceOf(JavaSourceLoader.class);
assertThat(runtime.builders).hasSize(1);
Expand All @@ -84,7 +84,7 @@ public void testLoadJavaWithRestConfiguration() throws Exception {
public void testLoadJavaConfiguration() throws Exception {
TestRuntime runtime = new TestRuntime();
Source source = Sources.fromURI("classpath:MyRoutesConfig.java");
SourceLoader loader = RoutesConfigurer.load(runtime, source);
SourceLoader loader = SourcesConfigurer.load(runtime, source);

assertThat(loader).isInstanceOf(JavaSourceLoader.class);
assertThat(runtime.builders).isEmpty();
Expand All @@ -95,7 +95,7 @@ public void testLoadJavaConfiguration() throws Exception {
public void testLoadJavaWithModel() throws Exception {
TestRuntime runtime = new TestRuntime();
Source source = Sources.fromURI("classpath:MyRoutesWithModel.java");
SourceLoader loader = RoutesConfigurer.load(runtime, source);
SourceLoader loader = SourcesConfigurer.load(runtime, source);

assertThat(loader).isInstanceOf(JavaSourceLoader.class);
assertThat(runtime.builders).hasSize(1);
Expand All @@ -114,7 +114,7 @@ public void testLoadJavaWithModel() throws Exception {
public void testLoadJavaWithNestedType() throws Exception {
TestRuntime runtime = new TestRuntime();
Source source = Sources.fromURI("classpath:MyRoutesWithNestedTypes.java");
SourceLoader loader = RoutesConfigurer.load(runtime, source);
SourceLoader loader = SourcesConfigurer.load(runtime, source);

assertThat(loader).isInstanceOf(JavaSourceLoader.class);
assertThat(runtime.builders).hasSize(1);
Expand All @@ -129,7 +129,7 @@ public void testLoadJavaWithNestedType() throws Exception {
public void testLoaders(String location, Class<? extends SourceLoader> type) throws Exception {
TestRuntime runtime = new TestRuntime();
Source source = Sources.fromURI(location);
SourceLoader loader = RoutesConfigurer.load(runtime, source);
SourceLoader loader = SourcesConfigurer.load(runtime, source);

assertThat(loader).isInstanceOf(type);
assertThat(runtime.builders).hasSize(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.camel.k.Source;
import org.apache.camel.k.SourceLoader;
import org.apache.camel.k.Sources;
import org.apache.camel.k.listener.RoutesConfigurer;
import org.apache.camel.k.listener.SourcesConfigurer;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.ToDefinition;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -44,7 +44,7 @@ public class RoutesLoaderTest {
public void testLoaders(String location, Class<? extends SourceLoader> type) throws Exception {
TestRuntime runtime = new TestRuntime();
Source source = Sources.fromURI(location);
SourceLoader loader = RoutesConfigurer.load(runtime, source);
SourceLoader loader = SourcesConfigurer.load(runtime, source);

assertThat(loader).isInstanceOf(type);
assertThat(runtime.builders).hasSize(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.camel.component.seda.SedaComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.listener.RoutesConfigurer;
import org.apache.camel.k.listener.SourcesConfigurer;
import org.apache.camel.model.FromDefinition;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.RouteDefinition;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void shutDown() {
}

private void configureRoutes(String... routes) {
RoutesConfigurer.forRoutes(routes).accept(Runtime.Phase.ConfigureRoutes, runtime);
SourcesConfigurer.forRoutes(routes).accept(Runtime.Phase.ConfigureRoutes, runtime);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.camel.k.Runtime;
import org.apache.camel.k.Source;
import org.apache.camel.k.Sources;
import org.apache.camel.k.listener.RoutesConfigurer;
import org.apache.camel.k.listener.SourcesConfigurer;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -33,7 +33,7 @@ public void testLoad() throws Exception {
final Runtime runtime = Runtime.on(context);
final Source source = Sources.fromURI("classpath:routes.kts");

RoutesConfigurer.load(runtime, source);
SourcesConfigurer.load(runtime, source);

try {
context.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.apache.camel.builder.RouteBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.Runtime
import org.apache.camel.k.Sources
import org.apache.camel.k.listener.RoutesConfigurer
import org.apache.camel.k.listener.SourcesConfigurer
import org.apache.camel.model.ProcessDefinition
import org.apache.camel.model.ToDefinition
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -35,7 +35,7 @@ class LoaderTest {
fun `load routes`() {
val runtime = TestRuntime()
val source = Sources.fromURI("classpath:routes.kts")
val loader = RoutesConfigurer.load(runtime, source)
val loader = SourcesConfigurer.load(runtime, source)

assertThat(loader).isInstanceOf(KotlinSourceLoader::class.java)
assertThat(runtime.builders).hasSize(1)
Expand All @@ -56,7 +56,7 @@ class LoaderTest {
fun `load routes with endpoint dsl`() {
val runtime = TestRuntime()
val source = Sources.fromURI("classpath:routes-with-endpoint-dsl.kts")
val loader = RoutesConfigurer.load(runtime, source)
val loader = SourcesConfigurer.load(runtime, source)

assertThat(loader).isInstanceOf(KotlinSourceLoader::class.java)
assertThat(runtime.builders).hasSize(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import org.apache.camel.component.log.LogComponent
import org.apache.camel.component.seda.SedaComponent
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.k.Runtime
import org.apache.camel.k.listener.RoutesConfigurer.forRoutes
import org.apache.camel.k.listener.SourcesConfigurer.forRoutes
import org.apache.camel.language.bean.BeanLanguage
import org.apache.camel.model.ModelCamelContext
import org.apache.camel.model.rest.GetVerbDefinition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.k.CompositeClassloader;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.support.PropertiesSupport;
import org.apache.camel.main.BaseMainSupport;
import org.apache.camel.main.MainSupport;
import org.apache.camel.main.RoutesCollector;
import org.apache.camel.model.RouteTemplatesDefinition;
import org.apache.camel.model.RoutesDefinition;
import org.apache.camel.model.rest.RestsDefinition;
import org.apache.camel.spi.HasId;
import org.apache.camel.util.function.ThrowingConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -103,15 +101,6 @@ public void addListeners(Iterable<Runtime.Listener> listeners) {
}

public void addListener(Runtime.Listener listener) {
if (listener instanceof HasId) {
String id = ((HasId) listener).getId();
if (!id.endsWith(".")) {
id = id + ".";
}

PropertiesSupport.bindProperties(getCamelContext(), listener, id);
}

LOGGER.info("Add listener: {}", listener);

this.listeners.add(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.camel.k.Runtime;
import org.apache.camel.k.http.PlatformHttpServiceContextCustomizer;
import org.apache.camel.k.listener.ContextConfigurer;
import org.apache.camel.k.listener.RoutesConfigurer;
import org.apache.camel.k.listener.SourcesConfigurer;
import org.apache.camel.k.test.AvailablePortFinder;
import org.apache.camel.model.ModelCamelContext;
import org.apache.camel.model.ToDefinition;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void cleanUp() throws Exception {
@Test
void testLoadMultipleRoutes() throws Exception {
runtime.addListener(new ContextConfigurer());
runtime.addListener(RoutesConfigurer.forRoutes("classpath:r1.js", "classpath:r2.mytype?language=js"));
runtime.addListener(SourcesConfigurer.forRoutes("classpath:r1.js", "classpath:r2.mytype?language=js"));
runtime.addListener(Runtime.Phase.Started, r -> {
CamelContext context = r.getCamelContext();
List<Route> routes = context.getRoutes();
Expand All @@ -73,7 +73,7 @@ void testLoadMultipleRoutes() throws Exception {
@Test
void testLoadRouteAndRest() throws Exception {
runtime.addListener(new ContextConfigurer());
runtime.addListener(RoutesConfigurer.forRoutes("classpath:routes.xml", "classpath:rests.xml"));
runtime.addListener(SourcesConfigurer.forRoutes("classpath:routes.xml", "classpath:rests.xml"));
runtime.addListener(Runtime.Phase.Started, r -> {
CamelContext context = r.getCamelContext();

Expand All @@ -93,16 +93,55 @@ void testLoadRouteWithExpression() throws Exception {
));

runtime.addListener(new ContextConfigurer());
runtime.addListener(RoutesConfigurer.forRoutes("classpath:routes-with-expression.xml"));
runtime.addListener(SourcesConfigurer.forRoutes("classpath:routes-with-expression.xml"));
runtime.addListener(Runtime.Phase.Started, r -> runtime.stop());
runtime.run();
}

@Test
public void testLoadJavaSource() throws Exception {
ApplicationRuntime runtime = new ApplicationRuntime();
runtime.addListener(RoutesConfigurer.forRoutes("classpath:MyRoutesWithBeans.java", "classpath:MyRoutesConfig.java"));
runtime.addListener(Runtime.Phase.Started, r -> runtime.stop());
runtime.addListener(SourcesConfigurer.forRoutes("classpath:MyRoutesWithBeans.java", "classpath:MyRoutesConfig.java"));
runtime.addListener(Runtime.Phase.Started, r -> {
assertThat(runtime.getCamelContext().getRoutes()).hasSize(1);
assertThat(runtime.getRegistry().lookupByName("my-processor")).isNotNull();
assertThat(runtime.getRegistry().lookupByName("my-bean")).isInstanceOfSatisfying(MyBean.class, b -> {
assertThat(b).hasFieldOrPropertyWithValue("name", "my-bean-name");
});
r.stop();
});
runtime.run();
}

@Test
public void testLoadJavaSourceFromProperties() throws Exception {
runtime.setInitialProperties(
"camel.k.sources[0].name", "MyRoutesWithBeans",
"camel.k.sources[0].location", "classpath:MyRoutesWithBeans.java",
"camel.k.sources[0].language", "java",
"camel.k.sources[1].name", "MyRoutesConfig",
"camel.k.sources[1].location", "classpath:MyRoutesConfig.java",
"camel.k.sources[1].language", "java"
);
runtime.addListener(new SourcesConfigurer());
runtime.addListener(Runtime.Phase.Started, r -> {
assertThat(runtime.getCamelContext().getRoutes()).hasSize(1);
assertThat(runtime.getRegistry().lookupByName("my-processor")).isNotNull();
assertThat(runtime.getRegistry().lookupByName("my-bean")).isInstanceOfSatisfying(MyBean.class, b -> {
assertThat(b).hasFieldOrPropertyWithValue("name", "my-bean-name");
});
r.stop();
});
runtime.run();
}

@Test
public void testLoadJavaSourceFromSimpleProperties() throws Exception {
runtime.setInitialProperties(
"camel.k.sources[0].location", "classpath:MyRoutesWithBeans.java",
"camel.k.sources[1].location", "classpath:MyRoutesConfig.java"
);
runtime.addListener(new SourcesConfigurer());
runtime.addListener(Runtime.Phase.Started, Runtime::stop);
runtime.run();

assertThat(runtime.getRegistry().lookupByName("my-processor")).isNotNull();
Expand All @@ -123,7 +162,7 @@ public void testLoadJavaSourceWrap() throws Exception {
phsc.apply(runtime.getCamelContext());

runtime.getCamelContext().addComponent("knative", component);
runtime.addListener(RoutesConfigurer.forRoutes("classpath:MyRoutesWithBeans.java?interceptors=knative-source"));
runtime.addListener(SourcesConfigurer.forRoutes("classpath:MyRoutesWithBeans.java?interceptors=knative-source"));
runtime.addListener(Runtime.Phase.Started, r -> runtime.stop());
runtime.run();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import io.restassured.path.json.JsonPath;
import org.apache.camel.k.CompositeClassloader;
import org.apache.camel.k.listener.ContextConfigurer;
import org.apache.camel.k.listener.RoutesConfigurer;
import org.apache.camel.k.listener.SourcesConfigurer;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -45,7 +45,7 @@ public void testServices() {

assertThat(p.getList("services", String.class)).contains(
ContextConfigurer.class.getName(),
RoutesConfigurer.class.getName()
SourcesConfigurer.class.getName()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.quarkus.ApplicationRecorder;
import org.apache.camel.quarkus.core.deployment.spi.CamelServiceDestination;
import org.apache.camel.quarkus.core.deployment.spi.CamelServicePatternBuildItem;
import org.apache.camel.quarkus.main.CamelMainApplication;
import org.apache.camel.quarkus.main.deployment.spi.CamelMainListenerBuildItem;
import org.apache.camel.spi.HasId;

public class DeploymentProcessor {
@BuildStep
Expand All @@ -42,19 +39,7 @@ public ReflectiveClassBuildItem reflectiveClasses() {
@BuildStep
CamelMainListenerBuildItem registerListener(ApplicationRecorder recorder) {
List<Runtime.Listener> listeners = new ArrayList<>();
ServiceLoader.load(Runtime.Listener.class).forEach(listener -> {
if (listener instanceof HasId) {
String id = ((HasId) listener).getId();
if (!id.endsWith(".")) {
id = id + ".";
}

// TODO: this has to be done at runtime
//PropertiesSupport.bindProperties(getCamelContext(), listener, id);
}

listeners.add(listener);
});
ServiceLoader.load(Runtime.Listener.class).forEach(listeners::add);

return new CamelMainListenerBuildItem(recorder.createMainListener(listeners));
}
Expand Down
Loading

0 comments on commit 16a524b

Please sign in to comment.