Skip to content

Commit

Permalink
Merge pull request #19902 from mkouba/issue-19864
Browse files Browse the repository at this point in the history
gRPC - archive with a MutinyBean implementor is a bean archive
  • Loading branch information
mkouba authored Sep 6, 2021
2 parents 717df52 + 13bc4d2 commit ed1fc5f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.arc.deployment;

import java.util.function.Predicate;

import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.deployment.ApplicationArchive;

/**
*
* By default, only explict/implicit bean archives (as defined by the spec) are considered during the bean discovery. However,
* extensions can register a logic to identify additional bean archives.
*/
public final class BeanArchivePredicateBuildItem extends MultiBuildItem {

private final Predicate<ApplicationArchive> predicate;

public BeanArchivePredicateBuildItem(Predicate<ApplicationArchive> predicate) {
this.predicate = predicate;
}

public Predicate<ApplicationArchive> getPredicate() {
return predicate;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package io.quarkus.arc.deployment;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.jboss.jandex.AnnotationInstance;
Expand Down Expand Up @@ -35,12 +40,13 @@ public BeanArchiveIndexBuildItem build(ArcConfig config, ApplicationArchivesBuil
List<BeanDefiningAnnotationBuildItem> additionalBeanDefiningAnnotations,
List<AdditionalBeanBuildItem> additionalBeans, List<GeneratedBeanBuildItem> generatedBeans,
LiveReloadBuildItem liveReloadBuildItem, BuildProducer<GeneratedClassBuildItem> generatedClass,
CustomScopeAnnotationsBuildItem customScopes, List<ExcludeDependencyBuildItem> excludeDependencyBuildItems)
CustomScopeAnnotationsBuildItem customScopes, List<ExcludeDependencyBuildItem> excludeDependencyBuildItems,
List<BeanArchivePredicateBuildItem> beanArchivePredicates)
throws Exception {

// First build an index from application archives
IndexView applicationIndex = buildApplicationIndex(config, applicationArchivesBuildItem,
additionalBeanDefiningAnnotations, customScopes, excludeDependencyBuildItems);
additionalBeanDefiningAnnotations, customScopes, excludeDependencyBuildItems, beanArchivePredicates);

// Then build additional index for beans added by extensions
Indexer additionalBeanIndexer = new Indexer();
Expand Down Expand Up @@ -81,7 +87,8 @@ public BeanArchiveIndexBuildItem build(ArcConfig config, ApplicationArchivesBuil

private IndexView buildApplicationIndex(ArcConfig config, ApplicationArchivesBuildItem applicationArchivesBuildItem,
List<BeanDefiningAnnotationBuildItem> additionalBeanDefiningAnnotations,
CustomScopeAnnotationsBuildItem customScopes, List<ExcludeDependencyBuildItem> excludeDependencyBuildItems) {
CustomScopeAnnotationsBuildItem customScopes, List<ExcludeDependencyBuildItem> excludeDependencyBuildItems,
List<BeanArchivePredicateBuildItem> beanArchivePredicates) {

Set<ApplicationArchive> archives = applicationArchivesBuildItem.getAllApplicationArchives();

Expand Down Expand Up @@ -116,17 +123,35 @@ private IndexView buildApplicationIndex(ArcConfig config, ApplicationArchivesBui
continue;
}
IndexView index = archive.getIndex();
// NOTE: Implicit bean archive without beans.xml contains one or more bean classes with a bean defining annotation and no extension
if (archive.getChildPath("META-INF/beans.xml") != null || archive.getChildPath("WEB-INF/beans.xml") != null
|| (index.getAllKnownImplementors(DotNames.EXTENSION).isEmpty()
&& containsBeanDefiningAnnotation(index, beanDefiningAnnotations))) {
if (isExplicitBeanArchive(archive) || isImplicitBeanArchive(index, beanDefiningAnnotations)
|| isAdditionalBeanArchive(archive, beanArchivePredicates)) {
indexes.add(index);
}
}
indexes.add(applicationArchivesBuildItem.getRootArchive().getIndex());
return CompositeIndex.create(indexes);
}

private boolean isExplicitBeanArchive(ApplicationArchive archive) {
return archive.getChildPath("META-INF/beans.xml") != null || archive.getChildPath("WEB-INF/beans.xml") != null;
}

private boolean isImplicitBeanArchive(IndexView index, Set<DotName> beanDefiningAnnotations) {
// NOTE: Implicit bean archive without beans.xml contains one or more bean classes with a bean defining annotation and no extension
return index.getAllKnownImplementors(DotNames.EXTENSION).isEmpty()
&& containsBeanDefiningAnnotation(index, beanDefiningAnnotations);
}

private boolean isAdditionalBeanArchive(ApplicationArchive archive,
List<BeanArchivePredicateBuildItem> beanArchivePredicates) {
for (BeanArchivePredicateBuildItem p : beanArchivePredicates) {
if (p.getPredicate().test(archive)) {
return true;
}
}
return false;
}

private boolean isApplicationArchiveExcluded(ArcConfig config, List<ExcludeDependencyBuildItem> excludeDependencyBuildItems,
ApplicationArchive archive) {
if (archive.getArtifactKey() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.grpc.internal.ServerImpl;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem;
import io.quarkus.arc.deployment.BeanArchivePredicateBuildItem;
import io.quarkus.arc.deployment.CustomScopeAnnotationsBuildItem;
import io.quarkus.arc.deployment.GeneratedBeanBuildItem;
import io.quarkus.arc.deployment.GeneratedBeanGizmoAdaptor;
Expand All @@ -49,6 +50,7 @@
import io.quarkus.arc.processor.AnnotationsTransformer;
import io.quarkus.arc.processor.BeanInfo;
import io.quarkus.arc.processor.BuiltinScope;
import io.quarkus.deployment.ApplicationArchive;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.annotations.BuildProducer;
Expand Down Expand Up @@ -546,6 +548,18 @@ void configureMetrics(GrpcBuildTimeConfig configuration, Optional<MetricsCapabil
}
}

@BuildStep
BeanArchivePredicateBuildItem additionalBeanArchives() {
return new BeanArchivePredicateBuildItem(new Predicate<ApplicationArchive>() {

@Override
public boolean test(ApplicationArchive archive) {
// Every archive that contains a generated implementor of MutinyBean is considered a bean archive
return !archive.getIndex().getKnownDirectImplementors(GrpcDotNames.MUTINY_BEAN).isEmpty();
}
});
}

private static class GrpcInterceptors {
final Set<String> globalInterceptors;
final Set<String> nonGlobalInterceptors;
Expand Down

0 comments on commit ed1fc5f

Please sign in to comment.