You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by pp...@apache.org on 2022/06/27 08:08:15 UTC

[camel-quarkus] branch 2.7.x updated: Registry lookup for overridden DefaultBean types does not work

This is an automated email from the ASF dual-hosted git repository.

ppalaga pushed a commit to branch 2.7.x
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/2.7.x by this push:
     new f986565ea2 Registry lookup for overridden DefaultBean types does not work
f986565ea2 is described below

commit f986565ea2f909d9dff74c1892840ff2aefe0eb2
Author: JiriOndrusek <on...@gmail.com>
AuthorDate: Mon May 30 15:48:50 2022 +0200

    Registry lookup for overridden DefaultBean types does not work
---
 .../camel/quarkus/core/RuntimeBeanRepository.java  |  17 ++-
 .../camel/quarkus/component/bean/BeanResource.java |  39 +++++++
 .../quarkus/component/bean/cdi/Producers.java      | 114 +++++++++++++++++++++
 .../camel/quarkus/component/bean/BeanTest.java     |  47 +++++++++
 4 files changed, 216 insertions(+), 1 deletion(-)

diff --git a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/RuntimeBeanRepository.java b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/RuntimeBeanRepository.java
index 9db54a34f3..934cb22ae0 100644
--- a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/RuntimeBeanRepository.java
+++ b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/RuntimeBeanRepository.java
@@ -24,6 +24,7 @@ import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
 
+import javax.enterprise.inject.AmbiguousResolutionException;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 
@@ -32,6 +33,19 @@ import io.quarkus.arc.ArcContainer;
 import org.apache.camel.spi.BeanRepository;
 
 public final class RuntimeBeanRepository implements BeanRepository {
+
+    private static <T> Set<Bean<? extends T>> resolveAmbiguity(BeanManager manager, Set<Bean<? extends T>> beans) {
+        if (beans.size() > 1) {
+            try {
+                return Collections.singleton(manager.resolve(beans));
+            } catch (AmbiguousResolutionException are) {
+                //in case of AmbiguousResolutionException, original collection is returned
+            }
+        }
+
+        return beans;
+    }
+
     private static <T> Map<String, T> getReferencesByTypeWithName(Class<T> type, Annotation... qualifiers) {
         return getBeanManager()
                 .map(manager -> getReferencesByTypeWithName(manager, type, qualifiers))
@@ -41,7 +55,7 @@ public final class RuntimeBeanRepository implements BeanRepository {
     private static <T> Set<T> getReferencesByType(BeanManager manager, Class<T> type, Annotation... qualifiers) {
         Set<T> answer = new HashSet<>();
 
-        for (Bean<?> bean : manager.getBeans(type, qualifiers)) {
+        for (Bean<?> bean : resolveAmbiguity(manager, manager.getBeans(type, qualifiers))) {
             T ref = getReference(manager, type, bean);
             if (ref != null) {
                 answer.add(ref);
@@ -64,6 +78,7 @@ public final class RuntimeBeanRepository implements BeanRepository {
         Map<String, T> answer = new HashMap<>();
 
         for (Bean<?> bean : manager.getBeans(type, qualifiers)) {
+
             T ref = getReference(manager, type, bean);
             if (ref != null) {
                 answer.put(bean.getName(), ref);
diff --git a/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/BeanResource.java b/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/BeanResource.java
index e19587eed5..1c91f82137 100644
--- a/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/BeanResource.java
+++ b/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/BeanResource.java
@@ -18,6 +18,8 @@ package org.apache.camel.quarkus.component.bean;
 
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
 
 import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
@@ -30,9 +32,11 @@ import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.MediaType;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.Produce;
 import org.apache.camel.ProducerTemplate;
+import org.apache.camel.quarkus.component.bean.cdi.Producers;
 import org.apache.camel.quarkus.component.bean.model.Employee;
 
 @Path("/bean")
@@ -47,6 +51,9 @@ public class BeanResource {
     @Inject
     EagerAppScopedRouteBuilder routeBuilder;
 
+    @Inject
+    CamelContext camelContext;
+
     public interface ProduceInterface {
         String sayHello(String name);
     }
@@ -164,4 +171,36 @@ public class BeanResource {
         return list.get(0);
     }
 
+    @Path("/withDefaultBeanCount")
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public Set<String> withDefaultBean() {
+        return camelContext.getRegistry().findByType(Producers.WithDefaultBeanInstance.class).stream()
+                .map(b -> b.getName()).collect(Collectors.toSet());
+    }
+
+    @Path("/withAlternativeBeanCount")
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public Set<String> withAlternativeBean() {
+        return camelContext.getRegistry().findByType(Producers.WithAlternateBeanInstance.class).stream()
+                .map(b -> b.getName()).collect(Collectors.toSet());
+    }
+
+    @Path("/withoutDefaultBeans")
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public Set<String> withoutDefaultBeans() {
+        return camelContext.getRegistry().findByType(Producers.WithoutDefaultBeanInstance.class).stream()
+                .map(b -> b.getName()).collect(Collectors.toSet());
+    }
+
+    @Path("/allBeanInstances")
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public Set<String> withAllBeanInstances() {
+        return camelContext.getRegistry().findByType(Producers.BeanInstance.class).stream()
+                .map(b -> b.getName()).collect(Collectors.toSet());
+    }
+
 }
diff --git a/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/cdi/Producers.java b/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/cdi/Producers.java
new file mode 100644
index 0000000000..8bbd6dc059
--- /dev/null
+++ b/integration-test-groups/foundation/bean/src/main/java/org/apache/camel/quarkus/component/bean/cdi/Producers.java
@@ -0,0 +1,114 @@
+/*
+ * 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.quarkus.component.bean.cdi;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Alternative;
+import javax.enterprise.inject.Produces;
+
+import io.quarkus.arc.DefaultBean;
+import io.quarkus.arc.Unremovable;
+
+public class Producers {
+
+    public static interface BeanInstance {
+        String getName();
+    }
+
+    //beans with default bean
+    public static class WithDefaultBeanInstance implements BeanInstance {
+        private final String name;
+
+        public WithDefaultBeanInstance(String name) {
+            this.name = name;
+        }
+
+        public String getName() {
+            return name;
+        }
+    }
+
+    @Produces
+    @ApplicationScoped
+    @DefaultBean
+    @Unremovable
+    public WithDefaultBeanInstance defaultBean() {
+        return new WithDefaultBeanInstance("defaultBean");
+    }
+
+    @Produces
+    @ApplicationScoped
+    @Unremovable
+    public WithDefaultBeanInstance defaultOverridingBean() {
+        return new WithDefaultBeanInstance("overridingBean");
+    }
+
+    //beans without default bean
+    public static class WithoutDefaultBeanInstance implements BeanInstance {
+        private final String name;
+
+        public WithoutDefaultBeanInstance(String name) {
+            this.name = name;
+        }
+
+        public String getName() {
+            return name;
+        }
+    }
+
+    @Produces
+    @ApplicationScoped
+    @Unremovable
+    public WithoutDefaultBeanInstance withoutDefaultBean1() {
+        return new WithoutDefaultBeanInstance("bean1");
+    }
+
+    @Produces
+    @ApplicationScoped
+    @Unremovable
+    public WithoutDefaultBeanInstance withoutDefaultBean2() {
+        return new WithoutDefaultBeanInstance("bean2");
+    }
+
+    //beans with alternate beans
+    public static class WithAlternateBeanInstance implements BeanInstance {
+        private final String name;
+
+        public WithAlternateBeanInstance(String name) {
+            this.name = name;
+        }
+
+        public String getName() {
+            return name;
+        }
+    }
+
+    @Produces
+    @ApplicationScoped
+    @Alternative
+    @Unremovable
+    public WithAlternateBeanInstance toBeAlternatedBean() {
+        return new WithAlternateBeanInstance("toBeAlteredBean");
+    }
+
+    @Produces
+    @ApplicationScoped
+    @Unremovable
+    public WithAlternateBeanInstance alternatingBean() {
+        return new WithAlternateBeanInstance("alternatingBean");
+    }
+}
diff --git a/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/BeanTest.java b/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/BeanTest.java
index 8bccac1d47..5b6d0bf38b 100644
--- a/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/BeanTest.java
+++ b/integration-test-groups/foundation/bean/src/test/java/org/apache/camel/quarkus/component/bean/BeanTest.java
@@ -22,7 +22,10 @@ import io.restassured.http.ContentType;
 import org.apache.camel.quarkus.component.bean.model.Employee;
 import org.junit.jupiter.api.Test;
 
+import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.containsString;
 import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.is;
 
 @QuarkusTest
 public class BeanTest {
@@ -213,4 +216,48 @@ public class BeanTest {
                 .body(equalTo("produceInterface xyz1234"));
     }
 
+    @Test
+    public void resolveBeanWithDefaultBean() {
+        RestAssured.given()
+                .get("/bean/withDefaultBeanCount")
+                .then()
+                .body("size()", is(1))
+                .body(containsString("overridingBean"));
+    }
+
+    @Test
+    public void resolveBeanWithAlternativeBean() {
+        RestAssured.given()
+                .get("/bean/withAlternativeBeanCount")
+                .then()
+                .body("size()", is(1))
+                .body(containsString("alternatingBean"));
+        ;
+    }
+
+    @Test
+    public void resolveBeanWithoutDefaultBean() {
+        RestAssured.given()
+                .get("/bean/withoutDefaultBeans")
+                .then()
+                .body("size()", is(2))
+                .body(allOf(
+                        containsString("bean1"),
+                        containsString("bean2")));
+    }
+
+    @Test
+    public void notReducedTest() {
+        RestAssured.given()
+                .get("/bean/allBeanInstances")
+                .then()
+                .body("size()", is(5))
+                .body(allOf(
+                        containsString("defaultBean"),
+                        containsString("overridingBean"),
+                        containsString("bean1"),
+                        containsString("bean2"),
+                        containsString("alternatingBean")));
+    }
+
 }