This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
The following commit(s) were added to refs/heads/master by this push: new 2cd910a loader(java): support for bean registration 2cd910a is described below commit 2cd910a1330c8192b8123720274e6532c108c5c7 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Sat Jul 20 08:58:51 2019 +0200 loader(java): support for bean registration --- camel-k-loader-java/pom.xml | 10 ++++ .../k/loader/java/JavaSourceRoutesLoader.java | 57 +++------------------- .../org/apache/camel/k/loader/java/MyBean.java | 34 +++++++++++++ .../camel/k/loader/java/MyRoutesWithBeans.java | 34 +++++++++++++ .../camel/k/loader/java/RoutesLoaderTest.java | 29 ++++++++++- .../src/test/resources/MyRoutesWithBeans.java | 33 +++++++++++++ .../src/main/java/org/apache/camel/k/Runtime.java | 10 ++++ .../apache/camel/k/listener/RoutesConfigurer.java | 6 +-- .../apache/camel/k/main/ApplicationRuntime.java | 30 ++++++++---- 9 files changed, 177 insertions(+), 66 deletions(-) diff --git a/camel-k-loader-java/pom.xml b/camel-k-loader-java/pom.xml index c575a5b..a3c9349 100644 --- a/camel-k-loader-java/pom.xml +++ b/camel-k-loader-java/pom.xml @@ -50,6 +50,16 @@ </dependency> <dependency> <groupId>org.apache.camel</groupId> + <artifactId>camel-main</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-bean</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> <artifactId>camel-undertow</artifactId> <scope>test</scope> </dependency> diff --git a/camel-k-loader-java/src/main/java/org/apache/camel/k/loader/java/JavaSourceRoutesLoader.java b/camel-k-loader-java/src/main/java/org/apache/camel/k/loader/java/JavaSourceRoutesLoader.java index d41ab7c..7c0a6ef 100644 --- a/camel-k-loader-java/src/main/java/org/apache/camel/k/loader/java/JavaSourceRoutesLoader.java +++ b/camel-k-loader-java/src/main/java/org/apache/camel/k/loader/java/JavaSourceRoutesLoader.java @@ -20,18 +20,14 @@ import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.camel.CamelContext; -import org.apache.camel.RoutesBuilder; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.k.RoutesLoader; import org.apache.camel.k.Source; import org.apache.camel.k.support.URIResolver; -import org.apache.camel.model.rest.RestConfigurationDefinition; -import org.apache.camel.spi.RestConfiguration; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.joor.Reflect; @@ -44,53 +40,14 @@ public class JavaSourceRoutesLoader implements RoutesLoader { @Override public RouteBuilder load(CamelContext camelContext, Source source) throws Exception { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - final CamelContext context = getContext(); + try (InputStream is = URIResolver.resolve(camelContext, source)) { + final String content = IOUtils.toString(is, StandardCharsets.UTF_8); + final String name = determineQualifiedName(source, content); + final Reflect compiled = Reflect.compile(name, content); - try (InputStream is = URIResolver.resolve(context, source)) { - // compile the source in memory - String content = IOUtils.toString(is, StandardCharsets.UTF_8); - String name = determineQualifiedName(source, content); - Reflect compiled = Reflect.compile(name, content); - - // create the builder - RoutesBuilder builder = compiled.create().get(); - - if (builder instanceof RouteBuilder) { - RouteBuilder rb = (RouteBuilder) builder; - - rb.setContext(context); - rb.configure(); - - Map<String, RestConfigurationDefinition> configurations = rb.getRestConfigurations(); - - // - // TODO: RouteBuilder.getRestConfigurations() should not - // return null - // - if (configurations != null) { - for (RestConfigurationDefinition definition : configurations.values()) { - RestConfiguration conf = definition.asRestConfiguration(context); - - // - // this is an hack to copy routes configuration - // to the camel context - // - // TODO: fix RouteBuilder.includeRoutes to include - // rest configurations - // - context.addRestConfiguration(conf); - } - } - - setRouteCollection(rb.getRouteCollection()); - setRestCollection(rb.getRestCollection()); - } - } - } - }; + RouteBuilder rb = compiled.create().get(); + return rb; + } } private static String determineQualifiedName(Source source, String content) throws Exception { diff --git a/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/MyBean.java b/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/MyBean.java new file mode 100644 index 0000000..94a1834 --- /dev/null +++ b/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/MyBean.java @@ -0,0 +1,34 @@ +/* + * 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.loader.java; + +public class MyBean { + private final String name; + + public MyBean(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return name; + } +} \ No newline at end of file diff --git a/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/MyRoutesWithBeans.java b/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/MyRoutesWithBeans.java new file mode 100644 index 0000000..15db22b --- /dev/null +++ b/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/MyRoutesWithBeans.java @@ -0,0 +1,34 @@ +/* + * 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.loader.java; + +import org.apache.camel.BindToRegistry; +import org.apache.camel.builder.RouteBuilder; + +public class MyRoutesWithBeans extends RouteBuilder { + @Override + public void configure() throws Exception { + from("direct:start") + .bean("my-bean", "getName") + .to("log:info"); + } + + @BindToRegistry("my-bean") + public org.apache.camel.k.loader.java.MyBean createMyBean() { + return new org.apache.camel.k.loader.java.MyBean("my-bean-name"); + } +} \ No newline at end of file diff --git a/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/RoutesLoaderTest.java b/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/RoutesLoaderTest.java index 45ac859..a778a07 100644 --- a/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/RoutesLoaderTest.java +++ b/camel-k-loader-java/src/test/java/org/apache/camel/k/loader/java/RoutesLoaderTest.java @@ -23,7 +23,10 @@ import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.k.RoutesLoader; +import org.apache.camel.k.Runtime; import org.apache.camel.k.Source; +import org.apache.camel.k.listener.RoutesConfigurer; +import org.apache.camel.k.main.ApplicationRuntime; import org.apache.camel.k.support.RuntimeSupport; import org.apache.camel.model.ProcessDefinition; import org.apache.camel.model.RouteDefinition; @@ -83,13 +86,35 @@ public class RoutesLoaderTest { assertThat(loader).isInstanceOf(JavaSourceRoutesLoader.class); assertThat(builder).isNotNull(); - builder.setContext(context); - builder.configure(); + context.addRoutes(builder); assertThat(context.getRestConfigurations()).hasSize(1); assertThat(context.getRestConfigurations().iterator().next()).hasFieldOrPropertyWithValue("component", "restlet"); } + @Test + public void testLoadJavaClassWithBeans() throws Exception { + ApplicationRuntime runtime = new ApplicationRuntime(); + runtime.addListener(RoutesConfigurer.forRoutes("classpath:" + MyRoutesWithBeans.class.getName() + ".class")); + runtime.addListener(Runtime.Phase.Started, r -> runtime.stop()); + runtime.run(); + + assertThat(runtime.getRegistry().lookupByName("my-bean")).isInstanceOfSatisfying(MyBean.class, b -> { + assertThat(b).hasFieldOrPropertyWithValue("name", "my-bean-name"); + }); + } + + @Test + public void testLoadJavaSourceWithBeans() throws Exception { + ApplicationRuntime runtime = new ApplicationRuntime(); + runtime.addListener(RoutesConfigurer.forRoutes("classpath:MyRoutesWithBeans.java")); + runtime.addListener(Runtime.Phase.Started, r -> runtime.stop()); + runtime.run(); + + assertThat(runtime.getRegistry().lookupByName("my-bean")).isInstanceOfSatisfying(MyBean.class, b -> { + assertThat(b).hasFieldOrPropertyWithValue("name", "my-bean-name"); + }); + } @ParameterizedTest @MethodSource("parameters") diff --git a/camel-k-loader-java/src/test/resources/MyRoutesWithBeans.java b/camel-k-loader-java/src/test/resources/MyRoutesWithBeans.java new file mode 100644 index 0000000..f232dd6 --- /dev/null +++ b/camel-k-loader-java/src/test/resources/MyRoutesWithBeans.java @@ -0,0 +1,33 @@ +/* + * 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. + */ + +import org.apache.camel.BindToRegistry; +import org.apache.camel.builder.RouteBuilder; + +public class MyRoutesWithBeans extends RouteBuilder { + @Override + public void configure() throws Exception { + from("direct:start") + .bean("my-bean", "getName") + .to("log:info"); + } + + @BindToRegistry("my-bean") + public org.apache.camel.k.loader.java.MyBean createMyBean() { + return new org.apache.camel.k.loader.java.MyBean("my-bean-name"); + } +} \ No newline at end of file diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java index b90ffd9..34ca0d2 100644 --- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java +++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/Runtime.java @@ -20,6 +20,8 @@ import java.util.Properties; import org.apache.camel.CamelContext; import org.apache.camel.Ordered; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.component.properties.PropertiesComponent; import org.apache.camel.spi.HasCamelContext; import org.apache.camel.spi.Registry; @@ -39,6 +41,14 @@ public interface Runtime extends HasCamelContext { getRegistry().bind("properties", pc); } + default void addRoutes(RoutesBuilder builder) { + try { + getCamelContext().addRoutes(builder); + } catch (Exception e) { + throw RuntimeCamelException.wrapRuntimeCamelException(e); + } + } + enum Phase { Starting, ConfigureContext, diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/RoutesConfigurer.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/RoutesConfigurer.java index 7df76c6..10fc4f3 100644 --- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/RoutesConfigurer.java +++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/RoutesConfigurer.java @@ -74,11 +74,7 @@ public class RoutesConfigurer extends AbstractPhaseListener { LOGGER.info("Loading routes from: {}", route); - try { - runtime.getCamelContext().addRoutes(builder); - } catch (Exception e) { - throw RuntimeCamelException.wrapRuntimeCamelException(e); - } + runtime.addRoutes(builder); } } diff --git a/camel-k-runtime-main/src/main/java/org/apache/camel/k/main/ApplicationRuntime.java b/camel-k-runtime-main/src/main/java/org/apache/camel/k/main/ApplicationRuntime.java index 2df43e4..43830aa 100644 --- a/camel-k-runtime-main/src/main/java/org/apache/camel/k/main/ApplicationRuntime.java +++ b/camel-k-runtime-main/src/main/java/org/apache/camel/k/main/ApplicationRuntime.java @@ -22,7 +22,9 @@ import java.util.Properties; import java.util.Set; import org.apache.camel.CamelContext; +import org.apache.camel.RoutesBuilder; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.k.Runtime; import org.apache.camel.k.support.PropertiesSupport; @@ -46,13 +48,7 @@ public final class ApplicationRuntime implements Runtime { this.context = new DefaultCamelContext(); this.context.setName("camel-k"); - this.main = new Main() { - @Override - protected CamelContext createCamelContext() { - return ApplicationRuntime.this.context; - } - }; - + this.main = new MainAdapter(); this.main.addMainListener(new MainListenerAdapter()); } @@ -61,6 +57,15 @@ public final class ApplicationRuntime implements Runtime { return this.context; } + @Override + public void addRoutes(RoutesBuilder builder) { + if (builder instanceof RouteBuilder) { + this.main.addRouteBuilder((RouteBuilder) builder); + } else { + Runtime.super.addRoutes(builder); + } + } + public void run() throws Exception { this.main.run(); } @@ -108,16 +113,16 @@ public final class ApplicationRuntime implements Runtime { }); } - private class MainListenerAdapter implements org.apache.camel.main.MainListener { + private final class MainListenerAdapter implements org.apache.camel.main.MainListener { @Override public void beforeStart(MainSupport main) { invokeListeners(Phase.Starting); + invokeListeners(Phase.ConfigureRoutes); } @Override public void configure(CamelContext context) { invokeListeners(Phase.ConfigureContext); - invokeListeners(Phase.ConfigureRoutes); } @Override @@ -145,5 +150,12 @@ public final class ApplicationRuntime implements Runtime { }); } } + + private final class MainAdapter extends Main { + @Override + protected CamelContext createCamelContext() { + return ApplicationRuntime.this.context; + } + } }