Skip to content

Commit

Permalink
Fix #6301 to resort the beans if priority is same the default one wins (
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfeng committed Jul 31, 2024
1 parent 9072aa2 commit 91cb3a2
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.camel.quarkus.core;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -27,9 +28,11 @@

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InjectableBean;
import io.quarkus.arc.InstanceHandle;
import io.smallrye.common.annotation.Identifier;
import jakarta.enterprise.inject.AmbiguousResolutionException;
import jakarta.enterprise.inject.Default;
import jakarta.enterprise.inject.literal.NamedLiteral;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.BeanManager;
Expand Down Expand Up @@ -163,8 +166,30 @@ public <T> T findSingleByType(Class<T> type) {
} else {
handles = container.listAll(type);
}
if (handles.size() > 0) {
return handles.get(0).get();

List<InstanceHandle<T>> sortedHandles = new ArrayList<>(handles.size());
sortedHandles.addAll(handles);

if (sortedHandles.size() > 1) {
sortedHandles.sort((bean1, bean2) -> {
Integer priority2 = bean2.getBean().getPriority();
Integer priority1 = bean1.getBean().getPriority();

int result = priority2.compareTo(priority1);
// If the priority is same, the default bean wins
if (result == 0) {
if (isDefaultBean(bean1.getBean())) {
result = -1;
} else if (isDefaultBean(bean2.getBean())) {
result = 1;
}
}
return result;
});
}

if (sortedHandles.size() > 0) {
return sortedHandles.get(0).get();
}
}
return null;
Expand All @@ -177,4 +202,8 @@ private Optional<Annotation[]> resolveQualifiersForType(Class<?> type) {
}
return Optional.empty();
}

private boolean isDefaultBean(InjectableBean<?> bean) {
return bean.isDefaultBean() || bean.getQualifiers().stream().anyMatch(q -> q.annotationType().equals(Default.class));
}
}
8 changes: 8 additions & 0 deletions integration-test-groups/foundation/bean/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
</dependency>

<!-- test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Set;
import java.util.stream.Collectors;

import javax.sql.DataSource;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
Expand All @@ -38,6 +40,7 @@
import org.apache.camel.ProducerTemplate;
import org.apache.camel.quarkus.component.bean.cdi.Producers;
import org.apache.camel.quarkus.component.bean.model.Employee;
import org.apache.camel.support.CamelContextHelper;

@Path("/bean")
@ApplicationScoped
Expand Down Expand Up @@ -212,4 +215,12 @@ public String propertyInject(
Map<String, Object> headers = Map.of("beanName", beanName, "beanMethod", beanMethod);
return template.requestBodyAndHeaders("direct:propertyInject", null, headers, String.class);
}

@Path("/dataSource")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String dataSourceBean() throws Exception {
DataSource ds = CamelContextHelper.findSingleByType(camelContext, DataSource.class);
return ds.getConnection().getMetaData().getURL();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ my.foo.property = foo
my.injected.property.a = Test @PropertyInject
my.injected.property.d = Test @PropertyInject Setter Method
my.injected.property.e = Test @PropertyInject Method Argument

# DataSource Test
quarkus.datasource.db-kind = h2

quarkus.datasource."test".db-kind = h2
quarkus.datasource."test".jdbc.url = jdbc:h2:mem:test
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,13 @@ public void notReducedTest() {
containsString("alternatingBean")));
}

@Test
public void testDataSourceBean() {
for (int i = 0; i < 10; i++) {
RestAssured.given()
.get("/bean/dataSource")
.then()
.body(containsString("mem:quarkus"));
}
}
}
8 changes: 8 additions & 0 deletions integration-tests/foundation-grouped/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@

<!-- Regenerate the dependencies via `mvn process-resources -Pformat -N` from the source tree root directory -->
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jsonb</artifactId>
Expand Down

0 comments on commit 91cb3a2

Please sign in to comment.