lburgazzoli closed pull request #288: Support for camel context customizer URL: https://github.com/apache/camel-k/pull/288
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/runtime/api/pom.xml b/runtime/api/pom.xml new file mode 100644 index 00000000..e8e59e63 --- /dev/null +++ b/runtime/api/pom.xml @@ -0,0 +1,77 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <groupId>org.apache.camel.k</groupId> + <artifactId>camel-k-runtime-parent</artifactId> + <version>0.1.1-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>camel-k-runtime-api</artifactId> + + <dependencies> + + <!-- ****************************** --> + <!-- --> + <!-- RUNTIME --> + <!-- --> + <!-- ****************************** --> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>${slf4j.version}</version> + </dependency> + + <!-- ****************************** --> + <!-- --> + <!-- TESTS --> + <!-- --> + <!-- ****************************** --> + + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-api</artifactId> + <version>${junit-jupiter.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <version>${junit-jupiter.version}</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>${assertj.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + +</project> diff --git a/runtime/api/src/main/java/org/apache/camel/k/CamelContextCustomizer.java b/runtime/api/src/main/java/org/apache/camel/k/CamelContextCustomizer.java new file mode 100644 index 00000000..11a2ae02 --- /dev/null +++ b/runtime/api/src/main/java/org/apache/camel/k/CamelContextCustomizer.java @@ -0,0 +1,28 @@ +package org.apache.camel.k; + +import org.apache.camel.CamelContext; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@FunctionalInterface +public interface CamelContextCustomizer { + /** + * Perform CamelContext customization. + */ + void customize(CamelContext camelContext); +} diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RuntimeRegistry.java b/runtime/api/src/main/java/org/apache/camel/k/RuntimeRegistry.java similarity index 96% rename from runtime/jvm/src/main/java/org/apache/camel/k/jvm/RuntimeRegistry.java rename to runtime/api/src/main/java/org/apache/camel/k/RuntimeRegistry.java index 7e13a283..895fe339 100644 --- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RuntimeRegistry.java +++ b/runtime/api/src/main/java/org/apache/camel/k/RuntimeRegistry.java @@ -14,17 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.k.jvm; +package org.apache.camel.k; import java.util.Map; import org.apache.camel.spi.Registry; public interface RuntimeRegistry extends Registry { - - /** - * - */ void bind(String name, Object bean); @SuppressWarnings("deprecation") diff --git a/runtime/api/src/main/java/org/apache/camel/k/RuntimeRegistryAware.java b/runtime/api/src/main/java/org/apache/camel/k/RuntimeRegistryAware.java new file mode 100644 index 00000000..f440aa05 --- /dev/null +++ b/runtime/api/src/main/java/org/apache/camel/k/RuntimeRegistryAware.java @@ -0,0 +1,23 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.k; + +public interface RuntimeRegistryAware { + void setRuntimeRegistry(RuntimeRegistry registry); + + RuntimeRegistry getRuntimeRegistry(); +} diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/GroovyRoutesLoader.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/GroovyRoutesLoader.groovy index 932f5265..d25e6aaf 100644 --- a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/GroovyRoutesLoader.groovy +++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/GroovyRoutesLoader.groovy @@ -18,6 +18,7 @@ package org.apache.camel.k.groovy import org.apache.camel.builder.RouteBuilder +import org.apache.camel.k.RuntimeRegistry import org.apache.camel.k.groovy.dsl.IntegrationConfiguration import org.apache.camel.k.jvm.* import org.codehaus.groovy.control.CompilerConfiguration diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ContextConfiguration.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ContextConfiguration.groovy index 405fe8c1..d70dd04d 100644 --- a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ContextConfiguration.groovy +++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/ContextConfiguration.groovy @@ -17,7 +17,7 @@ package org.apache.camel.k.groovy.dsl import org.apache.camel.CamelContext -import org.apache.camel.k.jvm.RuntimeRegistry +import org.apache.camel.k.RuntimeRegistry class ContextConfiguration { private final CamelContext context diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy index 5b07bd53..fdc860aa 100644 --- a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy +++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/IntegrationConfiguration.groovy @@ -21,7 +21,7 @@ import org.apache.camel.Exchange import org.apache.camel.Predicate import org.apache.camel.Processor import org.apache.camel.builder.RouteBuilder -import org.apache.camel.k.jvm.RuntimeRegistry +import org.apache.camel.k.RuntimeRegistry import org.apache.camel.k.jvm.dsl.Components import org.apache.camel.model.RouteDefinition diff --git a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RegistryConfiguration.groovy b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RegistryConfiguration.groovy index 0b7b23d8..f695ec19 100644 --- a/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RegistryConfiguration.groovy +++ b/runtime/groovy/src/main/groovy/org/apache/camel/k/groovy/dsl/RegistryConfiguration.groovy @@ -16,7 +16,7 @@ */ package org.apache.camel.k.groovy.dsl -import org.apache.camel.k.jvm.RuntimeRegistry +import org.apache.camel.k.RuntimeRegistry class RegistryConfiguration { private final RuntimeRegistry registry diff --git a/runtime/jvm/pom.xml b/runtime/jvm/pom.xml index b02dc3cd..7199ec8a 100644 --- a/runtime/jvm/pom.xml +++ b/runtime/jvm/pom.xml @@ -29,10 +29,6 @@ <artifactId>camel-k-runtime-jvm</artifactId> - <properties> - <kotlin.version>1.2.71</kotlin.version> - </properties> - <dependencies> <!-- ****************************** --> @@ -42,13 +38,9 @@ <!-- ****************************** --> <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-core</artifactId> - </dependency> - <dependency> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-api</artifactId> - <version>${slf4j.version}</version> + <groupId>org.apache.camel.k</groupId> + <artifactId>camel-k-runtime-api</artifactId> + <version>${project.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Application.java b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Application.java index 917b5660..3a0d2e79 100644 --- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Application.java +++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Application.java @@ -21,6 +21,8 @@ import org.apache.camel.CamelContext; import org.apache.camel.Component; import org.apache.camel.component.properties.PropertiesComponent; +import org.apache.camel.k.RuntimeRegistry; +import org.apache.camel.k.RuntimeRegistryAware; import org.apache.camel.main.MainListenerSupport; import org.apache.camel.support.LifecycleStrategySupport; import org.apache.camel.util.ObjectHelper; @@ -64,9 +66,22 @@ public static void main(String[] args) throws Exception { // // ******************************* - static class ComponentPropertiesBinder extends MainListenerSupport { + static class ComponentPropertiesBinder extends MainListenerSupport implements RuntimeRegistryAware { + private RuntimeRegistry runtimeRegistry; + + @Override + public RuntimeRegistry getRuntimeRegistry() { + return runtimeRegistry; + } + + @Override + public void setRuntimeRegistry(RuntimeRegistry runtimeRegistry) { + this.runtimeRegistry = runtimeRegistry; + } + @Override public void configure(CamelContext context) { + final PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class); final Properties properties = component.getInitialProperties(); @@ -80,6 +95,13 @@ public void configure(CamelContext context) { // RuntimeSupport.bindProperties(properties, context, "camel.context."); + // Programmatically customize the camel context. + // + // This is useful to configure services such as the ClusterService, + // RouteController, etc + // + RuntimeSupport.configureContext(runtimeRegistry, context); + context.addLifecycleStrategy(new LifecycleStrategySupport() { @SuppressWarnings("unchecked") @Override diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoader.java b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoader.java index faac8edb..ce90db65 100644 --- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoader.java +++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoader.java @@ -19,6 +19,7 @@ import java.util.List; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.k.RuntimeRegistry; public interface RoutesLoader { /** diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoaders.java b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoaders.java index 5f670780..92814a97 100644 --- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoaders.java +++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RoutesLoaders.java @@ -32,6 +32,7 @@ import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.k.RuntimeRegistry; import org.apache.camel.k.jvm.dsl.Components; import org.apache.camel.model.RouteDefinition; import org.apache.camel.model.rest.RestConfigurationDefinition; diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Runtime.java b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Runtime.java index 1e05e70b..45a90451 100644 --- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Runtime.java +++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Runtime.java @@ -27,6 +27,9 @@ import org.apache.camel.component.properties.PropertiesComponent; import org.apache.camel.impl.CompositeRegistry; import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.k.RuntimeRegistry; +import org.apache.camel.k.RuntimeRegistryAware; +import org.apache.camel.main.MainListener; import org.apache.camel.main.MainSupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,6 +60,15 @@ public void load(String[] routes) throws Exception { } } + @Override + public void addMainListener(MainListener listener) { + if (listener instanceof RuntimeRegistryAware) { + ((RuntimeRegistryAware) listener).setRuntimeRegistry(registry); + } + + super.addMainListener(listener); + } + public RuntimeRegistry getRegistry() { return registry; } diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java index 936e4d15..4ffd0e7b 100644 --- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java +++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java @@ -27,7 +27,11 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.Objects; import java.util.Properties; +import java.util.ServiceLoader; +import org.apache.camel.CamelContext; +import org.apache.camel.k.CamelContextCustomizer; +import org.apache.camel.k.RuntimeRegistry; import org.apache.camel.util.IntrospectionSupport; import org.apache.camel.util.ObjectHelper; import org.apache.commons.io.FilenameUtils; @@ -102,6 +106,20 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO return properties; } + public static void configureContext(RuntimeRegistry registry, CamelContext context) { + ServiceLoader.load(CamelContextCustomizer.class, context.getApplicationContextClassLoader()).forEach( + customizer -> { + customizer.customize(context); + } + ); + + registry.findByType(CamelContextCustomizer.class).forEach( + customizer -> { + customizer.customize(context); + } + ); + } + public static void configureLogging() { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Properties properties = loadProperties(); diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/SimpleRuntimeRegistry.java b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/SimpleRuntimeRegistry.java index 1035b9c4..c62175fe 100644 --- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/SimpleRuntimeRegistry.java +++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/SimpleRuntimeRegistry.java @@ -24,6 +24,7 @@ import java.util.stream.Collectors; import org.apache.camel.NoSuchBeanException; +import org.apache.camel.k.RuntimeRegistry; public class SimpleRuntimeRegistry implements RuntimeRegistry { private final ConcurrentMap<String, Object> registry; diff --git a/runtime/jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java b/runtime/jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java index 6ae83242..ab47495b 100644 --- a/runtime/jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java +++ b/runtime/jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java @@ -19,12 +19,11 @@ import java.util.Properties; import java.util.concurrent.ThreadLocalRandom; -import org.apache.camel.CamelContext; import org.apache.camel.component.seda.SedaComponent; -import org.apache.camel.main.MainListenerSupport; -import org.apache.camel.main.MainSupport; +import org.apache.camel.k.CamelContextCustomizer; import org.junit.jupiter.api.Test; +import static org.apache.camel.k.jvm.RuntimeTestSupport.afterStart; import static org.assertj.core.api.Assertions.assertThat; public class PropertiesTest { @@ -37,23 +36,13 @@ public void testLoadProperties() throws Exception { runtime.setProperties(properties); runtime.setDuration(5); runtime.addMainListener(new Application.ComponentPropertiesBinder()); - runtime.addMainListener(new MainListenerSupport() { - @Override - public void afterStart(MainSupport main) { - try { - CamelContext context = main.getCamelContexts().get(0); - - assertThat(context.resolvePropertyPlaceholders("{{root.key}}")).isEqualTo("root.value"); - assertThat(context.resolvePropertyPlaceholders("{{001.key}}")).isEqualTo("001.value"); - assertThat(context.resolvePropertyPlaceholders("{{002.key}}")).isEqualTo("002.value"); - assertThat(context.resolvePropertyPlaceholders("{{a.key}}")).isEqualTo("a.002"); - - main.stop(); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - }); + runtime.addMainListener(afterStart((main, context) -> { + assertThat(context.resolvePropertyPlaceholders("{{root.key}}")).isEqualTo("root.value"); + assertThat(context.resolvePropertyPlaceholders("{{001.key}}")).isEqualTo("001.value"); + assertThat(context.resolvePropertyPlaceholders("{{002.key}}")).isEqualTo("002.value"); + assertThat(context.resolvePropertyPlaceholders("{{a.key}}")).isEqualTo("a.002"); + main.stop(); + })); runtime.run(); } @@ -67,21 +56,12 @@ public void testSystemProperties() throws Exception { runtime.setProperties(System.getProperties()); runtime.setDuration(5); runtime.addMainListener(new Application.ComponentPropertiesBinder()); - runtime.addMainListener(new MainListenerSupport() { - @Override - public void afterStart(MainSupport main) { - try { - CamelContext context = main.getCamelContexts().get(0); - String value = context.resolvePropertyPlaceholders("{{my.property}}"); - - assertThat(value).isEqualTo("my.value"); - - main.stop(); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - }); + runtime.addMainListener(afterStart((main, context) -> { + String value = context.resolvePropertyPlaceholders("{{my.property}}"); + + assertThat(value).isEqualTo("my.value"); + main.stop(); + })); runtime.run(); } finally { @@ -103,21 +83,11 @@ public void testComponentConfiguration() throws Exception { runtime.setDuration(5); runtime.getRegistry().bind("my-seda", new SedaComponent()); runtime.addMainListener(new Application.ComponentPropertiesBinder()); - runtime.addMainListener(new MainListenerSupport() { - @Override - public void afterStart(MainSupport main) { - try { - CamelContext context = main.getCamelContexts().get(0); - - assertThat(context.getComponent("seda", true)).hasFieldOrPropertyWithValue("queueSize", queueSize1); - assertThat(context.getComponent("my-seda", true)).hasFieldOrPropertyWithValue("queueSize", queueSize2); - - main.stop(); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - }); + runtime.addMainListener(afterStart((main, context) -> { + assertThat(context.getComponent("seda", true)).hasFieldOrPropertyWithValue("queueSize", queueSize1); + assertThat(context.getComponent("my-seda", true)).hasFieldOrPropertyWithValue("queueSize", queueSize2); + main.stop(); + })); runtime.run(); } finally { @@ -135,23 +105,12 @@ public void testContextConfiguration() throws Exception { Runtime runtime = new Runtime(); runtime.setProperties(System.getProperties()); runtime.setDuration(5); - runtime.getRegistry().bind("my-seda", new SedaComponent()); runtime.addMainListener(new Application.ComponentPropertiesBinder()); - runtime.addMainListener(new MainListenerSupport() { - @Override - public void afterStart(MainSupport main) { - try { - CamelContext context = main.getCamelContexts().get(0); - - assertThat(context.isMessageHistory()).isFalse(); - assertThat(context.isLoadTypeConverters()).isFalse(); - - main.stop(); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - }); + runtime.addMainListener(afterStart((main, context) -> { + assertThat(context.isMessageHistory()).isFalse(); + assertThat(context.isLoadTypeConverters()).isFalse(); + main.stop(); + })); runtime.run(); } finally { @@ -159,4 +118,23 @@ public void afterStart(MainSupport main) { System.getProperties().remove("camel.context.loadTypeConverters"); } } + + @Test + public void testContextCustomizer() throws Exception { + Runtime runtime = new Runtime(); + runtime.setProperties(System.getProperties()); + runtime.setDuration(5); + runtime.getRegistry().bind("c1", (CamelContextCustomizer) context -> { + context.setMessageHistory(false); + context.setLoadTypeConverters(false); + }); + runtime.addMainListener(new Application.ComponentPropertiesBinder()); + runtime.addMainListener(afterStart((main, context) -> { + assertThat(context.isMessageHistory()).isFalse(); + assertThat(context.isLoadTypeConverters()).isFalse(); + main.stop(); + })); + + runtime.run(); + } } diff --git a/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTestSupport.java b/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTestSupport.java new file mode 100644 index 00000000..d72ddddd --- /dev/null +++ b/runtime/jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTestSupport.java @@ -0,0 +1,41 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.k.jvm; + +import org.apache.camel.CamelContext; +import org.apache.camel.main.MainListener; +import org.apache.camel.main.MainListenerSupport; +import org.apache.camel.main.MainSupport; +import org.apache.camel.util.function.ThrowingBiConsumer; + +public final class RuntimeTestSupport { + private RuntimeTestSupport() { + } + + public static MainListener afterStart(ThrowingBiConsumer<MainSupport, CamelContext, Exception> consumer) { + return new MainListenerSupport() { + @Override + public void afterStart(MainSupport main) { + try { + consumer.accept(main, main.getCamelContexts().get(0)); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + }; + } +} diff --git a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt index fba85d60..fa33b55c 100644 --- a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt +++ b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/KotlinRoutesLoader.kt @@ -17,6 +17,7 @@ package org.apache.camel.k.kotlin import org.apache.camel.builder.RouteBuilder +import org.apache.camel.k.RuntimeRegistry import org.apache.camel.k.jvm.* import org.apache.camel.k.kotlin.dsl.IntegrationConfiguration import org.slf4j.Logger diff --git a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/ContextConfiguration.kt b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/ContextConfiguration.kt index 618d1c79..9dbf77f2 100644 --- a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/ContextConfiguration.kt +++ b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/ContextConfiguration.kt @@ -17,7 +17,7 @@ package org.apache.camel.k.kotlin.dsl import org.apache.camel.CamelContext -import org.apache.camel.k.jvm.RuntimeRegistry +import org.apache.camel.k.RuntimeRegistry class ContextConfiguration (val registry: RuntimeRegistry, val context: CamelContext) { diff --git a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/IntegrationConfiguration.kt b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/IntegrationConfiguration.kt index 52d20f4b..926ad3ac 100644 --- a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/IntegrationConfiguration.kt +++ b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/IntegrationConfiguration.kt @@ -20,7 +20,7 @@ import org.apache.camel.Exchange import org.apache.camel.Predicate import org.apache.camel.Processor import org.apache.camel.builder.RouteBuilder -import org.apache.camel.k.jvm.RuntimeRegistry +import org.apache.camel.k.RuntimeRegistry import org.apache.camel.model.RouteDefinition abstract class IntegrationConfiguration( diff --git a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/RegistryConfiguration.kt b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/RegistryConfiguration.kt index f15f71ab..b3abc957 100644 --- a/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/RegistryConfiguration.kt +++ b/runtime/kotlin/src/main/kotlin/org/apache/camel/k/kotlin/dsl/RegistryConfiguration.kt @@ -16,7 +16,7 @@ */ package org.apache.camel.k.kotlin.dsl -import org.apache.camel.k.jvm.RuntimeRegistry +import org.apache.camel.k.RuntimeRegistry class RegistryConfiguration(val registry: RuntimeRegistry) { fun bind(name: String, value: Any) { diff --git a/runtime/pom.xml b/runtime/pom.xml index e7f2c3aa..19c6ed65 100644 --- a/runtime/pom.xml +++ b/runtime/pom.xml @@ -98,6 +98,7 @@ </build> <modules> + <module>api</module> <module>jvm</module> <module>groovy</module> <module>kotlin</module> diff --git a/runtime/spring-boot/src/main/java/org/apache/camel/k/spring/boot/Application.java b/runtime/spring-boot/src/main/java/org/apache/camel/k/spring/boot/Application.java index 0fe24ff6..e2547446 100644 --- a/runtime/spring-boot/src/main/java/org/apache/camel/k/spring/boot/Application.java +++ b/runtime/spring-boot/src/main/java/org/apache/camel/k/spring/boot/Application.java @@ -22,10 +22,10 @@ import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.k.RuntimeRegistry; import org.apache.camel.k.jvm.Constants; import org.apache.camel.k.jvm.RoutesLoader; import org.apache.camel.k.jvm.RoutesLoaders; -import org.apache.camel.k.jvm.RuntimeRegistry; import org.apache.camel.k.jvm.RuntimeSupport; import org.apache.camel.k.jvm.Source; import org.apache.camel.spi.Registry; @@ -85,6 +85,13 @@ public void beforeApplicationStart(CamelContext context) { throw new IllegalStateException("No valid routes found in " + Constants.ENV_CAMEL_K_ROUTES + " environment variable"); } + // Programmatically customize the camel context. + // + // This is useful to configure services such as the ClusterService, + // RouteController, etc + // + RuntimeSupport.configureContext(registry, context); + try { for (String route : routes.split(",")) { final Source source = Source.create(route); ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
