This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git
commit 15cbddf9a9720a490b6caed64be7362a7b16babc Author: James Netherton <jamesnether...@gmail.com> AuthorDate: Thu Jun 25 13:49:17 2020 +0100 Ensure CamelContextAware beans have CamelContext set when bound to the registry --- .../core/deployment/CamelRegistryProcessor.java | 25 ++++++++++++++-------- .../apache/camel/quarkus/core/CoreResource.java | 13 ++++++++++- .../org/apache/camel/quarkus/core/CoreTest.java | 5 +++++ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelRegistryProcessor.java b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelRegistryProcessor.java index 22ceeac..8ffbd52 100644 --- a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelRegistryProcessor.java +++ b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CamelRegistryProcessor.java @@ -23,10 +23,10 @@ import io.quarkus.deployment.annotations.ExecutionTime; import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.ApplicationArchivesBuildItem; import io.quarkus.deployment.recording.RecorderContext; -import io.quarkus.runtime.RuntimeValue; import org.apache.camel.quarkus.core.CamelConfig; import org.apache.camel.quarkus.core.CamelRecorder; import org.apache.camel.quarkus.core.deployment.spi.CamelBeanBuildItem; +import org.apache.camel.quarkus.core.deployment.spi.CamelContextBuildItem; import org.apache.camel.quarkus.core.deployment.spi.CamelRegistryBuildItem; import org.apache.camel.quarkus.core.deployment.spi.CamelRuntimeBeanBuildItem; import org.apache.camel.quarkus.core.deployment.spi.CamelRuntimeTaskBuildItem; @@ -36,7 +36,6 @@ import org.apache.camel.quarkus.core.deployment.spi.CamelServicePatternBuildItem import org.apache.camel.quarkus.core.deployment.spi.ContainerBeansBuildItem; import org.apache.camel.quarkus.core.deployment.util.CamelSupport; import org.apache.camel.quarkus.core.deployment.util.PathFilter; -import org.apache.camel.spi.Registry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,18 +44,25 @@ public class CamelRegistryProcessor { @Record(ExecutionTime.STATIC_INIT) @BuildStep - CamelRegistryBuildItem registry( + CamelRegistryBuildItem registry(CamelRecorder recorder) { + return new CamelRegistryBuildItem(recorder.createRegistry()); + } + + @Record(ExecutionTime.STATIC_INIT) + @BuildStep + public void bindBeansToRegistry( CamelRecorder recorder, RecorderContext recorderContext, CamelConfig camelConfig, ApplicationArchivesBuildItem applicationArchives, ContainerBeansBuildItem containerBeans, + CamelRegistryBuildItem registry, + // CamelContextBuildItem placeholder ensures this build step runs after the CamelContext is created + CamelContextBuildItem camelContextBuildItem, List<CamelBeanBuildItem> registryItems, List<CamelServiceFilterBuildItem> serviceFilters, List<CamelServicePatternBuildItem> servicePatterns) { - final RuntimeValue<Registry> registry = recorder.createRegistry(); - final PathFilter pathFilter = servicePatterns.stream() .filter(patterns -> patterns.getDestination() == CamelServiceDestination.REGISTRY) .collect( @@ -87,7 +93,7 @@ public class CamelRegistryProcessor { LOGGER.debug("Binding bean with name: {}, type {}", si.name, si.type); recorder.bind( - registry, + registry.getRegistry(), si.name, recorderContext.classProxy(si.type)); }); @@ -98,7 +104,7 @@ public class CamelRegistryProcessor { LOGGER.debug("Binding bean with name: {}, type {}", item.getName(), item.getType()); if (item.getValue().isPresent()) { recorder.bind( - registry, + registry.getRegistry(), item.getName(), recorderContext.classProxy(item.getType()), item.getValue().get()); @@ -106,13 +112,12 @@ public class CamelRegistryProcessor { // the instance of the service will be instantiated by the recorder, this avoid // creating a recorder for trivial services. recorder.bind( - registry, + registry.getRegistry(), item.getName(), recorderContext.classProxy(item.getType())); } }); - return new CamelRegistryBuildItem(registry); } @Record(ExecutionTime.RUNTIME_INIT) @@ -122,6 +127,8 @@ public class CamelRegistryProcessor { RecorderContext recorderContext, ContainerBeansBuildItem containerBeans, CamelRegistryBuildItem registry, + // CamelContextBuildItem placeholder ensures this build step runs after the CamelContext is created + CamelContextBuildItem camelContextBuildItem, List<CamelRuntimeBeanBuildItem> registryItems) { registryItems.stream() diff --git a/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CoreResource.java b/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CoreResource.java index 0dbb16e..21e5e80 100644 --- a/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CoreResource.java +++ b/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CoreResource.java @@ -22,6 +22,7 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; +import java.util.stream.Collectors; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; @@ -35,6 +36,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; import org.apache.camel.ExtendedCamelContext; import org.apache.camel.NoSuchLanguageException; import org.apache.camel.catalog.RuntimeCamelCatalog; @@ -82,6 +84,16 @@ public class CoreResource { return registry.findByType(CamelContext.class).size() == 1; } + @Path("/registry/camel-context-aware/initialized") + @GET + @Produces(MediaType.TEXT_PLAIN) + public boolean camelContextAwareBeansHaveContextSet() { + return registry.findByType(CamelContextAware.class).stream() + .filter(camelContextAware -> camelContextAware.getCamelContext() == null) + .collect(Collectors.toList()) + .isEmpty(); + } + @Path("/context/version") @GET @Produces(MediaType.TEXT_PLAIN) @@ -211,5 +223,4 @@ public class CoreResource { return Response.serverError().entity(e.getClass().getName() + ": " + e.getMessage()).build(); } } - } diff --git a/integration-tests/core/src/test/java/org/apache/camel/quarkus/core/CoreTest.java b/integration-tests/core/src/test/java/org/apache/camel/quarkus/core/CoreTest.java index b460c29..42db9c8 100644 --- a/integration-tests/core/src/test/java/org/apache/camel/quarkus/core/CoreTest.java +++ b/integration-tests/core/src/test/java/org/apache/camel/quarkus/core/CoreTest.java @@ -40,6 +40,11 @@ public class CoreTest { } @Test + public void testCamelContextAwareRegistryBeansInitialized() { + RestAssured.when().get("/test/registry/camel-context-aware/initialized").then().body(is("true")); + } + + @Test public void testCamelBeanBuildItem() { Response response = RestAssured.get("/test/registry/log/exchange-formatter").andReturn();