You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zh...@apache.org on 2023/10/31 08:08:57 UTC

(camel-quarkus) branch camel-main updated: Fix #5464 to support precede beans (#5473)

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

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


The following commit(s) were added to refs/heads/camel-main by this push:
     new 6ce53d8af6 Fix #5464 to support precede beans (#5473)
6ce53d8af6 is described below

commit 6ce53d8af6246fb50baa321f499b867dfb425450
Author: Zheng Feng <zh...@gmail.com>
AuthorDate: Tue Oct 31 16:08:50 2023 +0800

    Fix #5464 to support precede beans (#5473)
    
    * Fix #5464 to support precede beans
    
    * Update extensions-core/core/deployment/src/test/java/org/apache/camel/quarkus/core/runtime/CamelPrecedeBeanTest.java
    
    Co-authored-by: James Netherton <ja...@users.noreply.github.com>
    
    ---------
    
    Co-authored-by: James Netherton <ja...@users.noreply.github.com>
---
 .../quarkus/core/runtime/CamelPrecedeBeanTest.java | 70 ++++++++++++++++++++++
 .../camel/quarkus/core/RuntimeBeanRepository.java  | 20 +++++++
 2 files changed, 90 insertions(+)

diff --git a/extensions-core/core/deployment/src/test/java/org/apache/camel/quarkus/core/runtime/CamelPrecedeBeanTest.java b/extensions-core/core/deployment/src/test/java/org/apache/camel/quarkus/core/runtime/CamelPrecedeBeanTest.java
new file mode 100644
index 0000000000..71620aa058
--- /dev/null
+++ b/extensions-core/core/deployment/src/test/java/org/apache/camel/quarkus/core/runtime/CamelPrecedeBeanTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.core.runtime;
+
+import io.quarkus.test.QuarkusUnitTest;
+import jakarta.annotation.Priority;
+import jakarta.enterprise.inject.Produces;
+import jakarta.inject.Inject;
+import org.apache.camel.CamelContext;
+import org.apache.camel.support.CamelContextHelper;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class CamelPrecedeBeanTest {
+    @RegisterExtension
+    static final QuarkusUnitTest CONFIG = new QuarkusUnitTest()
+            .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
+                    .addClasses(MyTestBean.class));
+
+    @Inject
+    CamelContext context;
+
+    @Test
+    public void testBeanPrecedence() {
+        MyTestBean bean = CamelContextHelper.findSingleByType(context, MyTestBean.class);
+        assertEquals("bar", bean.getName());
+    }
+
+    @Produces
+    @Priority(100)
+    MyTestBean createFoo() {
+        return new MyTestBean("foo");
+    }
+
+    @Produces
+    @Priority(200)
+    MyTestBean createBar() {
+        return new MyTestBean("bar");
+    }
+
+    public static final class MyTestBean {
+        private final String name;
+
+        public MyTestBean(String name) {
+            this.name = name;
+        }
+
+        public String getName() {
+            return name;
+        }
+    }
+}
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 cfb34569f3..deb85823c5 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
@@ -20,12 +20,14 @@ import java.lang.annotation.Annotation;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
 
 import io.quarkus.arc.Arc;
 import io.quarkus.arc.ArcContainer;
+import io.quarkus.arc.InstanceHandle;
 import io.smallrye.common.annotation.Identifier;
 import jakarta.enterprise.inject.AmbiguousResolutionException;
 import jakarta.enterprise.inject.literal.NamedLiteral;
@@ -150,6 +152,24 @@ public final class RuntimeBeanRepository implements BeanRepository {
                 .orElseGet(Collections::emptySet);
     }
 
+    @Override
+    public <T> T findSingleByType(Class<T> type) {
+        ArcContainer container = Arc.container();
+        Optional<Annotation[]> qualifiers = resolveQualifiersForType(type);
+        if (container != null) {
+            List<InstanceHandle<T>> handles;
+            if (qualifiers.isPresent()) {
+                handles = container.listAll(type, qualifiers.get());
+            } else {
+                handles = container.listAll(type);
+            }
+            if (handles.size() > 0) {
+                return handles.get(0).get();
+            }
+        }
+        return null;
+    }
+
     private Optional<Annotation[]> resolveQualifiersForType(Class<?> type) {
         CamelBeanQualifierResolver resolver = beanQualifierResolvers.get(type.getName());
         if (resolver != null) {